fix inconsistent LoadStage behavior when creating a new save
This commit is contained in:
parent
2be3b7fb01
commit
f2dd11fe3f
|
@ -25,6 +25,7 @@ These changes have not been released yet.
|
||||||
* Removed all deprecated APIs.
|
* Removed all deprecated APIs.
|
||||||
* Removed the `Monitor.ExitGameImmediately` method.
|
* Removed the `Monitor.ExitGameImmediately` method.
|
||||||
* Updated to Json.NET 12.0.1.
|
* Updated to Json.NET 12.0.1.
|
||||||
|
* Fixed `LoadStageChanged` event not raising correct flags in some cases when creating a new save.
|
||||||
|
|
||||||
## 2.11.3
|
## 2.11.3
|
||||||
Released 13 September 2019 for Stardew Valley 1.3.36.
|
Released 13 September 2019 for Stardew Valley 1.3.36.
|
||||||
|
|
|
@ -774,7 +774,7 @@ namespace StardewModdingAPI.Framework
|
||||||
}
|
}
|
||||||
|
|
||||||
// preloaded
|
// preloaded
|
||||||
if (Context.IsSaveLoaded && Context.LoadStage != LoadStage.Loaded && Context.LoadStage != LoadStage.Ready)
|
if (Context.IsSaveLoaded && Context.LoadStage != LoadStage.Loaded && Context.LoadStage != LoadStage.Ready && Game1.dayOfMonth != 0)
|
||||||
this.OnLoadStageChanged(LoadStage.Loaded);
|
this.OnLoadStageChanged(LoadStage.Loaded);
|
||||||
|
|
||||||
// update tick
|
// update tick
|
||||||
|
|
|
@ -1,16 +1,15 @@
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.ObjectModel;
|
|
||||||
using System.Collections.Specialized;
|
|
||||||
using Harmony;
|
using Harmony;
|
||||||
using StardewModdingAPI.Enums;
|
using StardewModdingAPI.Enums;
|
||||||
using StardewModdingAPI.Framework.Patching;
|
using StardewModdingAPI.Framework.Patching;
|
||||||
using StardewModdingAPI.Framework.Reflection;
|
using StardewModdingAPI.Framework.Reflection;
|
||||||
using StardewValley;
|
using StardewValley;
|
||||||
using StardewValley.Menus;
|
using StardewValley.Menus;
|
||||||
|
using StardewValley.Minigames;
|
||||||
|
|
||||||
namespace StardewModdingAPI.Patches
|
namespace StardewModdingAPI.Patches
|
||||||
{
|
{
|
||||||
/// <summary>A Harmony patch for <see cref="Game1.loadForNewGame"/> which notifies SMAPI for save creation load stages.</summary>
|
/// <summary>Harmony patches which notify SMAPI for save creation load stages.</summary>
|
||||||
/// <remarks>This patch hooks into <see cref="Game1.loadForNewGame"/>, checks if <c>TitleMenu.transitioningCharacterCreationMenu</c> is true (which means the player is creating a new save file), then raises <see cref="LoadStage.CreatedBasicInfo"/> after the location list is cleared twice (the second clear happens right before locations are created), and <see cref="LoadStage.CreatedLocations"/> when the method ends.</remarks>
|
/// <remarks>This patch hooks into <see cref="Game1.loadForNewGame"/>, checks if <c>TitleMenu.transitioningCharacterCreationMenu</c> is true (which means the player is creating a new save file), then raises <see cref="LoadStage.CreatedBasicInfo"/> after the location list is cleared twice (the second clear happens right before locations are created), and <see cref="LoadStage.CreatedLocations"/> when the method ends.</remarks>
|
||||||
internal class LoadContextPatch : IHarmonyPatch
|
internal class LoadContextPatch : IHarmonyPatch
|
||||||
{
|
{
|
||||||
|
@ -23,12 +22,6 @@ namespace StardewModdingAPI.Patches
|
||||||
/// <summary>A callback to invoke when the load stage changes.</summary>
|
/// <summary>A callback to invoke when the load stage changes.</summary>
|
||||||
private static Action<LoadStage> OnStageChanged;
|
private static Action<LoadStage> OnStageChanged;
|
||||||
|
|
||||||
/// <summary>Whether <see cref="Game1.loadForNewGame"/> was called as part of save creation.</summary>
|
|
||||||
private static bool IsCreating;
|
|
||||||
|
|
||||||
/// <summary>The number of times that <see cref="Game1.locations"/> has been cleared since <see cref="Game1.loadForNewGame"/> started.</summary>
|
|
||||||
private static int TimesLocationsCleared;
|
|
||||||
|
|
||||||
|
|
||||||
/*********
|
/*********
|
||||||
** Accessors
|
** Accessors
|
||||||
|
@ -53,9 +46,15 @@ namespace StardewModdingAPI.Patches
|
||||||
/// <param name="harmony">The Harmony instance.</param>
|
/// <param name="harmony">The Harmony instance.</param>
|
||||||
public void Apply(HarmonyInstance harmony)
|
public void Apply(HarmonyInstance harmony)
|
||||||
{
|
{
|
||||||
|
// detect CreatedBasicInfo
|
||||||
|
harmony.Patch(
|
||||||
|
original: AccessTools.Method(typeof(TitleMenu), nameof(TitleMenu.createdNewCharacter)),
|
||||||
|
prefix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Before_TitleMenu_CreatedNewCharacter))
|
||||||
|
);
|
||||||
|
|
||||||
|
// detect CreatedLocations
|
||||||
harmony.Patch(
|
harmony.Patch(
|
||||||
original: AccessTools.Method(typeof(Game1), nameof(Game1.loadForNewGame)),
|
original: AccessTools.Method(typeof(Game1), nameof(Game1.loadForNewGame)),
|
||||||
prefix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Before_Game1_LoadForNewGame)),
|
|
||||||
postfix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.After_Game1_LoadForNewGame))
|
postfix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.After_Game1_LoadForNewGame))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
@ -64,45 +63,25 @@ namespace StardewModdingAPI.Patches
|
||||||
/*********
|
/*********
|
||||||
** Private methods
|
** Private methods
|
||||||
*********/
|
*********/
|
||||||
/// <summary>The method to call instead of <see cref="Game1.loadForNewGame"/>.</summary>
|
/// <summary>Called before <see cref="TitleMenu.createdNewCharacter"/>.</summary>
|
||||||
/// <returns>Returns whether to execute the original method.</returns>
|
/// <returns>Returns whether to execute the original method.</returns>
|
||||||
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
|
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
|
||||||
private static bool Before_Game1_LoadForNewGame()
|
private static bool Before_TitleMenu_CreatedNewCharacter()
|
||||||
{
|
{
|
||||||
LoadContextPatch.IsCreating = Game1.activeClickableMenu is TitleMenu menu && LoadContextPatch.Reflection.GetField<bool>(menu, "transitioningCharacterCreationMenu").GetValue();
|
LoadContextPatch.OnStageChanged(LoadStage.CreatedBasicInfo);
|
||||||
LoadContextPatch.TimesLocationsCleared = 0;
|
|
||||||
if (LoadContextPatch.IsCreating)
|
|
||||||
{
|
|
||||||
// raise CreatedBasicInfo after locations are cleared twice
|
|
||||||
ObservableCollection<GameLocation> locations = (ObservableCollection<GameLocation>)Game1.locations;
|
|
||||||
locations.CollectionChanged += LoadContextPatch.OnLocationListChanged;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The method to call instead after <see cref="Game1.loadForNewGame"/>.</summary>
|
/// <summary>Called after <see cref="Game1.loadForNewGame"/>.</summary>
|
||||||
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
|
/// <remarks>This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments.</remarks>
|
||||||
private static void After_Game1_LoadForNewGame()
|
private static void After_Game1_LoadForNewGame()
|
||||||
{
|
{
|
||||||
if (LoadContextPatch.IsCreating)
|
bool creating =
|
||||||
{
|
(Game1.currentMinigame is Intro) // creating save with intro
|
||||||
// clean up
|
|| (Game1.activeClickableMenu is TitleMenu menu && LoadContextPatch.Reflection.GetField<bool>(menu, "transitioningCharacterCreationMenu").GetValue()); // creating save, skipped intro
|
||||||
ObservableCollection<GameLocation> locations = (ObservableCollection<GameLocation>)Game1.locations;
|
|
||||||
locations.CollectionChanged -= LoadContextPatch.OnLocationListChanged;
|
|
||||||
|
|
||||||
// raise stage changed
|
if (creating)
|
||||||
LoadContextPatch.OnStageChanged(LoadStage.CreatedLocations);
|
LoadContextPatch.OnStageChanged(LoadStage.CreatedLocations);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <summary>Raised when <see cref="Game1.locations"/> changes.</summary>
|
|
||||||
/// <param name="sender">The event sender.</param>
|
|
||||||
/// <param name="e">The event arguments.</param>
|
|
||||||
private static void OnLocationListChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
||||||
{
|
|
||||||
if (++LoadContextPatch.TimesLocationsCleared == 2)
|
|
||||||
LoadContextPatch.OnStageChanged(LoadStage.CreatedBasicInfo);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue