Show / Hide Table of Contents

Class CodeMatcher

A CodeInstruction matcher

Inheritance
object
CodeMatcher
Namespace: HarmonyLib
Assembly: 0Harmony.dll
Syntax
public class CodeMatcher

Constructors

CodeMatcher()

Creates an empty code matcher

Declaration
public CodeMatcher()

CodeMatcher(IEnumerable<CodeInstruction>, ILGenerator)

Creates a code matcher from an enumeration of instructions

Declaration
public CodeMatcher(IEnumerable<CodeInstruction> instructions, ILGenerator generator = null)
Parameters
Type Name Description
IEnumerable<CodeInstruction> instructions

The instructions (transpiler argument)

ILGenerator generator

An optional IL generator

Properties

Blocks

Gets the exception blocks at the current position

Declaration
public ref List<ExceptionBlock> Blocks { get; }
Property Value
Type Description
List<ExceptionBlock>

The blocks

Instruction

Gets instructions at the current position

Declaration
public CodeInstruction Instruction { get; }
Property Value
Type Description
CodeInstruction

The instruction

IsInvalid

Checks whether the position of this CodeMatcher is outside its bounds

Declaration
public bool IsInvalid { get; }
Property Value
Type Description
bool

True if this CodeMatcher is invalid

IsValid

Checks whether the position of this CodeMatcher is within bounds

Declaration
public bool IsValid { get; }
Property Value
Type Description
bool

True if this CodeMatcher is valid

Labels

Gets the labels at the current position

Declaration
public ref List<Label> Labels { get; }
Property Value
Type Description
List<Label>

The labels

Length

Gets the number of code instructions in this matcher

Declaration
public int Length { get; }
Property Value
Type Description
int

The count

Opcode

Gets the opcode at the current position

Declaration
public ref OpCode Opcode { get; }
Property Value
Type Description
OpCode

The opcode

Operand

Gets the operand at the current position

Declaration
public ref object Operand { get; }
Property Value
Type Description
object

The operand

Pos

The current position

Declaration
public int Pos { get; }
Property Value
Type Description
int

The index or -1 if out of bounds

Remaining

Gets the remaining code instructions

Declaration
public int Remaining { get; }
Property Value
Type Description
int

The remaining count

Methods

AddLabels(IEnumerable<Label>)

Adds an enumeration of labels to current position

Declaration
public CodeMatcher AddLabels(IEnumerable<Label> labels)
Parameters
Type Name Description
IEnumerable<Label> labels

The labels

Returns
Type Description
CodeMatcher

The same code matcher

AddLabelsAt(int, IEnumerable<Label>)

Adds an enumeration of labels at a position

Declaration
public CodeMatcher AddLabelsAt(int position, IEnumerable<Label> labels)
Parameters
Type Name Description
int position

The position

IEnumerable<Label> labels

The labels

Returns
Type Description
CodeMatcher

The same code matcher

Advance(int)

Advances the current position

Declaration
public CodeMatcher Advance(int offset = 1)
Parameters
Type Name Description
int offset

The offset

Returns
Type Description
CodeMatcher

The same code matcher

Clone()

Makes a clone of this instruction matcher

Declaration
public CodeMatcher Clone()
Returns
Type Description
CodeMatcher

A copy of this matcher

CreateLabel(out Label)

Creates a label at current position

Declaration
public CodeMatcher CreateLabel(out Label label)
Parameters
Type Name Description
Label label

[out] The label

Returns
Type Description
CodeMatcher

The same code matcher

CreateLabelAt(int, out Label)

Creates a label at a position

Declaration
public CodeMatcher CreateLabelAt(int position, out Label label)
Parameters
Type Name Description
int position

The position

Label label

[out] The new label

Returns
Type Description
CodeMatcher

The same code matcher

CreateLabelWithOffsets(int, out Label)

Creates a label at the given offset from the current position

Declaration
public CodeMatcher CreateLabelWithOffsets(int offset, out Label label)
Parameters
Type Name Description
int offset

The offset

Label label

[out] The new label

Returns
Type Description
CodeMatcher

The same code matcher

DeclareLocal(Type, out LocalBuilder)

Declares a local variable but does not add it

Declaration
public CodeMatcher DeclareLocal(Type variableType, out LocalBuilder localVariable)
Parameters
Type Name Description
Type variableType

The variable type

LocalBuilder localVariable

[out] The new local variable

Returns
Type Description
CodeMatcher

The same code matcher

DefineLabel(out Label)

Declares a new label but does not add it

Declaration
public CodeMatcher DefineLabel(out Label label)
Parameters
Type Name Description
Label label

[out] The new label

Returns
Type Description
CodeMatcher

The same code matcher

DistinctLabels(IEnumerable<CodeInstruction>)

Gets a list of all distinct labels

Declaration
public List<Label> DistinctLabels(IEnumerable<CodeInstruction> instructions)
Parameters
Type Name Description
IEnumerable<CodeInstruction> instructions

The instructions (transpiler argument)

Returns
Type Description
List<Label>

A list of Labels

Do(Action<CodeMatcher>)

Runs some code when chaining CodeMatcher at the current position

Declaration
public CodeMatcher Do(Action<CodeMatcher> action)
Parameters
Type Name Description
Action<CodeMatcher> action

The System.Action<T> to run

Returns
Type Description
CodeMatcher

The same code matcher

End()

Moves the current position to the end

Declaration
public CodeMatcher End()
Returns
Type Description
CodeMatcher

The same code matcher

Insert(params CodeInstruction[])

Inserts some instructions at the current position

Declaration
public CodeMatcher Insert(params CodeInstruction[] instructions)
Parameters
Type Name Description
CodeInstruction[] instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

Insert(IEnumerable<CodeInstruction>)

Inserts an enumeration of instructions at the current position

Declaration
public CodeMatcher Insert(IEnumerable<CodeInstruction> instructions)
Parameters
Type Name Description
IEnumerable<CodeInstruction> instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertAfter(params CodeInstruction[])

Inserts instructions immediately after the current position

Declaration
public CodeMatcher InsertAfter(params CodeInstruction[] instructions)
Parameters
Type Name Description
CodeInstruction[] instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertAfter(IEnumerable<CodeInstruction>)

Inserts an enumeration of instructions immediately after the current position

Declaration
public CodeMatcher InsertAfter(IEnumerable<CodeInstruction> instructions)
Parameters
Type Name Description
IEnumerable<CodeInstruction> instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertAfterAndAdvance(params CodeInstruction[])

Inserts instructions immediately after the current position and advances to the last inserted instruction

Declaration
public CodeMatcher InsertAfterAndAdvance(params CodeInstruction[] instructions)
Parameters
Type Name Description
CodeInstruction[] instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertAfterAndAdvance(IEnumerable<CodeInstruction>)

Inserts an enumeration of instructions immediately after the current position and advances to the last inserted instruction

Declaration
public CodeMatcher InsertAfterAndAdvance(IEnumerable<CodeInstruction> instructions)
Parameters
Type Name Description
IEnumerable<CodeInstruction> instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertAndAdvance(params CodeInstruction[])

Inserts some instructions at the current position and advances it

Declaration
public CodeMatcher InsertAndAdvance(params CodeInstruction[] instructions)
Parameters
Type Name Description
CodeInstruction[] instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertAndAdvance(IEnumerable<CodeInstruction>)

Inserts an enumeration of instructions at the current position and advances it

Declaration
public CodeMatcher InsertAndAdvance(IEnumerable<CodeInstruction> instructions)
Parameters
Type Name Description
IEnumerable<CodeInstruction> instructions

The instructions

Returns
Type Description
CodeMatcher

The same code matcher

InsertBranch(OpCode, int)

Inserts a branch at the current position

Declaration
public CodeMatcher InsertBranch(OpCode opcode, int destination)
Parameters
Type Name Description
OpCode opcode

The branch opcode

int destination

Branch destination

Returns
Type Description
CodeMatcher

The same code matcher

InsertBranchAfter(OpCode, int)

Inserts a branch instruction immediately after the current position

Declaration
public CodeMatcher InsertBranchAfter(OpCode opcode, int destination)
Parameters
Type Name Description
OpCode opcode

The branch opcode

int destination

Branch destination index

Returns
Type Description
CodeMatcher

The same code matcher

InsertBranchAfterAndAdvance(OpCode, int)

Inserts a branch instruction immediately after the current position and advances the position

Declaration
public CodeMatcher InsertBranchAfterAndAdvance(OpCode opcode, int destination)
Parameters
Type Name Description
OpCode opcode

The branch opcode

int destination

Branch destination index

Returns
Type Description
CodeMatcher

The same code matcher

InsertBranchAndAdvance(OpCode, int)

Inserts a branch at the current position and advances it

Declaration
public CodeMatcher InsertBranchAndAdvance(OpCode opcode, int destination)
Parameters
Type Name Description
OpCode opcode

The branch opcode

int destination

Branch destination

Returns
Type Description
CodeMatcher

The same code matcher

InstructionAt(int)

Gets instructions at the current position with offset

Declaration
public CodeInstruction InstructionAt(int offset)
Parameters
Type Name Description
int offset

The offset

Returns
Type Description
CodeInstruction

The instruction

InstructionEnumeration()

Gets all instructions as an enumeration

Declaration
public IEnumerable<CodeInstruction> InstructionEnumeration()
Returns
Type Description
IEnumerable<CodeInstruction>

A list of instructions

Instructions()

Gets all instructions

Declaration
public List<CodeInstruction> Instructions()
Returns
Type Description
List<CodeInstruction>

A list of instructions

Instructions(int)

Gets some instructions counting from current position

Declaration
public List<CodeInstruction> Instructions(int count)
Parameters
Type Name Description
int count

Number of instructions

Returns
Type Description
List<CodeInstruction>

A list of instructions

InstructionsInRange(int, int)

Gets all instructions within a range

Declaration
public List<CodeInstruction> InstructionsInRange(int start, int end)
Parameters
Type Name Description
int start

The start index

int end

The end index

Returns
Type Description
List<CodeInstruction>

A list of instructions

InstructionsWithOffsets(int, int)

Gets all instructions within a range (relative to current position)

Declaration
public List<CodeInstruction> InstructionsWithOffsets(int startOffset, int endOffset)
Parameters
Type Name Description
int startOffset

The start offset

int endOffset

The end offset

Returns
Type Description
List<CodeInstruction>

A list of instructions

MatchEndBackwards(params CodeMatch[])

Matches backwards and moves the position to ending of matching sequence

Declaration
public CodeMatcher MatchEndBackwards(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

MatchEndForward(params CodeMatch[])

Matches forward and advances position to ending of matching sequence

Declaration
public CodeMatcher MatchEndForward(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

MatchStartBackwards(params CodeMatch[])

Matches backwards and moves the position to beginning of matching sequence

Declaration
public CodeMatcher MatchStartBackwards(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

MatchStartForward(params CodeMatch[])

Matches forward and advances position to beginning of matching sequence

Declaration
public CodeMatcher MatchStartForward(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

NamedMatch(string)

Gets a match by its name

Declaration
public CodeInstruction NamedMatch(string name)
Parameters
Type Name Description
string name

The match name

Returns
Type Description
CodeInstruction

An instruction

OnError(ErrorHandler)

Registers an error handler that is invoked instead of throwing an exception

Declaration
public CodeMatcher OnError(CodeMatcher.ErrorHandler errorHandler)
Parameters
Type Name Description
CodeMatcher.ErrorHandler errorHandler

The CodeMatcher.ErrorHandler to register or null to remove the current handler

Returns
Type Description
CodeMatcher

The same code matcher

PrepareMatchEndBackwards(params CodeMatch[])

Prepares matching backwards and reversing position to ending of matching sequence

Declaration
public CodeMatcher PrepareMatchEndBackwards(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

PrepareMatchEndForward(params CodeMatch[])

Prepares matching forward and advancing position to ending of matching sequence

Declaration
public CodeMatcher PrepareMatchEndForward(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

PrepareMatchStartBackwards(params CodeMatch[])

Prepares matching backwards and reversing position to beginning of matching sequence

Declaration
public CodeMatcher PrepareMatchStartBackwards(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

PrepareMatchStartForward(params CodeMatch[])

Prepares matching forward and advancing position to beginning of matching sequence

Declaration
public CodeMatcher PrepareMatchStartForward(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

RemoveInstruction()

Removes current instruction

Declaration
public CodeMatcher RemoveInstruction()
Returns
Type Description
CodeMatcher

The same code matcher

RemoveInstructions(int)

Removes some instruction from current position by count

Declaration
public CodeMatcher RemoveInstructions(int count)
Parameters
Type Name Description
int count

Number of instructions

Returns
Type Description
CodeMatcher

The same code matcher

RemoveInstructionsInRange(int, int)

Removes the instructions in a range

Declaration
public CodeMatcher RemoveInstructionsInRange(int start, int end)
Parameters
Type Name Description
int start

The start

int end

The end

Returns
Type Description
CodeMatcher

The same code matcher

RemoveInstructionsWithOffsets(int, int)

Removes the instructions in an offset range

Declaration
public CodeMatcher RemoveInstructionsWithOffsets(int startOffset, int endOffset)
Parameters
Type Name Description
int startOffset

The start offset

int endOffset

The end offset

Returns
Type Description
CodeMatcher

The same code matcher

RemoveSearchBackward(Func<CodeInstruction, bool>)

Removes instructions from the current position backward until a predicate is matched. The matched instruction is not removed

Declaration
public CodeMatcher RemoveSearchBackward(Func<CodeInstruction, bool> predicate)
Parameters
Type Name Description
Func<CodeInstruction, bool> predicate

A function to test each instruction for a match

Returns
Type Description
CodeMatcher

The same code matcher

RemoveSearchForward(Func<CodeInstruction, bool>)

Removes instructions from the current position forward until a predicate is matched. The matched instruction is not removed

Declaration
public CodeMatcher RemoveSearchForward(Func<CodeInstruction, bool> predicate)
Parameters
Type Name Description
Func<CodeInstruction, bool> predicate

A function to test each instruction for a match

Returns
Type Description
CodeMatcher

The same code matcher

RemoveUntilBackward(params CodeMatch[])

Removes instructions backwards from the current position to the previous match (exclusive)

Declaration
public CodeMatcher RemoveUntilBackward(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

RemoveUntilForward(params CodeMatch[])

Removes instructions from the current position up to the next match (exclusive)

Declaration
public CodeMatcher RemoveUntilForward(params CodeMatch[] matches)
Parameters
Type Name Description
CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

Repeat(Action<CodeMatcher>, Action<string>)

Repeats a match action until boundaries are met

Declaration
public CodeMatcher Repeat(Action<CodeMatcher> matchAction, Action<string> notFoundAction = null)
Parameters
Type Name Description
Action<CodeMatcher> matchAction

The match action

Action<string> notFoundAction

An optional action that is executed when no match is found

Returns
Type Description
CodeMatcher

The same code matcher

ReportFailure(MethodBase, Action<string>)

Reports a failure

Declaration
public bool ReportFailure(MethodBase method, Action<string> logger)
Parameters
Type Name Description
MethodBase method

The method involved

Action<string> logger

The logger

Returns
Type Description
bool

True if current position is invalid and error was logged

Reset(bool)

Resets the current position to -1 and clears last matches and errors

Declaration
public CodeMatcher Reset(bool atFirstInstruction = true)
Parameters
Type Name Description
bool atFirstInstruction

If true, sets position to 0, otherwise sets it to -1

Returns
Type Description
CodeMatcher

The same code matcher

SearchBackwards(Func<CodeInstruction, bool>)

Searches backwards with a predicate and moves the position

Declaration
public CodeMatcher SearchBackwards(Func<CodeInstruction, bool> predicate)
Parameters
Type Name Description
Func<CodeInstruction, bool> predicate

A function to test each instruction for a match

Returns
Type Description
CodeMatcher

The same code matcher

SearchForward(Func<CodeInstruction, bool>)

Searches forward with a predicate and advances position

Declaration
public CodeMatcher SearchForward(Func<CodeInstruction, bool> predicate)
Parameters
Type Name Description
Func<CodeInstruction, bool> predicate

A function to test each instruction for a match

Returns
Type Description
CodeMatcher

The same code matcher

Set(OpCode, object)

Sets opcode and operand at current position

Declaration
public CodeMatcher Set(OpCode opcode, object operand)
Parameters
Type Name Description
OpCode opcode

The opcode

object operand

The operand

Returns
Type Description
CodeMatcher

The same code matcher

SetAndAdvance(OpCode, object)

Sets opcode and operand at current position and advances

Declaration
public CodeMatcher SetAndAdvance(OpCode opcode, object operand)
Parameters
Type Name Description
OpCode opcode

The opcode

object operand

The operand

Returns
Type Description
CodeMatcher

The same code matcher

SetInstruction(CodeInstruction)

Sets an instruction at current position

Declaration
public CodeMatcher SetInstruction(CodeInstruction instruction)
Parameters
Type Name Description
CodeInstruction instruction

The instruction to set

Returns
Type Description
CodeMatcher

The same code matcher

SetInstructionAndAdvance(CodeInstruction)

Sets instruction at current position and advances

Declaration
public CodeMatcher SetInstructionAndAdvance(CodeInstruction instruction)
Parameters
Type Name Description
CodeInstruction instruction

The instruction

Returns
Type Description
CodeMatcher

The same code matcher

SetJumpTo(OpCode, int, out Label)

Sets jump to

Declaration
public CodeMatcher SetJumpTo(OpCode opcode, int destination, out Label label)
Parameters
Type Name Description
OpCode opcode

Branch instruction

int destination

Destination for the jump

Label label

[out] The created label

Returns
Type Description
CodeMatcher

The same code matcher

SetOpcodeAndAdvance(OpCode)

Sets opcode at current position and advances

Declaration
public CodeMatcher SetOpcodeAndAdvance(OpCode opcode)
Parameters
Type Name Description
OpCode opcode

The opcode

Returns
Type Description
CodeMatcher

The same code matcher

SetOperandAndAdvance(object)

Sets operand at current position and advances

Declaration
public CodeMatcher SetOperandAndAdvance(object operand)
Parameters
Type Name Description
object operand

The operand

Returns
Type Description
CodeMatcher

The same code matcher

Start()

Moves the current position to the start

Declaration
public CodeMatcher Start()
Returns
Type Description
CodeMatcher

The same code matcher

ThrowIfFalse(string, Func<CodeMatcher, bool>)

Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), or if the check function returns false

Declaration
public CodeMatcher ThrowIfFalse(string explanation, Func<CodeMatcher, bool> stateCheckFunc)
Parameters
Type Name Description
string explanation

Explanation of where/why the exception was thrown that will be added to the exception message

Func<CodeMatcher, bool> stateCheckFunc

Function that checks validity of current state. If it returns false, an exception is thrown

Returns
Type Description
CodeMatcher

The same code matcher

ThrowIfInvalid(string)

Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed)

Declaration
public CodeMatcher ThrowIfInvalid(string explanation)
Parameters
Type Name Description
string explanation

Explanation of where/why the exception was thrown that will be added to the exception message

Returns
Type Description
CodeMatcher

The same code matcher

ThrowIfNotMatch(string, params CodeMatch[])

Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), or if the matches do not match at current position

Declaration
public CodeMatcher ThrowIfNotMatch(string explanation, params CodeMatch[] matches)
Parameters
Type Name Description
string explanation

Explanation of where/why the exception was thrown that will be added to the exception message

CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

ThrowIfNotMatchBack(string, params CodeMatch[])

Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), or if the matches do not match at any point between current position and the start

Declaration
public CodeMatcher ThrowIfNotMatchBack(string explanation, params CodeMatch[] matches)
Parameters
Type Name Description
string explanation

Explanation of where/why the exception was thrown that will be added to the exception message

CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

ThrowIfNotMatchForward(string, params CodeMatch[])

Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), or if the matches do not match at any point between current position and the end

Declaration
public CodeMatcher ThrowIfNotMatchForward(string explanation, params CodeMatch[] matches)
Parameters
Type Name Description
string explanation

Explanation of where/why the exception was thrown that will be added to the exception message

CodeMatch[] matches

Some code matches

Returns
Type Description
CodeMatcher

The same code matcher

In this article
Back to top Generated by DocFX