From 89221b8b2d88668238d89af38484784c8bd16116 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 14:38:47 -0400 Subject: add editorconfig, minor style fixes --- src/.editorconfig | 58 ++++++++++++++++++++++ src/StardewModdingAPI.sln | 3 +- src/StardewModdingAPI.sln.DotSettings | 1 + .../Framework/DeprecationManager.cs | 2 +- src/StardewModdingAPI/Framework/Monitor.cs | 4 +- 5 files changed, 63 insertions(+), 5 deletions(-) create mode 100644 src/.editorconfig (limited to 'src') diff --git a/src/.editorconfig b/src/.editorconfig new file mode 100644 index 00000000..3037884e --- /dev/null +++ b/src/.editorconfig @@ -0,0 +1,58 @@ +# topmost editorconfig +root: true + +########## +## General formatting +## documentation: http://editorconfig.org +########## +[*] +indent_style = space +indent_size = 4 +insert_final_newline = true +trim_trailing_whitespace = true + + +########## +## C# formatting +## documentation: https://docs.microsoft.com/en-us/visualstudio/ide/editorconfig-code-style-settings-reference +## some undocumented settings from: https://github.com/dotnet/roslyn/blob/master/.editorconfig +########## +[*.cs] + +#sort 'system' usings first +dotnet_sort_system_directives_first = true + +# use 'this.' qualifier +dotnet_style_qualification_for_field = true:error +dotnet_style_qualification_for_property = true:error +dotnet_style_qualification_for_method = true:error +dotnet_style_qualification_for_event = true:error + +# use language keywords (like int) instead of type (like Int32) +dotnet_style_predefined_type_for_locals_parameters_members = true:error +dotnet_style_predefined_type_for_member_access = true:error + +# don't use 'var' for language keywords +csharp_style_var_for_built_in_types = false:error + +# suggest modern C# features where simpler +dotnet_style_object_initializer = true:suggestion +dotnet_style_collection_initializer = true:suggestion +dotnet_style_coalesce_expression = true:suggestion +dotnet_style_null_propagation = true:suggestion +dotnet_style_explicit_tuple_names = true:suggestion +csharp_style_pattern_matching_over_is_with_cast_check = true:suggestion +csharp_style_pattern_matching_over_as_with_null_check = true:suggestion +csharp_style_conditional_delegate_call = true:suggestion + +# prefer method block bodies +csharp_style_expression_bodied_methods = false:suggestion +csharp_style_expression_bodied_constructors = false:suggestion + +# prefer property expression bodies +csharp_style_expression_bodied_properties = true:suggestion +csharp_style_expression_bodied_indexers = true:suggestion +csharp_style_expression_bodied_accessors = true:suggestion + +# prefer inline out variables +csharp_style_inlined_variable_declaration = true:warning diff --git a/src/StardewModdingAPI.sln b/src/StardewModdingAPI.sln index 441b51a9..57f94648 100644 --- a/src/StardewModdingAPI.sln +++ b/src/StardewModdingAPI.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 15 -VisualStudioVersion = 15.0.26228.4 +VisualStudioVersion = 15.0.26403.7 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TrainerMod", "TrainerMod\TrainerMod.csproj", "{28480467-1A48-46A7-99F8-236D95225359}" EndProject @@ -9,6 +9,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StardewModdingAPI", "Starde EndProject Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "metadata", "metadata", "{86C452BE-D2D8-45B4-B63F-E329EB06CEDA}" ProjectSection(SolutionItems) = preProject + .editorconfig = .editorconfig ..\.gitattributes = ..\.gitattributes ..\.gitignore = ..\.gitignore crossplatform.targets = crossplatform.targets diff --git a/src/StardewModdingAPI.sln.DotSettings b/src/StardewModdingAPI.sln.DotSettings index 81b52fd4..9620737a 100644 --- a/src/StardewModdingAPI.sln.DotSettings +++ b/src/StardewModdingAPI.sln.DotSettings @@ -1,5 +1,6 @@  HINT + HINT Field, Property, Event, Method Field, Property, Event, Method True diff --git a/src/StardewModdingAPI/Framework/DeprecationManager.cs b/src/StardewModdingAPI/Framework/DeprecationManager.cs index e44cd369..6b95960b 100644 --- a/src/StardewModdingAPI/Framework/DeprecationManager.cs +++ b/src/StardewModdingAPI/Framework/DeprecationManager.cs @@ -78,7 +78,7 @@ namespace StardewModdingAPI.Framework break; default: - throw new NotImplementedException($"Unknown deprecation level '{severity}'"); + throw new NotSupportedException($"Unknown deprecation level '{severity}'"); } } diff --git a/src/StardewModdingAPI/Framework/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs index 51feff78..dac81f14 100644 --- a/src/StardewModdingAPI/Framework/Monitor.cs +++ b/src/StardewModdingAPI/Framework/Monitor.cs @@ -64,12 +64,10 @@ namespace StardewModdingAPI.Framework // validate if (string.IsNullOrWhiteSpace(source)) throw new ArgumentException("The log source cannot be empty."); - if (logFile == null) - throw new ArgumentNullException(nameof(logFile), "The log file manager cannot be null."); // initialise this.Source = source; - this.LogFile = logFile; + this.LogFile = logFile ?? throw new ArgumentNullException(nameof(logFile), "The log file manager cannot be null."); this.ConsoleManager = consoleManager; this.RequestExit = requestExitDelegate; } -- cgit From e7606884adee2ada103439aca2d53cc29b57da17 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 14:40:55 -0400 Subject: handle edge case in JSON file read/write code --- src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs index bd15c7bb..64d8738e 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/JsonHelper.cs @@ -31,9 +31,14 @@ namespace StardewModdingAPI.Framework.Serialisation /// The model type. /// The absolete file path. /// Returns the deserialised model, or null if the file doesn't exist or is empty. + /// The given path is empty or invalid. public TModel ReadJsonFile(string fullPath) where TModel : class { + // validate + if (string.IsNullOrWhiteSpace(fullPath)) + throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath)); + // read file string json; try @@ -53,11 +58,18 @@ namespace StardewModdingAPI.Framework.Serialisation /// The model type. /// The absolete file path. /// The model to save. + /// The given path is empty or invalid. public void WriteJsonFile(string fullPath, TModel model) where TModel : class { + // validate + if (string.IsNullOrWhiteSpace(fullPath)) + throw new ArgumentException("The file path is empty or invalid.", nameof(fullPath)); + // create directory if needed string dir = Path.GetDirectoryName(fullPath); + if (dir == null) + throw new ArgumentException("The file path is invalid.", nameof(fullPath)); if (!Directory.Exists(dir)) Directory.CreateDirectory(dir); -- cgit From 4ef957c191f3ad012b234d533810dd59717f30c1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 16:04:20 -0400 Subject: optimise console interception for the way Stardew Valley logs messages --- release-notes.md | 6 +++ .../Logging/ConsoleInterceptionManager.cs | 6 +-- .../Framework/Logging/InterceptingTextWriter.cs | 50 ++++++++-------------- src/StardewModdingAPI/Program.cs | 2 +- 4 files changed, 27 insertions(+), 37 deletions(-) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index d600db57..2f98df0b 100644 --- a/release-notes.md +++ b/release-notes.md @@ -10,6 +10,12 @@ For mod developers: images). --> +## 1.11 +See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11). + +For players: +* Optimised console logging. + ## 1.10 See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...1.10). diff --git a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs index d84671ee..b8f2c34e 100644 --- a/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs +++ b/src/StardewModdingAPI/Framework/Logging/ConsoleInterceptionManager.cs @@ -18,8 +18,8 @@ namespace StardewModdingAPI.Framework.Logging /// Whether the current console supports color formatting. public bool SupportsColor { get; } - /// The event raised when something writes a line to the console directly. - public event Action OnLineIntercepted; + /// The event raised when a message is written to the console directly. + public event Action OnMessageIntercepted; /********* @@ -30,7 +30,7 @@ namespace StardewModdingAPI.Framework.Logging { // redirect output through interceptor this.Output = new InterceptingTextWriter(Console.Out); - this.Output.OnLineIntercepted += line => this.OnLineIntercepted?.Invoke(line); + this.Output.OnMessageIntercepted += line => this.OnMessageIntercepted?.Invoke(line); Console.SetOut(this.Output); // test color support diff --git a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs index 14789109..9ca61b59 100644 --- a/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs +++ b/src/StardewModdingAPI/Framework/Logging/InterceptingTextWriter.cs @@ -1,5 +1,4 @@ using System; -using System.Collections.Generic; using System.IO; using System.Text; @@ -8,13 +7,6 @@ namespace StardewModdingAPI.Framework.Logging /// A text writer which allows intercepting output. internal class InterceptingTextWriter : TextWriter { - /********* - ** Properties - *********/ - /// The current line being intercepted. - private readonly List Line = new List(); - - /********* ** Accessors *********/ @@ -27,8 +19,8 @@ namespace StardewModdingAPI.Framework.Logging /// Whether to intercept console output. public bool ShouldIntercept { get; set; } - /// The event raised when a line of text is intercepted. - public event Action OnLineIntercepted; + /// The event raised when a message is written to the console directly. + public event Action OnMessageIntercepted; /********* @@ -41,39 +33,31 @@ namespace StardewModdingAPI.Framework.Logging this.Out = output; } + /// Writes a subarray of characters to the text string or stream. + /// The character array to write data from. + /// The character position in the buffer at which to start retrieving data. + /// The number of characters to write. + public override void Write(char[] buffer, int index, int count) + { + if (this.ShouldIntercept) + this.OnMessageIntercepted?.Invoke(new string(buffer, index, count).TrimEnd('\r', '\n')); + else + this.Out.Write(buffer, index, count); + } + /// Writes a character to the text string or stream. /// The character to write to the text stream. + /// Console log messages from the game should be caught by . This method passes through anything that bypasses that method for some reason, since it's better to show it to users than hide it from everyone. public override void Write(char ch) { - // intercept - if (this.ShouldIntercept) - { - switch (ch) - { - case '\r': - return; - - case '\n': - this.OnLineIntercepted?.Invoke(new string(this.Line.ToArray())); - this.Line.Clear(); - break; - - default: - this.Line.Add(ch); - break; - } - } - - // pass through - else - this.Out.Write(ch); + this.Out.Write(ch); } /// Releases the unmanaged resources used by the and optionally releases the managed resources. /// true to release both managed and unmanaged resources; false to release only unmanaged resources. protected override void Dispose(bool disposing) { - this.OnLineIntercepted = null; + this.OnMessageIntercepted = null; } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 31aeb3a6..6e7f9950 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -249,7 +249,7 @@ namespace StardewModdingAPI Monitor monitor = this.GetSecondaryMonitor("Console.Out"); monitor.WriteToFile = false; // not useful for troubleshooting mods per discussion if (monitor.WriteToConsole) - this.ConsoleManager.OnLineIntercepted += line => monitor.Log(line, LogLevel.Trace); + this.ConsoleManager.OnMessageIntercepted += line => monitor.Log(line, LogLevel.Trace); } // add warning headers -- cgit From afc8ae69fead4099ac86dcea6aa874f5b1540e29 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 16:21:03 -0400 Subject: No longer suppress console output from the log file Console messages appear in the console (in developer mode only), but weren't saved to the log file based on the argument that they weren't relevant. However, that also suppresses the game's load-game errors in Stardew Valley 1.2, which makes troubleshooting save issues more complicated. To avoid any such issues in the future, they're now always logged to the file. If you need to log a message that isn't shown to the user, use System.Diagnostics.Debug instead. --- release-notes.md | 3 +++ src/StardewModdingAPI/Program.cs | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index 2f98df0b..dc800704 100644 --- a/release-notes.md +++ b/release-notes.md @@ -16,6 +16,9 @@ See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11). For players: * Optimised console logging. +For mod developers: +* `Console.Out` messages are now written to the log file. + ## 1.10 See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...1.10). diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 6e7f9950..18a5c999 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -247,7 +247,6 @@ namespace StardewModdingAPI // redirect direct console output { Monitor monitor = this.GetSecondaryMonitor("Console.Out"); - monitor.WriteToFile = false; // not useful for troubleshooting mods per discussion if (monitor.WriteToConsole) this.ConsoleManager.OnMessageIntercepted += line => monitor.Log(line, LogLevel.Trace); } -- cgit From 971bfd32d2f44d2fa1795807ce1ba1b700ff4f86 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 16:22:41 -0400 Subject: detect exceptions logged directly to the console and log them as errors --- release-notes.md | 3 ++- src/StardewModdingAPI/Program.cs | 11 ++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index dc800704..311128d0 100644 --- a/release-notes.md +++ b/release-notes.md @@ -15,9 +15,10 @@ See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11). For players: * Optimised console logging. +* Errors when loading a save are now shown in the SMAPI console. For mod developers: -* `Console.Out` messages are now written to the log file. +* `Console.Out` messages are now written to the log file. ## 1.10 See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...1.10). diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 18a5c999..6e57fd65 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -248,7 +248,7 @@ namespace StardewModdingAPI { Monitor monitor = this.GetSecondaryMonitor("Console.Out"); if (monitor.WriteToConsole) - this.ConsoleManager.OnMessageIntercepted += line => monitor.Log(line, LogLevel.Trace); + this.ConsoleManager.OnMessageIntercepted += message => this.HandleConsoleMessage(monitor, message); } // add warning headers @@ -603,6 +603,15 @@ namespace StardewModdingAPI } } + /// Redirect messages logged directly to the console to the given monitor. + /// The monitor with which to log messages. + /// The message to log. + private void HandleConsoleMessage(IMonitor monitor, string message) + { + LogLevel level = message.Contains("Exception") ? LogLevel.Error : LogLevel.Trace; // intercept potential exceptions + monitor.Log(message, level); + } + /// Show a 'press any key to exit' message, and exit when they press a key. private void PressAnyKeyToExit() { -- cgit From 0cf15d36d9e6f5a40a16e17f3bd3d53682fe648c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Wed, 26 Apr 2017 18:25:59 -0400 Subject: revamp 'exit immediately' to abort ongoing SMAPI tasks --- release-notes.md | 1 + .../Framework/InternalExtensions.cs | 8 ++++ src/StardewModdingAPI/Framework/Monitor.cs | 31 ++++++++------ src/StardewModdingAPI/Framework/SGame.cs | 7 +++ src/StardewModdingAPI/IMonitor.cs | 7 +++ src/StardewModdingAPI/Program.cs | 50 ++++++++++------------ 6 files changed, 64 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index 311128d0..acb584cd 100644 --- a/release-notes.md +++ b/release-notes.md @@ -19,6 +19,7 @@ For players: For mod developers: * `Console.Out` messages are now written to the log file. +* `Monitor.ExitGameImmediately` now aborts SMAPI initialisation and events more quickly. ## 1.10 See [log](https://github.com/Pathoschild/SMAPI/compare/1.9...1.10). diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index a2d589ff..2f6f7490 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -41,6 +41,14 @@ namespace StardewModdingAPI.Framework foreach (EventHandler handler in handlers.Cast()) { + // handle SMAPI exiting + if (monitor.IsExiting) + { + monitor.Log($"SMAPI shutting down: aborting {name} event.", LogLevel.Warn); + return; + } + + // raise event try { handler.Invoke(sender, args ?? EventArgs.Empty); diff --git a/src/StardewModdingAPI/Framework/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs index dac81f14..925efc33 100644 --- a/src/StardewModdingAPI/Framework/Monitor.cs +++ b/src/StardewModdingAPI/Framework/Monitor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading; using StardewModdingAPI.Framework.Logging; namespace StardewModdingAPI.Framework @@ -34,13 +35,16 @@ namespace StardewModdingAPI.Framework [LogLevel.Alert] = ConsoleColor.Magenta }; - /// A delegate which requests that SMAPI immediately exit the game. This should only be invoked when an irrecoverable fatal error happens that risks save corruption or game-breaking bugs. - private readonly RequestExitDelegate RequestExit; + /// Propagates notification that SMAPI should exit. + private readonly CancellationTokenSource ExitTokenSource; /********* ** Accessors *********/ + /// Whether SMAPI is aborting. Mods don't need to worry about this unless they have background tasks. + public bool IsExiting => this.ExitTokenSource.IsCancellationRequested; + /// Whether to show trace messages in the console. internal bool ShowTraceInConsole { get; set; } @@ -58,8 +62,8 @@ namespace StardewModdingAPI.Framework /// The name of the module which logs messages using this instance. /// Manages access to the console output. /// The log file to which to write messages. - /// A delegate which requests that SMAPI immediately exit the game. - public Monitor(string source, ConsoleInterceptionManager consoleManager, LogFileManager logFile, RequestExitDelegate requestExitDelegate) + /// Propagates notification that SMAPI should exit. + public Monitor(string source, ConsoleInterceptionManager consoleManager, LogFileManager logFile, CancellationTokenSource exitTokenSource) { // validate if (string.IsNullOrWhiteSpace(source)) @@ -69,7 +73,7 @@ namespace StardewModdingAPI.Framework this.Source = source; this.LogFile = logFile ?? throw new ArgumentNullException(nameof(logFile), "The log file manager cannot be null."); this.ConsoleManager = consoleManager; - this.RequestExit = requestExitDelegate; + this.ExitTokenSource = exitTokenSource; } /// Log a message for the player or developer. @@ -84,14 +88,8 @@ namespace StardewModdingAPI.Framework /// The reason for the shutdown. public void ExitGameImmediately(string reason) { - this.RequestExit(this.Source, reason); - } - - /// Log a fatal error message. - /// The message to log. - internal void LogFatal(string message) - { - this.LogImpl(this.Source, message, LogLevel.Error, ConsoleColor.White, background: ConsoleColor.Red); + this.LogFatal($"{this.Source} requested an immediate game shutdown: {reason}"); + this.ExitTokenSource.Cancel(); } /// Log a message for the player or developer, using the specified console color. @@ -109,6 +107,13 @@ namespace StardewModdingAPI.Framework /********* ** Private methods *********/ + /// Log a fatal error message. + /// The message to log. + private void LogFatal(string message) + { + this.LogImpl(this.Source, message, LogLevel.Error, ConsoleColor.White, background: ConsoleColor.Red); + } + /// Write a message line to the log. /// The name of the mod logging the message. /// The message to log. diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 61493e87..e7c07889 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -200,6 +200,13 @@ namespace StardewModdingAPI.Framework /// A snapshot of the game timing state. protected override void Update(GameTime gameTime) { + // SMAPI exiting, stop processing game updates + if (this.Monitor.IsExiting) + { + this.Monitor.Log("SMAPI shutting down: aborting update.", LogLevel.Trace); + return; + } + // While a background new-day task is in progress, the game skips its own update logic // and defers to the XNA Update method. Running mod code in parallel to the background // update is risky, because data changes can conflict (e.g. collection changed during diff --git a/src/StardewModdingAPI/IMonitor.cs b/src/StardewModdingAPI/IMonitor.cs index 571c7403..62c479bc 100644 --- a/src/StardewModdingAPI/IMonitor.cs +++ b/src/StardewModdingAPI/IMonitor.cs @@ -3,6 +3,13 @@ /// Encapsulates monitoring and logging for a given module. public interface IMonitor { + /********* + ** Accessors + *********/ + /// Whether SMAPI is aborting. Mods don't need to worry about this unless they have background tasks. + bool IsExiting { get; } + + /********* ** Methods *********/ diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 6e57fd65..57ec1a17 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -32,7 +32,7 @@ namespace StardewModdingAPI /// Manages console output interception. private readonly ConsoleInterceptionManager ConsoleManager = new ConsoleInterceptionManager(); - /// The core logger for SMAPI. + /// The core logger and monitor for SMAPI. private readonly Monitor Monitor; /// Tracks whether the game should exit immediately and any pending initialisation should be cancelled. @@ -99,7 +99,7 @@ namespace StardewModdingAPI public Program(bool writeToConsole, string logPath) { this.LogFile = new LogFileManager(logPath); - this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.ExitGameImmediately) { WriteToConsole = writeToConsole }; + this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.CancellationTokenSource) { WriteToConsole = writeToConsole }; } /// Launch SMAPI. @@ -142,6 +142,17 @@ namespace StardewModdingAPI this.GameInstance = new SGame(this.Monitor); StardewValley.Program.gamePtr = this.GameInstance; + // add exit handler + new Thread(() => + { + this.CancellationTokenSource.Token.WaitHandle.WaitOne(); + if (this.IsGameRunning) + { + this.GameInstance.Exiting += (sender, e) => this.PressAnyKeyToExit(); + this.GameInstance.Exit(); + } + }).Start(); + // hook into game events #if SMAPI_FOR_WINDOWS ((Form)Control.FromHandle(this.GameInstance.Window.Handle)).FormClosing += (sender, args) => this.Dispose(); @@ -180,20 +191,6 @@ namespace StardewModdingAPI } } - /// Immediately exit the game without saving. This should only be invoked when an irrecoverable fatal error happens that risks save corruption or game-breaking bugs. - /// The module which requested an immediate exit. - /// The reason provided for the shutdown. - public void ExitGameImmediately(string module, string reason) - { - this.Monitor.LogFatal($"{module} requested an immediate game shutdown: {reason}"); - this.CancellationTokenSource.Cancel(); - if (this.IsGameRunning) - { - this.GameInstance.Exiting += (sender, e) => this.PressAnyKeyToExit(); - this.GameInstance.Exit(); - } - } - /// Get a monitor for legacy code which doesn't have one passed in. [Obsolete("This method should only be used when needed for backwards compatibility.")] internal IMonitor GetLegacyMonitorForMod() @@ -264,9 +261,9 @@ namespace StardewModdingAPI // load mods int modsLoaded = this.LoadMods(); - if (this.CancellationTokenSource.IsCancellationRequested) + if (this.Monitor.IsExiting) { - this.Monitor.Log("Shutdown requested; interrupting initialisation.", LogLevel.Error); + this.Monitor.Log("SMAPI shutting down: aborting initialisation.", LogLevel.Warn); return; } @@ -307,7 +304,7 @@ namespace StardewModdingAPI inputThread.Start(); // keep console thread alive while the game is running - while (this.IsGameRunning) + while (this.IsGameRunning && !this.Monitor.IsExiting) Thread.Sleep(1000 / 10); if (inputThread.ThreadState == ThreadState.Running) inputThread.Abort(); @@ -368,18 +365,17 @@ namespace StardewModdingAPI List deprecationWarnings = new List(); // queue up deprecation warnings to show after mod list foreach (string directoryPath in Directory.GetDirectories(Constants.ModPath)) { + if (this.Monitor.IsExiting) + { + this.Monitor.Log("SMAPI shutting down: aborting mod scan.", LogLevel.Warn); + return modsLoaded; + } + // passthrough empty directories DirectoryInfo directory = new DirectoryInfo(directoryPath); while (!directory.GetFiles().Any() && directory.GetDirectories().Length == 1) directory = directory.GetDirectories().First(); - // check for cancellation - if (this.CancellationTokenSource.IsCancellationRequested) - { - this.Monitor.Log("Shutdown requested; interrupting mod loading.", LogLevel.Error); - return modsLoaded; - } - // get manifest path string manifestPath = Path.Combine(directory.FullName, "manifest.json"); if (!File.Exists(manifestPath)) @@ -625,7 +621,7 @@ namespace StardewModdingAPI /// The name of the module which will log messages with this instance. private Monitor GetSecondaryMonitor(string name) { - return new Monitor(name, this.ConsoleManager, this.LogFile, this.ExitGameImmediately) { WriteToConsole = this.Monitor.WriteToConsole, ShowTraceInConsole = this.Settings.DeveloperMode }; + return new Monitor(name, this.ConsoleManager, this.LogFile, this.CancellationTokenSource) { WriteToConsole = this.Monitor.WriteToConsole, ShowTraceInConsole = this.Settings.DeveloperMode }; } /// Get a human-readable name for the current platform. -- cgit From 40f174b22d4ceb93281a03414048c04598780170 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 27 Apr 2017 15:46:19 -0400 Subject: simplify exception summary code --- .../Framework/InternalExtensions.cs | 25 +++++++++++----------- 1 file changed, 12 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index 2f6f7490..5199c72d 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -92,21 +92,20 @@ namespace StardewModdingAPI.Framework /// The error to summarise. public static string GetLogSummary(this Exception exception) { - // type load exception - if (exception is TypeLoadException typeLoadEx) - return $"Failed loading type: {typeLoadEx.TypeName}: {exception}"; - - // reflection type load exception - if (exception is ReflectionTypeLoadException reflectionTypeLoadEx) + switch (exception) { - string summary = exception.ToString(); - foreach (Exception childEx in reflectionTypeLoadEx.LoaderExceptions) - summary += $"\n\n{childEx.GetLogSummary()}"; - return summary; - } + case TypeLoadException ex: + return $"Failed loading type '{ex.TypeName}': {exception}"; - // anything else - return exception.ToString(); + case ReflectionTypeLoadException ex: + string summary = exception.ToString(); + foreach (Exception childEx in ex.LoaderExceptions) + summary += $"\n\n{childEx.GetLogSummary()}"; + return summary; + + default: + return exception.ToString(); + } } /**** -- cgit From ee5351c38e9657a7b7a2d929d5704c3439456a39 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 28 Apr 2017 00:58:54 -0400 Subject: detect broken ObjectInformation.xnb data --- release-notes.md | 3 ++- src/StardewModdingAPI/Program.cs | 57 ++++++++++++++++++++++++++++++++++++++-- 2 files changed, 57 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index acb584cd..7818ac26 100644 --- a/release-notes.md +++ b/release-notes.md @@ -14,8 +14,9 @@ For mod developers: See [log](https://github.com/Pathoschild/SMAPI/compare/1.10...1.11). For players: -* Optimised console logging. +* SMAPI now detects issues in `ObjectInformation.xnb` files caused by outdated XNB mods. * Errors when loading a save are now shown in the SMAPI console. +* Improved console logging performance. For mod developers: * `Console.Out` messages are now written to the log file. diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 57ec1a17..ed8fb592 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -16,7 +16,9 @@ using StardewModdingAPI.Framework; using StardewModdingAPI.Framework.Logging; using StardewModdingAPI.Framework.Models; using StardewModdingAPI.Framework.Serialisation; +using StardewValley; using Monitor = StardewModdingAPI.Framework.Monitor; +using SObject = StardewValley.Object; namespace StardewModdingAPI { @@ -248,17 +250,21 @@ namespace StardewModdingAPI this.ConsoleManager.OnMessageIntercepted += message => this.HandleConsoleMessage(monitor, message); } - // add warning headers + // add headers if (this.Settings.DeveloperMode) { this.Monitor.ShowTraceInConsole = true; - this.Monitor.Log($"You configured SMAPI to run in developer mode. The console may be much more verbose. You can disable developer mode by installing the non-developer version of SMAPI, or by editing {Constants.ApiConfigPath}.", LogLevel.Warn); + this.Monitor.Log($"You configured SMAPI to run in developer mode. The console may be much more verbose. You can disable developer mode by installing the non-developer version of SMAPI, or by editing {Constants.ApiConfigPath}.", LogLevel.Info); } if (!this.Settings.CheckForUpdates) this.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 reinstalling SMAPI or editing {Constants.ApiConfigPath}.", LogLevel.Warn); if (!this.Monitor.WriteToConsole) this.Monitor.Log("Writing to the terminal is disabled because the --no-terminal argument was received. This usually means launching the terminal failed.", LogLevel.Warn); + // validate XNB integrity + if (!this.ValidateContentIntegrity()) + this.Monitor.Log("SMAPI found problems in the game's XNB files which may cause errors or crashes while you're playing. Consider uninstalling XNB mods or reinstalling the game.", LogLevel.Warn); + // load mods int modsLoaded = this.LoadMods(); if (this.Monitor.IsExiting) @@ -310,6 +316,53 @@ namespace StardewModdingAPI inputThread.Abort(); } + /// Look for common issues with the game's XNB content, and log warnings if anything looks broken or outdated. + /// Returns whether all integrity checks passed. + private bool ValidateContentIntegrity() + { + this.Monitor.Log("Detecting common issues..."); + bool issuesFound = false; + + + // object format (commonly broken by outdated files) + { + void LogIssue(int id, string issue) => this.Monitor.Log($"Detected issue: item #{id} in Content\\Data\\ObjectInformation is invalid ({issue}).", LogLevel.Warn); + foreach (KeyValuePair entry in Game1.objectInformation) + { + // must not be empty + if (string.IsNullOrWhiteSpace(entry.Value)) + { + LogIssue(entry.Key, "entry is empty"); + issuesFound = true; + continue; + } + + // require core fields + string[] fields = entry.Value.Split('/'); + if (fields.Length < SObject.objectInfoDescriptionIndex + 1) + { + LogIssue(entry.Key, $"too few fields for an object"); + issuesFound = true; + continue; + } + + // check min length for specific types + switch (fields[SObject.objectInfoTypeIndex].Split(new[] { ' ' }, 2)[0]) + { + case "Cooking": + if (fields.Length < SObject.objectInfoBuffDurationIndex + 1) + { + LogIssue(entry.Key, "too few fields for a cooking item"); + issuesFound = true; + } + break; + } + } + } + + return !issuesFound; + } + /// Asynchronously check for a new version of SMAPI, and print a message to the console if an update is available. private void CheckForUpdateAsync() { -- cgit From 9fecaa79890ab7e6a38768aa840cfcbd8f6272b1 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 01:30:30 -0400 Subject: make mod helpers disposable (#257) --- src/StardewModdingAPI/Framework/ModHelper.cs | 11 ++++++++++- src/StardewModdingAPI/Program.cs | 6 ++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs index c8c44dba..52e482f2 100644 --- a/src/StardewModdingAPI/Framework/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelper.cs @@ -6,7 +6,7 @@ using StardewModdingAPI.Framework.Serialisation; namespace StardewModdingAPI.Framework { /// Provides simplified APIs for writing mods. - internal class ModHelper : IModHelper + internal class ModHelper : IModHelper, IDisposable { /********* ** Properties @@ -107,5 +107,14 @@ namespace StardewModdingAPI.Framework path = Path.Combine(this.DirectoryPath, path); this.JsonHelper.WriteJsonFile(path, model); } + + /**** + ** Disposal + ****/ + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + public void Dispose() + { + // nothing to dispose yet + } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index ed8fb592..7fa78dce 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -204,10 +204,16 @@ namespace StardewModdingAPI /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. public void Dispose() { + // skip if already disposed if (this.IsDisposed) return; this.IsDisposed = true; + // dispose mod helpers + foreach (var mod in this.ModRegistry.GetMods()) + (mod.Helper as IDisposable)?.Dispose(); + + // dispose core components this.IsGameRunning = false; this.LogFile?.Dispose(); this.ConsoleManager?.Dispose(); -- cgit From 6b9372237c79517a44a4ce3e096634f0273f5ba3 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 12:23:38 -0400 Subject: fix errors in the game's update causing the game to freeze until the player presses a key in the SMAPI console --- release-notes.md | 1 + src/StardewModdingAPI/Framework/SGame.cs | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index 7818ac26..465448d1 100644 --- a/release-notes.md +++ b/release-notes.md @@ -17,6 +17,7 @@ For players: * SMAPI now detects issues in `ObjectInformation.xnb` files caused by outdated XNB mods. * Errors when loading a save are now shown in the SMAPI console. * Improved console logging performance. +* Fixed errors during game update causing the game to hang. For mod developers: * `Console.Out` messages are now written to the log file. diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index e7c07889..20c6b886 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -242,7 +242,6 @@ namespace StardewModdingAPI.Framework catch (Exception ex) { this.Monitor.Log($"An error occured in the base update loop: {ex.GetLogSummary()}", LogLevel.Error); - Console.ReadKey(); } // raise update events -- cgit From 9b615fadaa3bb8fbf4fe011320aa1cc709113f3f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 14:13:55 -0400 Subject: add initial content API (#257) --- src/StardewModdingAPI/ContentSource.cs | 12 ++ src/StardewModdingAPI/Framework/ContentHelper.cs | 147 +++++++++++++++++++++ src/StardewModdingAPI/Framework/ModHelper.cs | 17 ++- src/StardewModdingAPI/Framework/SContentManager.cs | 33 ++++- src/StardewModdingAPI/IContentHelper.cs | 14 ++ src/StardewModdingAPI/IModHelper.cs | 5 +- src/StardewModdingAPI/Program.cs | 2 +- src/StardewModdingAPI/StardewModdingAPI.csproj | 3 + 8 files changed, 222 insertions(+), 11 deletions(-) create mode 100644 src/StardewModdingAPI/ContentSource.cs create mode 100644 src/StardewModdingAPI/Framework/ContentHelper.cs create mode 100644 src/StardewModdingAPI/IContentHelper.cs (limited to 'src') diff --git a/src/StardewModdingAPI/ContentSource.cs b/src/StardewModdingAPI/ContentSource.cs new file mode 100644 index 00000000..35c8bc21 --- /dev/null +++ b/src/StardewModdingAPI/ContentSource.cs @@ -0,0 +1,12 @@ +namespace StardewModdingAPI +{ + /// Specifies a source containing content that can be loaded. + public enum ContentSource + { + /// Assets in the game's content manager (i.e. XNBs in the game's content folder). + GameContent, + + /// XNB files in the current mod's folder. + ModFolder + } +} diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs new file mode 100644 index 00000000..0d063ef0 --- /dev/null +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -0,0 +1,147 @@ +using System; +using System.IO; +using System.Linq; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; +using StardewValley; + +namespace StardewModdingAPI.Framework +{ + /// Provides an API for loading content assets. + internal class ContentHelper : IContentHelper + { + /********* + ** Properties + *********/ + /// SMAPI's underlying content manager. + private readonly SContentManager ContentManager; + + /// The absolute path to the mod folder. + private readonly string ModFolderPath; + + /// The path to the mod's folder, relative to the game's content folder (e.g. "../Mods/ModName"). + private readonly string RelativeContentFolder; + + /// The friendly mod name for use in errors. + private readonly string ModName; + + + /********* + ** Public methods + *********/ + /// Construct an instance. + /// SMAPI's underlying content manager. + /// The absolute path to the mod folder. + /// The friendly mod name for use in errors. + public ContentHelper(SContentManager contentManager, string modFolderPath, string modName) + { + this.ContentManager = contentManager; + this.ModFolderPath = modFolderPath; + this.ModName = modName; + this.RelativeContentFolder = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath); + } + + /// Fetch and cache content from the game content or mod folder (if not already cached), and return it. + /// The expected data type. The main supported types are and dictionaries; other types may be supported by the game's content pipeline. + /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. + /// Where to search for a matching content asset. + /// The is empty or contains invalid characters. + /// The content asset couldn't be loaded (e.g. because it doesn't exist). + public T Load(string key, ContentSource source) + { + // validate + if (string.IsNullOrWhiteSpace(key)) + throw new ArgumentException("The asset key or local path is empty."); + if (key.Intersect(Path.GetInvalidPathChars()).Any()) + throw new ArgumentException("The asset key or local path contains invalid characters."); + + // load content + switch (source) + { + case ContentSource.GameContent: + return this.LoadFromGameContent(key, key, source); + + case ContentSource.ModFolder: + // find content file + FileInfo file = new FileInfo(Path.Combine(this.ModFolderPath, key)); + if (!file.Exists && file.Extension == "") + file = new FileInfo(Path.Combine(this.ModFolderPath, key + ".xnb")); + if (!file.Exists) + throw new ContentLoadException($"There is no file at path '{file.FullName}'."); + + // get content-relative path + string contentPath = Path.Combine(this.RelativeContentFolder, key); + if (contentPath.EndsWith(".xnb")) + contentPath = contentPath.Substring(0, contentPath.Length - 4); + + // load content + switch (file.Extension.ToLower()) + { + case ".xnb": + return this.LoadFromGameContent(contentPath, key, source); + + case ".png": + // validate + if (typeof(T) != typeof(Texture2D)) + throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'."); + + // try cache + if (this.ContentManager.IsLoaded(contentPath)) + return this.LoadFromGameContent(contentPath, key, source); + + // fetch & cache + using (FileStream stream = File.OpenRead(file.FullName)) + { + Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); + this.ContentManager.Inject(contentPath, texture); + return (T)(object)texture; + } + + default: + throw new ContentLoadException($"Unknown file extension '{file.Extension}'; must be '.xnb' or '.png'."); + } + + default: + throw new NotSupportedException($"Unknown content source '{source}'."); + } + } + + + /********* + ** Private methods + *********/ + /// Load a content asset through the underlying content manager, and throw a friendly error if it fails. + /// The expected data type. + /// The content key. + /// The friendly content key to show in errors. + /// The content source for use in errors. + /// The content couldn't be loaded. + private T LoadFromGameContent(string assetKey, string friendlyKey, ContentSource source) + { + try + { + return this.ContentManager.Load(assetKey); + } + catch (Exception ex) + { + throw new ContentLoadException($"{this.ModName} failed loading content asset '{friendlyKey}' from {source}.", ex); + } + } + + /// Get a directory path relative to a given root. + /// The root path from which the path should be relative. + /// The target file path. + private string GetRelativePath(string rootPath, string targetPath) + { + // convert to URIs + Uri from = new Uri(rootPath + "/"); + Uri to = new Uri(targetPath + "/"); + if (from.Scheme != to.Scheme) + throw new InvalidOperationException($"Can't get path for '{targetPath}' relative to '{rootPath}'."); + + // get relative path + return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()) + .Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform + } + } +} diff --git a/src/StardewModdingAPI/Framework/ModHelper.cs b/src/StardewModdingAPI/Framework/ModHelper.cs index 52e482f2..09297a65 100644 --- a/src/StardewModdingAPI/Framework/ModHelper.cs +++ b/src/StardewModdingAPI/Framework/ModHelper.cs @@ -18,9 +18,12 @@ namespace StardewModdingAPI.Framework /********* ** Accessors *********/ - /// The mod directory path. + /// The full path to the mod's folder. public string DirectoryPath { get; } + /// An API for loading content assets. + public IContentHelper Content { get; } + /// Simplifies access to private game code. public IReflectionHelper Reflection { get; } = new ReflectionHelper(); @@ -35,14 +38,15 @@ namespace StardewModdingAPI.Framework ** Public methods *********/ /// Construct an instance. - /// The friendly mod name. - /// The mod directory path. + /// The manifest for the associated mod. + /// The full path to the mod's folder. /// Encapsulate SMAPI's JSON parsing. /// Metadata about loaded mods. /// Manages console commands. + /// The content manager which loads content assets. /// An argument is null or empty. /// The path does not exist on disk. - public ModHelper(string modName, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager) + public ModHelper(IManifest manifest, string modDirectory, JsonHelper jsonHelper, IModRegistry modRegistry, CommandManager commandManager, SContentManager contentManager) { // validate if (string.IsNullOrWhiteSpace(modDirectory)) @@ -55,10 +59,11 @@ namespace StardewModdingAPI.Framework throw new InvalidOperationException("The specified mod directory does not exist."); // initialise - this.JsonHelper = jsonHelper; this.DirectoryPath = modDirectory; + this.JsonHelper = jsonHelper; + this.Content = new ContentHelper(contentManager, modDirectory, manifest.Name); this.ModRegistry = modRegistry; - this.ConsoleCommands = new CommandHelper(modName, commandManager); + this.ConsoleCommands = new CommandHelper(manifest.Name, commandManager); } /**** diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index ef5855b2..e363e6b4 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -5,6 +5,7 @@ using System.IO; using System.Linq; using System.Threading; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Content; using StardewModdingAPI.AssemblyRewriters; using StardewModdingAPI.Events; using StardewModdingAPI.Framework.Content; @@ -17,7 +18,7 @@ namespace StardewModdingAPI.Framework internal class SContentManager : LocalizedContentManager { /********* - ** Accessors + ** Properties *********/ /// The possible directory separator characters in an asset key. private static readonly char[] PossiblePathSeparators = new[] { '/', '\\', Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }.Distinct().ToArray(); @@ -38,6 +39,13 @@ namespace StardewModdingAPI.Framework private readonly IPrivateMethod GetKeyLocale; + /********* + ** Accessors + *********/ + /// The absolute path to the . + public string FullRootDirectory => Path.Combine(Constants.ExecutionPath, this.RootDirectory); + + /********* ** Public methods *********/ @@ -85,7 +93,7 @@ namespace StardewModdingAPI.Framework string cacheLocale = this.GetCacheLocale(assetName); // skip if already loaded - if (this.IsLoaded(assetName)) + if (this.IsNormalisedKeyLoaded(assetName)) return base.Load(assetName); // load data @@ -98,6 +106,25 @@ namespace StardewModdingAPI.Framework return (T)helper.Data; } + /// Inject an asset into the cache. + /// The type of asset to inject. + /// The asset path relative to the loader root directory, not including the .xnb extension. + /// The asset value. + public void Inject(string assetName, T value) + { + assetName = this.NormaliseAssetName(assetName); + this.Cache[assetName] = value; + } + + /// Get whether the content manager has already loaded and cached the given asset. + /// The asset path relative to the loader root directory, not including the .xnb extension. + public bool IsLoaded(string assetName) + { + assetName = this.NormaliseAssetName(assetName); + return this.IsNormalisedKeyLoaded(assetName); + + } + /********* ** Private methods @@ -116,7 +143,7 @@ namespace StardewModdingAPI.Framework /// Get whether an asset has already been loaded. /// The normalised asset name. - private bool IsLoaded(string normalisedAssetName) + private bool IsNormalisedKeyLoaded(string normalisedAssetName) { return this.Cache.ContainsKey(normalisedAssetName) || this.Cache.ContainsKey($"{normalisedAssetName}.{this.GetKeyLocale.Invoke()}"); // translated asset diff --git a/src/StardewModdingAPI/IContentHelper.cs b/src/StardewModdingAPI/IContentHelper.cs new file mode 100644 index 00000000..09f58a71 --- /dev/null +++ b/src/StardewModdingAPI/IContentHelper.cs @@ -0,0 +1,14 @@ +using Microsoft.Xna.Framework.Graphics; + +namespace StardewModdingAPI +{ + /// Provides an API for loading content assets. + public interface IContentHelper + { + /// Fetch and cache content from the game content or mod folder (if not already cached), and return it. + /// The expected data type. The main supported types are and dictionaries; other types may be supported by the game's content pipeline. + /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. + /// Where to search for a matching content asset. + T Load(string key, ContentSource source); + } +} diff --git a/src/StardewModdingAPI/IModHelper.cs b/src/StardewModdingAPI/IModHelper.cs index ef67cd1c..cdff6ac8 100644 --- a/src/StardewModdingAPI/IModHelper.cs +++ b/src/StardewModdingAPI/IModHelper.cs @@ -6,9 +6,12 @@ /********* ** Accessors *********/ - /// The mod directory path. + /// The full path to the mod's folder. string DirectoryPath { get; } + /// An API for loading content assets. + IContentHelper Content { get; } + /// Simplifies access to private game code. IReflectionHelper Reflection { get; } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 7fa78dce..1e5fcfc3 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -596,7 +596,7 @@ namespace StardewModdingAPI // inject data // get helper mod.ModManifest = manifest; - mod.Helper = new ModHelper(manifest.Name, directory.FullName, jsonHelper, this.ModRegistry, this.CommandManager); + mod.Helper = new ModHelper(manifest, directory.FullName, jsonHelper, this.ModRegistry, this.CommandManager, (SContentManager)Game1.content); mod.Monitor = this.GetSecondaryMonitor(manifest.Name); mod.PathOnDisk = directory.FullName; diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index bcd0c390..baac777f 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -114,6 +114,7 @@ Properties\GlobalAssemblyInfo.cs + @@ -146,6 +147,7 @@ + @@ -166,6 +168,7 @@ + -- cgit From 014014ca0f50f44d8767e46eb82625b2120282e0 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 19:51:47 -0400 Subject: premultiply alpha when loading PNGs to avoid transparency issues (#257) --- src/StardewModdingAPI/Framework/ContentHelper.cs | 179 +++++++++++++++-------- src/StardewModdingAPI/IContentHelper.cs | 8 +- 2 files changed, 120 insertions(+), 67 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 0d063ef0..9abfc7e9 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -1,6 +1,7 @@ using System; using System.IO; using System.Linq; +using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.Graphics; using StardewValley; @@ -41,7 +42,7 @@ namespace StardewModdingAPI.Framework this.RelativeContentFolder = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath); } - /// Fetch and cache content from the game content or mod folder (if not already cached), and return it. + /// Load content from the game folder or mod folder (if not already cached), and return it. When loading a .png file, this must be called outside the game's draw loop. /// The expected data type. The main supported types are and dictionaries; other types may be supported by the game's content pipeline. /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. /// Where to search for a matching content asset. @@ -56,78 +57,68 @@ namespace StardewModdingAPI.Framework throw new ArgumentException("The asset key or local path contains invalid characters."); // load content - switch (source) - { - case ContentSource.GameContent: - return this.LoadFromGameContent(key, key, source); - - case ContentSource.ModFolder: - // find content file - FileInfo file = new FileInfo(Path.Combine(this.ModFolderPath, key)); - if (!file.Exists && file.Extension == "") - file = new FileInfo(Path.Combine(this.ModFolderPath, key + ".xnb")); - if (!file.Exists) - throw new ContentLoadException($"There is no file at path '{file.FullName}'."); - - // get content-relative path - string contentPath = Path.Combine(this.RelativeContentFolder, key); - if (contentPath.EndsWith(".xnb")) - contentPath = contentPath.Substring(0, contentPath.Length - 4); - - // load content - switch (file.Extension.ToLower()) - { - case ".xnb": - return this.LoadFromGameContent(contentPath, key, source); - - case ".png": - // validate - if (typeof(T) != typeof(Texture2D)) - throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'."); - - // try cache - if (this.ContentManager.IsLoaded(contentPath)) - return this.LoadFromGameContent(contentPath, key, source); - - // fetch & cache - using (FileStream stream = File.OpenRead(file.FullName)) - { - Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); - this.ContentManager.Inject(contentPath, texture); - return (T)(object)texture; - } - - default: - throw new ContentLoadException($"Unknown file extension '{file.Extension}'; must be '.xnb' or '.png'."); - } - - default: - throw new NotSupportedException($"Unknown content source '{source}'."); - } - } - - - /********* - ** Private methods - *********/ - /// Load a content asset through the underlying content manager, and throw a friendly error if it fails. - /// The expected data type. - /// The content key. - /// The friendly content key to show in errors. - /// The content source for use in errors. - /// The content couldn't be loaded. - private T LoadFromGameContent(string assetKey, string friendlyKey, ContentSource source) - { try { - return this.ContentManager.Load(assetKey); + switch (source) + { + case ContentSource.GameContent: + return this.ContentManager.Load(key); + + case ContentSource.ModFolder: + // find content file + FileInfo file = new FileInfo(Path.Combine(this.ModFolderPath, key)); + if (!file.Exists && file.Extension == "") + file = new FileInfo(Path.Combine(this.ModFolderPath, key + ".xnb")); + if (!file.Exists) + throw new ContentLoadException($"There is no file at path '{file.FullName}'."); + + // get content-relative path + string contentPath = Path.Combine(this.RelativeContentFolder, key); + if (contentPath.EndsWith(".xnb")) + contentPath = contentPath.Substring(0, contentPath.Length - 4); + + // load content + switch (file.Extension.ToLower()) + { + case ".xnb": + return this.ContentManager.Load(contentPath); + + case ".png": + // validate + if (typeof(T) != typeof(Texture2D)) + throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'."); + + // try cache + if (this.ContentManager.IsLoaded(contentPath)) + return this.ContentManager.Load(contentPath); + + // fetch & cache + using (FileStream stream = File.OpenRead(file.FullName)) + { + var texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); + texture = this.PremultiplyTransparency(texture); + this.ContentManager.Inject(contentPath, texture); + return (T)(object)texture; + } + + default: + throw new ContentLoadException($"Unknown file extension '{file.Extension}'; must be '.xnb' or '.png'."); + } + + default: + throw new NotSupportedException($"Unknown content source '{source}'."); + } } catch (Exception ex) { - throw new ContentLoadException($"{this.ModName} failed loading content asset '{friendlyKey}' from {source}.", ex); + throw new ContentLoadException($"{this.ModName} failed loading content asset '{key}' from {source}.", ex); } } + + /********* + ** Private methods + *********/ /// Get a directory path relative to a given root. /// The root path from which the path should be relative. /// The target file path. @@ -143,5 +134,63 @@ namespace StardewModdingAPI.Framework return Uri.UnescapeDataString(from.MakeRelativeUri(to).ToString()) .Replace(Path.DirectorySeparatorChar == '/' ? '\\' : '/', Path.DirectorySeparatorChar); // use correct separator for platform } + + /// Premultiply a texture's alpha values to avoid transparency issues in the game. This is only possible if the game isn't currently drawing. + /// The texture to premultiply. + /// Returns a premultiplied texture. + /// Based on code by Layoric. + private Texture2D PremultiplyTransparency(Texture2D texture) + { + if (Game1.graphics.GraphicsDevice.GetRenderTargets().Any()) // TODO: use a more robust check to detect if the game is drawing + throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop."); + + using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height)) + using (SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice)) + { + //Viewport originalViewport = Game1.graphics.GraphicsDevice.Viewport; + + // create blank slate in render target + Game1.graphics.GraphicsDevice.SetRenderTarget(renderTarget); + Game1.graphics.GraphicsDevice.Clear(Color.Black); + + // multiply each color by the source alpha, and write just the color values into the final texture + spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState + { + ColorDestinationBlend = Blend.Zero, + ColorWriteChannels = ColorWriteChannels.Red | ColorWriteChannels.Green | ColorWriteChannels.Blue, + AlphaDestinationBlend = Blend.Zero, + AlphaSourceBlend = Blend.SourceAlpha, + ColorSourceBlend = Blend.SourceAlpha + }); + spriteBatch.Draw(texture, texture.Bounds, Color.White); + spriteBatch.End(); + + // copy the alpha values from the source texture into the final one without multiplying them + spriteBatch.Begin(SpriteSortMode.Immediate, new BlendState + { + ColorWriteChannels = ColorWriteChannels.Alpha, + AlphaDestinationBlend = Blend.Zero, + ColorDestinationBlend = Blend.Zero, + AlphaSourceBlend = Blend.One, + ColorSourceBlend = Blend.One + }); + spriteBatch.Draw(texture, texture.Bounds, Color.White); + spriteBatch.End(); + + // release the GPU + Game1.graphics.GraphicsDevice.SetRenderTarget(null); + //Game1.graphics.GraphicsDevice.Viewport = originalViewport; + + // store data from render target because the RenderTarget2D is volatile + var data = new Color[texture.Width * texture.Height]; + renderTarget.GetData(data); + + // unset texture from graphic device and set modified data back to it + Game1.graphics.GraphicsDevice.Textures[0] = null; + texture.SetData(data); + } + + return texture; + } } } diff --git a/src/StardewModdingAPI/IContentHelper.cs b/src/StardewModdingAPI/IContentHelper.cs index 09f58a71..b878dfe5 100644 --- a/src/StardewModdingAPI/IContentHelper.cs +++ b/src/StardewModdingAPI/IContentHelper.cs @@ -1,14 +1,18 @@ -using Microsoft.Xna.Framework.Graphics; +using System; +using Microsoft.Xna.Framework.Content; +using Microsoft.Xna.Framework.Graphics; namespace StardewModdingAPI { /// Provides an API for loading content assets. public interface IContentHelper { - /// Fetch and cache content from the game content or mod folder (if not already cached), and return it. + /// Load content from the game folder or mod folder (if not already cached), and return it. When loading a .png file, this must be called outside the game's draw loop. /// The expected data type. The main supported types are and dictionaries; other types may be supported by the game's content pipeline. /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. /// Where to search for a matching content asset. + /// The is empty or contains invalid characters. + /// The content asset couldn't be loaded (e.g. because it doesn't exist). T Load(string key, ContentSource source); } } -- cgit From ff5d1ef4e4a096405b343de3f6d27715c248de3b Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 21:45:37 -0400 Subject: add internal context for more robust draw loop detection (#257) --- src/StardewModdingAPI/Constants.cs | 9 +++------ src/StardewModdingAPI/Context.cs | 17 +++++++++++++++++ src/StardewModdingAPI/Framework/ContentHelper.cs | 4 +++- src/StardewModdingAPI/Framework/SGame.cs | 4 +++- src/StardewModdingAPI/Mod.cs | 2 +- src/StardewModdingAPI/StardewModdingAPI.csproj | 1 + 6 files changed, 28 insertions(+), 9 deletions(-) create mode 100644 src/StardewModdingAPI/Context.cs (limited to 'src') diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index 6ba16935..cca99027 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -20,10 +20,10 @@ namespace StardewModdingAPI ** Properties *********/ /// The directory path containing the current save's data (if a save is loaded). - private static string RawSavePath => Constants.IsSaveLoaded ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : null; + private static string RawSavePath => Context.IsSaveLoaded ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : null; /// Whether the directory containing the current save's data exists on disk. - private static bool SavePathReady => Constants.IsSaveLoaded && Directory.Exists(Constants.RawSavePath); + private static bool SavePathReady => Context.IsSaveLoaded && Directory.Exists(Constants.RawSavePath); /********* @@ -54,7 +54,7 @@ namespace StardewModdingAPI public static string SavesPath { get; } = Path.Combine(Constants.DataPath, "Saves"); /// The directory name containing the current save's data (if a save is loaded and the directory exists). - public static string SaveFolderName => Constants.IsSaveLoaded ? Constants.GetSaveFolderName() : ""; + public static string SaveFolderName => Context.IsSaveLoaded ? Constants.GetSaveFolderName() : ""; /// The directory path containing the current save's data (if a save is loaded and the directory exists). public static string CurrentSavePath => Constants.SavePathReady ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : ""; @@ -74,9 +74,6 @@ namespace StardewModdingAPI /// The full path to the folder containing mods. internal static string ModPath { get; } = Path.Combine(Constants.ExecutionPath, "Mods"); - /// Whether a player save has been loaded. - internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); - /// The game's current semantic version. internal static ISemanticVersion GameVersion { get; } = Constants.GetGameVersion(); diff --git a/src/StardewModdingAPI/Context.cs b/src/StardewModdingAPI/Context.cs new file mode 100644 index 00000000..d737bd58 --- /dev/null +++ b/src/StardewModdingAPI/Context.cs @@ -0,0 +1,17 @@ +using StardewValley; + +namespace StardewModdingAPI +{ + /// Provides information about the current game state. + internal static class Context + { + /********* + ** Accessors + *********/ + /// Whether a player save has been loaded. + public static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); + + /// Whether the game is currently running the draw loop. + public static bool IsInDrawLoop { get; set; } + } +} diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 9abfc7e9..04317a84 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -141,9 +141,11 @@ namespace StardewModdingAPI.Framework /// Based on code by Layoric. private Texture2D PremultiplyTransparency(Texture2D texture) { - if (Game1.graphics.GraphicsDevice.GetRenderTargets().Any()) // TODO: use a more robust check to detect if the game is drawing + // validate + if (Context.IsInDrawLoop) throw new NotSupportedException("Can't load a PNG file while the game is drawing to the screen. Make sure you load content outside the draw loop."); + // process texture using (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height)) using (SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice)) { diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 20c6b886..7e04c391 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -287,6 +287,7 @@ namespace StardewModdingAPI.Framework [SuppressMessage("ReSharper", "RedundantTypeArgumentsOfMethod", Justification = "copied from game code as-is")] protected override void Draw(GameTime gameTime) { + Context.IsInDrawLoop = true; try { if (Game1.debugMode) @@ -938,6 +939,7 @@ namespace StardewModdingAPI.Framework { this.Monitor.Log($"An error occured in the overridden draw loop: {ex.GetLogSummary()}", LogLevel.Error); } + Context.IsInDrawLoop = false; } /**** @@ -1081,7 +1083,7 @@ namespace StardewModdingAPI.Framework } // save loaded event - if (Constants.IsSaveLoaded && !SaveGame.IsProcessing/*still loading save*/ && this.AfterLoadTimer >= 0) + if (Context.IsSaveLoaded && !SaveGame.IsProcessing/*still loading save*/ && this.AfterLoadTimer >= 0) { if (this.AfterLoadTimer == 0) { diff --git a/src/StardewModdingAPI/Mod.cs b/src/StardewModdingAPI/Mod.cs index caa20774..8033e1fd 100644 --- a/src/StardewModdingAPI/Mod.cs +++ b/src/StardewModdingAPI/Mod.cs @@ -65,7 +65,7 @@ namespace StardewModdingAPI { Mod.DeprecationManager.Warn($"{nameof(Mod)}.{nameof(Mod.PerSaveConfigPath)}", "1.0", DeprecationLevel.Info); Mod.DeprecationManager.MarkWarned($"{nameof(Mod)}.{nameof(Mod.PerSaveConfigFolder)}", "1.0"); // avoid redundant warnings - return Constants.IsSaveLoaded ? Path.Combine(this.PerSaveConfigFolder, $"{Constants.SaveFolderName}.json") : ""; + return Context.IsSaveLoaded ? Path.Combine(this.PerSaveConfigFolder, $"{Constants.SaveFolderName}.json") : ""; } } diff --git a/src/StardewModdingAPI/StardewModdingAPI.csproj b/src/StardewModdingAPI/StardewModdingAPI.csproj index baac777f..87ce65b0 100644 --- a/src/StardewModdingAPI/StardewModdingAPI.csproj +++ b/src/StardewModdingAPI/StardewModdingAPI.csproj @@ -152,6 +152,7 @@ + -- cgit From f52edf9e0b267e79f88c492e7d7e34f63744f21c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 21:46:01 -0400 Subject: fix mod events triggering during game save in Stardew Valley 1.2 --- release-notes.md | 1 + src/StardewModdingAPI/Context.cs | 3 +++ src/StardewModdingAPI/Framework/SGame.cs | 10 ++++++++++ 3 files changed, 14 insertions(+) (limited to 'src') diff --git a/release-notes.md b/release-notes.md index 465448d1..fe8c8adb 100644 --- a/release-notes.md +++ b/release-notes.md @@ -18,6 +18,7 @@ For players: * Errors when loading a save are now shown in the SMAPI console. * Improved console logging performance. * Fixed errors during game update causing the game to hang. +* Fixed errors due to mod events triggering during game save in Stardew Valley 1.2. For mod developers: * `Console.Out` messages are now written to the log file. diff --git a/src/StardewModdingAPI/Context.cs b/src/StardewModdingAPI/Context.cs index d737bd58..415b4aac 100644 --- a/src/StardewModdingAPI/Context.cs +++ b/src/StardewModdingAPI/Context.cs @@ -11,6 +11,9 @@ namespace StardewModdingAPI /// Whether a player save has been loaded. public static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); + /// Whether the game is currently writing to the save file. + public static bool IsSaving => SaveGame.IsProcessing; + /// Whether the game is currently running the draw loop. public static bool IsInDrawLoop { get; set; } } diff --git a/src/StardewModdingAPI/Framework/SGame.cs b/src/StardewModdingAPI/Framework/SGame.cs index 7e04c391..fe7d3aa3 100644 --- a/src/StardewModdingAPI/Framework/SGame.cs +++ b/src/StardewModdingAPI/Framework/SGame.cs @@ -223,6 +223,16 @@ namespace StardewModdingAPI.Framework return; } + // While the game is writing to the save file in the background, mods can unexpectedly + // fail since they don't have exclusive access to resources (e.g. collection changed + // during enumeration errors). To avoid problems, events are not invoked while a save + // is in progress. + if (Context.IsSaving) + { + base.Update(gameTime); + return; + } + // raise game loaded if (this.FirstUpdate) { -- cgit From 3cfe14d27946518c1cd631607f878f7d2f4a2cdb Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 22:44:57 -0400 Subject: add contentHelper.GetActualAssetKey(..) to support custom map tilesheets (#257) --- src/StardewModdingAPI/Framework/ContentHelper.cs | 65 +++++++++++++++------- src/StardewModdingAPI/Framework/SContentManager.cs | 42 +++++++------- src/StardewModdingAPI/IContentHelper.cs | 6 ++ 3 files changed, 72 insertions(+), 41 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 04317a84..4d116b54 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -1,4 +1,5 @@ using System; +using System.Diagnostics.CodeAnalysis; using System.IO; using System.Linq; using Microsoft.Xna.Framework; @@ -21,7 +22,7 @@ namespace StardewModdingAPI.Framework private readonly string ModFolderPath; /// The path to the mod's folder, relative to the game's content folder (e.g. "../Mods/ModName"). - private readonly string RelativeContentFolder; + private readonly string ModFolderPathFromContent; /// The friendly mod name for use in errors. private readonly string ModName; @@ -39,7 +40,7 @@ namespace StardewModdingAPI.Framework this.ContentManager = contentManager; this.ModFolderPath = modFolderPath; this.ModName = modName; - this.RelativeContentFolder = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath); + this.ModFolderPathFromContent = this.GetRelativePath(contentManager.FullRootDirectory, modFolderPath); } /// Load content from the game folder or mod folder (if not already cached), and return it. When loading a .png file, this must be called outside the game's draw loop. @@ -50,13 +51,7 @@ namespace StardewModdingAPI.Framework /// The content asset couldn't be loaded (e.g. because it doesn't exist). public T Load(string key, ContentSource source) { - // validate - if (string.IsNullOrWhiteSpace(key)) - throw new ArgumentException("The asset key or local path is empty."); - if (key.Intersect(Path.GetInvalidPathChars()).Any()) - throw new ArgumentException("The asset key or local path contains invalid characters."); - - // load content + this.AssertValidAssetKeyFormat(key); try { switch (source) @@ -72,16 +67,14 @@ namespace StardewModdingAPI.Framework if (!file.Exists) throw new ContentLoadException($"There is no file at path '{file.FullName}'."); - // get content-relative path - string contentPath = Path.Combine(this.RelativeContentFolder, key); - if (contentPath.EndsWith(".xnb")) - contentPath = contentPath.Substring(0, contentPath.Length - 4); + // get underlying asset key + string actualKey = this.GetActualAssetKey(key, source); // load content switch (file.Extension.ToLower()) { case ".xnb": - return this.ContentManager.Load(contentPath); + return this.ContentManager.Load(actualKey); case ".png": // validate @@ -89,15 +82,15 @@ namespace StardewModdingAPI.Framework throw new ContentLoadException($"Can't read file with extension '{file.Extension}' as type '{typeof(T)}'; must be type '{typeof(Texture2D)}'."); // try cache - if (this.ContentManager.IsLoaded(contentPath)) - return this.ContentManager.Load(contentPath); + if (this.ContentManager.IsLoaded(actualKey)) + return this.ContentManager.Load(actualKey); // fetch & cache using (FileStream stream = File.OpenRead(file.FullName)) { - var texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); + Texture2D texture = Texture2D.FromStream(Game1.graphics.GraphicsDevice, stream); texture = this.PremultiplyTransparency(texture); - this.ContentManager.Inject(contentPath, texture); + this.ContentManager.Inject(actualKey, texture); return (T)(object)texture; } @@ -115,10 +108,44 @@ namespace StardewModdingAPI.Framework } } + /// Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists. + /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. + /// Where to search for a matching content asset. + /// The is empty or contains invalid characters. + public string GetActualAssetKey(string key, ContentSource source) + { + switch (source) + { + case ContentSource.GameContent: + return this.ContentManager.NormaliseAssetName(key); + + case ContentSource.ModFolder: + string contentPath = Path.Combine(this.ModFolderPathFromContent, key); + if (contentPath.EndsWith(".xnb")) + contentPath = contentPath.Substring(0, contentPath.Length - 4); + return this.ContentManager.NormaliseAssetName(contentPath); + + default: + throw new NotSupportedException($"Unknown content source '{source}'."); + } + } + /********* ** Private methods *********/ + /// Assert that the given key has a valid format. + /// The asset key to check. + /// The asset key is empty or contains invalid characters. + [SuppressMessage("ReSharper", "UnusedParameter.Local", Justification = "Parameter is only used for assertion checks by design.")] + private void AssertValidAssetKeyFormat(string key) + { + if (string.IsNullOrWhiteSpace(key)) + throw new ArgumentException("The asset key or local path is empty."); + if (key.Intersect(Path.GetInvalidPathChars()).Any()) + throw new ArgumentException("The asset key or local path contains invalid characters."); + } + /// Get a directory path relative to a given root. /// The root path from which the path should be relative. /// The target file path. @@ -184,7 +211,7 @@ namespace StardewModdingAPI.Framework //Game1.graphics.GraphicsDevice.Viewport = originalViewport; // store data from render target because the RenderTarget2D is volatile - var data = new Color[texture.Width * texture.Height]; + Color[] data = new Color[texture.Width * texture.Height]; renderTarget.GetData(data); // unset texture from graphic device and set modified data back to it diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index e363e6b4..99814c94 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -83,6 +83,26 @@ namespace StardewModdingAPI.Framework this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load logic } + /// Normalise an asset name so it's consistent with the underlying cache. + /// The asset key. + public string NormaliseAssetName(string assetName) + { + // ensure name format is consistent + string[] parts = assetName.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries); + assetName = string.Join(SContentManager.PreferredPathSeparator, parts); + + // apply platform normalisation logic + return this.NormaliseAssetNameForPlatform(assetName); + } + + /// Get whether the content manager has already loaded and cached the given asset. + /// The asset path relative to the loader root directory, not including the .xnb extension. + public bool IsLoaded(string assetName) + { + assetName = this.NormaliseAssetName(assetName); + return this.IsNormalisedKeyLoaded(assetName); + } + /// Load an asset that has been processed by the content pipeline. /// The type of asset to load. /// The asset path relative to the loader root directory, not including the .xnb extension. @@ -116,31 +136,9 @@ namespace StardewModdingAPI.Framework this.Cache[assetName] = value; } - /// Get whether the content manager has already loaded and cached the given asset. - /// The asset path relative to the loader root directory, not including the .xnb extension. - public bool IsLoaded(string assetName) - { - assetName = this.NormaliseAssetName(assetName); - return this.IsNormalisedKeyLoaded(assetName); - - } - - /********* ** Private methods *********/ - /// Normalise an asset name so it's consistent with the underlying cache. - /// The asset key. - private string NormaliseAssetName(string assetName) - { - // ensure name format is consistent - string[] parts = assetName.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries); - assetName = string.Join(SContentManager.PreferredPathSeparator, parts); - - // apply platform normalisation logic - return this.NormaliseAssetNameForPlatform(assetName); - } - /// Get whether an asset has already been loaded. /// The normalised asset name. private bool IsNormalisedKeyLoaded(string normalisedAssetName) diff --git a/src/StardewModdingAPI/IContentHelper.cs b/src/StardewModdingAPI/IContentHelper.cs index b878dfe5..7cde413b 100644 --- a/src/StardewModdingAPI/IContentHelper.cs +++ b/src/StardewModdingAPI/IContentHelper.cs @@ -14,5 +14,11 @@ namespace StardewModdingAPI /// The is empty or contains invalid characters. /// The content asset couldn't be loaded (e.g. because it doesn't exist). T Load(string key, ContentSource source); + + /// Get the underlying key in the game's content cache for an asset. This can be used to load custom map tilesheets, but should be avoided when you can use the content API instead. This does not validate whether the asset exists. + /// The asset key to fetch (if the is ), or the local path to an XNB file relative to the mod folder. + /// Where to search for a matching content asset. + /// The is empty or contains invalid characters. + string GetActualAssetKey(string key, ContentSource source); } } -- cgit From ddd9c0a80450fb161c1ff02cce94d4783a9f3bbc Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 23:38:45 -0400 Subject: let mods specify .xnb file extension explicitly (#257) --- src/StardewModdingAPI/Framework/ContentHelper.cs | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 4d116b54..7cf726e1 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -57,7 +57,7 @@ namespace StardewModdingAPI.Framework switch (source) { case ContentSource.GameContent: - return this.ContentManager.Load(key); + return this.ContentManager.Load(this.StripXnbExtension(key)); case ContentSource.ModFolder: // find content file @@ -117,13 +117,11 @@ namespace StardewModdingAPI.Framework switch (source) { case ContentSource.GameContent: - return this.ContentManager.NormaliseAssetName(key); + return this.ContentManager.NormaliseAssetName(this.StripXnbExtension(key)); case ContentSource.ModFolder: string contentPath = Path.Combine(this.ModFolderPathFromContent, key); - if (contentPath.EndsWith(".xnb")) - contentPath = contentPath.Substring(0, contentPath.Length - 4); - return this.ContentManager.NormaliseAssetName(contentPath); + return this.ContentManager.NormaliseAssetName(this.StripXnbExtension(contentPath)); default: throw new NotSupportedException($"Unknown content source '{source}'."); @@ -146,6 +144,15 @@ namespace StardewModdingAPI.Framework throw new ArgumentException("The asset key or local path contains invalid characters."); } + /// Strip the .xnb extension from an asset key, since it's assumed by the underlying content manager. + /// The asset key. + private string StripXnbExtension(string key) + { + if (key.EndsWith(".xnb", StringComparison.InvariantCultureIgnoreCase)) + return key.Substring(0, key.Length - 4); + return key; + } + /// Get a directory path relative to a given root. /// The root path from which the path should be relative. /// The target file path. -- cgit From 06c4273c1e263eee6efcecad16b22ef4e03a0f1f Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 29 Apr 2017 23:59:25 -0400 Subject: bump minimum game version to released 1.2.26 --- src/StardewModdingAPI/Constants.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index cca99027..ae877226 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -36,7 +36,7 @@ namespace StardewModdingAPI public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 10, 0); /// The minimum supported version of Stardew Valley. - public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.15"); + public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.26"); /// The maximum supported version of Stardew Valley. public static ISemanticVersion MaximumGameVersion { get; } = null; -- cgit From d21f8d6b22d500ec627f0448f132b37d516e3830 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 30 Apr 2017 01:06:57 -0400 Subject: fix crossplatform compatibility (#257) --- src/StardewModdingAPI/Framework/ContentHelper.cs | 1 + src/StardewModdingAPI/Framework/SContentManager.cs | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/StardewModdingAPI/Framework/ContentHelper.cs b/src/StardewModdingAPI/Framework/ContentHelper.cs index 7cf726e1..762b7e35 100644 --- a/src/StardewModdingAPI/Framework/ContentHelper.cs +++ b/src/StardewModdingAPI/Framework/ContentHelper.cs @@ -61,6 +61,7 @@ namespace StardewModdingAPI.Framework case ContentSource.ModFolder: // find content file + key = this.ContentManager.NormalisePathSeparators(key); FileInfo file = new FileInfo(Path.Combine(this.ModFolderPath, key)); if (!file.Exists && file.Extension == "") file = new FileInfo(Path.Combine(this.ModFolderPath, key + ".xnb")); diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index 99814c94..88e1df2b 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -83,15 +83,22 @@ namespace StardewModdingAPI.Framework this.NormaliseAssetNameForPlatform = key => key.Replace('\\', '/'); // based on MonoGame's ContentManager.Load logic } + /// Normalise path separators in a file path. For asset keys, see instead. + /// The file path to normalise. + public string NormalisePathSeparators(string path) + { + string[] parts = path.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries); + string normalised = string.Join(SContentManager.PreferredPathSeparator, parts); + if (path.StartsWith(SContentManager.PreferredPathSeparator)) + normalised = SContentManager.PreferredPathSeparator + normalised; // keep root slash + return normalised; + } + /// Normalise an asset name so it's consistent with the underlying cache. /// The asset key. public string NormaliseAssetName(string assetName) { - // ensure name format is consistent - string[] parts = assetName.Split(SContentManager.PossiblePathSeparators, StringSplitOptions.RemoveEmptyEntries); - assetName = string.Join(SContentManager.PreferredPathSeparator, parts); - - // apply platform normalisation logic + assetName = this.NormalisePathSeparators(assetName); return this.NormaliseAssetNameForPlatform(assetName); } -- cgit From 901353ee04e6874f16613b8339afb50d47199788 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 30 Apr 2017 01:07:22 -0400 Subject: tweak code inspection rules --- src/StardewModdingAPI.sln.DotSettings | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/StardewModdingAPI.sln.DotSettings b/src/StardewModdingAPI.sln.DotSettings index 9620737a..06cc66ef 100644 --- a/src/StardewModdingAPI.sln.DotSettings +++ b/src/StardewModdingAPI.sln.DotSettings @@ -1,4 +1,5 @@  + DO_NOT_SHOW HINT HINT Field, Property, Event, Method -- cgit From d4f172fef160d277d5161d96a26d5174e6fc14ca Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 30 Apr 2017 18:54:14 -0400 Subject: bump version for release --- src/GlobalAssemblyInfo.cs | 4 ++-- src/StardewModdingAPI/Constants.cs | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/GlobalAssemblyInfo.cs b/src/GlobalAssemblyInfo.cs index b591153a..3de78da4 100644 --- a/src/GlobalAssemblyInfo.cs +++ b/src/GlobalAssemblyInfo.cs @@ -2,5 +2,5 @@ using System.Runtime.InteropServices; [assembly: ComVisible(false)] -[assembly: AssemblyVersion("1.8.0.0")] -[assembly: AssemblyFileVersion("1.8.0.0")] \ No newline at end of file +[assembly: AssemblyVersion("1.11.0.0")] +[assembly: AssemblyFileVersion("1.11.0.0")] \ No newline at end of file diff --git a/src/StardewModdingAPI/Constants.cs b/src/StardewModdingAPI/Constants.cs index ae877226..fec634e0 100644 --- a/src/StardewModdingAPI/Constants.cs +++ b/src/StardewModdingAPI/Constants.cs @@ -33,7 +33,7 @@ namespace StardewModdingAPI ** Public ****/ /// SMAPI's current semantic version. - public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 10, 0); + public static ISemanticVersion ApiVersion { get; } = new SemanticVersion(1, 11, 0); /// The minimum supported version of Stardew Valley. public static ISemanticVersion MinimumGameVersion { get; } = new SemanticVersion("1.2.26"); -- cgit