Skip to content

ILProcessorModule

UserR00T edited this page Aug 9, 2021 · 1 revision

AddMethodModule

This module is used to fetch a reference to the IL body processor. By decorating your method with the ILProcessorAttribute you will allow UniversalUnityHooks to invoke your method with an instance of ILProcessor, MethodDefinition, & AssemblyDefinition of the target type. This is useful for manual operations on the target method/type/assembly.

Example

// Source (plugin)
[ILProcessorModule(typeof(ExampleType), "ExampleMethod")]
public static void IlProcessor(ILProcessor il, MethodDefinition method, AssemblyDefinition assembly)
{
    foreach (var instruction in il.Body.Instructions)
    {
        Console.WriteLine($"{instruction.OpCode}: {instruction.Offset} {instruction.Operand}");
    }
    il.Replace(Instruction.Create(OpCodes.Ldstr), Instruction.Create(OpCodes.Ldstr, "This is a shorter string!"));
}

// Target _before_ injecting (Assembly-CSharp.dll)
public class ExampleType
{
    public void ExampleMethod()
    {
        Debug.Log("This is a nice long string being written to some kind of stout."); // Console.log for non Unity applications.
    }
}

// Target _after_ injecting (Assembly-CSharp.dll)
public class ExampleType
{
    public void ExampleMethod()
    {
        Debug.Log("This is a shorter string!"); // Console.log for non Unity applications.
    }
}

Attribute arguments

Name Description Default
Type
Method
Clone this wiki locally