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