/// <summary> /// Creates a new patch operation using the supplied target method, stub method, and patch location. /// </summary> /// <param name="targetMethod">The target method or constructor that will be patched.</param> /// <param name="stubMethod">The stub method that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> public HPatchOperation(MethodBase targetMethod, MethodInfo stubMethod, HPatchLocation patchLocation, int patchPriority = -1) { TargetMethod = targetMethod; StubMethod = stubMethod; PatchLocation = patchLocation; PatchPriority = patchPriority; }
/// <summary> /// Creates a new patch operation using the supplied target type & method name, stub method, and patch location. /// </summary> /// <param name="targetType">The target type that contains the target method.</param> /// <param name="targetMethodName">The name of the target method.</param> /// <param name="stubMethod">The stub method that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> public HPatchOperation(Type targetType, string targetMethodName, MethodInfo stubMethod, HPatchLocation patchLocation, int patchPriority = -1) { MethodBase targetMethod = targetType.GetRuntimeMethods().Where(m => m.Name == targetMethodName).FirstOrDefault(); // Search regular methods first if (targetMethod == null) // Search constructors next if nothing was found { targetMethod = targetType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Where(m => m.Name == targetMethodName).FirstOrDefault(); } if (targetMethod == null) { throw new Exception("No method or constructor with the name \"" + targetMethodName + "\" was found in target type \"" + targetType.FullName + "\""); } TargetMethod = targetMethod; StubMethod = stubMethod; PatchLocation = patchLocation; PatchPriority = patchPriority; }
/// <summary> /// Creates a new patch operation using the supplied target type and method name, stub method, and patch location. /// </summary> /// <param name="targetType">The target type (in Terraria) that contains the target method.</param> /// <param name="targetMethodName">The name of the target method.</param> /// <param name="stubMethod">The stub method (in your plugin) that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> protected void CreateHPatchOperation(Type targetType, string targetMethodName, MethodInfo stubMethod, HPatchLocation patchLocation, int patchPriority = -1) { PatchOperations.Add(new HPatchOperation(targetType, targetMethodName, stubMethod, patchLocation, patchPriority)); }
/// <summary> /// Creates a new patch operation using the supplied target method, stub method, and patch location. /// </summary> /// <param name="targetMethod">The target method (in Terraria) that will be patched.</param> /// <param name="stubMethod">The stub method (in your plugin) that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> protected void CreateHPatchOperation(MethodBase targetMethod, MethodInfo stubMethod, HPatchLocation patchLocation, int patchPriority = -1) { PatchOperations.Add(new HPatchOperation(targetMethod, stubMethod, patchLocation, patchPriority)); }
/// <summary> /// Creates a new patch operation using the supplied target type name, target method name, target method parameter count, target method last parameter type, stub method name, and patch location. /// </summary> /// <param name="targetTypeFullName">The full name of target type (in Terraria) that contains the target method, e.g. "Terraria.Main".</param> /// <param name="targetMethodName">The name of the target method.</param> /// <param name="targetMethodParamCount">The number of parameters in the target method. Can be used to help discern between method overloads.</param> /// <param name="targetMethodLastParamType">The type of the target method's last parameter. Can be used to help discern between method overloads.</param> /// <param name="stubMethodName">The name of the stub method IN THIS CLASS that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> protected void CreateHPatchOperation(string targetTypeFullName, string targetMethodName, int targetMethodParamCount, Type targetMethodLastParamType, string stubMethodName, HPatchLocation patchLocation, int patchPriority = -1) { Type targetType = null; if (!HHelpers.TryGetTerrariaType(targetTypeFullName, out targetType)) { throw new Exception("Invalid targetTypeFullName. Terraria does not contain a type named \"" + targetTypeFullName + "\"."); } MethodInfo targetMethod = targetType.GetRuntimeMethods().Where(x => x.Name == targetMethodName && x.GetParameters().Count() == targetMethodParamCount && x.GetParameters().Count() > 0 && x.GetParameters()[x.GetParameters().Count() - 1].ParameterType == targetMethodLastParamType).FirstOrDefault(); if (targetMethod == null) { throw new Exception("Invalid target method. The target type does not contain a method named \"" + stubMethodName + "\" with " + targetMethodParamCount + " parameters and a final parameter of type \"" + targetMethodLastParamType.FullName + "\""); } CreateHPatchOperation(targetMethod, stubMethodName, patchLocation, patchPriority); }
/// <summary> /// Creates a new patch operation using the supplied target type name, target method name, target method parameter count, stub method name, and patch location. /// </summary> /// <param name="targetTypeFullName">The full name of target type (in Terraria) that contains the target method, e.g. "Terraria.Main".</param> /// <param name="targetMethodName">The name of the target method.</param> /// <param name="targetMethodParamCount">The number of parameters in the target method. Can be used to help discern between method overloads.</param> /// <param name="stubMethodName">The name of the stub method IN THIS CLASS that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> protected void CreateHPatchOperation(string targetTypeFullName, string targetMethodName, int targetMethodParamCount, string stubMethodName, HPatchLocation patchLocation, int patchPriority = -1) { Type targetType = null; if (!HHelpers.TryGetTerrariaType(targetTypeFullName, out targetType)) { throw new Exception("Invalid targetTypeFullName. Terraria does not contain a type named \"" + targetTypeFullName + "\"."); } // First look in normal methods MethodBase targetMethod = targetType.GetRuntimeMethods().Where(x => x.Name == targetMethodName && x.GetParameters().Count() == targetMethodParamCount).FirstOrDefault(); if (targetMethod == null) // If nothing was found, look in constructors next { targetMethod = targetType.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static).Where(x => x.Name == targetMethodName && x.GetParameters().Count() == targetMethodParamCount).FirstOrDefault(); } if (targetMethod == null) { throw new Exception("Invalid target method. The target type does not contain a method or constructor named \"" + stubMethodName + "\" with " + targetMethodParamCount + " parameters."); } CreateHPatchOperation(targetMethod, stubMethodName, patchLocation, patchPriority); }
/// <summary> /// Creates a new patch operation using the supplied target type name, target method name, stub method name, and patch location. /// </summary> /// <param name="targetTypeFullName">The full name of target type (in Terraria) that contains the target method, e.g. "Terraria.Main".</param> /// <param name="targetMethodName">The name of the target method.</param> /// <param name="stubMethodName">The name of the stub method IN THIS CLASS that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> protected void CreateHPatchOperation(string targetTypeFullName, string targetMethodName, string stubMethodName, HPatchLocation patchLocation, int patchPriority = -1) { Type targetType = null; if (!HHelpers.TryGetTerrariaType(targetTypeFullName, out targetType)) { throw new Exception("Invalid targetTypeFullName. Terraria does not contain a type named \"" + targetTypeFullName + "\"."); } CreateHPatchOperation(targetType, targetMethodName, stubMethodName, patchLocation, patchPriority); }
/// <summary> /// Creates a new patch operation using the supplied target type and method name, stub method name, and patch location. /// </summary> /// <param name="targetType">The target type (in Terraria) that contains the target method.</param> /// <param name="targetMethodName">The name of the target method.</param> /// <param name="stubMethodName">The name of the stub method IN THIS CLASS that will be either prepended or appended to the target method. Must be a static method!</param> /// <param name="patchLocation">Whether the stub method will be prepended as a prefix or appended as a postfix to the target method.</param> /// <param name="patchPriority">The priority of this patch, as used by Harmony to order multiple patches on the same method. Patches with higher numbers go first. Set to -1 to use default priority (typically = 400).</param> protected void CreateHPatchOperation(Type targetType, string targetMethodName, string stubMethodName, HPatchLocation patchLocation, int patchPriority = -1) { MethodInfo stubMethod = this.GetType().GetRuntimeMethods().Where(x => x.Name == stubMethodName).FirstOrDefault(); if (stubMethod == null) { throw new Exception("Invalid stubMethodName. This type does not contain a method named \"" + stubMethodName + "\"."); } if (!stubMethod.Attributes.HasFlag(MethodAttributes.Static)) { throw new Exception("Invalid stub method. The stub method specified is not static."); } CreateHPatchOperation(targetType, targetMethodName, stubMethod, patchLocation, patchPriority); }