Table of Contents

Interface ICodePatcher

Namespace
SkyrimForge.Hooks
Assembly
SkyrimForge.dll

Changes the machine code of the game, or of a native SKSE plugin, if it matches the bytes each patch expects.

public interface ICodePatcher

Remarks

A patch remains until the process exits. Apply a patch while no thread runs the bytes it overwrites or is inside a call made from them, as during a plugin's Start for most code. A patch overlapping a recorded patch or hook throws. An InstructionHook at the address of an earlier instruction hook shares that hook's jump instead. A branch into a patch through a jump table or a register is not detected. SkyrimForge.Runtime.log records each patch and its owner. Once the game's data has loaded, it also records each patch another module has overwritten.

Methods

AllocateCode(nint, int)

Allocates length bytes of executable memory, all within a 32-bit displacement of near, for machine code a JumpPatch or a CallPatch reaches, and returns the address of the first byte.

nint AllocateCode(nint near, int length)

Parameters

near nint
length int

Returns

nint

Remarks

The memory is never released. Code placed in it has no unwind data, so an exception or a stack walk passing through a frame there is not unwound correctly.

Exceptions

ArgumentOutOfRangeException

near is 0, or length is not positive.

InvalidOperationException

No free memory lies near enough to near.

Apply(params ReadOnlySpan<CodePatch>)

Applies every patch, or none if one cannot be applied.

void Apply(params ReadOnlySpan<CodePatch> patches)

Parameters

patches ReadOnlySpan<CodePatch>

Exceptions

ArgumentNullException

A patch is null.

ArgumentException

The expected bytes of two patches overlap. The target of a JumpPatch or a CallPatch lies inside the expected bytes of a patch past their first byte.

InvalidOperationException

A patch's expected bytes lie outside one executable section of a loaded module, lie in a module of Windows or the .NET runtime, in a managed assembly, or in SkyrimForge.Runtime.dll, do not begin an instruction, do not match the code, overlap a recorded patch or hook, or contain the target of a recorded jump or call past their first byte. The code's memory protection could not be changed. A patch's expected bytes or its target break one of the rules in the remarks on its type, no memory within reach could be allocated, or an instruction hook could not be written. The message states the reason, and states any earlier instruction hook of the group that could not be removed.