diff options
| author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-30 13:40:48 -0400 | 
|---|---|---|
| committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2018-06-30 13:40:48 -0400 | 
| commit | 79ad322a8e31e977e455817469427f9c3cf218fd (patch) | |
| tree | 41e4693aeaeb709bced710e15dbb870cb9fd1b99 /src | |
| parent | d67690ea3ea76512af194168af02a29120765247 (diff) | |
| download | SMAPI-79ad322a8e31e977e455817469427f9c3cf218fd.tar.gz SMAPI-79ad322a8e31e977e455817469427f9c3cf218fd.tar.bz2 SMAPI-79ad322a8e31e977e455817469427f9c3cf218fd.zip | |
tweak world-ready events to handle edge cases
In particular:
- world was never considered ready if the player's name was blank;
- AfterReturnToTitle didn't trigger after being disconnected in multiplayer (#545).
Diffstat (limited to 'src')
| -rw-r--r-- | src/SMAPI/Context.cs | 2 | ||||
| -rw-r--r-- | src/SMAPI/Framework/SGame.cs | 31 | 
2 files changed, 15 insertions, 18 deletions
| diff --git a/src/SMAPI/Context.cs b/src/SMAPI/Context.cs index 79067b2d..74def086 100644 --- a/src/SMAPI/Context.cs +++ b/src/SMAPI/Context.cs @@ -35,7 +35,7 @@ namespace StardewModdingAPI          ** Internal          ****/          /// <summary>Whether a player save has been loaded.</summary> -        internal static bool IsSaveLoaded => Game1.hasLoadedGame && !string.IsNullOrEmpty(Game1.player.Name); +        internal static bool IsSaveLoaded => Game1.hasLoadedGame && !(Game1.activeClickableMenu is TitleMenu);          /// <summary>Whether the game is currently writing to the save file.</summary>          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 diff --git a/src/SMAPI/Framework/SGame.cs b/src/SMAPI/Framework/SGame.cs index 57afec06..def0943c 100644 --- a/src/SMAPI/Framework/SGame.cs +++ b/src/SMAPI/Framework/SGame.cs @@ -61,7 +61,7 @@ namespace StardewModdingAPI.Framework          /// <summary>The number of ticks until SMAPI should notify mods that the game has loaded.</summary>          /// <remarks>Skipping a few frames ensures the game finishes initialising the world before mods try to change it.</remarks> -        private int AfterLoadTimer = 5; +        private readonly Countdown AfterLoadTimer = new Countdown(5);          /// <summary>Whether the after-load events were raised for this session.</summary>          private bool RaisedAfterLoadEvent; @@ -313,13 +313,14 @@ namespace StardewModdingAPI.Framework                  /*********                  ** Update context                  *********/ -                if (Context.IsWorldReady && !Context.IsSaveLoaded) +                bool wasWorldReady = Context.IsWorldReady; +                if ((Context.IsWorldReady && !Context.IsSaveLoaded) || Game1.exitToTitle)                      this.MarkWorldNotReady(); -                else if (Context.IsSaveLoaded && !SaveGame.IsProcessing /*still loading save*/ && this.AfterLoadTimer >= 0 && Game1.currentLocation != null) +                else if (Context.IsSaveLoaded && !SaveGame.IsProcessing /*still loading save*/ && this.AfterLoadTimer.Current > 0 && Game1.currentLocation != null)                  {                      if (Game1.dayOfMonth != 0) // wait until new-game intro finishes (world not fully initialised yet) -                        this.AfterLoadTimer--; -                    Context.IsWorldReady = this.AfterLoadTimer <= 0; +                        this.AfterLoadTimer.Decrement(); +                    Context.IsWorldReady = this.AfterLoadTimer.Current == 0;                  }                  /********* @@ -342,9 +343,14 @@ namespace StardewModdingAPI.Framework                  }                  /********* -                ** After load events +                ** Load / return-to-title events                  *********/ -                if (!this.RaisedAfterLoadEvent && Context.IsWorldReady) +                if (wasWorldReady && !Context.IsWorldReady) +                { +                    this.Monitor.Log("Context: returned to title", LogLevel.Trace); +                    this.Events.Save_AfterReturnToTitle.Raise(); +                } +                else if (!this.RaisedAfterLoadEvent && Context.IsWorldReady)                  {                      // print context                      string context = $"Context: loaded saved game '{Constants.SaveFolderName}', starting {Game1.currentSeason} {Game1.dayOfMonth} Y{Game1.year}."; @@ -364,15 +370,6 @@ namespace StardewModdingAPI.Framework                  }                  /********* -                ** Exit to title events -                *********/ -                if (Game1.exitToTitle) -                { -                    this.Monitor.Log("Context: returned to title", LogLevel.Trace); -                    this.Events.Save_AfterReturnToTitle.Raise(); -                } - -                /*********                  ** Window events                  *********/                  // Here we depend on the game's viewport instead of listening to the Window.Resize @@ -1342,7 +1339,7 @@ namespace StardewModdingAPI.Framework          private void MarkWorldNotReady()          {              Context.IsWorldReady = false; -            this.AfterLoadTimer = 5; +            this.AfterLoadTimer.Reset();              this.RaisedAfterLoadEvent = false;          } | 
