-
Notifications
You must be signed in to change notification settings - Fork 43
Multitargeted patches
Geoffrey Horsington edited this page May 28, 2021
·
2 revisions
In some cases you may want to apply the same patch onto multiple methods easily. This is useful for patching similar getters or for quick logging. By default, Harmony does not allow you to mix multiple patch targets via multiple HarmonyPatch
attributes -- instead they are all merged into one.
HarmonyX lifts this limitation for method attributes to allow more concise patching syntax with type-level patching.
The functionality relies on the notion of complete HarmonyPatch
attributes.
HarmonyPatch
attribute is complete, if it defines both target type and target method.
Thus, to use multitargeting:
- Define one or more complete
HarmonyPatch
attributes on a method. - Define on of patch type attributes:
HarmonyPrefix
,HarmonyPostfix
,HarmonyTranspiler
orHarmonyFinalizer
. Only one type attribute is allowed. - Any incomplete
HarmonyPatch
attribute will get merged into the last complete attribute. - Patch parameters must be compatible with all targets.
// Example target classes
class TargetType1 {
void SomeMethod1();
}
class TargetType2 {
void SomeMethod2();
}
// Apply prefix onto both methods at the same time
[HarmonyPrefix]
[HarmonyPatch(typeof(TargetType1), "SomeMethod1")]
[HarmonyPatch(typeof(TargetType2), "SomeMethod2")]
static void SomeMethodPrefix();
- Basic usage
-
HarmonyX extensions
1.1. Patching and unpatching
1.2. Prefixes are flowthrough
1.3. Targeting multiple methods with one patch
1.4. Patching enumerators
1.5. Transpiler helpers
1.6. ILManipulators
1.7. Extended patch targets
1.8. New patch attributes -
Extending HarmonyX
2.1. Custom patcher backends -
Misc
4.1. Patch parameters - Implementation differences from Harmony