/// <summary> /// Runs the method, passing the required parameters if any. /// </summary> /// <param name="instance">The Harmony instance to use if the method wants to /// perform a patch.</param> public void Run(HarmonyInstance instance) { if (PPatchManager.CheckConditions(Descriptor.RequireAssembly, Descriptor. RequireType, out Type requiredType)) { // Only runs once, no meaningful savings with a delegate var paramTypes = Method.GetParameterTypes(); int len = paramTypes.Length; if (len <= 0) { // No parameters, static method only Method.Invoke(null, null); } else if (paramTypes[0] == typeof(HarmonyInstance)) { if (len == 1) { // Harmony instance parameter Method.Invoke(null, new object[] { instance }); } else if (len == 2 && paramTypes[1] == typeof(Type)) { // Type parameter Method.Invoke(null, new object[] { instance, requiredType }); } } else { PUtil.LogWarning("Invalid signature for PLibMethod - must have (), " + "(HarmonyInstance), or (HarmonyInstance, Type)"); } } }
/// <summary> /// Applies the patch. /// </summary> /// <param name="instance">The Harmony instance to use.</param> /// <exception cref="InvalidOperationException">If the </exception> /// <exception cref="AmbiguousMatchException">If no parameter types were specified, /// and multiple options match the method name.</exception> public void Run(HarmonyInstance instance) { if (PPatchManager.CheckConditions(Descriptor.RequireAssembly, Descriptor. RequireType, out Type requiredType)) { var dest = new HarmonyMethod(Method); if (instance == null) { throw new ArgumentNullException("instance"); } try { var method = GetTargetMethod(requiredType); switch (GetPatchType()) { case HarmonyPatchType.Postfix: instance.Patch(method, postfix: dest); break; case HarmonyPatchType.Prefix: instance.Patch(method, prefix: dest); break; case HarmonyPatchType.Transpiler: instance.Patch(method, transpiler: dest); break; default: throw new ArgumentOutOfRangeException("PatchType"); } } catch (AmbiguousMatchException e) { // Multi catch or filtering is not available in this version of C# if (!LogIgnoreOnFail(e)) { throw; } } catch (InvalidOperationException e) { if (!LogIgnoreOnFail(e)) { throw; } } } }