From 7d16e96c5f880c0df3ad82d8a54990a96db963ab Mon Sep 17 00:00:00 2001 From: Hamunii Date: Wed, 10 Apr 2024 15:12:08 +0300 Subject: [PATCH] Add notice about patching & hooking terms and fix unhooking example --- docs/dev/fundamentals/patching-code.md | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/docs/dev/fundamentals/patching-code.md b/docs/dev/fundamentals/patching-code.md index b04275acd43..f4809c568ab 100644 --- a/docs/dev/fundamentals/patching-code.md +++ b/docs/dev/fundamentals/patching-code.md @@ -14,6 +14,10 @@ MonoMod and Harmony are incredibly powerful tools, and with them we can do nearl However, keep in mind that you aren't restricted to using only one of these tools, as the version of Harmony we are using is [HarmonyX](https://github.com/BepInEx/HarmonyX), which is Harmony built on top of **MonoMod** so there will not be any conflicts. +:::info NOTE +In this and the following articles, we use *"patching"* and *"hooking"* to refer to the act of running our code before and after the original method, or directly modifying the original method. +::: + ## MonoMod MonoMod is arguably easier to use than Harmony in most cases thanks to its `MMHOOK` assemblies generated by **MonoMod.RuntimeDetour.HookGen**. @@ -23,8 +27,6 @@ To use these events, we can for example do the following: ```cs // Subscribe to event, applying our patch On.GameNetcodeStuff.PlayerControllerB.Update += MyPatch; -// Unsubscribe from event, undoing the patch -On.GameNetcodeStuff.PlayerControllerB.Update -= MyPatch; ``` ::: info `PlayerControllerB` is the class/script that controls all player characters in Lethal Company. It is defined under the `GameNetcodeStuff` namespace. @@ -37,6 +39,15 @@ The `On` namespace contains normal **Hooks** which allow running our code before This will add our method `MyPatch` as an event handler for the `MMHOOK`'s `Update` event of `PlayerControllerB`, which runs when the original `Update` method of `PlayerControllerB` runs. +If we want to undo our patch, we can simply do this: +```cs +// Unsubscribe from event, undoing the patch +On.GameNetcodeStuff.PlayerControllerB.Update -= MyPatch; +``` +Do note however that hooking and unhooking isn't cheap performance wise, and therefore it isn't a great idea to e.g. unhook your patch method inside the patch method, as the game will have to wait for unhooking to finish. + +In such a case, you could unhook on a thread other than the game's main thread, or avoid unhooking altogether by having a `boolean` to determine whether or not your custom logic should run. + ::: warning IMPORTANT To make use of `MMHOOK` assemblies, you will need to reference them in your project. This can be simply done by e.g. adding the following to your `csproj` file: ```xml