Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patching Code (Minor Fixes): Add notice about patching & hooking terms and Fix unhooking example #115

Merged
merged 1 commit into from
Apr 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions docs/dev/fundamentals/patching-code.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**.

Expand All @@ -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.
Expand All @@ -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
Expand Down