/// <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(Harmony 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(Harmony))
         {
             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)");
         }
     }
 }
示例#2
0
        /// <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(Harmony instance)
        {
            if (PPatchManager.CheckConditions(Descriptor.RequireAssembly, Descriptor.
                                              RequireType, out Type requiredType))
            {
                var dest = new HarmonyMethod(Method);
                if (instance == null)
                {
                    throw new ArgumentNullException(nameof(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(nameof(HarmonyPatchType));
                    }
                } 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;
                    }
                }
            }
        }
示例#3
0
        public override void Initialize(Harmony plibInstance)
        {
            Instance = this;

            // Db
            plibInstance.Patch(typeof(Db), nameof(Db.Initialize), prefix: PatchMethod(nameof(
                                                                                          Initialize_Prefix)), postfix: PatchMethod(nameof(Initialize_Postfix)));

            // Game
            plibInstance.Patch(typeof(Game), "DestroyInstances", postfix: PatchMethod(nameof(
                                                                                          Game_DestroyInstances_Postfix)));
            plibInstance.Patch(typeof(Game), "OnPrefabInit", postfix: PatchMethod(nameof(
                                                                                      Game_OnPrefabInit_Postfix)));

            // GlobalResources
            plibInstance.Patch(typeof(GlobalResources), "Instance", postfix:
                               PatchMethod(nameof(Instance_Postfix)));

            // MainMenu
            plibInstance.Patch(typeof(MainMenu), "OnSpawn", postfix: PatchMethod(
                                   nameof(MainMenu_OnSpawn_Postfix)));
        }