Skip to content

Commit

Permalink
fixes #25 by implementing __args; updates docs; bump to v2.2
Browse files Browse the repository at this point in the history
  • Loading branch information
pardeike committed Dec 30, 2021
1 parent 80c92f5 commit 5d99dcd
Show file tree
Hide file tree
Showing 19 changed files with 406 additions and 54 deletions.
6 changes: 5 additions & 1 deletion Harmony/Documentation/articles/patching-auxilary.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Each of those methods can take up to three optional arguments that are injected
- `Harmony harmony` - the current Harmony instance
- `Exception ex` - only valid in `Cleanup` and receives a possible exception

Here is a simple example that patches a method inside a private type:

[!code-csharp[example](../examples/basics.cs?name=target_method)]

### Prepare

Before the patching, Harmony gives you a chance to prepare your state. For this, Harmony searches for a method called
Expand Down Expand Up @@ -72,4 +76,4 @@ static Exception MyCleanup(MethodBase original, ...)

Similar to `Prepare()` this method is called with `original` set to the method that just has been patched and then finally one more time before ending the overall patching (original will be `null`).

Additionally, you can intercept exceptions that are thrown while patching. Use the injection of `exception` to learn what happened and check if you can cast it to `HarmonyException` to get more information. Finally, you can return `Exception` to replace the exception or `null` to suppress it.
Additionally, you can intercept exceptions that are thrown while patching. Use the injection of `exception` to learn what happened and check if you can cast it to `HarmonyException` to get more information. Finally, you can return `Exception` to replace the exception or `null` to suppress it.
49 changes: 28 additions & 21 deletions Harmony/Documentation/articles/patching-injections.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,55 @@

## Common injected values

Each patch method (except a transpiler) can get all the arguments of the original method as well as the instance if the original method is not static and the return value. Patches only need to define the parameters they want to access.
Each patch method (except a transpiler) can get all the arguments of the original method as well as the instance if the original method is not static and the return value.

### Instance
You only need to define the parameters you want to access.

Patches can use an argument named `__instance` to access the instance value if original method is not static. This is similar to the C# keyword `this` when used in the original method.
### __instance

### Result
Patches can use an argument called **`__instance`** to access the instance value if original method is not static. This is similar to the C# keyword `this` when used in the original method.

Patches can use an argument named `__result` to access the returned value. The type `T` of argument must match the return type of the original or be assignable from it. For prefixes, as the original method hasn't run yet, the value of `__result` is default(T). For most reference types, that would be `null`. If you wish to alter the `__result`, you need to define it by reference like `ref string name`.
### __result

### State
Patches can use an argument called **`__result`** to access the returned value. The type must match the return type of the original or be assignable from it. For prefixes, as the original method hasn't run yet, the value of `__result` is the default for that type. For most reference types, that would be `null`. If you wish to **alter** the `__result`, you need to define it **by reference** like `ref string name`.

Patches can use an argument named `__state` to store information in the prefix method that can be accessed again in the postfix method. Think of it as a local variable. It can be any type and you are responsible to initialize its value in the prefix. It only works if both patches are defined in the same class.
### __state

### (Private) Fields
Patches can use an argument called **`__state`** to store information in the prefix method that can be accessed again in the postfix method. Think of it as a local variable. It can be any type and you are responsible to initialize its value in the prefix. **Note:** It only works if both patches are defined in the same class.

Argument names starting with three underscores, for example `___someField`, can be used to read and (with `ref`) write private fields on the instance that has the corresponding name (minus the underscores).
### ___fields

### Original Method Argument Matching
Argument names starting with **three** underscores like **`___someField`** can be used to read/write private fields that have that name minus the underscores. To write to field you need to use the **`ref`** keyword like `ref string ___name`.

In order for the original method arguments to be properly matched to the patched method, some restrictions are placed on the types and names of arguments in the patched method:
### __args

#### - Argument Types
To access all arguments at once, you can let Harmony inject **`object[] __args`** that will contain all arguments in the order they appear. Editing the contents of that array (no ref needed) will automatically update the values of the corresponding arguments.

The type of a given argument (that is to be matched to the argument of the original method) must either be the same type or be `object`.
**Note:** This way of manipulation comes with some small overhead so if possible use normal argument injection

#### - Argument Names
### method arguments

The name of a given argument (that is to be matched to the argument of the original method) must either be the same name or of the form `__n`, where `n` is the zero-based index of the argument in the orignal method (you can use argument annotations to map to custom names).
To access or change one or several of the original methods arguments, simply repeat them with the same name in your patch. Some restrictions are placed on the types and names of arguments in the patched method:

### - The original
- The type of an injected argument must be assignable from the original argument (or just use `object`)
- The name of a given argument (that is to be matched to the argument of the original method) must either be the same name or of the form **`__n`**, where `n` is the zero-based index of the argument in the orignal method (you can also use argument annotations to map to custom names).

To allow patches to identify on which method they are attachted you can inject the original methods MethodBase by using an argument named `__originalMethod`.
### __originalMethod

To allow patches to identify on which method they are attachted you can inject the original methods MethodBase by using an argument called **`__originalMethod`**.

![note] **You cannot call the original method with that**. The value is only for conditional code in your patch that can selectively run if the patch is applied to multiple methods. The original does not exist after patching and this will point to the patched version.

### - Special arguments
### __runOriginal

To learn if the original is/was skipped you can inject **`bool __runOriginal`**. This is a readonly injection to understand if the original will be run (in a Prefix) or was run (in a Postfix).

### Transpilers

In transpilers, arguments are only matched by their type so you can choose any argument name you like.

An argument of type `IEnumerable<CodeInstruction>` is required and will be used to pass the IL codes to the transpiler
An argument of type `ILGenerator` will be set to the current IL code generator
An argument of type `MethodBase` will be set to the current original method being patched
An argument of type **`IEnumerable<CodeInstruction>`** is required and will be used to pass the IL codes to the transpiler
An argument of type **`ILGenerator`** will be set to the current IL code generator
An argument of type **`MethodBase`** will be set to the current original method being patched

[note]: https://raw.githubusercontent.com/pardeike/Harmony/master/Harmony/Documentation/images/note.png
2 changes: 1 addition & 1 deletion Harmony/Documentation/examples/annotations_basic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace Annotations_Basics
using HarmonyLib;

[HarmonyPatch(typeof(SomeTypeHere))]
[HarmonyPatch("SomeMethodName")]
[HarmonyPatch("SomeMethodName")] // if possible use nameof() here
class MyPatches
{
static void Postfix(/*...*/)
Expand Down
8 changes: 6 additions & 2 deletions Harmony/Documentation/examples/annotations_multiple.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,13 @@ static IEnumerable<MethodBase> TargetMethods()
}

// prefix all methods in someAssembly with a non-void return type and beginning with "Player"
static void Prefix(MethodBase __originalMethod)
static void Prefix(object[] __args, MethodBase __originalMethod)
{
// use __originalMethod to decide what to do
// use dynamic code to handle all method calls
var parameters = __originalMethod.GetParameters();
FileLog.Log($"Method {__originalMethod.FullDescription()}:");
for (var i = 0; i < __args.Length; i++)
FileLog.Log($"{parameters[i].Name} of type {parameters[i].ParameterType} is {__args[i]}");
}
}
// </example>
Expand Down
25 changes: 25 additions & 0 deletions Harmony/Documentation/examples/basics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ void PatchManual()
{
// <patch_manual>
// add null checks to the following lines, they are omitted for clarity
// when possible, don't use string and instead use nameof(...)
var original = typeof(TheClass).GetMethod("TheMethod");
var prefix = typeof(MyPatchClass1).GetMethod("SomeMethod");
var postfix = typeof(MyPatchClass2).GetMethod("SomeMethod");
Expand Down Expand Up @@ -141,6 +142,30 @@ void Unpatch()
// </unpatch_one>
}

// <target_method>
[HarmonyPatch] // at least one Harmony annotation makes Harmony not skip this patch class when calling PatchAll()
class MyPatch
{
// here, inside the patch class, you can place the auxilary patch methods
// for example TargetMethod:

public MethodBase TargetMethod()
{
// use normal reflection or helper methods in <AccessTools> to find the method/constructor
// you want to patch and return its MethodInfo/ConstructorInfo
//
var type = AccessTools.FirstInner(typeof(TheClass), t => t.Name.Contains("Stuff"));
return AccessTools.FirstMethod(type, method => method.Name.Contains("SomeMethod"));
}

// your patches
public void Prefix()
{
// ...
}
}
// </target_method>

class TheClass { }
class MyPatchClass1 { }
class MyPatchClass2 { }
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Documentation/examples/intro_annotations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public static void DoPatching()
}

[HarmonyPatch(typeof(SomeGameClass))]
[HarmonyPatch("DoSomething")]
[HarmonyPatch("DoSomething")] // if possible use nameof() here
class Patch01
{
static AccessTools.FieldRef<SomeGameClass, bool> isRunningRef =
Expand Down
2 changes: 1 addition & 1 deletion Harmony/Documentation/examples/intro_manual.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public static void DoPatching()
{
var harmony = new Harmony("com.example.patch");

var mOriginal = AccessTools.Method(typeof(SomeGameClass), "DoSomething");
var mOriginal = AccessTools.Method(typeof(SomeGameClass), "DoSomething"); // if possible use nameof() here
var mPrefix = SymbolExtensions.GetMethodInfo(() => MyPrefix());
var mPostfix = SymbolExtensions.GetMethodInfo(() => MyPostfix());
// in general, add null checks here (new HarmonyMethod() does it for you too)
Expand Down
2 changes: 2 additions & 0 deletions Harmony/Documentation/examples/patching-auxilary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class Example
// <yield>
static IEnumerable<MethodBase> TargetMethods()
{
// if possible use nameof() or SymbolExtensions.GetMethodInfo() here
yield return AccessTools.Method(typeof(Foo), "Method1");
yield return AccessTools.Method(typeof(Bar), "Method2");

// you could also iterate using reflections over many methods
}
// </yield>
Expand Down
8 changes: 4 additions & 4 deletions Harmony/Documentation/examples/patching-postfix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public string GetName()
}
}

[HarmonyPatch(typeof(OriginalCode), "GetName")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
class Patch
{
static void Postfix(ref string __result)
Expand Down Expand Up @@ -46,7 +46,7 @@ public IEnumerable<int> GetNumbers()
}
}

[HarmonyPatch(typeof(OriginalCode), "GetName")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
class Patch1
{
static string Postfix(string name)
Expand All @@ -55,7 +55,7 @@ static string Postfix(string name)
}
}

[HarmonyPatch(typeof(OriginalCode), "GetNumbers")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetNumbers))]
class Patch2
{
static IEnumerable<int> Postfix(IEnumerable<int> values)
Expand Down Expand Up @@ -83,7 +83,7 @@ public void Test(int counter)
}
}

[HarmonyPatch(typeof(OriginalCode), "Test")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.Test))]
class Patch
{
static void Prefix(int counter)
Expand Down
8 changes: 4 additions & 4 deletions Harmony/Documentation/examples/patching-prefix.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public void Test(int counter, string name)
}
}

[HarmonyPatch(typeof(OriginalCode), "Test")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.Test))]
class Patch
{
static void Prefix(int counter, ref string name)
Expand All @@ -37,7 +37,7 @@ public string GetName()
}
}

[HarmonyPatch(typeof(OriginalCode), "GetName")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.GetName))]
class Patch
{
static bool Prefix(ref string __result)
Expand All @@ -62,7 +62,7 @@ public bool IsFullAfterTakingIn(int i)
}
}

[HarmonyPatch(typeof(OriginalCode), "IsFullAfterTakingIn")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.IsFullAfterTakingIn))]
class Patch
{
static bool Prefix(ref bool __result, int i)
Expand Down Expand Up @@ -91,7 +91,7 @@ public void Test(int counter, string name)
}
}

[HarmonyPatch(typeof(OriginalCode), "Test")]
[HarmonyPatch(typeof(OriginalCode), nameof(OriginalCode.Test))]
class Patch
{
// this example uses a Stopwatch type to measure
Expand Down
8 changes: 4 additions & 4 deletions Harmony/Documentation/examples/patching-transpiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Patching_Transpiler
class TypicalExample
{
// <typical>
static FieldInfo f_someField = AccessTools.Field(typeof(SomeType), "someField");
static FieldInfo f_someField = AccessTools.Field(typeof(SomeType), nameof(SomeType.someField));
static MethodInfo m_MyExtraMethod = SymbolExtensions.GetMethodInfo(() => Tools.MyExtraMethod());

// looks for STDFLD someField and inserts CALL MyExtraMethod before it
Expand All @@ -30,7 +30,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
// </typical>

class SomeType { }
class SomeType { public string someField; }

class Tools
{
Expand All @@ -44,7 +44,7 @@ class CaravanExample
{
// <caravan>
[HarmonyPatch(typeof(Dialog_FormCaravan))]
[HarmonyPatch("CheckForErrors")]
[HarmonyPatch(nameof(Dialog_FormCaravan.CheckForErrors))]
public static class Dialog_FormCaravan_CheckForErrors_Patch
{
static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
Expand Down Expand Up @@ -98,7 +98,7 @@ static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> inst
}
// </caravan>

class Dialog_FormCaravan { }
class Dialog_FormCaravan { public void CheckForErrors() { } }

class Log
{
Expand Down
6 changes: 3 additions & 3 deletions Harmony/Harmony.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@
<Authors>Andreas Pardeike</Authors>
<AssemblyName>0Harmony</AssemblyName>
<SignAssembly>true</SignAssembly>
<Version>2.1.2.0</Version>
<Version>2.2.0.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/pardeike/Harmony</PackageProjectUrl>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageTags>Harmony,Mono,Patch,Patching,Runtime,Detour,Detours,Aspect,Aspects</PackageTags>
<AssemblyVersion>2.1.2.0</AssemblyVersion>
<FileVersion>2.1.2.0</FileVersion>
<AssemblyVersion>2.2.0.0</AssemblyVersion>
<FileVersion>2.2.0.0</FileVersion>
<PackageIcon>HarmonyLogo.png</PackageIcon>
<PackageIconUrl>https://raw.githubusercontent.com/pardeike/Harmony/master/HarmonyLogo.png</PackageIconUrl>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
Expand Down
Loading

0 comments on commit 5d99dcd

Please sign in to comment.