From 0255bd11275b3779039cef32d57a0928317e909a Mon Sep 17 00:00:00 2001 From: Meigyoku Thmn Date: Sun, 16 Oct 2022 10:58:48 +0700 Subject: [PATCH] Add an example for patch factory methods Somehow there isn't any example for this so here is my proposed one. --- Harmony/Documentation/articles/patching.md | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/Harmony/Documentation/articles/patching.md b/Harmony/Documentation/articles/patching.md index af81c930..b77385ed 100644 --- a/Harmony/Documentation/articles/patching.md +++ b/Harmony/Documentation/articles/patching.md @@ -63,6 +63,25 @@ If you prefer manual patching, you can use any method name or class structure yo ![note] Patch methods _must_ be static but you can define them public or private. They cannot be dynamic methods but you can write static patch factory methods that return dynamic methods. +```csharp +[HarmonyPatch(...)] +class Patch +{ + // the return type of factory methods can be either MethodInfo or DynamicMethod + [HarmonyPrefix] + static MethodInfo PrefixFactory(MethodBase originalMethod) + { + // return an instance of MethodInfo or an instance of DynamicMethod + } + + [HarmonyPostfix] + static MethodInfo PostfixFactory(MethodBase originalMethod) + { + // return an instance of MethodInfo or an instance of DynamicMethod + } +} +``` + ### Method names Manual patching knows four main patch types: **Prefix**, **Postfix**, **Transpiler** and **Finalizer**. If you use attributes for patching, you can also use the helper methods: **Prepare**, **TargetMethod**, **TargetMethods** and **Cleanup** as explained below.