/// <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.prioritiy = priority.Value; } return(method); }
/// <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> /// <exception cref="InvalidOperationException">The type has no matching method.</exception> public static MethodInfo RequireMethod <TTarget>(string name, Type[] parameters = null, Type[] generics = null) { return (AccessTools.Method(typeof(TTarget), name, parameters, generics) ?? throw new InvalidOperationException($"Can't find method {PatchHelper.GetMethodString(typeof(TTarget), name, parameters, generics)} to patch.")); }
/********* ** Public methods *********/ /// <summary>Get a constructor 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> /// <exception cref="InvalidOperationException">The type has no matching constructor.</exception> public static ConstructorInfo RequireConstructor <TTarget>(Type[] parameters = null) { return (AccessTools.Constructor(typeof(TTarget), parameters) ?? throw new InvalidOperationException($"Can't find constructor {PatchHelper.GetMethodString(typeof(TTarget), null, parameters)} to patch.")); }