summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI
diff options
context:
space:
mode:
Diffstat (limited to 'src/StardewModdingAPI')
-rw-r--r--src/StardewModdingAPI/Constants.cs9
-rw-r--r--src/StardewModdingAPI/Context.cs17
-rw-r--r--src/StardewModdingAPI/Framework/ContentHelper.cs4
-rw-r--r--src/StardewModdingAPI/Framework/SGame.cs4
-rw-r--r--src/StardewModdingAPI/Mod.cs2
-rw-r--r--src/StardewModdingAPI/StardewModdingAPI.csproj1
6 files changed, 28 insertions, 9 deletions
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
*********/
/// <summary>The directory path containing the current save's data (if a save is loaded).</summary>
- 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;
/// <summary>Whether the directory containing the current save's data exists on disk.</summary>
- 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");
/// <summary>The directory name containing the current save's data (if a save is loaded and the directory exists).</summary>
- public static string SaveFolderName => Constants.IsSaveLoaded ? Constants.GetSaveFolderName() : "";
+ public static string SaveFolderName => Context.IsSaveLoaded ? Constants.GetSaveFolderName() : "";
/// <summary>The directory path containing the current save's data (if a save is loaded and the directory exists).</summary>
public static string CurrentSavePath => Constants.SavePathReady ? Path.Combine(Constants.SavesPath, Constants.GetSaveFolderName()) : "";
@@ -74,9 +74,6 @@ namespace StardewModdingAPI
/// <summary>The full path to the folder containing mods.</summary>
internal static string ModPath { get; } = Path.Combine(Constants.ExecutionPath, "Mods");
- /// <summary>Whether a player save has been loaded.</summary>
- internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name);
-
/// <summary>The game's current semantic version.</summary>
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
+{
+ /// <summary>Provides information about the current game state.</summary>
+ internal static class Context
+ {
+ /*********
+ ** Accessors
+ *********/
+ /// <summary>Whether a player save has been loaded.</summary>
+ public static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name);
+
+ /// <summary>Whether the game is currently running the draw loop.</summary>
+ 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
/// <remarks>Based on <a href="https://gist.github.com/Layoric/6255384">code by Layoric</a>.</remarks>
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 @@
<Compile Include="Framework\Content\ContentEventHelper.cs" />
<Compile Include="Framework\Content\ContentEventHelperForDictionary.cs" />
<Compile Include="Framework\Content\ContentEventHelperForImage.cs" />
+ <Compile Include="Context.cs" />
<Compile Include="Framework\Logging\ConsoleInterceptionManager.cs" />
<Compile Include="Framework\Logging\InterceptingTextWriter.cs" />
<Compile Include="Framework\CommandHelper.cs" />