diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-07-30 00:54:15 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2021-07-30 00:54:15 -0400 |
commit | 948c800a98f00b1bdcfd05ee6228e3423f9eb465 (patch) | |
tree | b958b3cdb4428fa8788a5698a207a45912923de7 /src/SMAPI.Internal.Patching/BasePatcher.cs | |
parent | 4074f697d73f5cac6699836550b144fd0c4e2803 (diff) | |
download | SMAPI-948c800a98f00b1bdcfd05ee6228e3423f9eb465.tar.gz SMAPI-948c800a98f00b1bdcfd05ee6228e3423f9eb465.tar.bz2 SMAPI-948c800a98f00b1bdcfd05ee6228e3423f9eb465.zip |
migrate to the new Harmony patch pattern used in my mods
That improves validation and error-handling.
Diffstat (limited to 'src/SMAPI.Internal.Patching/BasePatcher.cs')
-rw-r--r-- | src/SMAPI.Internal.Patching/BasePatcher.cs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/SMAPI.Internal.Patching/BasePatcher.cs b/src/SMAPI.Internal.Patching/BasePatcher.cs new file mode 100644 index 00000000..87155d7f --- /dev/null +++ b/src/SMAPI.Internal.Patching/BasePatcher.cs @@ -0,0 +1,54 @@ +using System; +using System.Reflection; +using HarmonyLib; + +namespace StardewModdingAPI.Internal.Patching +{ + /// <summary>Provides base implementation logic for <see cref="IPatcher"/> instances.</summary> + internal abstract class BasePatcher : IPatcher + { + /********* + ** Public methods + *********/ + /// <inheritdoc /> + public abstract void Apply(Harmony harmony, IMonitor monitor); + + + /********* + ** Protected methods + *********/ + /// <summary>Get a method and assert that it was found.</summary> + /// <typeparam name="TTarget">The type containing the method.</typeparam> + /// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param> + protected ConstructorInfo RequireConstructor<TTarget>(params Type[] parameters) + { + return PatchHelper.RequireConstructor<TTarget>(parameters); + } + + /// <summary>Get a method and assert that it was found.</summary> + /// <typeparam name="TTarget">The type containing the method.</typeparam> + /// <param name="name">The method name.</param> + /// <param name="parameters">The method parameter types, or <c>null</c> if it's not overloaded.</param> + /// <param name="generics">The method generic types, or <c>null</c> if it's not generic.</param> + protected MethodInfo RequireMethod<TTarget>(string name, Type[] parameters = null, Type[] generics = null) + { + return PatchHelper.RequireMethod<TTarget>(name, parameters, generics); + } + + /// <summary>Get a Harmony patch method on the current patcher instance.</summary> + /// <param name="name">The method name.</param> + /// <param name="priority">The patch priority to apply, usually specified using Harmony's <see cref="Priority"/> enum, or <c>null</c> to keep the default value.</param> + protected HarmonyMethod GetHarmonyMethod(string name, int? priority = null) + { + var method = new HarmonyMethod( + AccessTools.Method(this.GetType(), name) + ?? throw new InvalidOperationException($"Can't find patcher method {PatchHelper.GetMethodString(this.GetType(), name)}.") + ); + + if (priority.HasValue) + method.priority = priority.Value; + + return method; + } + } +} |