Skip to content

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

TypeTimingPurpose
PrefixBefore method executionModify parameters, early return (intercept)
PostfixAfter method executionRead / modify return value
FinalizerAfter method ends (regardless of exceptions)Exception handling
TranspilerIL levelDeep modification of method internals

This series covers each in 11 chapters:

ChapterContent
Prefix PatchIntercept methods, modify parameters
Postfix PatchRead / modify return values
Finalizer PatchException handling
Magic Parameters__instance / __result / __state etc.
HarmonyPatch DetailsTarget syntax and priorities
Patch LifecycleHow patches are applied and removed
Transpiler IntroductionWhat IL is, how to write patches
Transpiler PracticeCodeMatcher advanced usage
Manual PatchingDynamic patching with code
Reverse PatchCall 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:

  1. [HarmonyPatch] — declares the patch target (class + method)
  2. Patch method namePrefix / Postfix / Finalizer / Transpiler; Harmony identifies by name
  3. Patch class — must be a static class; PatchAll scans 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

An organization that researches and expands the functions of ADOFAI