Back to Harmony

Class CodeInstructionExtensions

docs/api/HarmonyLib.CodeInstructionExtensions.html

2.4.2.020.0 KB
Original Source

Class CodeInstructionExtensions

Extensions for CodeInstruction

Inheritance

System.Object

CodeInstructionExtensions

Namespace : HarmonyLib
Assembly : 0Harmony.dll
Syntax
public static class CodeInstructionExtensions

Methods

ArgumentIndex(CodeInstruction)

Returns the index targeted by this ldarg, ldarga, or starg

Declaration
public static int ArgumentIndex(this CodeInstruction code)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

|

Returns
TypeDescription
System.Int32

The index it targets

|

See Also

LoadArgument(System.Int32, System.Boolean)

StoreArgument(System.Int32)

Branches(CodeInstruction, out Nullable<Label>)

Tests if the code instruction branches

Declaration
public static bool Branches(this CodeInstruction code, out Label? label)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Nullable\ | label |

The label if the instruction is a branch operation or null if not

|

Returns
TypeDescription
System.Boolean

True if the instruction branches

|

Calls(CodeInstruction, MethodInfo)

Tests if the code instruction calls the method/constructor

Declaration
public static bool Calls(this CodeInstruction code, MethodInfo method)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.MethodInfo | method |

The method

|

Returns
TypeDescription
System.Boolean

True if the instruction calls the method or constructor

|

ExtractBlocks(CodeInstruction)

Extracts all ExceptionBlocks from the code instruction and returns them

Declaration
public static List<ExceptionBlock> ExtractBlocks(this CodeInstruction code)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

|

Returns
TypeDescription
System.Collections.Generic.List<ExceptionBlock>

A list of ExceptionBlock

|

ExtractLabels(CodeInstruction)

Extracts all labels from the code instruction and returns them

Declaration
public static List<Label> ExtractLabels(this CodeInstruction code)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

|

Returns
TypeDescription
System.Collections.Generic.List\

A list of System.Reflection.Emit.Label

|

Is(CodeInstruction, OpCode, Object)

Shortcut for

code.opcode == opcode && code.OperandIs(operand)
Declaration
public static bool Is(this CodeInstruction code, OpCode opcode, object operand)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.Emit.OpCode | opcode |

The System.Reflection.Emit.OpCode

| | System.Object | operand |

The operand value

|

Returns
TypeDescription
System.Boolean

True if the opcode is equal to the given opcode and the operand has the same type and is equal to the given operand

|

Is(CodeInstruction, OpCode, MemberInfo)

Shortcut for

code.opcode == opcode && code.OperandIs(operand)
Declaration
public static bool Is(this CodeInstruction code, OpCode opcode, MemberInfo operand)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.Emit.OpCode | opcode |

The System.Reflection.Emit.OpCode

| | System.Reflection.MemberInfo | operand |

The System.Reflection.MemberInfo operand value

|

Returns
TypeDescription
System.Boolean

True if the opcode is equal to the given opcode and the operand is equal to the given operand

|

Remarks

This is an optimized version of Is(CodeInstruction, OpCode, Object) for System.Reflection.MemberInfo

IsLdarg(CodeInstruction, Nullable<Int32>)

Tests for any form of Ldarg*

Declaration
public static bool IsLdarg(this CodeInstruction code, int? n = default(int? ))
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Nullable\ | n |

The (optional) index

|

Returns
TypeDescription
System.Boolean

True if it matches one of the variations

|

IsLdarga(CodeInstruction, Nullable<Int32>)

Tests for Ldarga/Ldarga_S

Declaration
public static bool IsLdarga(this CodeInstruction code, int? n = default(int? ))
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Nullable\ | n |

The (optional) index

|

Returns
TypeDescription
System.Boolean

True if it matches one of the variations

|

IsLdloc(CodeInstruction, LocalBuilder)

Tests for any form of Ldloc*

Declaration
public static bool IsLdloc(this CodeInstruction code, LocalBuilder variable = null)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.Emit.LocalBuilder | variable |

The optional local variable

|

Returns
TypeDescription
System.Boolean

True if it matches one of the variations

|

IsStarg(CodeInstruction, Nullable<Int32>)

Tests for Starg/Starg_S

Declaration
public static bool IsStarg(this CodeInstruction code, int? n = default(int? ))
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Nullable\ | n |

The (optional) index

|

Returns
TypeDescription
System.Boolean

True if it matches one of the variations

|

IsStloc(CodeInstruction, LocalBuilder)

Tests for any form of Stloc*

Declaration
public static bool IsStloc(this CodeInstruction code, LocalBuilder variable = null)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.Emit.LocalBuilder | variable |

The optional local variable

|

Returns
TypeDescription
System.Boolean

True if it matches one of the variations

|

IsValid(OpCode)

Returns if an System.Reflection.Emit.OpCode is initialized and valid

Declaration
public static bool IsValid(this OpCode code)
Parameters
TypeNameDescription
System.Reflection.Emit.OpCodecode

The System.Reflection.Emit.OpCode

|

Returns
TypeDescription
System.Boolean

LoadsConstant(CodeInstruction)

Tests if the code instruction loads a constant

Declaration
public static bool LoadsConstant(this CodeInstruction code)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

|

Returns
TypeDescription
System.Boolean

True if the instruction loads a constant

|

LoadsConstant(CodeInstruction, Double)

Tests if the code instruction loads a floating point constant

Declaration
public static bool LoadsConstant(this CodeInstruction code, double number)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Double | number |

The floating point constant

|

Returns
TypeDescription
System.Boolean

True if the instruction loads the constant

|

LoadsConstant(CodeInstruction, Enum)

Tests if the code instruction loads an enum constant

Declaration
public static bool LoadsConstant(this CodeInstruction code, Enum e)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Enum | e |

The enum

|

Returns
TypeDescription
System.Boolean

True if the instruction loads the constant

|

LoadsConstant(CodeInstruction, Int64)

Tests if the code instruction loads an integer constant

Declaration
public static bool LoadsConstant(this CodeInstruction code, long number)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Int64 | number |

The integer constant

|

Returns
TypeDescription
System.Boolean

True if the instruction loads the constant

|

LoadsConstant(CodeInstruction, String)

Tests if the code instruction loads a string constant

Declaration
public static bool LoadsConstant(this CodeInstruction code, string str)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.String | str |

The string

|

Returns
TypeDescription
System.Boolean

True if the instruction loads the constant

|

LoadsField(CodeInstruction, FieldInfo, Boolean)

Tests if the code instruction loads a field

Declaration
public static bool LoadsField(this CodeInstruction code, FieldInfo field, bool byAddress = false)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.FieldInfo | field |

The field

| | System.Boolean | byAddress |

Set to true if the address of the field is loaded

|

Returns
TypeDescription
System.Boolean

True if the instruction loads the field

|

LocalIndex(CodeInstruction)

Returns the index targeted by this ldloc, ldloca, or stloc

Declaration
public static int LocalIndex(this CodeInstruction code)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

|

Returns
TypeDescription
System.Int32

The index it targets

|

See Also

LoadLocal(System.Int32, System.Boolean)

StoreLocal(System.Int32)

MoveBlocksFrom(CodeInstruction, CodeInstruction)

Moves all ExceptionBlocks from another code instruction to the current one

Declaration
public static CodeInstruction MoveBlocksFrom(this CodeInstruction code, CodeInstruction other)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction to move the ExceptionBlocks to

| | CodeInstruction | other |

The other CodeInstruction to move the ExceptionBlocks from

|

Returns
TypeDescription
CodeInstruction

The code instruction that received the blocks

|

MoveBlocksTo(CodeInstruction, CodeInstruction)

Moves all ExceptionBlocks from the code instruction to another one

Declaration
public static CodeInstruction MoveBlocksTo(this CodeInstruction code, CodeInstruction other)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction to move the ExceptionBlocks from

| | CodeInstruction | other |

The other CodeInstruction to move the ExceptionBlocks to

|

Returns
TypeDescription
CodeInstruction

The code instruction blocks were moved from (now empty)

|

MoveLabelsFrom(CodeInstruction, CodeInstruction)

Moves all labels from another code instruction to the current one

Declaration
public static CodeInstruction MoveLabelsFrom(this CodeInstruction code, CodeInstruction other)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction to move the labels to

| | CodeInstruction | other |

The other CodeInstruction to move the labels from

|

Returns
TypeDescription
CodeInstruction

The code instruction that received the labels

|

MoveLabelsTo(CodeInstruction, CodeInstruction)

Moves all labels from the code instruction to another one

Declaration
public static CodeInstruction MoveLabelsTo(this CodeInstruction code, CodeInstruction other)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction to move the labels from

| | CodeInstruction | other |

The other CodeInstruction to move the labels to

|

Returns
TypeDescription
CodeInstruction

The code instruction labels were moved from (now empty)

|

OperandIs(CodeInstruction, Object)

Shortcut for testing whether the operand is equal to a non-null value

Declaration
public static bool OperandIs(this CodeInstruction code, object value)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Object | value |

The value

|

Returns
TypeDescription
System.Boolean

True if the operand has the same type and is equal to the value

|

OperandIs(CodeInstruction, MemberInfo)

Shortcut for testing whether the operand is equal to a non-null value

Declaration
public static bool OperandIs(this CodeInstruction code, MemberInfo value)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.MemberInfo | value |

The System.Reflection.MemberInfo value

|

Returns
TypeDescription
System.Boolean

True if the operand is equal to the value

|

Remarks

This is an optimized version of OperandIs(CodeInstruction, Object) for System.Reflection.MemberInfo

StoresField(CodeInstruction, FieldInfo)

Tests if the code instruction stores a field

Declaration
public static bool StoresField(this CodeInstruction code, FieldInfo field)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.FieldInfo | field |

The field

|

Returns
TypeDescription
System.Boolean

True if the instruction stores this field

|

WithBlocks(CodeInstruction, ExceptionBlock[])

Adds ExceptionBlocks to the code instruction and return it

Declaration
public static CodeInstruction WithBlocks(this CodeInstruction code, params ExceptionBlock[] blocks)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | ExceptionBlock[] | blocks |

One or several ExceptionBlock to add

|

Returns
TypeDescription
CodeInstruction

The same code instruction

|

WithBlocks(CodeInstruction, IEnumerable<ExceptionBlock>)

Adds ExceptionBlocks to the code instruction and return it

Declaration
public static CodeInstruction WithBlocks(this CodeInstruction code, IEnumerable<ExceptionBlock> blocks)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Collections.Generic.IEnumerable<ExceptionBlock> | blocks |

An enumeration of ExceptionBlock

|

Returns
TypeDescription
CodeInstruction

The same code instruction

|

WithLabels(CodeInstruction, IEnumerable<Label>)

Adds labels to the code instruction and return it

Declaration
public static CodeInstruction WithLabels(this CodeInstruction code, IEnumerable<Label> labels)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Collections.Generic.IEnumerable\ | labels |

An enumeration of System.Reflection.Emit.Label

|

Returns
TypeDescription
CodeInstruction

The same code instruction

|

WithLabels(CodeInstruction, Label[])

Adds labels to the code instruction and return it

Declaration
public static CodeInstruction WithLabels(this CodeInstruction code, params Label[] labels)
Parameters
TypeNameDescription
CodeInstructioncode

The CodeInstruction

| | System.Reflection.Emit.Label[] | labels |

One or several System.Reflection.Emit.Label to add

|

Returns
TypeDescription
CodeInstruction

The same code instruction

|