Browse documentation

Class Harmony

Namespace
HarmonyLib
Assembly
0Harmony.dll

The Harmony instance is the main entry to Harmony. After creating one with an unique identifier, it is used to patch and query the current application domain

public class Harmony
Inheritance
object
Harmony

Constructors

Harmony(string)

Creates a new Harmony instance

public Harmony(string id)

Parameters

id string

A unique identifier (you choose your own)

Fields

DEBUG

Set to true before instantiating Harmony to debug Harmony or use an environment variable to set HARMONY_DEBUG to '1' like this: cmd /C "set HARMONY_DEBUG=1 && game.exe"

public static bool DEBUG

Field Value

bool

Remarks

This is for full debugging. To debug only specific patches, use the HarmonyDebug attribute

Properties

Id

The unique identifier

public string Id { get; }

Property Value

string

Methods

ClearSwitch(string)

Clears a MonoMod switch value

public static void ClearSwitch(string name)

Parameters

name string

The switch name

CreateClassProcessor(Type)

Creates a patch class processor from a class

public PatchClassProcessor CreateClassProcessor(Type type)

Parameters

type Type

The class/type

Returns

PatchClassProcessor

A new PatchClassProcessor instance

CreateProcessor(MethodBase)

Creates a empty patch processor for an original method

public PatchProcessor CreateProcessor(MethodBase original)

Parameters

original MethodBase

The original method/constructor

Returns

PatchProcessor

A new PatchProcessor instance

CreateReversePatcher(MethodBase, HarmonyMethod)

Creates a reverse patcher for one of your stub methods

public ReversePatcher CreateReversePatcher(MethodBase original, HarmonyMethod standin)

Parameters

original MethodBase

The original method/constructor

standin HarmonyMethod

The stand-in stub method as HarmonyMethod

Returns

ReversePatcher

A new ReversePatcher instance

GetAllPatchedMethods()

Gets all patched original methods in the appdomain

public static IEnumerable<MethodBase> GetAllPatchedMethods()

Returns

IEnumerable<MethodBase>

An enumeration of patched original methods/constructors

GetMethodFromStackframe(StackFrame)

Tries to get the method from a stackframe including dynamic replacement methods

public static MethodBase GetMethodFromStackframe(StackFrame frame)

Parameters

frame StackFrame

The System.Diagnostics.StackFrame

Returns

MethodBase

For normal frames, frame.GetMethod() is returned. For frames containing patched methods, the replacement method is returned or null if no method can be found

GetOriginalMethod(MethodInfo)

Gets the original method from a given replacement method

public static MethodBase GetOriginalMethod(MethodInfo replacement)

Parameters

replacement MethodInfo

A replacement method (patched original method)

Returns

MethodBase

The original method/constructor or null if not found

GetOriginalMethodFromStackframe(StackFrame)

Gets the original method from the stackframe and uses original if method is a dynamic replacement

public static MethodBase GetOriginalMethodFromStackframe(StackFrame frame)

Parameters

frame StackFrame

The System.Diagnostics.StackFrame

Returns

MethodBase

The original method from that stackframe

GetPatchInfo(MethodBase)

Gets patch information for a given original method

public static Patches GetPatchInfo(MethodBase method)

Parameters

method MethodBase

The original method/constructor

Returns

Patches

The patch information as Patches

GetPatchedMethods()

Gets the methods this instance has patched

public IEnumerable<MethodBase> GetPatchedMethods()

Returns

IEnumerable<MethodBase>

An enumeration of original methods/constructors

HasAnyPatches(string)

Test for patches from a specific Harmony ID

public static bool HasAnyPatches(string harmonyID)

Parameters

harmonyID string

The Harmony ID

Returns

bool

True if patches for this ID exist

Patch(MethodBase, HarmonyMethod, HarmonyMethod, HarmonyMethod, HarmonyMethod)

Creates patches by manually specifying the methods

public MethodInfo Patch(MethodBase original, HarmonyMethod prefix = null, HarmonyMethod postfix = null, HarmonyMethod transpiler = null, HarmonyMethod finalizer = null)

Parameters

original MethodBase

The original method/constructor

prefix HarmonyMethod

An optional prefix method wrapped in a HarmonyMethod object

postfix HarmonyMethod

An optional postfix method wrapped in a HarmonyMethod object

transpiler HarmonyMethod

An optional transpiler method wrapped in a HarmonyMethod object

finalizer HarmonyMethod

An optional finalizer method wrapped in a HarmonyMethod object

Returns

MethodInfo

The replacement method that was created to patch the original method

PatchAll()

Searches the current assembly for Harmony annotations and uses them to create patches

public void PatchAll()

Remarks

This method can fail to use the correct assembly when being inlined. It calls StackTrace.GetFrame(1) which can point to the wrong method/assembly. If you are unsure or run into problems, use

PatchAll(Assembly.GetExecutingAssembly())

instead.

PatchAll(Assembly)

Searches an assembly for HarmonyPatch-annotated classes/structs and uses them to create patches

public void PatchAll(Assembly assembly)

Parameters

assembly Assembly

The assembly

PatchAllUncategorized()

Searches an assembly for HarmonyPatch-annotated classes/structs without category annotations and uses them to create patches

public void PatchAllUncategorized()

PatchAllUncategorized(Assembly)

Searches an assembly for HarmonyPatch-annotated classes/structs without category annotations and uses them to create patches

public void PatchAllUncategorized(Assembly assembly)

Parameters

assembly Assembly

The assembly

PatchCategory(Assembly, string)

Searches an assembly for HarmonyPatch-annotated classes/structs with a specific category and uses them to create patches

public void PatchCategory(Assembly assembly, string category)

Parameters

assembly Assembly

The assembly

category string

Name of patch category

PatchCategory(string)

Searches the current assembly for Harmony annotations with a specific category and uses them to create patches

public void PatchCategory(string category)

Parameters

category string

Name of patch category

ReversePatch(MethodBase, HarmonyMethod, MethodInfo)

Patches a foreign method onto a stub method of yours and optionally applies transpilers during the process

public static MethodInfo ReversePatch(MethodBase original, HarmonyMethod standin, MethodInfo transpiler = null)

Parameters

original MethodBase

The original method/constructor you want to duplicate

standin HarmonyMethod

Your stub method as HarmonyMethod that will become the original. Needs to have the correct signature (either original or whatever your transpilers generates)

transpiler MethodInfo

An optional transpiler as method that will be applied during the process

Returns

MethodInfo

The replacement method that was created to patch the stub method

SetSwitch(string, object)

Sets a MonoMod switch value (e.g., "DMDDebug", "DMDDumpTo")

public static void SetSwitch(string name, object value)

Parameters

name string

The switch name

value object

The value to set (bool, string, etc.)

TryGetSwitch(string, out object)

Tries to get a MonoMod switch value

public static bool TryGetSwitch(string name, out object value)

Parameters

name string

The switch name

value object

The switch value if found

Returns

bool

True if the switch was found, false otherwise

TryIsSwitchEnabled(string, out bool)

Tries to determine if a MonoMod switch is enabled

public static bool TryIsSwitchEnabled(string name, out bool isEnabled)

Parameters

name string

The switch name

isEnabled bool

True if the switch is enabled, false otherwise

Returns

bool

True if the switch enablement state could be determined, false otherwise

Unpatch(MethodBase, HarmonyPatchType, string)

Unpatches a method by patching it with zero patches. Fully unpatching is not supported. Be careful, unpatching is global

public void Unpatch(MethodBase original, HarmonyPatchType type, string harmonyID = "*")

Parameters

original MethodBase

The original method/constructor

type HarmonyPatchType

The HarmonyPatchType

harmonyID string

The optional Harmony ID to restrict unpatching to a specific Harmony instance

Unpatch(MethodBase, MethodInfo)

Unpatches a method by patching it with zero patches. Fully unpatching is not supported. Be careful, unpatching is global

public void Unpatch(MethodBase original, MethodInfo patch)

Parameters

original MethodBase

The original method/constructor

patch MethodInfo

The patch method as method to remove

UnpatchAll(string)

Unpatches methods by patching them with zero patches. Fully unpatching is not supported. Be careful, unpatching is global

public void UnpatchAll(string harmonyID = null)

Parameters

harmonyID string

The optional Harmony ID to restrict unpatching to a specific Harmony instance

Remarks

This method could be static if it wasn't for the fact that unpatching creates a new replacement method that contains your harmony ID

UnpatchCategory(Assembly, string)

Searches an assembly for HarmonyPatch-annotated classes/structs with a specific category annotation and uses them to unpatch existing patches. Fully unpatching is not supported. Be careful, unpatching is global

public void UnpatchCategory(Assembly assembly, string category)

Parameters

assembly Assembly

The assembly

category string

Name of patch category

UnpatchCategory(string)

Searches the current assembly for types with a specific category annotation and uses them to unpatch existing patches. Fully unpatching is not supported. Be careful, unpatching is global

public void UnpatchCategory(string category)

Parameters

category string

Name of patch category

VersionInfo(out Version)

Gets Harmony version for all active Harmony instances

public static Dictionary<string, Version> VersionInfo(out Version currentVersion)

Parameters

currentVersion Version

[out] The current Harmony version

Returns

Dictionary<string, Version>

A dictionary containing assembly versions keyed by Harmony IDs

Harmony 3 preview

For the stable release, read the 2.x documentation.