using System; using System.Collections.ObjectModel; using System.Collections.Specialized; using Harmony; using StardewModdingAPI.Enums; using StardewModdingAPI.Framework.Patching; using StardewModdingAPI.Framework.Reflection; using StardewValley; using StardewValley.Menus; namespace StardewModdingAPI.Patches { /// A Harmony patch for which notifies SMAPI for save creation load stages. /// This patch hooks into , checks if TitleMenu.transitioningCharacterCreationMenu is true (which means the player is creating a new save file), then raises after the location list is cleared twice (the second clear happens right before locations are created), and when the method ends. internal class LoadContextPatch : IHarmonyPatch { /********* ** Fields *********/ /// Simplifies access to private code. private static Reflector Reflection; /// A callback to invoke when the load stage changes. private static Action OnStageChanged; /// Whether was called as part of save creation. private static bool IsCreating; /// The number of times that has been cleared since started. private static int TimesLocationsCleared; /********* ** Accessors *********/ /// A unique name for this patch. public string Name => $"{nameof(LoadContextPatch)}"; /********* ** Public methods *********/ /// Construct an instance. /// Simplifies access to private code. /// A callback to invoke when the load stage changes. public LoadContextPatch(Reflector reflection, Action onStageChanged) { LoadContextPatch.Reflection = reflection; LoadContextPatch.OnStageChanged = onStageChanged; } /// Apply the Harmony patch. /// The Harmony instance. public void Apply(HarmonyInstance harmony) { harmony.Patch( 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)) ); } /********* ** Private methods *********/ /// The method to call instead of . /// Returns whether to execute the original method. /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. private static bool Before_Game1_LoadForNewGame() { LoadContextPatch.IsCreating = Game1.activeClickableMenu is TitleMenu menu && LoadContextPatch.Reflection.GetField(menu, "transitioningCharacterCreationMenu").GetValue(); LoadContextPatch.TimesLocationsCleared = 0; if (LoadContextPatch.IsCreating) { // raise CreatedBasicInfo after locations are cleared twice ObservableCollection locations = (ObservableCollection)Game1.locations; locations.CollectionChanged += LoadContextPatch.OnLocationListChanged; } return true; } /// The method to call instead after . /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. private static void After_Game1_LoadForNewGame() { if (LoadContextPatch.IsCreating) { // clean up ObservableCollection locations = (ObservableCollection)Game1.locations; locations.CollectionChanged -= LoadContextPatch.OnLocationListChanged; // raise stage changed LoadContextPatch.OnStageChanged(LoadStage.CreatedLocations); } } /// Raised when changes. /// The event sender. /// The event arguments. private static void OnLocationListChanged(object sender, NotifyCollectionChangedEventArgs e) { if (++LoadContextPatch.TimesLocationsCleared == 2) LoadContextPatch.OnStageChanged(LoadStage.CreatedBasicInfo); } } }