From 176eddbf7b70934c2665aa3a0ac8b46bef04012a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 16 Feb 2017 00:54:41 -0500 Subject: make SMAPI core non-static, eliminate direct access between core components --- src/StardewModdingAPI/Framework/InternalExtensions.cs | 16 +++++++++++++++- src/StardewModdingAPI/Framework/Monitor.cs | 9 ++++++--- src/StardewModdingAPI/Framework/RequestExitDelegate.cs | 7 +++++++ 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/RequestExitDelegate.cs (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/InternalExtensions.cs b/src/StardewModdingAPI/Framework/InternalExtensions.cs index c4bd2d35..4ca79518 100644 --- a/src/StardewModdingAPI/Framework/InternalExtensions.cs +++ b/src/StardewModdingAPI/Framework/InternalExtensions.cs @@ -8,9 +8,23 @@ namespace StardewModdingAPI.Framework /// Provides extension methods for SMAPI's internal use. internal static class InternalExtensions { + /********* + ** Properties + *********/ + /// Tracks the installed mods. + private static ModRegistry ModRegistry; + + /********* ** Public methods *********/ + /// Injects types required for backwards compatibility. + /// Tracks the installed mods. + internal static void Shim(ModRegistry modRegistry) + { + InternalExtensions.ModRegistry = modRegistry; + } + /**** ** IMonitor ****/ @@ -103,7 +117,7 @@ namespace StardewModdingAPI.Framework foreach (Delegate handler in handlers) { - string modName = Program.ModRegistry.GetModFrom(handler) ?? "an unknown mod"; // suppress stack trace for unknown mods, not helpful here + string modName = InternalExtensions.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/Monitor.cs b/src/StardewModdingAPI/Framework/Monitor.cs index c1735917..64075f2f 100644 --- a/src/StardewModdingAPI/Framework/Monitor.cs +++ b/src/StardewModdingAPI/Framework/Monitor.cs @@ -34,6 +34,9 @@ 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 RequestExitDelegate RequestExit; + /********* ** Accessors @@ -55,7 +58,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. - public Monitor(string source, ConsoleInterceptionManager consoleManager, LogFileManager logFile) + /// A delegate which requests that SMAPI immediately exit the game. + public Monitor(string source, ConsoleInterceptionManager consoleManager, LogFileManager logFile, RequestExitDelegate requestExitDelegate) { // validate if (string.IsNullOrWhiteSpace(source)) @@ -81,8 +85,7 @@ namespace StardewModdingAPI.Framework /// The reason for the shutdown. public void ExitGameImmediately(string reason) { - Program.ExitGameImmediately(this.Source, reason); - Program.GameInstance.Exit(); + this.RequestExit(this.Source, reason); } /// Log a fatal error message. diff --git a/src/StardewModdingAPI/Framework/RequestExitDelegate.cs b/src/StardewModdingAPI/Framework/RequestExitDelegate.cs new file mode 100644 index 00000000..12d0ea0c --- /dev/null +++ b/src/StardewModdingAPI/Framework/RequestExitDelegate.cs @@ -0,0 +1,7 @@ +namespace StardewModdingAPI.Framework +{ + /// 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. + /// The module which requested an immediate exit. + /// The reason provided for the shutdown. + internal delegate void RequestExitDelegate(string module, string reason); +} \ No newline at end of file -- cgit