Class Harmony
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
Inheritance
Namespace: HarmonyLib
Assembly: 0Harmony.dll
Syntax
public class Harmony
Constructors
| Improve this Doc View SourceHarmony(String)
Creates a new Harmony instance
Declaration
public Harmony(string id)
Parameters
Type | Name | Description |
---|---|---|
System.String | id | A unique identifier (you choose your own) |
Fields
| Improve this Doc View SourceDEBUG
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"
Declaration
public static bool DEBUG
Field Value
Type | Description |
---|---|
System.Boolean |
Remarks
This is for full debugging. To debug only specific patches, use the HarmonyDebug attribute
Properties
| Improve this Doc View SourceId
The unique identifier
Declaration
public string Id { get; }
Property Value
Type | Description |
---|---|
System.String |
Methods
| Improve this Doc View SourceCreateClassProcessor(Type)
Creates a patch class processor from an annotated class
Declaration
public PatchClassProcessor CreateClassProcessor(Type type)
Parameters
Type | Name | Description |
---|---|---|
System.Type | type | The class/type |
Returns
Type | Description |
---|---|
PatchClassProcessor | A new PatchClassProcessor instance |
CreateProcessor(MethodBase)
Creates a empty patch processor for an original method
Declaration
public PatchProcessor CreateProcessor(MethodBase original)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | original | The original method/constructor |
Returns
Type | Description |
---|---|
PatchProcessor | A new PatchProcessor instance |
CreateReversePatcher(MethodBase, HarmonyMethod)
Creates a reverse patcher for one of your stub methods
Declaration
public ReversePatcher CreateReversePatcher(MethodBase original, HarmonyMethod standin)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | original | The original method/constructor |
HarmonyMethod | standin | The stand-in stub method as HarmonyMethod |
Returns
Type | Description |
---|---|
ReversePatcher | A new ReversePatcher instance |
GetAllPatchedMethods()
Gets all patched original methods in the appdomain
Declaration
public static IEnumerable<MethodBase> GetAllPatchedMethods()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Reflection.MethodBase> | An enumeration of patched original methods/constructors |
GetMethodFromStackframe(StackFrame)
Tries to get the method from a stackframe including dynamic replacement methods
Declaration
public static MethodBase GetMethodFromStackframe(StackFrame frame)
Parameters
Type | Name | Description |
---|---|---|
System.Diagnostics.StackFrame | frame | The System.Diagnostics.StackFrame |
Returns
Type | Description |
---|---|
System.Reflection.MethodBase | For normal frames, |
GetOriginalMethod(MethodInfo)
Gets the original method from a given replacement method
Declaration
public static MethodBase GetOriginalMethod(MethodInfo replacement)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodInfo | replacement | A replacement method (patched original method) |
Returns
Type | Description |
---|---|
System.Reflection.MethodBase | The original method/constructor or |
GetOriginalMethodFromStackframe(StackFrame)
Gets the original method from the stackframe and uses original if method is a dynamic replacement
Declaration
public static MethodBase GetOriginalMethodFromStackframe(StackFrame frame)
Parameters
Type | Name | Description |
---|---|---|
System.Diagnostics.StackFrame | frame | The System.Diagnostics.StackFrame |
Returns
Type | Description |
---|---|
System.Reflection.MethodBase | The original method from that stackframe |
GetPatchedMethods()
Gets the methods this instance has patched
Declaration
public IEnumerable<MethodBase> GetPatchedMethods()
Returns
Type | Description |
---|---|
System.Collections.Generic.IEnumerable<System.Reflection.MethodBase> | An enumeration of original methods/constructors |
GetPatchInfo(MethodBase)
Gets patch information for a given original method
Declaration
public static Patches GetPatchInfo(MethodBase method)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | method | The original method/constructor |
Returns
Type | Description |
---|---|
Patches | The patch information as Patches |
HasAnyPatches(String)
Test for patches from a specific Harmony ID
Declaration
public static bool HasAnyPatches(string harmonyID)
Parameters
Type | Name | Description |
---|---|---|
System.String | harmonyID | The Harmony ID |
Returns
Type | Description |
---|---|
System.Boolean | True if patches for this ID exist |
Patch(MethodBase, HarmonyMethod, HarmonyMethod, HarmonyMethod, HarmonyMethod)
Creates patches by manually specifying the methods
Declaration
public MethodInfo Patch(MethodBase original, HarmonyMethod prefix = null, HarmonyMethod postfix = null, HarmonyMethod transpiler = null, HarmonyMethod finalizer = null)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | original | The original method/constructor |
HarmonyMethod | prefix | An optional prefix method wrapped in a HarmonyMethod object |
HarmonyMethod | postfix | An optional postfix method wrapped in a HarmonyMethod object |
HarmonyMethod | transpiler | An optional transpiler method wrapped in a HarmonyMethod object |
HarmonyMethod | finalizer | An optional finalizer method wrapped in a HarmonyMethod object |
Returns
Type | Description |
---|---|
System.Reflection.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
Declaration
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 Harmony annotations and uses them to create patches
Declaration
public void PatchAll(Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | The assembly |
PatchAllUncategorized()
Searches an assembly for Harmony-annotated classes without category annotations and uses them to create patches
Declaration
public void PatchAllUncategorized()
PatchAllUncategorized(Assembly)
Searches an assembly for Harmony-annotated classes without category annotations and uses them to create patches
Declaration
public void PatchAllUncategorized(Assembly assembly)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | The assembly |
PatchCategory(Assembly, String)
Searches an assembly for Harmony annotations with a specific category and uses them to create patches
Declaration
public void PatchCategory(Assembly assembly, string category)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | The assembly |
System.String | category | Name of patch category |
PatchCategory(String)
Searches the current assembly for Harmony annotations with a specific category and uses them to create patches
Declaration
public void PatchCategory(string category)
Parameters
Type | Name | Description |
---|---|---|
System.String | category | 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
Declaration
public static MethodInfo ReversePatch(MethodBase original, HarmonyMethod standin, MethodInfo transpiler = null)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | original | The original method/constructor you want to duplicate |
HarmonyMethod | standin | Your stub method as HarmonyMethod that will become the original. Needs to have the correct signature (either original or whatever your transpilers generates) |
System.Reflection.MethodInfo | transpiler | An optional transpiler as method that will be applied during the process |
Returns
Type | Description |
---|---|
System.Reflection.MethodInfo | The replacement method that was created to patch the stub method |
Unpatch(MethodBase, HarmonyPatchType, String)
Unpatches a method by patching it with zero patches. Fully unpatching is not supported. Be careful, unpatching is global
Declaration
public void Unpatch(MethodBase original, HarmonyPatchType type, string harmonyID = "*")
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | original | The original method/constructor |
HarmonyPatchType | type | The HarmonyPatchType |
System.String | harmonyID | 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
Declaration
public void Unpatch(MethodBase original, MethodInfo patch)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.MethodBase | original | The original method/constructor |
System.Reflection.MethodInfo | patch | 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
Declaration
public void UnpatchAll(string harmonyID = null)
Parameters
Type | Name | Description |
---|---|---|
System.String | harmonyID | 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 types with a specific category annotation and uses them to unpatch existing patches. Fully unpatching is not supported. Be careful, unpatching is global
Declaration
public void UnpatchCategory(Assembly assembly, string category)
Parameters
Type | Name | Description |
---|---|---|
System.Reflection.Assembly | assembly | The assembly |
System.String | category | 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
Declaration
public void UnpatchCategory(string category)
Parameters
Type | Name | Description |
---|---|---|
System.String | category | Name of patch category |
VersionInfo(out Version)
Gets Harmony version for all active Harmony instances
Declaration
public static Dictionary<string, Version> VersionInfo(out Version currentVersion)
Parameters
Type | Name | Description |
---|---|---|
System.Version | currentVersion | [out] The current Harmony version |
Returns
Type | Description |
---|---|
System.Collections.Generic.Dictionary<System.String, System.Version> | A dictionary containing assembly versions keyed by Harmony IDs |