Back to Harmony

Class Harmony

docs/api/HarmonyLib.Harmony.html

2.4.2.013.4 KB
Original Source

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

System.Object

Harmony

Namespace : HarmonyLib
Assembly : 0Harmony.dll
Syntax
public class Harmony

Constructors

Harmony(String)

Creates a new Harmony instance

Declaration
public Harmony(string id)
Parameters
TypeNameDescription
System.Stringid

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"

Declaration
public static bool DEBUG
Field Value
TypeDescription
System.Boolean
Remarks

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

Properties

Id

The unique identifier

Declaration
public string Id { get; }
Property Value
TypeDescription
System.String

Methods

CreateClassProcessor(Type)

Creates a patch class processor from a class

Declaration
public PatchClassProcessor CreateClassProcessor(Type type)
Parameters
TypeNameDescription
System.Typetype

The class/type

|

Returns
TypeDescription
PatchClassProcessor

A new PatchClassProcessor instance

|

CreateProcessor(MethodBase)

Creates a empty patch processor for an original method

Declaration
public PatchProcessor CreateProcessor(MethodBase original)
Parameters
TypeNameDescription
System.Reflection.MethodBaseoriginal

The original method/constructor

|

Returns
TypeDescription
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
TypeNameDescription
System.Reflection.MethodBaseoriginal

The original method/constructor

| | HarmonyMethod | standin |

The stand-in stub method as HarmonyMethod

|

Returns
TypeDescription
ReversePatcher

A new ReversePatcher instance

|

GetAllPatchedMethods()

Gets all patched original methods in the appdomain

Declaration
public static IEnumerable<MethodBase> GetAllPatchedMethods()
Returns
TypeDescription
System.Collections.Generic.IEnumerable\

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
TypeNameDescription
System.Diagnostics.StackFrameframe

The System.Diagnostics.StackFrame

|

Returns
TypeDescription
System.Reflection.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

Declaration
public static MethodBase GetOriginalMethod(MethodInfo replacement)
Parameters
TypeNameDescription
System.Reflection.MethodInforeplacement

A replacement method (patched original method)

|

Returns
TypeDescription
System.Reflection.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

Declaration
public static MethodBase GetOriginalMethodFromStackframe(StackFrame frame)
Parameters
TypeNameDescription
System.Diagnostics.StackFrameframe

The System.Diagnostics.StackFrame

|

Returns
TypeDescription
System.Reflection.MethodBase

The original method from that stackframe

|

GetPatchedMethods()

Gets the methods this instance has patched

Declaration
public IEnumerable<MethodBase> GetPatchedMethods()
Returns
TypeDescription
System.Collections.Generic.IEnumerable\

An enumeration of original methods/constructors

|

GetPatchInfo(MethodBase)

Gets patch information for a given original method

Declaration
public static Patches GetPatchInfo(MethodBase method)
Parameters
TypeNameDescription
System.Reflection.MethodBasemethod

The original method/constructor

|

Returns
TypeDescription
Patches

The patch information as Patches

|

HasAnyPatches(String)

Test for patches from a specific Harmony ID

Declaration
public static bool HasAnyPatches(string harmonyID)
Parameters
TypeNameDescription
System.StringharmonyID

The Harmony ID

|

Returns
TypeDescription
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
TypeNameDescription
System.Reflection.MethodBaseoriginal

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
TypeDescription
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 HarmonyPatch-annotated classes/structs and uses them to create patches

Declaration
public void PatchAll(Assembly assembly)
Parameters
TypeNameDescription
System.Reflection.Assemblyassembly

The assembly

|

PatchAllUncategorized()

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

Declaration
public void PatchAllUncategorized()

PatchAllUncategorized(Assembly)

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

Declaration
public void PatchAllUncategorized(Assembly assembly)
Parameters
TypeNameDescription
System.Reflection.Assemblyassembly

The assembly

|

PatchCategory(Assembly, String)

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

Declaration
public void PatchCategory(Assembly assembly, string category)
Parameters
TypeNameDescription
System.Reflection.Assemblyassembly

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
TypeNameDescription
System.Stringcategory

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
TypeNameDescription
System.Reflection.MethodBaseoriginal

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
TypeDescription
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
TypeNameDescription
System.Reflection.MethodBaseoriginal

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
TypeNameDescription
System.Reflection.MethodBaseoriginal

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
TypeNameDescription
System.StringharmonyID

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

Declaration
public void UnpatchCategory(Assembly assembly, string category)
Parameters
TypeNameDescription
System.Reflection.Assemblyassembly

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
TypeNameDescription
System.Stringcategory

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
TypeNameDescription
System.VersioncurrentVersion

[out] The current Harmony version

|

Returns
TypeDescription
System.Collections.Generic.Dictionary\

A dictionary containing assembly versions keyed by Harmony IDs

|