Browse documentation

Class CodeMatch

Namespace
HarmonyLib
Assembly
0Harmony.dll

A CodeInstruction match

public class CodeMatch : CodeInstruction
Inheritance
object
CodeMatch
Inherited Members
Extension Methods

Constructors

CodeMatch(CodeInstruction, string)

Creates a code match

public CodeMatch(CodeInstruction instruction, string name = null)

Parameters

instruction CodeInstruction

The CodeInstruction

name string

An optional name

CodeMatch(Func<CodeInstruction, bool>, string)

Creates a code match

public CodeMatch(Func<CodeInstruction, bool> predicate, string name = null)

Parameters

predicate Func<CodeInstruction, bool>

The predicate

name string

An optional name

CodeMatch(Expression<Action>, string)

Creates a code match that calls a method

public CodeMatch(Expression<Action> expression, string name = null)

Parameters

expression Expression<Action>

The lambda expression using the method

name string

The optional name

CodeMatch(LambdaExpression, string)

Creates a code match that calls a method

public CodeMatch(LambdaExpression expression, string name = null)

Parameters

expression LambdaExpression

The lambda expression using the method

name string

The optional name

CodeMatch(OpCode?, object, string)

Creates a code match

public CodeMatch(OpCode? opcode = null, object operand = null, string name = null)

Parameters

opcode OpCode?

The optional opcode

operand object

The optional operand

name string

The optional name

Fields

jumpsFrom

The jumps from the match

public List<int> jumpsFrom

Field Value

List<int>

jumpsTo

The jumps to the match

public List<int> jumpsTo

Field Value

List<int>

name

The name of the match

public string name

Field Value

string

opcodeSet

The matched opcodes

public HashSet<OpCode> opcodeSet

Field Value

HashSet<OpCode>

operands

The matched operands

public List<object> operands

Field Value

List<object>

predicate

The match predicate

public Func<CodeInstruction, bool> predicate

Field Value

Func<CodeInstruction, bool>

Methods

Branches(string)

Creates a code match for branching

public static CodeMatch Branches(string name = null)

Parameters

name string

An optional name

Returns

CodeMatch

A new code match

Calls(Expression<Action>)

Creates a code match that calls a method

public static CodeMatch Calls(Expression<Action> expression)

Parameters

expression Expression<Action>

The lambda expression using the method

Returns

CodeMatch

A new code match

Calls(LambdaExpression)

Creates a code match that calls a method

public static CodeMatch Calls(LambdaExpression expression)

Parameters

expression LambdaExpression

The lambda expression using the method

Returns

CodeMatch

A new code match

Calls(ConstructorInfo)

Tests if the code instruction calls the constructor

public static CodeMatch Calls(ConstructorInfo constructor)

Parameters

constructor ConstructorInfo

The constructor

Returns

CodeMatch

A new code match

Calls(MethodInfo)

Tests if the code instruction calls the method

public static CodeMatch Calls(MethodInfo method)

Parameters

method MethodInfo

The method

Returns

CodeMatch

A new code match

GetsProperty(PropertyInfo)

Tests if the code instruction calls the getter of a property

public static CodeMatch GetsProperty(PropertyInfo property)

Parameters

property PropertyInfo

The property

Returns

CodeMatch

A new code match

Remarks

The match compares the called method with the getter, like Calls(MethodInfo). C# code calls an overridden property through the getter of its base declaration. To match those calls, pass the base property or use the expression overload.

Exceptions

ArgumentNullException

The property is null

ArgumentException

The property has no getter

GetsProperty<T>(Expression<Func<T>>)

Tests if the code instruction calls the getter of a property

public static CodeMatch GetsProperty<T>(Expression<Func<T>> expression)

Parameters

expression Expression<Func<T>>

The lambda expression reading the property, for example () => default(MyClass).MyProperty

Returns

CodeMatch

A new code match

Type Parameters

T

The property type

Exceptions

ArgumentNullException

The expression is null

ArgumentException

The expression does not read a property, or the property has no getter

IsLdarg(int?)

Tests for any form of Ldarg*

public static CodeMatch IsLdarg(int? n = null)

Parameters

n int?

The (optional) index

Returns

CodeMatch

A new code match

IsLdarga(int?)

Tests for Ldarga/Ldarga_S

public static CodeMatch IsLdarga(int? n = null)

Parameters

n int?

The (optional) index

Returns

CodeMatch

A new code match

IsLdloc(LocalBuilder)

Tests for any form of Ldloc*

public static CodeMatch IsLdloc(LocalBuilder variable = null)

Parameters

variable LocalBuilder

The optional local variable

Returns

CodeMatch

A new code match

IsStarg(int?)

Tests for Starg/Starg_S

public static CodeMatch IsStarg(int? n = null)

Parameters

n int?

The (optional) index

Returns

CodeMatch

A new code match

IsStloc(LocalBuilder)

Tests for any form of Stloc*

public static CodeMatch IsStloc(LocalBuilder variable = null)

Parameters

variable LocalBuilder

The optional local variable

Returns

CodeMatch

A new code match

LoadsArgument(bool, string)

Creates a code match for argument loads

public static CodeMatch LoadsArgument(bool useAddress = false, string name = null)

Parameters

useAddress bool

Whether to match for address loads

name string

An optional name

Returns

CodeMatch

A new code match

LoadsConstant()

Tests if the code instruction loads a constant

public static CodeMatch LoadsConstant()

Returns

CodeMatch

A new code match

LoadsConstant(double)

Tests if the code instruction loads a floating point constant

public static CodeMatch LoadsConstant(double number)

Parameters

number double

The floating point constant

Returns

CodeMatch

A new code match

LoadsConstant(Enum)

Tests if the code instruction loads an enum constant

public static CodeMatch LoadsConstant(Enum e)

Parameters

e Enum

The enum

Returns

CodeMatch

A new code match

LoadsConstant(long)

Tests if the code instruction loads an integer constant

public static CodeMatch LoadsConstant(long number)

Parameters

number long

The integer constant

Returns

CodeMatch

A new code match

LoadsConstant(string)

Tests if the code instruction loads a string constant

public static CodeMatch LoadsConstant(string str)

Parameters

str string

The string

Returns

CodeMatch

A new code match

LoadsField(FieldInfo, bool)

Tests if the code instruction loads a field

public static CodeMatch LoadsField(FieldInfo field, bool byAddress = false)

Parameters

field FieldInfo

The field

byAddress bool

Set to true if the address of the field is loaded

Returns

CodeMatch

A new code match

LoadsField<T>(Expression<Func<T>>, bool)

Tests if the code instruction loads a field

public static CodeMatch LoadsField<T>(Expression<Func<T>> expression, bool byAddress = false)

Parameters

expression Expression<Func<T>>

The lambda expression using the field

byAddress bool

Set to true if the address of the field is loaded

Returns

CodeMatch

A new code match

Type Parameters

T

The field type

LoadsLocal(bool, string)

Creates a code match for local loads

public static CodeMatch LoadsLocal(bool useAddress = false, string name = null)

Parameters

useAddress bool

Whether to match for address loads

name string

An optional name

Returns

CodeMatch

A new code match

SetsProperty(PropertyInfo)

Tests if the code instruction calls the setter of a property

public static CodeMatch SetsProperty(PropertyInfo property)

Parameters

property PropertyInfo

The property

Returns

CodeMatch

A new code match

Remarks

The match compares the called method with the setter, like Calls(MethodInfo). C# code calls an overridden property through the setter of its base declaration. To match those calls, pass the base property or use the expression overload.

Exceptions

ArgumentNullException

The property is null

ArgumentException

The property has no setter

SetsProperty<T>(Expression<Func<T>>)

Tests if the code instruction calls the setter of a property

public static CodeMatch SetsProperty<T>(Expression<Func<T>> expression)

Parameters

expression Expression<Func<T>>

The lambda expression reading the property, for example () => default(MyClass).MyProperty. C# expression trees cannot contain assignments, so the expression only names the property.

Returns

CodeMatch

A new code match

Type Parameters

T

The property type

Exceptions

ArgumentNullException

The expression is null

ArgumentException

The expression does not read a property, or the property has no setter

StoresArgument(string)

Creates a code match for argument stores

public static CodeMatch StoresArgument(string name = null)

Parameters

name string

An optional name

Returns

CodeMatch

A new code match

StoresField(FieldInfo)

Tests if the code instruction stores a field

public static CodeMatch StoresField(FieldInfo field)

Parameters

field FieldInfo

The field

Returns

CodeMatch

A new code match

StoresField<T>(Expression<Func<T>>)

Tests if the code instruction stores a field

public static CodeMatch StoresField<T>(Expression<Func<T>> expression)

Parameters

expression Expression<Func<T>>

The lambda expression using the field

Returns

CodeMatch

A new code match

Type Parameters

T

The field type

StoresLocal(string)

Creates a code match for local stores

public static CodeMatch StoresLocal(string name = null)

Parameters

name string

An optional name

Returns

CodeMatch

A new code match

ToString()

Returns a string that represents the match

public override string ToString()

Returns

string

A string representation

WithOpcodes(HashSet<OpCode>, object, string)

Creates a code match

public static CodeMatch WithOpcodes(HashSet<OpCode> opcodes, object operand = null, string name = null)

Parameters

opcodes HashSet<OpCode>

The opcodes

operand object

The optional operand

name string

The optional name

Returns

CodeMatch

A new code match

Harmony 3

For Harmony 2, read the 2.x documentation.