ReversePatchMethod
io.github.ykysnk.utils.Editor.Patches · public abstract class ReversePatchMethod<T> : IReversePatchMethod, ILogger where T : ReversePatchMethod<T>, new()
One reverse patch: a stub method that receives a callable copy of another method's body, optionally run through a transpiler. Instances are the classes nested inside a Patch group.
T is the concrete reverse patch class. It satisfies IReversePatchMethod and ILogger, and the new() constraint lets the framework and the lazy Instance create it. TargetMethod and the Target* properties are virtual, Prepare and Cleanup are virtual hooks with default implementations, and no member is abstract.
private sealed class ReadSettings : ReversePatchMethod<ReadSettings>
{
public override MethodInfo? TargetMethod { get; } = AccessTools.Method(typeof(SomeEditor), "ReadSettings");
// Receives the copied body; the signature must match TargetMethod.
private static void Reverse() { }
}Instance
public static T Instance { get; }Lazily created instance, for callers outside the patching pass; the framework instantiates the reverse patch itself.
ThisType
public static Type ThisType { get; }The reverse patch type; it is the type whose methods are reflected and it names the log prefix.
ReverseMethod
public virtual string ReverseMethod { get; }Name of the nested stub method that receives the copied body, "Reverse" by default; override it to use a different name.
TranspilerMethod
public virtual string TranspilerMethod { get; }Name of the nested method used as the transpiler, "Transpiler" by default; override it to use a different name.
QualifiedName
public virtual string QualifiedName { get; }Unique name of the reverse patch, used for logging and error reporting. It defaults to the full type name, or the short name when there is none.
DisplayName
public virtual string DisplayName { get; }Short name of the reverse patch, used as the prefix of every log entry it writes. It defaults to the type name.
Enabled
public virtual bool Enabled { get; }Whether the reverse patch is applied. It is true by default, and a disabled one is skipped without touching Harmony.
TargetMethod
public virtual MethodInfo? TargetMethod { get; }The method whose body is copied. The base value is null, so every concrete reverse patch class overrides it; when it stays null nothing is applied and a warning is logged.
TargetReverse
public virtual MethodInfo? TargetReverse { get; }The stub method that receives the copied body, i.e. the nested method named ReverseMethod, or null when it does not exist. Its signature must match TargetMethod.
TargetTranspiler
public virtual MethodInfo? TargetTranspiler { get; }The transpiler applied to the copied body: the nested method named TranspilerMethod, or null to fall back to a transpiler declared as a local function of the stub. That fallback is a generated method named after the stub that returns IEnumerable<CodeInstruction>.
Priority
public virtual int Priority { get; }Harmony priority given to this reverse patch. The default -1 means no priority is set explicitly.
Before
public virtual string[]? Before { get; }Harmony ids this reverse patch must run before, or null for no ordering constraint.
After
public virtual string[]? After { get; }Harmony ids this reverse patch must run after, or null for no ordering constraint.
Debug
public virtual bool? Debug { get; }Harmony debug flag of the reverse patch, or null to leave it unset.
Prepare
protected virtual bool Prepare(MethodInfo? original, Harmony harmony)Runs just before the reverse patch is applied, for state the stub needs while it runs. Returns true by default; false skips the reverse patch, in which case nothing is applied or cleaned up.
Cleanup
protected virtual Exception? Cleanup(Exception? exception, Harmony harmony, MethodInfo? original)Runs in the finally block of the reverse patch attempt, whether or not it succeeded. The base implementation passes exception through unchanged, and a non-null result is logged as an error.
Method
protected static MethodInfo? Method(string name)Resolves a method of the reverse patch class by name only, and returns null when the class has no such method. Results are cached by the patching core, and names that do not resolve are remembered so the failed reflection lookup happens only once.
Patch
void IReversePatchMethod.Patch(Harmony harmony)Applies the reverse patch, including Prepare and Cleanup: it returns when Enabled is false or when either the stub or TargetMethod is missing, then calls Harmony.ReversePatch with a HarmonyMethod built from Priority, Before, After and Debug and with TargetTranspiler or the local-function fallback. It is reachable through IReversePatchMethod only, which is how the group runs it.
Log
public void Log(object? message)
public void Log(object? message, Object context)Writes the message with Debug.Log, prefixed with DisplayName. The context overload also highlights that Unity object when the console entry is clicked.
LogWarning
public void LogWarning(object? message)
public void LogWarning(object? message, Object context)Writes the message with Debug.LogWarning, prefixed with DisplayName. The context overload also highlights that Unity object when the console entry is clicked.
LogError
public void LogError(object? message)
public void LogError(object? message, Object context)Writes the message with Debug.LogError, prefixed with DisplayName. The context overload also highlights that Unity object when the console entry is clicked.
Assert
public void Assert(bool condition, object? message)
public void Assert(bool condition, object? message, Object context)Asserts condition through Debug.Assert, reporting the message with the DisplayName prefix when it fails. The context overload also highlights that Unity object when the console entry is clicked.
Log2
public static void Log2(object? message)
public static void Log2(object? message, Object context)Same as Log, for use from static members: the prefix is ThisType.Name, because no instance is available there.
LogWarning2
public static void LogWarning2(object? message)
public static void LogWarning2(object? message, Object context)Same as LogWarning, for use from static members, with the type name as prefix.
LogError2
public static void LogError2(object? message)
public static void LogError2(object? message, Object context)Same as LogError, for use from static members, with the type name as prefix.
Assert2
public static void Assert2(bool condition, object? message)
public static void Assert2(bool condition, object? message, Object context)Same as Assert, for use from static members, with the type name as prefix.