Skip to content

Patch attributes

Geoffrey Horsington edited this page Feb 2, 2022 · 2 revisions

The page outlines the new added HarmonyX patch attributes. The attributes can be generally applied to methods and classes like the normal HarmonyPatch attribute.

HarmonyWrapSafe

Example:

[HarmonyPrefix]
[HarmonyWrapSafe]
[HarmonyPatch(typeof(TargetClass), "TargetMethod")]
static void ExamplePrefix();

The attribute will automatically wrap the patch into a generic try/catch block that catches all exceptions. Any exceptions thrown by the patch are logged by Harmony as errors but are otherwise skipped. Other patches and original code are unaffected.

HarmonyEmitIL

Example:

[HarmonyEmitIL("./dumps")]
[HarmonyPrefix]
[HarmonyPatch(typeof(TargetClass), "TargetMethod")]
static void ExamplePrefix();

When the attribute is specified, Harmony will emit a .dll assembly containing the patched version of the target method into the specified folder. The patched method is the code that the original method was replaced with and as such contains all prefixes, postfixes, transpilers, finalizers and manipulators applied to the method.

Each generated DLL is named HarmonyDump.<name of the method>.<random id>.dll.

NOTE:
The emitted patch method does not contain any detours applied by MonoMod.RuntimeDetour. Only Harmony patches and any ILHooks applied prior to Harmony are visible in the emitted DLL.