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
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
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
assemblyAssemblyThe 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
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)
Searches the current assembly for Harmony annotations with a specific category and uses them to create patches
public void PatchCategory(string category)
Parameters
categorystringName 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
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)
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
categorystringName of patch category
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