CorePatcher short helpers
A prefix runs your method before the game method, a postfix runs it after, and a finalizer runs it when the game method throws an error. These helpers set one of those up in one line. The owner is your mod id (see Owner).
CorePatcher one-line prefix, postfix and finalizer helpers
Call one of these with the game method, your handler’s type and name, and your mod id as owner.
public static class CorePatcher
{
public static bool Prefix(Type target, string method, Type handlerType, string handlerName, string owner);
public static bool Prefix(Type target, string method, Type[] args, Type handlerType, string handlerName, string owner);
public static bool Postfix(Type target, string method, Type handlerType, string handlerName, string owner);
public static bool Postfix(Type target, string method, Type[] args, Type handlerType, string handlerName, string owner);
public static bool Finalizer(Type target, string method, Type handlerType, string handlerName, string owner);
public static bool Finalizer(Type target, string method, Type[] args, Type handlerType, string handlerName, string owner);
}
| Method | Returns | What it does |
|---|---|---|
Prefix(target, method, handlerType, handlerName, owner) | bool | Runs your method before a game method. |
Prefix(target, method, args, handlerType, handlerName, owner) | bool | The same, for a game method with several versions. |
Postfix(target, method, handlerType, handlerName, owner) | bool | Runs your method after a game method. |
Postfix(target, method, args, handlerType, handlerName, owner) | bool | The same, for a game method with several versions. |
Finalizer(target, method, handlerType, handlerName, owner) | bool | Runs your method when a game method throws an error. |
Finalizer(target, method, args, handlerType, handlerName, owner) | bool | The same, for a game method with several versions. |
Things to know
- The result is
truewhen the change was made andfalsewhen it was not. It isfalsewhen it failed, and the reason goes to<owner>.log, or when the owner has been turned off. (Each helper callsCorePatcher.Patchwith only the one handler it names filled in and itscoreargument set tofalse, and the result is theboolthatPatchreturns.) - The handler can keep its Harmony settings.
[HarmonyPriority],[HarmonyBefore]and[HarmonyAfter]on your method still apply, the same as withPatch. See Harmony.
Limits
handlerNameis the name of a static method onhandlerType, and it may be private. If there is no such method, you get aMissingMethodException.- Use an owner that is not reserved. A reserved owner throws an
ArgumentException. The rules for the owner are the rules ofCorePatcher.Patch, described on the framework core page. - Without
args, the name must match exactly one method on the target. The method may be public or private, and an instance method or a static one. A name that is missing, or that has several versions, returnsfalseand writes one line to the owner’s log. For a method with several versions, use the version withType[] args.
Example
The long way and the short way to add a postfix, side by side:
MethodInfo postfix = typeof(Helpers).GetMethod("AfterDay", BindingFlags.Static | BindingFlags.NonPublic);
bool longWay = CorePatcher.Patch(typeof(TimeOfDayAzure), "IncreaseDayCounter", null, null, postfix, null, false, "example.helpers");
bool shortWay = CorePatcher.Postfix(typeof(TimeOfDayAzure), "IncreaseDayCounter", typeof(Helpers), "AfterDay", "example.helpers");