add Context.IsWorldReady flag

This commit is contained in:
Jesse Plamondon-Willard 2017-05-15 22:51:49 -04:00
parent aafb3315cb
commit bca78cd682
3 changed files with 20 additions and 9 deletions

View File

@ -22,6 +22,8 @@ For players:
* Updated mod compatibility list for Stardew Valley 1.2. * Updated mod compatibility list for Stardew Valley 1.2.
For mod developers: For mod developers:
* Added a `Context.IsWorldReady` flag.
_<small>This is set to `true` when the player has loaded a save and the world is finished initialising. This is set at the same point that `SaveEvents.AfterLoad` and `TimeEvents.AfterDayStarted` are raised, and is mainly useful with events which can be raised before the world is loaded.</small>_
* Added log entries for basic context changes (e.g. loaded save) to simplify troubleshooting. * Added log entries for basic context changes (e.g. loaded save) to simplify troubleshooting.
* Added a `debug` console command to TrainerMod which lets you pass debug commands to the game (e.g. `debug warp FarmHouse 1 1` warps the player to the farmhouse). * Added a `debug` console command to TrainerMod which lets you pass debug commands to the game (e.g. `debug warp FarmHouse 1 1` warps the player to the farmhouse).
* Added a deprecation warning for mods that don't set the `UniqueID` manifest field, which will be required in SMAPI 2.0. * Added a deprecation warning for mods that don't set the `UniqueID` manifest field, which will be required in SMAPI 2.0.

View File

@ -4,18 +4,27 @@ using StardewValley.Menus;
namespace StardewModdingAPI namespace StardewModdingAPI
{ {
/// <summary>Provides information about the current game state.</summary> /// <summary>Provides information about the current game state.</summary>
internal static class Context public static class Context
{ {
/********* /*********
** Accessors ** Accessors
*********/ *********/
/****
** Public
****/
/// <summary>Whether the player has loaded a save and the world has finished initialising.</summary>
public static bool IsWorldReady { get; internal set; }
/****
** Internal
****/
/// <summary>Whether a player save has been loaded.</summary> /// <summary>Whether a player save has been loaded.</summary>
public static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name); internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.name);
/// <summary>Whether the game is currently writing to the save file.</summary> /// <summary>Whether the game is currently writing to the save file.</summary>
public static bool IsSaving => Game1.activeClickableMenu is SaveGameMenu || Game1.activeClickableMenu is ShippingMenu; // saving is performed by SaveGameMenu, but it's wrapped by ShippingMenu on days when the player shipping something internal static bool IsSaving => Game1.activeClickableMenu is SaveGameMenu || Game1.activeClickableMenu is ShippingMenu; // saving is performed by SaveGameMenu, but it's wrapped by ShippingMenu on days when the player shipping something
/// <summary>Whether the game is currently running the draw loop.</summary> /// <summary>Whether the game is currently running the draw loop.</summary>
public static bool IsInDrawLoop { get; set; } internal static bool IsInDrawLoop { get; set; }
} }
} }

View File

@ -39,9 +39,6 @@ namespace StardewModdingAPI.Framework
/// <summary>The number of consecutive failed draws.</summary> /// <summary>The number of consecutive failed draws.</summary>
private int FailedDraws; private int FailedDraws;
/// <summary>Whether the player has loaded a save and the world has finished initialising.</summary>
private bool IsWorldReady => this.AfterLoadTimer < 0;
/// <summary>Whether the game is returning to the menu.</summary> /// <summary>Whether the game is returning to the menu.</summary>
private bool IsExiting; private bool IsExiting;
@ -309,6 +306,7 @@ namespace StardewModdingAPI.Framework
if (this.AfterLoadTimer == 0) if (this.AfterLoadTimer == 0)
{ {
this.Monitor.Log($"Context: loaded saved game '{Constants.SaveFolderName}', starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}.", LogLevel.Trace); this.Monitor.Log($"Context: loaded saved game '{Constants.SaveFolderName}', starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}.", LogLevel.Trace);
Context.IsWorldReady = true;
SaveEvents.InvokeAfterLoad(this.Monitor); SaveEvents.InvokeAfterLoad(this.Monitor);
PlayerEvents.InvokeLoadedGame(this.Monitor, new EventArgsLoadedGameChanged(Game1.hasLoadedGame)); PlayerEvents.InvokeLoadedGame(this.Monitor, new EventArgsLoadedGameChanged(Game1.hasLoadedGame));
@ -325,9 +323,11 @@ namespace StardewModdingAPI.Framework
this.IsExiting = true; this.IsExiting = true;
// after exit to title // after exit to title
if (this.IsWorldReady && this.IsExiting && Game1.activeClickableMenu is TitleMenu) if (Context.IsWorldReady && this.IsExiting && Game1.activeClickableMenu is TitleMenu)
{ {
this.Monitor.Log("Context: returned to title", LogLevel.Trace); this.Monitor.Log("Context: returned to title", LogLevel.Trace);
Context.IsWorldReady = false;
SaveEvents.InvokeAfterReturnToTitle(this.Monitor); SaveEvents.InvokeAfterReturnToTitle(this.Monitor);
this.AfterLoadTimer = 5; this.AfterLoadTimer = 5;
this.IsExiting = false; this.IsExiting = false;
@ -421,7 +421,7 @@ namespace StardewModdingAPI.Framework
/********* /*********
** World & player events ** World & player events
*********/ *********/
if (this.IsWorldReady) if (Context.IsWorldReady)
{ {
// raise events (only when something changes, not on the initial load) // raise events (only when something changes, not on the initial load)
if (Game1.uniqueIDForThisGame == this.PreviousSaveID) if (Game1.uniqueIDForThisGame == this.PreviousSaveID)