Class InsertionPatch
- Namespace
- SkyrimForge.Hooks
- Assembly
- SkyrimForge.dll
Inserts machine code at an address. The expected bytes become a jump to allocated memory, which runs the code and the displaced instructions, then jumps back.
public sealed class InsertionPatch : CodePatch
- Inheritance
-
InsertionPatch
- Inherited Members
Remarks
The expected bytes past the displaced ones are not run. The expected bytes and the displaced bytes each end on an instruction, and no branch elsewhere in the function lands inside the expected bytes.
No RIP-relative operand or relative branch in the code refers outside it. The code preserves every register, flag, and stack location the game reads afterward, and execution leaves it past its last byte, not through a return.
Apply(params ReadOnlySpan<CodePatch>) throws for a displaced instruction with an operand relative to its own address, except displaced bytes that form a single five-byte call. Allocated memory has no unwind data, so an exception or a stack walk passing through a frame there is not unwound correctly.
Constructors
InsertionPatch(nint, BytePattern, ReadOnlySpan<byte>, int, DisplacedPlacement)
Creates a patch inserting code at address.
public InsertionPatch(nint address, BytePattern expected, ReadOnlySpan<byte> code, int displacedLength, DisplacedPlacement placement)
Parameters
addressnintThe address of the first expected byte.
expectedBytePatternThe bytes the code at
addressmust match: the whole instructions replaced, at least five bytes.codeReadOnlySpan<byte>The machine code inserted.
displacedLengthintHow many of the expected bytes, from the first, run beside the code: 0 for none.
placementDisplacedPlacementWhether the displaced instructions run before or after the code.
Exceptions
- ArgumentOutOfRangeException
addressis 0,expectedis shorter than five bytes,codeis empty,displacedLengthis negative or greater than the length ofexpected, orplacementis not a defined value.- ArgumentNullException
expectedisnull.
Properties
Code
The machine code inserted.
public ReadOnlyMemory<byte> Code { get; }
Property Value
DisplacedLength
How many of the expected bytes, from the first, run beside the code.
public int DisplacedLength { get; }
Property Value
Placement
Whether the displaced instructions run before or after the code.
public DisplacedPlacement Placement { get; }