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
-
objectHarmony
Constructors
Harmony(string)
Creates a new Harmony instance
public Harmony(string id)
Parameters
idstringA 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
ValidateStack
Whether Harmony rejects a method body in which the paths that reach an instruction have different stack depths, or an instruction pops more than the stack holds
public static bool ValidateStack { get; set; }
Property Value
- bool
True by default
Remarks
ECMA-335 forbids both, but some runtimes accept such IL. Mono accepts a depth difference at a leave, and other runtimes accept more.
Set this to false to leave the decision to the runtime. Harmony then sizes the stack for the deepest path.
The environment variable HARMONY_VALIDATE_STACK overrides this property when it is set to 1, 0, true or false.
Only Harmony's own method body writer checks the stack. It runs on .NET Core and .NET 5 or later. On .NET Framework and Mono the runtime checks the IL.
A jump to a label that no instruction carries is always rejected, because that jump has no target.
Methods
ClearSwitch(string)
Clears a MonoMod switch value
public static void ClearSwitch(string name)
Parameters
namestringThe switch name
CreateClassProcessor(Type)
Creates a patch class processor from a class
public PatchClassProcessor CreateClassProcessor(Type type)
Parameters
typeTypeThe 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
originalMethodBaseThe 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
originalMethodBaseThe original method/constructor
standinHarmonyMethodThe 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
frameStackFrameThe System.Diagnostics.StackFrame
Returns
- MethodBase
For normal frames,
frame.GetMethod()is returned. For frames containing patched methods, the replacement method is returned ornullif no method can be found
GetOriginalMethod(MethodInfo)
Gets the original method from a given replacement method
public static MethodBase GetOriginalMethod(MethodInfo replacement)
Parameters
replacementMethodInfoA replacement method (patched original method)
Returns
- MethodBase
The original method/constructor or
nullif 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
frameStackFrameThe 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
methodMethodBaseThe original method/constructor
Returns
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
harmonyIDstringThe 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
originalMethodBaseThe original method/constructor
prefixHarmonyMethodAn optional prefix method wrapped in a HarmonyMethod object
postfixHarmonyMethodAn optional postfix method wrapped in a HarmonyMethod object
transpilerHarmonyMethodAn optional transpiler method wrapped in a HarmonyMethod object
finalizerHarmonyMethodAn optional finalizer method wrapped in a HarmonyMethod object
Returns
- MethodInfo
The replacement method that was created to patch the original method. An open generic definition installed by a registered MethodBodyPatching installer has no callable copy: the definition itself is returned, or null for a constructor.
PatchAll()
Infers the calling assembly and applies its annotated patches
[Obsolete("Automatic assembly detection can fail after inlining or tail calls. Use PatchAll(Assembly) with an explicit assembly.")]
public void PatchAll()
Remarks
Caller inlining or tail calls can select the wrong assembly. Use PatchAll(Assembly) with an explicit assembly.
PatchAll(Assembly)
Searches an assembly for HarmonyPatch-annotated classes/structs and uses them to create patches
public void PatchAll(Assembly assembly)
Parameters
assemblyAssemblyThe assembly
PatchAllUncategorized()
Infers the calling assembly and applies its annotated patches without categories
[Obsolete("Automatic assembly detection can fail after inlining or tail calls. Use PatchAllUncategorized(Assembly) with an explicit assembly.")]
public void PatchAllUncategorized()
Remarks
Caller inlining or tail calls can select the wrong assembly. Use PatchAllUncategorized(Assembly) with an explicit assembly.
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
assemblyAssemblyThe 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
assemblyAssemblyThe assembly
categorystringName of patch category
PatchCategory(string)
Infers the calling assembly and applies its annotated patches in a category
[Obsolete("Automatic assembly detection can fail after inlining or tail calls. Use PatchCategory(Assembly, string) with an explicit assembly.")]
public void PatchCategory(string category)
Parameters
categorystringName of patch category
Remarks
Caller inlining or tail calls can select the wrong assembly. Use PatchCategory(Assembly, string) with an explicit assembly.
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
originalMethodBaseThe original method/constructor you want to duplicate
standinHarmonyMethodYour stub method as HarmonyMethod that will become the original. Needs to have the correct signature (either original or whatever your transpilers generates)
transpilerMethodInfoAn 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
namestringThe switch name
valueobjectThe 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
namestringThe switch name
valueobjectThe 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
namestringThe switch name
isEnabledboolTrue 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
originalMethodBaseThe original method/constructor
typeHarmonyPatchTypeThe HarmonyPatchType
harmonyIDstringThe 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
originalMethodBaseThe original method/constructor
patchMethodInfoThe 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
harmonyIDstringThe 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
assemblyAssemblyThe assembly
categorystringName of patch category
UnpatchCategory(string)
Infers the calling assembly and unpatches its annotated types in a category. Fully unpatching is not supported. Be careful, unpatching is global
[Obsolete("Automatic assembly detection can fail after inlining or tail calls. Use UnpatchCategory(Assembly, string) with an explicit assembly.")]
public void UnpatchCategory(string category)
Parameters
categorystringName of patch category
Remarks
Caller inlining or tail calls can select the wrong assembly. Use UnpatchCategory(Assembly, string) with an explicit assembly.
VersionInfo(out Version)
Gets Harmony version for all active Harmony instances
public static Dictionary<string, Version> VersionInfo(out Version currentVersion)
Parameters
currentVersionVersion[out] The current Harmony version
Returns
- Dictionary<string, Version>
A dictionary containing assembly versions keyed by Harmony IDs