Harmony Introduction
Harmony is the most essential capability for ADOFAI Mods: intercepting and modifying game methods without touching the game source code.
Why Harmony?
The game is a compiled Unity program — we can't directly edit its source. Harmony rewrites methods at runtime, letting our code execute before, after, or even inside game methods.
Four Patch Types
| Type | Timing | Purpose |
|---|---|---|
| Prefix | Before method execution | Modify parameters, early return (intercept) |
| Postfix | After method execution | Read / modify return value |
| Finalizer | After method ends (regardless of exceptions) | Exception handling |
| Transpiler | IL level | Deep modification of method internals |
This series covers each in 11 chapters:
| Chapter | Content |
|---|---|
| Prefix Patch | Intercept methods, modify parameters |
| Postfix Patch | Read / modify return values |
| Finalizer Patch | Exception handling |
| Magic Parameters | __instance / __result / __state etc. |
| HarmonyPatch Details | Target syntax and priorities |
| Patch Lifecycle | How patches are applied and removed |
| Transpiler Introduction | What IL is, how to write patches |
| Transpiler Practice | CodeMatcher advanced usage |
| Manual Patching | Dynamic patching with code |
| Reverse Patch | Call game methods in reverse |
What a Complete Patch Looks Like
csharp
using HarmonyLib;
namespace MyFirstMod
{
[HarmonyPatch(typeof(SomeGameClass), nameof(SomeGameClass.SomeMethod))]
public static class SomeMethod_Prefix
{
public static bool Prefix()
{
// Returning false skips the original method (interception)
return false;
}
}
}Three essential elements:
[HarmonyPatch]— declares the patch target (class + method)- Patch method name —
Prefix/Postfix/Finalizer/Transpiler; Harmony identifies by name - Patch class — must be a
static class;PatchAllscans automatically
Finding the Target Method
Finding the target method is one of the hardest parts of Mod development. We recommend using dnSpy or ILSpy to decompile the game assembly. See the full guide in Finding Target Methods.
What You Learned
- Why Harmony exists
- The four patch types and their timing
- The three essential elements of a patch
Next Step
Learn the most common Prefix patch → Prefix Patch