using System; using System.Reflection; using HarmonyLib; namespace StardewModdingAPI.Internal.Patching { /// Provides base implementation logic for instances. internal abstract class BasePatcher : IPatcher { /********* ** Public methods *********/ /// public abstract void Apply(Harmony harmony, IMonitor monitor); /********* ** Protected methods *********/ /// Get a method and assert that it was found. /// The type containing the method. /// The method parameter types, or null if it's not overloaded. protected ConstructorInfo RequireConstructor(params Type[] parameters) { return PatchHelper.RequireConstructor(parameters); } /// Get a method and assert that it was found. /// The type containing the method. /// The method name. /// The method parameter types, or null if it's not overloaded. /// The method generic types, or null if it's not generic. protected MethodInfo RequireMethod(string name, Type[]? parameters = null, Type[]? generics = null) { return PatchHelper.RequireMethod(name, parameters, generics); } /// Get a Harmony patch method on the current patcher instance. /// The method name. /// The patch priority to apply, usually specified using Harmony's enum, or null to keep the default value. protected HarmonyMethod GetHarmonyMethod(string name, int? priority = null) { HarmonyMethod method = new( 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; } } }