diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-06-15 22:17:32 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2020-06-15 22:17:32 -0400 |
commit | dcd2c647a2abd836e8ee20f8ddad6568c9b4fbf2 (patch) | |
tree | 5a03bc9ec491467379f5bfea85e8778500461403 /src/SMAPI/Framework/Patching | |
parent | 6d1cd7d9b884bddd00675dfdca9f63dc7db1bd1f (diff) | |
download | SMAPI-dcd2c647a2abd836e8ee20f8ddad6568c9b4fbf2.tar.gz SMAPI-dcd2c647a2abd836e8ee20f8ddad6568c9b4fbf2.tar.bz2 SMAPI-dcd2c647a2abd836e8ee20f8ddad6568c9b4fbf2.zip |
temporarily restore Harmony 1.x support with compile flag (#711)
Diffstat (limited to 'src/SMAPI/Framework/Patching')
-rw-r--r-- | src/SMAPI/Framework/Patching/GamePatcher.cs | 8 | ||||
-rw-r--r-- | src/SMAPI/Framework/Patching/IHarmonyPatch.cs | 8 | ||||
-rw-r--r-- | src/SMAPI/Framework/Patching/PatchHelper.cs | 36 |
3 files changed, 52 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Patching/GamePatcher.cs b/src/SMAPI/Framework/Patching/GamePatcher.cs index dcad285a..82d7b9c8 100644 --- a/src/SMAPI/Framework/Patching/GamePatcher.cs +++ b/src/SMAPI/Framework/Patching/GamePatcher.cs @@ -1,5 +1,9 @@ using System; +#if HARMONY_2 using HarmonyLib; +#else +using Harmony; +#endif namespace StardewModdingAPI.Framework.Patching { @@ -27,7 +31,11 @@ namespace StardewModdingAPI.Framework.Patching /// <param name="patches">The patches to apply.</param> public void Apply(params IHarmonyPatch[] patches) { +#if HARMONY_2 Harmony harmony = new Harmony("SMAPI"); +#else + HarmonyInstance harmony = HarmonyInstance.Create("SMAPI"); +#endif foreach (IHarmonyPatch patch in patches) { try diff --git a/src/SMAPI/Framework/Patching/IHarmonyPatch.cs b/src/SMAPI/Framework/Patching/IHarmonyPatch.cs index 7d5eb3d4..922243fa 100644 --- a/src/SMAPI/Framework/Patching/IHarmonyPatch.cs +++ b/src/SMAPI/Framework/Patching/IHarmonyPatch.cs @@ -1,4 +1,8 @@ +#if HARMONY_2 using HarmonyLib; +#else +using Harmony; +#endif namespace StardewModdingAPI.Framework.Patching { @@ -10,6 +14,10 @@ namespace StardewModdingAPI.Framework.Patching /// <summary>Apply the Harmony patch.</summary> /// <param name="harmony">The Harmony instance.</param> +#if HARMONY_2 void Apply(Harmony harmony); +#else + void Apply(HarmonyInstance harmony); +#endif } } diff --git a/src/SMAPI/Framework/Patching/PatchHelper.cs b/src/SMAPI/Framework/Patching/PatchHelper.cs new file mode 100644 index 00000000..d1aa0185 --- /dev/null +++ b/src/SMAPI/Framework/Patching/PatchHelper.cs @@ -0,0 +1,36 @@ +#if !HARMONY_2 +using System; +using System.Collections.Generic; + +namespace StardewModdingAPI.Framework.Patching +{ + /// <summary>Provides generic methods for implementing Harmony patches.</summary> + internal class PatchHelper + { + /********* + ** Fields + *********/ + /// <summary>The interception keys currently being intercepted.</summary> + private static readonly HashSet<string> InterceptingKeys = new HashSet<string>(StringComparer.OrdinalIgnoreCase); + + + /********* + ** Public methods + *********/ + /// <summary>Track a method that will be intercepted.</summary> + /// <param name="key">The intercept key.</param> + /// <returns>Returns false if the method was already marked for interception, else true.</returns> + public static bool StartIntercept(string key) + { + return PatchHelper.InterceptingKeys.Add(key); + } + + /// <summary>Track a method as no longer being intercepted.</summary> + /// <param name="key">The intercept key.</param> + public static void StopIntercept(string key) + { + PatchHelper.InterceptingKeys.Remove(key); + } + } +} +#endif |