add internal context for more robust draw loop detection (#257)
This commit is contained in:
parent
014014ca0f
commit
ff5d1ef4e4
|
@ -20,10 +20,10 @@ namespace StardewModdingAPI
|
||||||
** Properties
|
** Properties
|
||||||
*********/
|
*********/
|
||||||
/// <summary>The directory path containing the current save's data (if a save is loaded).</summary>
|
/// <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>
|
/// <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");
|
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>
|
/// <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>
|
/// <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()) : "";
|
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>
|
/// <summary>The full path to the folder containing mods.</summary>
|
||||||
internal static string ModPath { get; } = Path.Combine(Constants.ExecutionPath, "Mods");
|
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>
|
/// <summary>The game's current semantic version.</summary>
|
||||||
internal static ISemanticVersion GameVersion { get; } = Constants.GetGameVersion();
|
internal static ISemanticVersion GameVersion { get; } = Constants.GetGameVersion();
|
||||||
|
|
||||||
|
|
|
@ -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; }
|
||||||
|
}
|
||||||
|
}
|
|
@ -141,9 +141,11 @@ namespace StardewModdingAPI.Framework
|
||||||
/// <remarks>Based on <a href="https://gist.github.com/Layoric/6255384">code by Layoric</a>.</remarks>
|
/// <remarks>Based on <a href="https://gist.github.com/Layoric/6255384">code by Layoric</a>.</remarks>
|
||||||
private Texture2D PremultiplyTransparency(Texture2D texture)
|
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.");
|
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 (RenderTarget2D renderTarget = new RenderTarget2D(Game1.graphics.GraphicsDevice, texture.Width, texture.Height))
|
||||||
using (SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice))
|
using (SpriteBatch spriteBatch = new SpriteBatch(Game1.graphics.GraphicsDevice))
|
||||||
{
|
{
|
||||||
|
|
|
@ -287,6 +287,7 @@ namespace StardewModdingAPI.Framework
|
||||||
[SuppressMessage("ReSharper", "RedundantTypeArgumentsOfMethod", Justification = "copied from game code as-is")]
|
[SuppressMessage("ReSharper", "RedundantTypeArgumentsOfMethod", Justification = "copied from game code as-is")]
|
||||||
protected override void Draw(GameTime gameTime)
|
protected override void Draw(GameTime gameTime)
|
||||||
{
|
{
|
||||||
|
Context.IsInDrawLoop = true;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (Game1.debugMode)
|
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);
|
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
|
// 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)
|
if (this.AfterLoadTimer == 0)
|
||||||
{
|
{
|
||||||
|
|
|
@ -65,7 +65,7 @@ namespace StardewModdingAPI
|
||||||
{
|
{
|
||||||
Mod.DeprecationManager.Warn($"{nameof(Mod)}.{nameof(Mod.PerSaveConfigPath)}", "1.0", DeprecationLevel.Info);
|
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
|
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") : "";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -152,6 +152,7 @@
|
||||||
<Compile Include="Framework\Content\ContentEventHelper.cs" />
|
<Compile Include="Framework\Content\ContentEventHelper.cs" />
|
||||||
<Compile Include="Framework\Content\ContentEventHelperForDictionary.cs" />
|
<Compile Include="Framework\Content\ContentEventHelperForDictionary.cs" />
|
||||||
<Compile Include="Framework\Content\ContentEventHelperForImage.cs" />
|
<Compile Include="Framework\Content\ContentEventHelperForImage.cs" />
|
||||||
|
<Compile Include="Context.cs" />
|
||||||
<Compile Include="Framework\Logging\ConsoleInterceptionManager.cs" />
|
<Compile Include="Framework\Logging\ConsoleInterceptionManager.cs" />
|
||||||
<Compile Include="Framework\Logging\InterceptingTextWriter.cs" />
|
<Compile Include="Framework\Logging\InterceptingTextWriter.cs" />
|
||||||
<Compile Include="Framework\CommandHelper.cs" />
|
<Compile Include="Framework\CommandHelper.cs" />
|
||||||
|
|
Loading…
Reference in New Issue