using System; using Harmony; using StardewModdingAPI.Enums; using StardewModdingAPI.Framework.Patching; using StardewModdingAPI.Framework.Reflection; using StardewValley; using StardewValley.Menus; using StardewValley.Minigames; namespace StardewModdingAPI.Patches { /// Harmony patches which notify 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; /********* ** 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) { // 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( original: AccessTools.Method(typeof(Game1), nameof(Game1.loadForNewGame)), postfix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.After_Game1_LoadForNewGame)) ); } /********* ** Private methods *********/ /// Called before . /// 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_TitleMenu_CreatedNewCharacter() { LoadContextPatch.OnStageChanged(LoadStage.CreatedBasicInfo); return true; } /// Called after . /// This method must be static for Harmony to work correctly. See the Harmony documentation before renaming arguments. private static void After_Game1_LoadForNewGame() { bool creating = (Game1.currentMinigame is Intro) // creating save with intro || (Game1.activeClickableMenu is TitleMenu menu && LoadContextPatch.Reflection.GetField(menu, "transitioningCharacterCreationMenu").GetValue()); // creating save, skipped intro if (creating) LoadContextPatch.OnStageChanged(LoadStage.CreatedLocations); } } }