diff options
Diffstat (limited to 'src')
24 files changed, 340 insertions, 122 deletions
diff --git a/src/GlobalAssemblyInfo.cs b/src/GlobalAssemblyInfo.cs index 7f1fa401..3df34a96 100644 --- a/src/GlobalAssemblyInfo.cs +++ b/src/GlobalAssemblyInfo.cs @@ -2,5 +2,5 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.5.0.0")] -[assembly: AssemblyFileVersion("1.5.0.0")]
\ No newline at end of file +[assembly: AssemblyVersion("1.6.0.0")] +[assembly: AssemblyFileVersion("1.6.0.0")]
\ No newline at end of file diff --git a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs index 5f89caf2..ef813eb3 100644 --- a/src/StardewModdingAPI.Installer/InteractiveInstaller.cs +++ b/src/StardewModdingAPI.Installer/InteractiveInstaller.cs @@ -77,6 +77,9 @@ namespace StardewModdingApi.Installer "StardewModdingAPI-settings.json" // 1.0-1.4 }; + /// <summary>Whether the current console supports color formatting.</summary> + private static readonly bool ConsoleSupportsColor = InteractiveInstaller.GetConsoleSupportsColor(); + /********* ** Public methods @@ -253,18 +256,18 @@ namespace StardewModdingApi.Installer /**** ** exit ****/ - Console.ForegroundColor = ConsoleColor.DarkGreen; - Console.WriteLine("Done!"); + this.PrintColor("Done!", ConsoleColor.DarkGreen); if (platform == Platform.Windows) { - Console.WriteLine(action == ScriptAction.Install - ? "Don't forget to launch StardewModdingAPI.exe instead of the normal game executable. See the readme.txt for details." - : "If you manually changed shortcuts or Steam to launch SMAPI, don't forget to change those back." + this.PrintColor( + action == ScriptAction.Install + ? "Don't forget to launch StardewModdingAPI.exe instead of the normal game executable. See the readme.txt for details." + : "If you manually changed shortcuts or Steam to launch SMAPI, don't forget to change those back.", + ConsoleColor.DarkGreen ); } else if (action == ScriptAction.Install) - Console.WriteLine("You can launch the game the same way as before to play with mods."); - Console.ResetColor(); + this.PrintColor("You can launch the game the same way as before to play with mods.", ConsoleColor.DarkGreen); Console.ReadKey(); } @@ -287,6 +290,20 @@ namespace StardewModdingApi.Installer } } + /// <summary>Test whether the current console supports color formatting.</summary> + private static bool GetConsoleSupportsColor() + { + try + { + Console.ForegroundColor = Console.ForegroundColor; + return true; + } + catch (Exception) + { + return false; // Mono bug + } + } + #if SMAPI_FOR_WINDOWS /// <summary>Get the value of a key in the Windows registry.</summary> /// <param name="key">The full path of the registry key relative to HKLM.</param> @@ -306,30 +323,39 @@ namespace StardewModdingApi.Installer /// <param name="text">The text to print.</param> private void PrintDebug(string text) { - Console.ForegroundColor = ConsoleColor.DarkGray; - Console.WriteLine(text); - Console.ResetColor(); + this.PrintColor(text, ConsoleColor.DarkGray); } /// <summary>Print a warning message.</summary> /// <param name="text">The text to print.</param> private void PrintWarning(string text) { - Console.ForegroundColor = ConsoleColor.DarkYellow; - Console.WriteLine(text); - Console.ResetColor(); + this.PrintColor(text, ConsoleColor.DarkYellow); } /// <summary>Print an error and pause the console if needed.</summary> /// <param name="error">The error text.</param> private void ExitError(string error) { - Console.ForegroundColor = ConsoleColor.Red; - Console.WriteLine(error); - Console.ResetColor(); + this.PrintColor(error, ConsoleColor.Red); Console.ReadLine(); } + /// <summary>Print a message to the console.</summary> + /// <param name="text">The message text.</param> + /// <param name="color">The text foreground color.</param> + private void PrintColor(string text, ConsoleColor color) + { + if (InteractiveInstaller.ConsoleSupportsColor) + { + Console.ForegroundColor = color; + Console.WriteLine(text); + Console.ResetColor(); + } + else + Console.WriteLine(text); + } + /// <summary>Interactively ask the user to choose a value.</summary> /// <param name="message">The message to print.</param> /// <param name="options">The allowed options (not case sensitive).</param> diff --git a/src/StardewModdingAPI.sln b/src/StardewModdingAPI.sln index 6e13b16b..7229cf30 100644 --- a/src/StardewModdingAPI.sln +++ b/src/StardewModdingAPI.sln @@ -41,11 +41,13 @@ Global {28480467-1A48-46A7-99F8-236D95225359}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Debug|x86.ActiveCfg = Debug|Any CPU + {28480467-1A48-46A7-99F8-236D95225359}.Debug|x86.Build.0 = Debug|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Release|Any CPU.ActiveCfg = Release|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Release|Any CPU.Build.0 = Release|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Release|Mixed Platforms.Build.0 = Release|Any CPU {28480467-1A48-46A7-99F8-236D95225359}.Release|x86.ActiveCfg = Release|Any CPU + {28480467-1A48-46A7-99F8-236D95225359}.Release|x86.Build.0 = Release|Any CPU {F1A573B0-F436-472C-AE29-0B91EA6B9F8F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F1A573B0-F436-472C-AE29-0B91EA6B9F8F}.Debug|Any CPU.Build.0 = Debug|Any CPU {F1A573B0-F436-472C-AE29-0B91EA6B9F8F}.Debug|Mixed Platforms.ActiveCfg = Debug|x86 diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index a79fd382..2211e167 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -30,7 +30,7 @@ namespace StardewModdingAPI public static readonly Version Version = (Version)Constants.ApiVersion; /// <summary>SMAPI's current semantic version.</summary> - public static ISemanticVersion ApiVersion => new Version(1, 5, 0, null, suppressDeprecationWarning: true); + public static ISemanticVersion ApiVersion => new Version(1, 6, 0, null, suppressDeprecationWarning: true); /// <summary>The minimum supported version of Stardew Valley.</summary> public const string MinimumGameVersion = "1.1"; diff --git a/src/StardewModdingAPI/Events/PlayerEvents.cs b/src/StardewModdingAPI/Events/PlayerEvents.cs index 3f301b07..dd3ff220 100644 --- a/src/StardewModdingAPI/Events/PlayerEvents.cs +++ b/src/StardewModdingAPI/Events/PlayerEvents.cs @@ -14,9 +14,11 @@ namespace StardewModdingAPI.Events ** Events *********/ /// <summary>Raised after the player loads a saved game.</summary> + [Obsolete("Use " + nameof(SaveEvents) + "." + nameof(SaveEvents.AfterLoad) + " instead")] public static event EventHandler<EventArgsLoadedGameChanged> LoadedGame; /// <summary>Raised after the game assigns a new player character. This happens just before <see cref="LoadedGame"/>; it's unclear how this would happen any other time.</summary> + [Obsolete("should no longer be used")] public static event EventHandler<EventArgsFarmerChanged> FarmerChanged; /// <summary>Raised after the player's inventory changes in any way (added or removed item, sorted, etc).</summary> @@ -34,7 +36,14 @@ namespace StardewModdingAPI.Events /// <param name="loaded">Whether the save has been loaded. This is always true.</param> internal static void InvokeLoadedGame(IMonitor monitor, EventArgsLoadedGameChanged loaded) { - monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.LoadedGame)}", PlayerEvents.LoadedGame?.GetInvocationList(), null, loaded); + if (PlayerEvents.LoadedGame == null) + return; + + string name = $"{nameof(PlayerEvents)}.{nameof(PlayerEvents.LoadedGame)}"; + Delegate[] handlers = PlayerEvents.LoadedGame.GetInvocationList(); + + Program.DeprecationManager.WarnForEvent(handlers, name, "1.6", DeprecationLevel.Notice); + monitor.SafelyRaiseGenericEvent(name, handlers, null, loaded); } /// <summary>Raise a <see cref="FarmerChanged"/> event.</summary> @@ -43,7 +52,14 @@ namespace StardewModdingAPI.Events /// <param name="newFarmer">The new player character.</param> internal static void InvokeFarmerChanged(IMonitor monitor, Farmer priorFarmer, Farmer newFarmer) { - monitor.SafelyRaiseGenericEvent($"{nameof(PlayerEvents)}.{nameof(PlayerEvents.FarmerChanged)}", PlayerEvents.FarmerChanged?.GetInvocationList(), null, new EventArgsFarmerChanged(priorFarmer, newFarmer)); + if (PlayerEvents.FarmerChanged == null) + return; + + string name = $"{nameof(PlayerEvents)}.{nameof(PlayerEvents.FarmerChanged)}"; + Delegate[] handlers = PlayerEvents.FarmerChanged.GetInvocationList(); + + Program.DeprecationManager.WarnForEvent(handlers, name, "1.6", DeprecationLevel.Notice); + monitor.SafelyRaiseGenericEvent(name, handlers, null, new EventArgsFarmerChanged(priorFarmer, newFarmer)); } /// <summary>Raise an <see cref="InventoryChanged"/> event.</summary> diff --git a/src/StardewModdingAPI/Events/SaveEvents.cs b/src/StardewModdingAPI/Events/SaveEvents.cs new file mode 100644 index 00000000..2921003a --- /dev/null +++ b/src/StardewModdingAPI/Events/SaveEvents.cs @@ -0,0 +1,46 @@ +using System; +using StardewModdingAPI.Framework; + +namespace StardewModdingAPI.Events +{ + /// <summary>Events raised before and after the player saves/loads the game.</summary> + public static class SaveEvents + { + /********* + ** Events + *********/ + /// <summary>Raised before the game begins writes data to the save file.</summary> + public static event EventHandler BeforeSave; + + /// <summary>Raised after the game finishes writing data to the save file.</summary> + public static event EventHandler AfterSave; + + /// <summary>Raised after the player loads a save slot.</summary> + public static event EventHandler AfterLoad; + + + /********* + ** Internal methods + *********/ + /// <summary>Raise a <see cref="BeforeSave"/> event.</summary> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + internal static void InvokeBeforeSave(IMonitor monitor) + { + monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.BeforeSave)}", SaveEvents.BeforeSave?.GetInvocationList(), null, EventArgs.Empty); + } + + /// <summary>Raise a <see cref="AfterSave"/> event.</summary> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + internal static void InvokeAfterSave(IMonitor monitor) + { + monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterSave)}", SaveEvents.AfterSave?.GetInvocationList(), null, EventArgs.Empty); + } + + /// <summary>Raise a <see cref="AfterLoad"/> event.</summary> + /// <param name="monitor">Encapsulates monitoring and logging.</param> + internal static void InvokeAfterLoad(IMonitor monitor) + { + monitor.SafelyRaisePlainEvent($"{nameof(SaveEvents)}.{nameof(SaveEvents.AfterLoad)}", SaveEvents.AfterLoad?.GetInvocationList(), null, EventArgs.Empty); + } + } +} diff --git a/src/StardewModdingAPI/Events/TimeEvents.cs b/src/StardewModdingAPI/Events/TimeEvents.cs index 1b367230..dedd7e77 100644 --- a/src/StardewModdingAPI/Events/TimeEvents.cs +++ b/src/StardewModdingAPI/Events/TimeEvents.cs @@ -22,6 +22,7 @@ namespace StardewModdingAPI.Events public static event EventHandler<EventArgsStringChanged> SeasonOfYearChanged; /// <summary>Raised when the player is transitioning to a new day and the game is performing its day update logic. This event is triggered twice: once after the game starts transitioning, and again after it finishes.</summary> + [Obsolete("Use " + nameof(TimeEvents) + "." + nameof(DayOfMonthChanged) + " or " + nameof(SaveEvents) + " instead")] public static event EventHandler<EventArgsNewDay> OnNewDay; @@ -71,7 +72,14 @@ namespace StardewModdingAPI.Events /// <param name="isTransitioning">Whether the game just started the transition (<c>true</c>) or finished it (<c>false</c>).</param> internal static void InvokeOnNewDay(IMonitor monitor, int priorDay, int newDay, bool isTransitioning) { - monitor.SafelyRaiseGenericEvent($"{nameof(TimeEvents)}.{nameof(TimeEvents.OnNewDay)}", TimeEvents.OnNewDay?.GetInvocationList(), null, new EventArgsNewDay(priorDay, newDay, isTransitioning)); + if (TimeEvents.OnNewDay == null) + return; + + string name = $"{nameof(TimeEvents)}.{nameof(TimeEvents.OnNewDay)}"; + Delegate[] handlers = TimeEvents.OnNewDay.GetInvocationList(); + + Program.DeprecationManager.WarnForEvent(handlers, name, "1.6", DeprecationLevel.Notice); + monitor.SafelyRaiseGenericEvent(name, handlers, null, new EventArgsNewDay(priorDay, newDay, isTransitioning)); } } } diff --git a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs index a747eaa8..4c3b86fe 100644 --- a/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs +++ b/src/StardewModdingAPI/Framework/AssemblyRewriting/CacheEntry.cs @@ -1,4 +1,5 @@ using System.IO; +using StardewModdingAPI.AssemblyRewriters; namespace StardewModdingAPI.Framework.AssemblyRewriting { @@ -14,6 +15,12 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// <summary>The SMAPI version used to rewrite the assembly.</summary> public readonly string ApiVersion; + /// <summary>The target platform.</summary> + public readonly Platform Platform; + + /// <summary>The <see cref="System.Environment.MachineName"/> value for the machine used to rewrite the assembly.</summary> + public readonly string MachineName; + /// <summary>Whether to use the cached assembly instead of the original assembly.</summary> public readonly bool UseCachedAssembly; @@ -24,11 +31,15 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// <summary>Construct an instance.</summary> /// <param name="hash">The MD5 hash for the original assembly.</param> /// <param name="apiVersion">The SMAPI version used to rewrite the assembly.</param> + /// <param name="platform">The target platform.</param> + /// <param name="machineName">The <see cref="System.Environment.MachineName"/> value for the machine used to rewrite the assembly.</param> /// <param name="useCachedAssembly">Whether to use the cached assembly instead of the original assembly.</param> - public CacheEntry(string hash, string apiVersion, bool useCachedAssembly) + public CacheEntry(string hash, string apiVersion, Platform platform, string machineName, bool useCachedAssembly) { this.Hash = hash; this.ApiVersion = apiVersion; + this.Platform = platform; + this.MachineName = machineName; this.UseCachedAssembly = useCachedAssembly; } @@ -36,10 +47,14 @@ namespace StardewModdingAPI.Framework.AssemblyRewriting /// <param name="paths">The paths for the cached assembly.</param> /// <param name="hash">The MD5 hash of the original assembly.</param> /// <param name="currentVersion">The current SMAPI version.</param> - public bool IsUpToDate(CachePaths paths, string hash, ISemanticVersion currentVersion) + /// <param name="platform">The target platform.</param> + /// <param name="machineName">The <see cref="System.Environment.MachineName"/> value for the machine reading the assembly.</param> + public bool IsUpToDate(CachePaths paths, string hash, ISemanticVersion currentVersion, Platform platform, string machineName) { return hash == this.Hash && this.ApiVersion == currentVersion.ToString() + && this.Platform == platform + && this.MachineName == machineName && (!this.UseCachedAssembly || File.Exists(paths.Assembly)); } } diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index 415785d9..c4bd2d35 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -86,5 +86,26 @@ namespace StardewModdingAPI.Framework // anything else return exception.ToString(); } + + /**** + ** Deprecation + ****/ + /// <summary>Log a deprecation warning for mods using an event.</summary> + /// <param name="deprecationManager">The deprecation manager to extend.</param> + /// <param name="handlers">The event handlers.</param> + /// <param name="nounPhrase">A noun phrase describing what is deprecated.</param> + /// <param name="version">The SMAPI version which deprecated it.</param> + /// <param name="severity">How deprecated the code is.</param> + public static void WarnForEvent(this DeprecationManager deprecationManager, Delegate[] handlers, string nounPhrase, string version, DeprecationLevel severity) + { + if (handlers == null || !handlers.Any()) + return; + + foreach (Delegate handler in handlers) + { + string modName = Program.ModRegistry.GetModFrom(handler) ?? "an unknown mod"; // suppress stack trace for unknown mods, not helpful here + deprecationManager.Warn(modName, nounPhrase, version, severity); + } + } } } diff --git a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs index a2c4ac23..e4760398 100644 --- a/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs +++ b/src/StardewModdingAPI/Framework/ModAssemblyLoader.cs @@ -29,6 +29,8 @@ namespace StardewModdingAPI.Framework /// <summary>Encapsulates monitoring and logging.</summary> private readonly IMonitor Monitor; + /// <summary>The current game platform.</summary> + private readonly Platform TargetPlatform; /********* ** Public methods @@ -40,6 +42,7 @@ namespace StardewModdingAPI.Framework public ModAssemblyLoader(string cacheDirName, Platform targetPlatform, IMonitor monitor) { this.CacheDirName = cacheDirName; + this.TargetPlatform = targetPlatform; this.Monitor = monitor; this.AssemblyMap = Constants.GetAssemblyMap(targetPlatform); this.AssemblyTypeRewriter = new AssemblyTypeRewriter(this.AssemblyMap, monitor); @@ -58,7 +61,7 @@ namespace StardewModdingAPI.Framework CachePaths cachePaths = this.GetCachePaths(assemblyPath); { CacheEntry cacheEntry = File.Exists(cachePaths.Metadata) ? JsonConvert.DeserializeObject<CacheEntry>(File.ReadAllText(cachePaths.Metadata)) : null; - if (cacheEntry != null && cacheEntry.IsUpToDate(cachePaths, hash, Constants.ApiVersion)) + if (cacheEntry != null && cacheEntry.IsUpToDate(cachePaths, hash, Constants.ApiVersion, this.TargetPlatform, Environment.MachineName)) return new RewriteResult(assemblyPath, cachePaths, assemblyBytes, cacheEntry.Hash, cacheEntry.UseCachedAssembly, isNewerThanCache: false); // no rewrite needed } this.Monitor.Log($"Preprocessing {Path.GetFileName(assemblyPath)} for compatibility...", LogLevel.Trace); @@ -99,7 +102,7 @@ namespace StardewModdingAPI.Framework // cache all results foreach (RewriteResult result in results) { - CacheEntry cacheEntry = new CacheEntry(result.Hash, Constants.ApiVersion.ToString(), forceCacheAssemblies || result.UseCachedAssembly); + CacheEntry cacheEntry = new CacheEntry(result.Hash, Constants.ApiVersion.ToString(), this.TargetPlatform, Environment.MachineName, forceCacheAssemblies || result.UseCachedAssembly); File.WriteAllText(result.CachePaths.Metadata, JsonConvert.SerializeObject(cacheEntry)); if (forceCacheAssemblies || result.UseCachedAssembly) File.WriteAllBytes(result.CachePaths.Assembly, result.AssemblyBytes); diff --git a/src/StardewModdingAPI/Framework/ModRegistry.cs b/src/StardewModdingAPI/Framework/ModRegistry.cs index b593142d..51ec7123 100644 --- a/src/StardewModdingAPI/Framework/ModRegistry.cs +++ b/src/StardewModdingAPI/Framework/ModRegistry.cs @@ -36,6 +36,34 @@ namespace StardewModdingAPI.Framework return (from mod in this.Mods select mod); } + /// <summary>Get the friendly mod name which handles a delegate.</summary> + /// <param name="delegate">The delegate to follow.</param> + /// <returns>Returns the mod name, or <c>null</c> if the delegate isn't implemented by a known mod.</returns> + public string GetModFrom(Delegate @delegate) + { + return @delegate?.Target != null + ? this.GetModFrom(@delegate.Target.GetType()) + : null; + } + + /// <summary>Get the friendly mod name which defines a type.</summary> + /// <param name="type">The type to check.</param> + /// <returns>Returns the mod name, or <c>null</c> if the type isn't part of a known mod.</returns> + public string GetModFrom(Type type) + { + // null + if (type == null) + return null; + + // known type + string assemblyName = type.Assembly.FullName; + if (this.ModNamesByAssembly.ContainsKey(assemblyName)) + return this.ModNamesByAssembly[assemblyName]; + + // not found + return null; + } + /// <summary>Get the friendly name for the closest assembly registered as a source of deprecation warnings.</summary> /// <returns>Returns the source name, or <c>null</c> if no registered assemblies were found.</returns> public string GetModFromStack() @@ -49,16 +77,10 @@ namespace StardewModdingAPI.Framework // search stack for a source assembly foreach (StackFrame frame in frames) { - // get assembly name MethodBase method = frame.GetMethod(); - Type type = method.ReflectedType; - if (type == null) - continue; - string assemblyName = type.Assembly.FullName; - - // get name if it's a registered source - if (this.ModNamesByAssembly.ContainsKey(assemblyName)) - return this.ModNamesByAssembly[assemblyName]; + string name = this.GetModFrom(method.ReflectedType); + if (name != null) + return name; } // no known assembly found diff --git a/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs b/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs index 9bf06552..bcf5639c 100644 --- a/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs +++ b/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs @@ -14,8 +14,11 @@ namespace StardewModdingAPI.Framework.Models /// <summary>The mod name.</summary> public string Name { get; set; } + /// <summary>The oldest incompatible mod version, or <c>null</c> for all past versions.</summary> + public string LowerVersion { get; set; } + /// <summary>The most recent incompatible mod version.</summary> - public string Version { get; set; } + public string UpperVersion { get; set; } /// <summary>The URL the user can check for an official updated version.</summary> public string UpdateUrl { get; set; } @@ -23,9 +26,13 @@ namespace StardewModdingAPI.Framework.Models /// <summary>The URL the user can check for an unofficial updated version.</summary> public string UnofficialUpdateUrl { get; set; } - /// <summary>A regular expression matching version strings to consider compatible, even if they technically precede <see cref="Version"/>.</summary> + /// <summary>A regular expression matching version strings to consider compatible, even if they technically precede <see cref="UpperVersion"/>.</summary> public string ForceCompatibleVersion { get; set; } + /// <summary>The reason phrase to show in the warning, or <c>null</c> to use the default value.</summary> + /// <example>"this version is incompatible with the latest version of the game"</example> + public string ReasonPhrase { get; set; } + /********* ** Public methods @@ -34,14 +41,17 @@ namespace StardewModdingAPI.Framework.Models /// <param name="version">The current version of the matching mod.</param> public bool IsCompatible(ISemanticVersion version) { - ISemanticVersion incompatibleVersion = new SemanticVersion(this.Version); + ISemanticVersion lowerVersion = this.LowerVersion != null ? new SemanticVersion(this.LowerVersion) : null; + ISemanticVersion upperVersion = new SemanticVersion(this.UpperVersion); - // allow newer versions - if (version.IsNewerThan(incompatibleVersion)) + // ignore versions not in range + if (lowerVersion != null && version.IsOlderThan(lowerVersion)) + return true; + if (version.IsNewerThan(upperVersion)) return true; // allow versions matching override return !string.IsNullOrWhiteSpace(this.ForceCompatibleVersion) && Regex.IsMatch(version.ToString(), this.ForceCompatibleVersion, RegexOptions.IgnoreCase); } } -}
\ No newline at end of file +} diff --git a/src/StardewModdingAPI/Framework/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs index cf46a474..39b567d8 100644 --- a/src/StardewModdingAPI/Framework/Monitor.cs +++ b/src/StardewModdingAPI/Framework/Monitor.cs @@ -34,9 +34,15 @@ namespace StardewModdingAPI.Framework /********* ** Accessors *********/ + /// <summary>Whether the current console supports color codes.</summary> + internal static readonly bool ConsoleSupportsColor = Monitor.GetConsoleSupportsColor(); + /// <summary>Whether to show trace messages in the console.</summary> internal bool ShowTraceInConsole { get; set; } + /// <summary>Whether to write anything to the console. This should be disabled if no console is available.</summary> + internal bool WriteToConsole { get; set; } = true; + /********* ** Public methods @@ -108,13 +114,32 @@ namespace StardewModdingAPI.Framework message = $"[{DateTime.Now:HH:mm:ss} {levelStr} {source}] {message}"; // log - if (this.ShowTraceInConsole || level != LogLevel.Trace) + if (this.WriteToConsole && (this.ShowTraceInConsole || level != LogLevel.Trace)) { - Console.ForegroundColor = color; - Console.WriteLine(message); - Console.ResetColor(); + if (Monitor.ConsoleSupportsColor) + { + Console.ForegroundColor = color; + Console.WriteLine(message); + Console.ResetColor(); + } + else + Console.WriteLine(message); } this.LogFile.WriteLine(message); } + + /// <summary>Test whether the current console supports color formatting.</summary> + private static bool GetConsoleSupportsColor() + { + try + { + Console.ForegroundColor = Console.ForegroundColor; + return true; + } + catch (Exception) + { + return false; // Mono bug + } + } } }
\ No newline at end of file diff --git a/src/StardewModdingAPI/Inheritance/SGame.cs b/src/StardewModdingAPI/Inheritance/SGame.cs index 16803a73..8e87bac6 100644 --- a/src/StardewModdingAPI/Inheritance/SGame.cs +++ b/src/StardewModdingAPI/Inheritance/SGame.cs @@ -913,10 +913,17 @@ namespace StardewModdingAPI.Inheritance // raise menu changed if (Game1.activeClickableMenu != this.PreviousActiveMenu) { - // raise events IClickableMenu previousMenu = this.PreviousActiveMenu; IClickableMenu newMenu = Game1.activeClickableMenu; - if (Game1.activeClickableMenu != null) + + // raise save events + if (newMenu is SaveGameMenu) + SaveEvents.InvokeBeforeSave(this.Monitor); + else if (previousMenu is SaveGameMenu) + SaveEvents.InvokeAfterSave(this.Monitor); + + // raise menu events + if (newMenu != null) MenuEvents.InvokeMenuChanged(this.Monitor, previousMenu, newMenu); else MenuEvents.InvokeMenuClosed(this.Monitor, previousMenu); @@ -1024,6 +1031,7 @@ namespace StardewModdingAPI.Inheritance // raise player loaded save (in the following tick to let the game finish updating first) if (this.FireLoadedGameEvent) { + SaveEvents.InvokeAfterLoad(this.Monitor); PlayerEvents.InvokeLoadedGame(this.Monitor, new EventArgsLoadedGameChanged(Game1.hasLoadedGame)); this.FireLoadedGameEvent = false; } diff --git a/src/StardewModdingAPI/LogWriter.cs b/src/StardewModdingAPI/LogWriter.cs index 058fa649..9c2ef515 100644 --- a/src/StardewModdingAPI/LogWriter.cs +++ b/src/StardewModdingAPI/LogWriter.cs @@ -42,9 +42,14 @@ namespace StardewModdingAPI string output = $"[{message.LogTime}] {message.Message}"; if (message.PrintConsole) { - Console.ForegroundColor = message.Colour; - Console.WriteLine(message); - Console.ForegroundColor = ConsoleColor.Gray; + if (Monitor.ConsoleSupportsColor) + { + Console.ForegroundColor = message.Colour; + Console.WriteLine(message); + Console.ResetColor(); + } + else + Console.WriteLine(message); } this.LogFile.WriteLine(output); } diff --git a/src/StardewModdingAPI/Manifest.cs b/src/StardewModdingAPI/Manifest.cs index 018b31ae..07dd3541 100644 --- a/src/StardewModdingAPI/Manifest.cs +++ b/src/StardewModdingAPI/Manifest.cs @@ -8,6 +8,7 @@ namespace StardewModdingAPI internal class ManifestImpl : Manifest, IManifest { /// <summary>The mod version.</summary> + [JsonProperty("Version", ObjectCreationHandling = ObjectCreationHandling.Auto/* avoids issue where Json.NET can't determine concrete type for interface */)] public new ISemanticVersion Version { get { return base.Version; } diff --git a/src/StardewModdingAPI/ModHelper.cs b/src/StardewModdingAPI/ModHelper.cs index 1fcc0182..78b3eefa 100644 --- a/src/StardewModdingAPI/ModHelper.cs +++ b/src/StardewModdingAPI/ModHelper.cs @@ -11,6 +11,17 @@ namespace StardewModdingAPI public class ModHelper : IModHelper { /********* + ** Properties + *********/ + /// <summary>The JSON settings to use when serialising and deserialising files.</summary> + private readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings + { + Formatting = Formatting.Indented, + ObjectCreationHandling = ObjectCreationHandling.Replace // avoid issue where default ICollection<T> values are duplicated each time the config is loaded + }; + + + /********* ** Accessors *********/ /// <summary>The mod directory path.</summary> @@ -76,13 +87,13 @@ namespace StardewModdingAPI { json = File.ReadAllText(fullPath); } - catch (FileNotFoundException) + catch (Exception ex) when (ex is DirectoryNotFoundException || ex is FileNotFoundException) { return null; } // deserialise model - TModel model = JsonConvert.DeserializeObject<TModel>(json); + TModel model = JsonConvert.DeserializeObject<TModel>(json, this.JsonSettings); if (model is IConfigFile) { var wrapper = (IConfigFile)model; @@ -108,7 +119,7 @@ namespace StardewModdingAPI Directory.CreateDirectory(dir); // write file - string json = JsonConvert.SerializeObject(model, Formatting.Indented); + string json = JsonConvert.SerializeObject(model, this.JsonSettings); File.WriteAllText(path, json); } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 090098ca..e5c27e71 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -93,12 +93,14 @@ namespace StardewModdingAPI ** Public methods *********/ /// <summary>The main entry point which hooks into and launches the game.</summary> - private static void Main() + /// <param name="args">The command-line arguments.</param> + private static void Main(string[] args) { - // set thread culture for consistent log formatting - Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); + // set log options + Program.Monitor.WriteToConsole = !args.Contains("--no-terminal"); + Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture("en-GB"); // for consistent log formatting - // add info header + // add info headers Program.Monitor.Log($"SMAPI {Constants.ApiVersion} with Stardew Valley {Game1.version} on {Environment.OSVersion}", LogLevel.Info); // initialise user settings @@ -123,9 +125,11 @@ namespace StardewModdingAPI } if (!Program.Settings.CheckForUpdates) Program.Monitor.Log($"You configured SMAPI to not check for updates. Running an old version of SMAPI is not recommended. You can enable update checks by editing or deleting {Constants.ApiConfigPath}.", LogLevel.Warn); + if (!Program.Monitor.WriteToConsole) + Program.Monitor.Log($"Writing to the terminal is disabled because the --no-terminal argument was received. This usually means launching the terminal failed.", LogLevel.Warn); // initialise legacy log - Log.Monitor = new Monitor("legacy mod", Program.LogFile) { ShowTraceInConsole = Program.Settings.DeveloperMode }; + Log.Monitor = Program.GetSecondaryMonitor("legacy mod"); Log.ModRegistry = Program.ModRegistry; // hook into & launch the game @@ -183,7 +187,7 @@ namespace StardewModdingAPI internal static IMonitor GetLegacyMonitorForMod() { string modName = Program.ModRegistry.GetModFromStack() ?? "unknown"; - return new Monitor(modName, Program.LogFile); + return Program.GetSecondaryMonitor(modName); } @@ -398,7 +402,8 @@ namespace StardewModdingAPI { if (!compatibility.IsCompatible(manifest.Version)) { - string warning = $"Skipped {compatibility.Name} ≤v{compatibility.Version} because this version is not compatible with the latest version of the game. Please check for a newer version of the mod here:"; + string reasonPhrase = compatibility.ReasonPhrase ?? "this version is not compatible with the latest version of the game"; + string warning = $"Skipped {compatibility.Name} {manifest.Version} because {reasonPhrase}. Please check for a newer version of the mod here:"; if (!string.IsNullOrWhiteSpace(compatibility.UpdateUrl)) warning += $"{Environment.NewLine}- official mod: {compatibility.UpdateUrl}"; if (!string.IsNullOrWhiteSpace(compatibility.UnofficialUpdateUrl)) @@ -518,7 +523,7 @@ namespace StardewModdingAPI // inject data mod.ModManifest = manifest; mod.Helper = helper; - mod.Monitor = new Monitor(manifest.Name, Program.LogFile) { ShowTraceInConsole = Program.Settings.DeveloperMode }; + mod.Monitor = Program.GetSecondaryMonitor(manifest.Name); mod.PathOnDisk = directory; // track mod @@ -590,5 +595,12 @@ namespace StardewModdingAPI Console.ReadKey(); Environment.Exit(0); } + + /// <summary>Get a monitor instance derived from SMAPI's current settings.</summary> + /// <param name="name">The name of the module which will log messages with this instance.</param> + private static Monitor GetSecondaryMonitor(string name) + { + return new Monitor(name, Program.LogFile) { WriteToConsole = Program.Monitor.WriteToConsole, ShowTraceInConsole = Program.Settings.DeveloperMode }; + } } } diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index 07b1ff5e..125f287e 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -152,6 +152,7 @@ <Compile Include="Events\MenuEvents.cs" /> <Compile Include="Events\MineEvents.cs" /> <Compile Include="Events\PlayerEvents.cs" /> + <Compile Include="Events\SaveEvents.cs" /> <Compile Include="Events\TimeEvents.cs" /> <Compile Include="Extensions.cs" /> <Compile Include="Framework\AssemblyRewriting\RewriteResult.cs" /> diff --git a/src/StardewModdingAPI/StardewModdingAPI.data.json b/src/StardewModdingAPI/StardewModdingAPI.data.json index 49b45018..3295336f 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.data.json +++ b/src/StardewModdingAPI/StardewModdingAPI.data.json @@ -6,18 +6,19 @@ This file contains advanced metadata for SMAPI. You shouldn't change this file. */ [ + /* versions not compatible with Stardew Valley 1.1+ */ { "ID": "SPDSprinklersMod", "Name": "Better Sprinklers", - "Version": "2.1", + "UpperVersion": "2.1", "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/41", "UnofficialUpdateUrl": "http://community.playstarbound.com/threads/125031", - "ForceCompatibleVersion": "^2.1-EntoPatch" + "ForceCompatibleVersion": "^2.1-EntoPatch" }, { "ID": "SPDChestLabel", "Name": "Chest Label System", - "Version": "1.5", + "UpperVersion": "1.5", "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/242", "UnofficialUpdateUrl": "http://community.playstarbound.com/threads/125031", "ForceCompatibleVersion": "^1.5-EntoPatch" @@ -25,17 +26,25 @@ This file contains advanced metadata for SMAPI. You shouldn't change this file. { "ID": "CJBCheatsMenu", "Name": "CJB Cheats Menu", - "Version": "1.12", + "UpperVersion": "1.12", "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/4", - "UnofficialUpdateUrl": "http://community.playstarbound.com/threads/125031", "ForceCompatibleVersion": "^1.12-EntoPatch" }, { "ID": "CJBItemSpawner", "Name": "CJB Item Spawner", - "Version": "1.5", + "UpperVersion": "1.5", "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/93", - "UnofficialUpdateUrl": "http://community.playstarbound.com/threads/125031", "ForceCompatibleVersion": "^1.5-EntoPatch" + }, + + /* versions which crash the game */ + { + "ID": "NPCMapLocationsMod", + "Name": "NPC Map Locations", + "LowerVersion": "1.42", + "UpperVersion": "1.43", + "UpdateUrl": "http://www.nexusmods.com/stardewvalley/mods/239", + "ReasonPhrase": "this version has an update check error which crashes the game" } ] diff --git a/src/StardewModdingAPI/unix-launcher.sh b/src/StardewModdingAPI/unix-launcher.sh index 93e33c78..bf0e9d5e 100644 --- a/src/StardewModdingAPI/unix-launcher.sh +++ b/src/StardewModdingAPI/unix-launcher.sh @@ -64,4 +64,12 @@ else else $LAUNCHER fi + + # some Linux users get error 127 (command not found) from the above block, even though + # `command -v` indicates the command is valid. As a fallback, launch SMAPI without a terminal when + # that happens and pass in an argument indicating SMAPI shouldn't try writing to the terminal + # (which can be slow if there is none). + if [ $? -eq 127 ]; then + $LAUNCHER --no-terminal + fi fi diff --git a/src/TrainerMod/TrainerMod.cs b/src/TrainerMod/TrainerMod.cs index f0c7549f..c76bb78c 100644 --- a/src/TrainerMod/TrainerMod.cs +++ b/src/TrainerMod/TrainerMod.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using Microsoft.Xna.Framework; using StardewModdingAPI; @@ -108,6 +109,9 @@ namespace TrainerMod Command.RegisterCommand("world_setseason", "Sets the season to the specified value | world_setseason <value>", new[] { "(winter, spring, summer, fall)<value> The target season" }).CommandFired += this.HandleWorldSetSeason; Command.RegisterCommand("world_downminelevel", "Goes down one mine level? | world_downminelevel", new[] { "" }).CommandFired += this.HandleWorldDownMineLevel; Command.RegisterCommand("world_setminelevel", "Sets mine level? | world_setminelevel", new[] { "(Int32)<value> The target level" }).CommandFired += this.HandleWorldSetMineLevel; + + Command.RegisterCommand("show_game_files", "Opens the game folder. | show_game_files").CommandFired += this.HandleShowGameFiles; + Command.RegisterCommand("show_data_files", "Opens the folder containing the save and log files. | show_data_files").CommandFired += this.HandleShowDataFiles; } /**** @@ -695,6 +699,22 @@ namespace TrainerMod this.LogValueNotSpecified(); } + /// <summary>The event raised when the 'show_game_files' command is triggered.</summary> + /// <param name="sender">The event sender.</param> + /// <param name="e">The event arguments.</param> + private void HandleShowGameFiles(object sender, EventArgsCommand e) + { + Process.Start(Constants.ExecutionPath); + } + + /// <summary>The event raised when the 'show_data_files' command is triggered.</summary> + /// <param name="sender">The event sender.</param> + /// <param name="e">The event arguments.</param> + private void HandleShowDataFiles(object sender, EventArgsCommand e) + { + Process.Start(Constants.DataPath); + } + /**** ** Helpers ****/ diff --git a/src/TrainerMod/TrainerMod.csproj b/src/TrainerMod/TrainerMod.csproj index e262e135..1457ac2b 100644 --- a/src/TrainerMod/TrainerMod.csproj +++ b/src/TrainerMod/TrainerMod.csproj @@ -37,54 +37,6 @@ <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow> <PlatformTarget>x86</PlatformTarget> </PropertyGroup> - <PropertyGroup> - <!-- Linux paths --> - <GamePath Condition="!Exists('$(GamePath)')">$(HOME)/GOG Games/Stardew Valley/game</GamePath> - <GamePath Condition="!Exists('$(GamePath)')">$(HOME)/.local/share/Steam/steamapps/common/Stardew Valley</GamePath> - <!-- Mac paths --> - <GamePath Condition="!Exists('$(GamePath)')">$(HOME)/Library/Application Support/Steam/steamapps/common/Stardew Valley/Contents/MacOS</GamePath> - <!-- Windows paths --> - <GamePath Condition="!Exists('$(GamePath)')">C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley</GamePath> - <GamePath Condition="!Exists('$(GamePath)')">C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley</GamePath> - </PropertyGroup> - <Choose> - <When Condition="$(OS) == 'Windows_NT'"> - <ItemGroup> - <Reference Include="Microsoft.Xna.Framework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86"> - <Private>False</Private> - </Reference> - <Reference Include="Microsoft.Xna.Framework.Game, Version=4.0.0.0, Culture=neutral, PublicKeyToken=842cf8be1de50553, processorArchitecture=x86"> - <Private>False</Private> - </Reference> - <Reference Include="Stardew Valley"> - <HintPath>$(GamePath)\Stardew Valley.exe</HintPath> - <Private>False</Private> - </Reference> - <Reference Include="xTile, Version=2.0.4.0, Culture=neutral, processorArchitecture=x86"> - <HintPath>$(GamePath)\xTile.dll</HintPath> - <Private>False</Private> - <SpecificVersion>False</SpecificVersion> - </Reference> - </ItemGroup> - </When> - <Otherwise> - <ItemGroup> - <Reference Include="MonoGame.Framework"> - <HintPath>$(GamePath)\MonoGame.Framework.dll</HintPath> - <Private>False</Private> - <SpecificVersion>False</SpecificVersion> - </Reference> - <Reference Include="StardewValley"> - <HintPath>$(GamePath)\StardewValley.exe</HintPath> - <Private>False</Private> - </Reference> - <Reference Include="xTile"> - <HintPath>$(GamePath)\xTile.dll</HintPath> - <Private>False</Private> - </Reference> - </ItemGroup> - </Otherwise> - </Choose> <ItemGroup> <Reference Include="Newtonsoft.Json, Version=8.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL"> <HintPath>..\packages\Newtonsoft.Json.8.0.3\lib\net45\Newtonsoft.Json.dll</HintPath> @@ -123,15 +75,12 @@ </None> <None Include="packages.config" /> </ItemGroup> + <Import Project="$(SolutionDir)\crossplatform.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <PropertyGroup> <PostBuildEvent> </PostBuildEvent> </PropertyGroup> - <Target Name="BeforeBuild"> - <!-- if game path is invalid, show one user-friendly error instead of a slew of reference errors --> - <Error Condition="!Exists('$(GamePath)')" Text="Failed to find the game install path automatically; edit the *.csproj file and manually add a <GamePath> setting with the full directory path containing the Stardew Valley executable." /> - </Target> <Target Name="AfterBuild" Condition="$(Configuration) == 'Debug'"> <Copy SourceFiles="$(TargetDir)\$(TargetName).dll" DestinationFolder="$(GamePath)\Mods\TrainerMod" /> <Copy SourceFiles="$(TargetDir)\$(TargetName).dll.mdb" DestinationFolder="$(GamePath)\Mods\TrainerMod" Condition="$(OS) != 'Windows_NT'" /> diff --git a/src/crossplatform.targets b/src/crossplatform.targets index 0eb1c776..ef2d341f 100644 --- a/src/crossplatform.targets +++ b/src/crossplatform.targets @@ -8,8 +8,8 @@ <!-- Windows paths --> <GamePath Condition="!Exists('$(GamePath)')">C:\Program Files (x86)\GalaxyClient\Games\Stardew Valley</GamePath> <GamePath Condition="!Exists('$(GamePath)')">C:\Program Files (x86)\Steam\steamapps\common\Stardew Valley</GamePath> - <GamePath Condition="!Exists('$(GamePath)') AND $(OS) == 'Windows_NT'">$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150@InstallLocation)</GamePath> - <GamePath Condition="!Exists('$(GamePath)') AND $(OS) == 'Windows_NT'">$(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\GOG.com\Games\1453375253@PATH)</GamePath> + <GamePath Condition="!Exists('$(GamePath)') AND '$(OS)' == 'Windows_NT'">$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\GOG.com\Games\1453375253', 'PATH', null, RegistryView.Registry32))</GamePath> + <GamePath Condition="!Exists('$(GamePath)') AND '$(OS)' == 'Windows_NT'">$([MSBuild]::GetRegistryValueFromView('HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 413150', 'InstallLocation', null, RegistryView.Registry64, RegistryView.Registry32))</GamePath> </PropertyGroup> <Choose> <When Condition="$(OS) == 'Windows_NT'"> |