summaryrefslogtreecommitdiff
path: root/src/SMAPI/Patches/LoadContextPatch.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-07-30 00:40:12 -0400
committerJesse Plamondon-Willard <Pathoschild@users.noreply.github.com>2021-07-30 00:40:12 -0400
commit4074f697d73f5cac6699836550b144fd0c4e2803 (patch)
tree022363fa6cf01b947cd39d8a754fb36326898059 /src/SMAPI/Patches/LoadContextPatch.cs
parentaa65b2e2f6ae2578f9d1f81d6fc1f4c5a261d90f (diff)
downloadSMAPI-4074f697d73f5cac6699836550b144fd0c4e2803.tar.gz
SMAPI-4074f697d73f5cac6699836550b144fd0c4e2803.tar.bz2
SMAPI-4074f697d73f5cac6699836550b144fd0c4e2803.zip
rename patch classes for consistency
Diffstat (limited to 'src/SMAPI/Patches/LoadContextPatch.cs')
-rw-r--r--src/SMAPI/Patches/LoadContextPatch.cs125
1 files changed, 0 insertions, 125 deletions
diff --git a/src/SMAPI/Patches/LoadContextPatch.cs b/src/SMAPI/Patches/LoadContextPatch.cs
deleted file mode 100644
index c7f7d986..00000000
--- a/src/SMAPI/Patches/LoadContextPatch.cs
+++ /dev/null
@@ -1,125 +0,0 @@
-using System;
-using System.Diagnostics.CodeAnalysis;
-using HarmonyLib;
-using StardewModdingAPI.Enums;
-using StardewModdingAPI.Framework.Patching;
-using StardewModdingAPI.Framework.Reflection;
-using StardewValley;
-using StardewValley.Menus;
-using StardewValley.Minigames;
-
-namespace StardewModdingAPI.Patches
-{
- /// <summary>Harmony patches which notify SMAPI for save creation load stages.</summary>
- /// <remarks>Patch methods must be static for Harmony to work correctly. See the Harmony documentation before renaming patch arguments.</remarks>
- [SuppressMessage("ReSharper", "InconsistentNaming", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
- [SuppressMessage("ReSharper", "IdentifierTypo", Justification = "Argument names are defined by Harmony and methods are named for clarity.")]
- internal class LoadContextPatch : IHarmonyPatch
- {
- /*********
- ** Fields
- *********/
- /// <summary>Simplifies access to private code.</summary>
- private static Reflector Reflection;
-
- /// <summary>A callback to invoke when the load stage changes.</summary>
- private static Action<LoadStage> OnStageChanged;
-
- /// <summary>Whether the game is running running the code in <see cref="Game1.loadForNewGame"/>.</summary>
- private static bool IsInLoadForNewGame;
-
-
- /*********
- ** Public methods
- *********/
- /// <summary>Construct an instance.</summary>
- /// <param name="reflection">Simplifies access to private code.</param>
- /// <param name="onStageChanged">A callback to invoke when the load stage changes.</param>
- public LoadContextPatch(Reflector reflection, Action<LoadStage> onStageChanged)
- {
- LoadContextPatch.Reflection = reflection;
- LoadContextPatch.OnStageChanged = onStageChanged;
- }
-
- /// <inheritdoc />
- public void Apply(Harmony harmony)
- {
- // detect CreatedInitialLocations and SaveAddedLocations
- harmony.Patch(
- original: AccessTools.Method(typeof(Game1), nameof(Game1.AddModNPCs)),
- prefix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Before_Game1_AddModNPCs))
- );
-
- // detect CreatedLocations, and track IsInLoadForNewGame
- 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))
- );
-
- // detect ReturningToTitle
- harmony.Patch(
- original: AccessTools.Method(typeof(Game1), nameof(Game1.CleanupReturningToTitle)),
- prefix: new HarmonyMethod(this.GetType(), nameof(LoadContextPatch.Before_Game1_CleanupReturningToTitle))
- );
- }
-
-
- /*********
- ** Private methods
- *********/
- /// <summary>Called before <see cref="Game1.AddModNPCs"/>.</summary>
- /// <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>
- private static bool Before_Game1_AddModNPCs()
- {
- // When this method is called from Game1.loadForNewGame, it happens right after adding the vanilla
- // locations but before initializing them.
- if (LoadContextPatch.IsInLoadForNewGame)
- {
- LoadContextPatch.OnStageChanged(LoadContextPatch.IsCreating()
- ? LoadStage.CreatedInitialLocations
- : LoadStage.SaveAddedLocations
- );
- }
-
- return true;
- }
-
- /// <summary>Called before <see cref="Game1.CleanupReturningToTitle"/>.</summary>
- /// <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>
- private static bool Before_Game1_CleanupReturningToTitle()
- {
- LoadContextPatch.OnStageChanged(LoadStage.ReturningToTitle);
- return true;
- }
-
- /// <summary>Called before <see cref="Game1.loadForNewGame"/>.</summary>
- /// <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>
- private static bool Before_Game1_LoadForNewGame()
- {
- LoadContextPatch.IsInLoadForNewGame = true;
- return true;
- }
-
- /// <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>
- private static void After_Game1_LoadForNewGame()
- {
- LoadContextPatch.IsInLoadForNewGame = false;
-
- if (LoadContextPatch.IsCreating())
- LoadContextPatch.OnStageChanged(LoadStage.CreatedLocations);
- }
-
- /// <summary>Get whether the save file is currently being created.</summary>
- private static bool IsCreating()
- {
- return
- (Game1.currentMinigame is Intro) // creating save with intro
- || (Game1.activeClickableMenu is TitleMenu menu && LoadContextPatch.Reflection.GetField<bool>(menu, "transitioningCharacterCreationMenu").GetValue()); // creating save, skipped intro
- }
- }
-}