using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Reflection;
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 LoadForNewGamePatch : 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 = 0;
/*********
** Accessors
*********/
/// A unique name for this patch.
public string Name => $"{nameof(LoadForNewGamePatch)}";
/*********
** Public methods
*********/
/// Construct an instance.
/// Simplifies access to private code.
/// A callback to invoke when the load stage changes.
public LoadForNewGamePatch(Reflector reflection, Action onStageChanged)
{
LoadForNewGamePatch.Reflection = reflection;
LoadForNewGamePatch.OnStageChanged = onStageChanged;
}
/// Apply the Harmony patch.
/// The Harmony instance.
public void Apply(HarmonyInstance harmony)
{
MethodInfo method = AccessTools.Method(typeof(Game1), nameof(Game1.loadForNewGame));
MethodInfo prefix = AccessTools.Method(this.GetType(), nameof(LoadForNewGamePatch.Prefix));
MethodInfo postfix = AccessTools.Method(this.GetType(), nameof(LoadForNewGamePatch.Postfix));
harmony.Patch(method, new HarmonyMethod(prefix), new HarmonyMethod(postfix));
}
/*********
** 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 Prefix()
{
LoadForNewGamePatch.IsCreating = Game1.activeClickableMenu is TitleMenu menu && LoadForNewGamePatch.Reflection.GetField(menu, "transitioningCharacterCreationMenu").GetValue();
LoadForNewGamePatch.TimesLocationsCleared = 0;
if (LoadForNewGamePatch.IsCreating)
{
// raise CreatedBasicInfo after locations are cleared twice
ObservableCollection locations = (ObservableCollection)Game1.locations;
locations.CollectionChanged += LoadForNewGamePatch.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 Postfix()
{
if (LoadForNewGamePatch.IsCreating)
{
// clean up
ObservableCollection locations = (ObservableCollection) Game1.locations;
locations.CollectionChanged -= LoadForNewGamePatch.OnLocationListChanged;
// raise stage changed
LoadForNewGamePatch.OnStageChanged(LoadStage.CreatedLocations);
}
}
/// Raised when changes.
/// The event sender.
/// The event arguments.
private static void OnLocationListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if (++LoadForNewGamePatch.TimesLocationsCleared == 2)
LoadForNewGamePatch.OnStageChanged(LoadStage.CreatedBasicInfo);
}
}
}