Show / Hide Table of Contents

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

| Improve this Doc View Source

Harmony(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 Source

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
Type Description
System.Boolean
Remarks

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

Properties

| Improve this Doc View Source

Id

The unique identifier

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

Methods

| Improve this Doc View Source

CreateClassProcessor(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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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, frame.GetMethod() is returned. For frames containing patched methods, the replacement method is returned or null if no method can be found

| Improve this Doc View Source

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 null if not found

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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.

| Improve this Doc View Source

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

| Improve this Doc View Source

PatchAllUncategorized()

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

Declaration
public void PatchAllUncategorized()
| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

| Improve this Doc View Source

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

  • Improve this Doc
  • View Source
In This Article
Back to top Generated by DocFX