docs/api/HarmonyLib.CodeMatcher.html
A CodeInstruction matcher
System.Object
CodeMatcher
public class CodeMatcher
Creates an empty code matcher
public CodeMatcher()
Creates a code matcher from an enumeration of instructions
public CodeMatcher(IEnumerable<CodeInstruction> instructions, ILGenerator generator = null)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> | instructions |
The instructions (transpiler argument)
| | System.Reflection.Emit.ILGenerator | generator |
An optional IL generator
|
Gets the exception blocks at the current position
public List<ExceptionBlock> Blocks { get; }
| Type | Description |
|---|---|
| System.Collections.Generic.List<ExceptionBlock> |
The blocks
|
Gets instructions at the current position
public CodeInstruction Instruction { get; }
| Type | Description |
|---|---|
| CodeInstruction |
The instruction
|
Checks whether the position of this CodeMatcher is outside its bounds
public bool IsInvalid { get; }
| Type | Description |
|---|---|
| System.Boolean |
True if this CodeMatcher is invalid
|
Checks whether the position of this CodeMatcher is within bounds
public bool IsValid { get; }
| Type | Description |
|---|---|
| System.Boolean |
True if this CodeMatcher is valid
|
Gets the labels at the current position
public List<Label> Labels { get; }
| Type | Description |
|---|---|
| System.Collections.Generic.List\ |
The labels
|
Gets the number of code instructions in this matcher
public int Length { get; }
| Type | Description |
|---|---|
| System.Int32 |
The count
|
Gets the opcode at the current position
public OpCode Opcode { get; }
| Type | Description |
|---|---|
| System.Reflection.Emit.OpCode |
The opcode
|
Gets the operand at the current position
public object Operand { get; }
| Type | Description |
|---|---|
| System.Object |
The operand
|
The current position
public int Pos { get; }
| Type | Description |
|---|---|
| System.Int32 |
The index or -1 if out of bounds
|
Gets the remaining code instructions
public int Remaining { get; }
| Type | Description |
|---|---|
| System.Int32 |
The remaining count
|
Adds an enumeration of labels to current position
public CodeMatcher AddLabels(IEnumerable<Label> labels)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable\ | labels |
The labels
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Adds an enumeration of labels at a position
public CodeMatcher AddLabelsAt(int position, IEnumerable<Label> labels)
| Type | Name | Description |
|---|---|---|
| System.Int32 | position |
The position
| | System.Collections.Generic.IEnumerable\ | labels |
The labels
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Advances the current position
public CodeMatcher Advance(int offset = 1)
| Type | Name | Description |
|---|---|---|
| System.Int32 | offset |
The offset
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Makes a clone of this instruction matcher
public CodeMatcher Clone()
| Type | Description |
|---|---|
| CodeMatcher |
A copy of this matcher
|
Creates a label at current position
public CodeMatcher CreateLabel(out Label label)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.Label | label |
[out] The label
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Creates a label at a position
public CodeMatcher CreateLabelAt(int position, out Label label)
| Type | Name | Description |
|---|---|---|
| System.Int32 | position |
The position
| | System.Reflection.Emit.Label | label |
[out] The new label
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Creates a label at the given offset from the current position
public CodeMatcher CreateLabelWithOffsets(int offset, out Label label)
| Type | Name | Description |
|---|---|---|
| System.Int32 | offset |
The offset
| | System.Reflection.Emit.Label | label |
[out] The new label
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Declares a local variable but does not add it
public CodeMatcher DeclareLocal(Type variableType, out LocalBuilder localVariable)
| Type | Name | Description |
|---|---|---|
| System.Type | variableType |
The variable type
| | System.Reflection.Emit.LocalBuilder | localVariable |
[out] The new local variable
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Declares a new label but does not add it
public CodeMatcher DefineLabel(out Label label)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.Label | label |
[out] The new label
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Gets a list of all distinct labels
public List<Label> DistinctLabels(IEnumerable<CodeInstruction> instructions)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> | instructions |
The instructions (transpiler argument)
|
| Type | Description |
|---|---|
| System.Collections.Generic.List\ |
A list of Labels
|
Runs some code when chaining CodeMatcher at the current position
public CodeMatcher Do(Action<CodeMatcher> action)
| Type | Name | Description |
|---|---|---|
| System.Action<CodeMatcher> | action |
The System.Action<T> to run
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Moves the current position to the end
public CodeMatcher End()
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts some instructions at the current position
public CodeMatcher Insert(params CodeInstruction[] instructions)
| Type | Name | Description |
|---|---|---|
| CodeInstruction[] | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts an enumeration of instructions at the current position
public CodeMatcher Insert(IEnumerable<CodeInstruction> instructions)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts instructions immediately after the current position
public CodeMatcher InsertAfter(params CodeInstruction[] instructions)
| Type | Name | Description |
|---|---|---|
| CodeInstruction[] | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts an enumeration of instructions immediately after the current position
public CodeMatcher InsertAfter(IEnumerable<CodeInstruction> instructions)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts instructions immediately after the current position and advances to the last inserted instruction
public CodeMatcher InsertAfterAndAdvance(params CodeInstruction[] instructions)
| Type | Name | Description |
|---|---|---|
| CodeInstruction[] | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts an enumeration of instructions immediately after the current position and advances to the last inserted instruction
public CodeMatcher InsertAfterAndAdvance(IEnumerable<CodeInstruction> instructions)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts some instructions at the current position and advances it
public CodeMatcher InsertAndAdvance(params CodeInstruction[] instructions)
| Type | Name | Description |
|---|---|---|
| CodeInstruction[] | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts an enumeration of instructions at the current position and advances it
public CodeMatcher InsertAndAdvance(IEnumerable<CodeInstruction> instructions)
| Type | Name | Description |
|---|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> | instructions |
The instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts a branch at the current position
public CodeMatcher InsertBranch(OpCode opcode, int destination)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The branch opcode
| | System.Int32 | destination |
Branch destination
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts a branch instruction immediately after the current position
public CodeMatcher InsertBranchAfter(OpCode opcode, int destination)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The branch opcode
| | System.Int32 | destination |
Branch destination index
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts a branch instruction immediately after the current position and advances the position
public CodeMatcher InsertBranchAfterAndAdvance(OpCode opcode, int destination)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The branch opcode
| | System.Int32 | destination |
Branch destination index
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Inserts a branch at the current position and advances it
public CodeMatcher InsertBranchAndAdvance(OpCode opcode, int destination)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The branch opcode
| | System.Int32 | destination |
Branch destination
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Gets instructions at the current position with offset
public CodeInstruction InstructionAt(int offset)
| Type | Name | Description |
|---|---|---|
| System.Int32 | offset |
The offset
|
| Type | Description |
|---|---|
| CodeInstruction |
The instruction
|
Gets all instructions as an enumeration
public IEnumerable<CodeInstruction> InstructionEnumeration()
| Type | Description |
|---|---|
| System.Collections.Generic.IEnumerable<CodeInstruction> |
A list of instructions
|
Gets all instructions
public List<CodeInstruction> Instructions()
| Type | Description |
|---|---|
| System.Collections.Generic.List<CodeInstruction> |
A list of instructions
|
Gets some instructions counting from current position
public List<CodeInstruction> Instructions(int count)
| Type | Name | Description |
|---|---|---|
| System.Int32 | count |
Number of instructions
|
| Type | Description |
|---|---|
| System.Collections.Generic.List<CodeInstruction> |
A list of instructions
|
Gets all instructions within a range
public List<CodeInstruction> InstructionsInRange(int start, int end)
| Type | Name | Description |
|---|---|---|
| System.Int32 | start |
The start index
| | System.Int32 | end |
The end index
|
| Type | Description |
|---|---|
| System.Collections.Generic.List<CodeInstruction> |
A list of instructions
|
Gets all instructions within a range (relative to current position)
public List<CodeInstruction> InstructionsWithOffsets(int startOffset, int endOffset)
| Type | Name | Description |
|---|---|---|
| System.Int32 | startOffset |
The start offset
| | System.Int32 | endOffset |
The end offset
|
| Type | Description |
|---|---|
| System.Collections.Generic.List<CodeInstruction> |
A list of instructions
|
Matches backwards and moves the position to ending of matching sequence
public CodeMatcher MatchEndBackwards(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Matches forward and advances position to ending of matching sequence
public CodeMatcher MatchEndForward(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Matches backwards and moves the position to beginning of matching sequence
public CodeMatcher MatchStartBackwards(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Matches forward and advances position to beginning of matching sequence
public CodeMatcher MatchStartForward(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Gets a match by its name
public CodeInstruction NamedMatch(string name)
| Type | Name | Description |
|---|---|---|
| System.String | name |
The match name
|
| Type | Description |
|---|---|
| CodeInstruction |
An instruction
|
Registers an error handler that is invoked instead of throwing an exception
public CodeMatcher OnError(CodeMatcher.ErrorHandler errorHandler)
| Type | Name | Description |
|---|---|---|
| CodeMatcher.ErrorHandler | errorHandler |
The CodeMatcher.ErrorHandler to register or null to remove the current handler
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Prepares matching backwards and reversing position to ending of matching sequence
public CodeMatcher PrepareMatchEndBackwards(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Prepares matching forward and advancing position to ending of matching sequence
public CodeMatcher PrepareMatchEndForward(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Prepares matching backwards and reversing position to beginning of matching sequence
public CodeMatcher PrepareMatchStartBackwards(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Prepares matching forward and advancing position to beginning of matching sequence
public CodeMatcher PrepareMatchStartForward(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes current instruction
public CodeMatcher RemoveInstruction()
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes some instruction from current position by count
public CodeMatcher RemoveInstructions(int count)
| Type | Name | Description |
|---|---|---|
| System.Int32 | count |
Number of instructions
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes the instructions in a range
public CodeMatcher RemoveInstructionsInRange(int start, int end)
| Type | Name | Description |
|---|---|---|
| System.Int32 | start |
The start
| | System.Int32 | end |
The end
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes the instructions in an offset range
public CodeMatcher RemoveInstructionsWithOffsets(int startOffset, int endOffset)
| Type | Name | Description |
|---|---|---|
| System.Int32 | startOffset |
The start offset
| | System.Int32 | endOffset |
The end offset
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes instructions from the current position backward until a predicate is matched. The matched instruction is not removed
public CodeMatcher RemoveSearchBackward(Func<CodeInstruction, bool> predicate)
| Type | Name | Description |
|---|---|---|
| System.Func<CodeInstruction, System.Boolean> | predicate |
A function to test each instruction for a match
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes instructions from the current position forward until a predicate is matched. The matched instruction is not removed
public CodeMatcher RemoveSearchForward(Func<CodeInstruction, bool> predicate)
| Type | Name | Description |
|---|---|---|
| System.Func<CodeInstruction, System.Boolean> | predicate |
A function to test each instruction for a match
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes instructions backwards from the current position to the previous match (exclusive)
public CodeMatcher RemoveUntilBackward(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Removes instructions from the current position up to the next match (exclusive)
public CodeMatcher RemoveUntilForward(params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Repeats a match action until boundaries are met
public CodeMatcher Repeat(Action<CodeMatcher> matchAction, Action<string> notFoundAction = null)
| Type | Name | Description |
|---|---|---|
| System.Action<CodeMatcher> | matchAction |
The match action
| | System.Action\ | notFoundAction |
An optional action that is executed when no match is found
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Reports a failure
public bool ReportFailure(MethodBase method, Action<string> logger)
| Type | Name | Description |
|---|---|---|
| System.Reflection.MethodBase | method |
The method involved
| | System.Action\ | logger |
The logger
|
| Type | Description |
|---|---|
| System.Boolean |
True if current position is invalid and error was logged
|
Resets the current position to -1 and clears last matches and errors
public CodeMatcher Reset(bool atFirstInstruction = true)
| Type | Name | Description |
|---|---|---|
| System.Boolean | atFirstInstruction |
If true, sets position to 0, otherwise sets it to -1
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Searches backwards with a predicate and moves the position
public CodeMatcher SearchBackwards(Func<CodeInstruction, bool> predicate)
| Type | Name | Description |
|---|---|---|
| System.Func<CodeInstruction, System.Boolean> | predicate |
A function to test each instruction for a match
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Searches forward with a predicate and advances position
public CodeMatcher SearchForward(Func<CodeInstruction, bool> predicate)
| Type | Name | Description |
|---|---|---|
| System.Func<CodeInstruction, System.Boolean> | predicate |
A function to test each instruction for a match
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets opcode and operand at current position
public CodeMatcher Set(OpCode opcode, object operand)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The opcode
| | System.Object | operand |
The operand
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets opcode and operand at current position and advances
public CodeMatcher SetAndAdvance(OpCode opcode, object operand)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The opcode
| | System.Object | operand |
The operand
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets an instruction at current position
public CodeMatcher SetInstruction(CodeInstruction instruction)
| Type | Name | Description |
|---|---|---|
| CodeInstruction | instruction |
The instruction to set
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets instruction at current position and advances
public CodeMatcher SetInstructionAndAdvance(CodeInstruction instruction)
| Type | Name | Description |
|---|---|---|
| CodeInstruction | instruction |
The instruction
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets jump to
public CodeMatcher SetJumpTo(OpCode opcode, int destination, out Label label)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
Branch instruction
| | System.Int32 | destination |
Destination for the jump
| | System.Reflection.Emit.Label | label |
[out] The created label
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets opcode at current position and advances
public CodeMatcher SetOpcodeAndAdvance(OpCode opcode)
| Type | Name | Description |
|---|---|---|
| System.Reflection.Emit.OpCode | opcode |
The opcode
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Sets operand at current position and advances
public CodeMatcher SetOperandAndAdvance(object operand)
| Type | Name | Description |
|---|---|---|
| System.Object | operand |
The operand
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Moves the current position to the start
public CodeMatcher Start()
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed), or if the check function returns false
public CodeMatcher ThrowIfFalse(string explanation, Func<CodeMatcher, bool> stateCheckFunc)
| Type | Name | Description |
|---|---|---|
| System.String | explanation |
Explanation of where/why the exception was thrown that will be added to the exception message
| | System.Func<CodeMatcher, System.Boolean> | stateCheckFunc |
Function that checks validity of current state. If it returns false, an exception is thrown
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
Throw an InvalidOperationException if current state is invalid (position out of bounds / last match failed)
public CodeMatcher ThrowIfInvalid(string explanation)
| Type | Name | Description |
|---|---|---|
| System.String | explanation |
Explanation of where/why the exception was thrown that will be added to the exception message
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
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
public CodeMatcher ThrowIfNotMatch(string explanation, params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| System.String | explanation |
Explanation of where/why the exception was thrown that will be added to the exception message
| | CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
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
public CodeMatcher ThrowIfNotMatchBack(string explanation, params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| System.String | explanation |
Explanation of where/why the exception was thrown that will be added to the exception message
| | CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|
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
public CodeMatcher ThrowIfNotMatchForward(string explanation, params CodeMatch[] matches)
| Type | Name | Description |
|---|---|---|
| System.String | explanation |
Explanation of where/why the exception was thrown that will be added to the exception message
| | CodeMatch[] | matches |
Some code matches
|
| Type | Description |
|---|---|
| CodeMatcher |
The same code matcher
|