Class InstructionHook
- Namespace
- SkyrimForge.Hooks
- Assembly
- SkyrimForge.dll
Runs a handler on each thread that reaches the instruction at Address, before that instruction runs.
public sealed class InstructionHook : CodePatch
- Inheritance
-
InstructionHook
- Inherited Members
Remarks
The thread then runs the instruction with every register as the handler left it. A handler that sets Rip continues there instead. The handler runs on any thread that reaches the instruction. An exception escaping it is written to the plugin's log, and the process then ends.
The hook moves the instructions spanning its first five bytes to a trampoline, or its first fourteen when no trampoline lies within a 32-bit displacement. The expected bytes must cover them. Apply(params ReadOnlySpan<CodePatch>) throws if a moved call is followed by another moved instruction, if a moved branch targets a moved byte, or if a direct branch elsewhere in the function lands inside the moved bytes past their first byte. A moved call runs without unwind data, so an exception passing through it is not unwound correctly.
A hook remains until the process exits. Instruction hooks exist at no more than 1024 addresses, and Apply(params ReadOnlySpan<CodePatch>) throws past that number. Hooks at one address share one jump, and their handlers run newest first. A handler that sets Rip to another address stops the earlier ones. A later hook's expected bytes are compared with the code as it was before the first hook. A hook overlapping a hook or patch at another address throws, as does one where DetourFunction(GameFunction, nint) detoured a function after the first hook there.
Constructors
InstructionHook(nint, BytePattern, InstructionHandler)
Creates a hook running handler before the instruction at address.
public InstructionHook(nint address, BytePattern expected, InstructionHandler handler)
Parameters
addressnintThe address of the hooked instruction.
expectedBytePatternThe bytes the code at
addressmust match: at least the whole instructions spanning the first five bytes.handlerInstructionHandlerThe handler run before the instruction.
Exceptions
- ArgumentOutOfRangeException
addressis 0, orexpectedis shorter than five bytes.- ArgumentNullException
expectedorhandlerisnull.
Properties
Handler
The handler run before the instruction.
public InstructionHandler Handler { get; }