Table of Contents

Interface IFunctionHooks

Namespace
SkyrimForge.Hooks
Assembly
SkyrimForge.dll

Hooks a game function at a virtual method table entry, a call instruction, or the function's entry.

public interface IFunctionHooks

Remarks

A replacement is the address of a static method marked UnmanagedCallersOnly whose parameters match the game function's. For a member function, mark it [UnmanagedCallersOnly(CallConvs = [typeof(CallConvThiscall)])], take the object as the first parameter, and call the displaced address as a delegate* unmanaged[Thiscall]. Declare a pointer to a game object as a view type, and a C++ bool as byte.

A replacement runs on the calling thread, which can be any thread, so queue work for the game's thread through ITaskQueue. An exception that reaches game code ends the process. Install a hook while no thread runs the code it redirects, as during a plugin's Start for most functions.

A hook remains until the process exits. A hook is chained to an earlier hook at the same table entry, call, or function entry, to a call patch at a call, and to a jump patch or an instruction hook at a function's entry: calling the displaced address runs the earlier one, so the hook installed last runs first. Any other overlap with a recorded hook or patch throws. SkyrimForge.Runtime.log records each hook and its owner. Once the game's data has loaded, it also records each hook another module has overwritten.

Methods

DetourFunction(GameFunction, nint)

Detours the entry of function, so every call to it reaches replacement, and returns the address of a trampoline, which the replacement calls to reach the game's own code.

nint DetourFunction(GameFunction function, nint replacement)

Parameters

function GameFunction
replacement nint

Returns

nint

Remarks

The instructions the jump overwrites are moved to the trampoline. A jump an earlier hook wrote at the entry is moved the same way, so the trampoline then jumps to that hook.

Exceptions

ArgumentOutOfRangeException

replacement is 0.

InvalidOperationException

The address library has no identifier for the function, its entry is not in the executable code of a module SkyrimForge patches, its opening instructions overlap a recorded hook or patch other than a jump at the entry or contain the target of a recorded jump or call past their first byte, or the detour could not be written, for a reason the message states.

FindCallSite(GameFunction, GameFunction, int, int)

Returns the address of a call from caller to callee in the first searchLength bytes of the caller, where occurrence is the zero-based index among those calls.

nint FindCallSite(GameFunction caller, GameFunction callee, int occurrence = 0, int searchLength = 512)

Parameters

caller GameFunction
callee GameFunction
occurrence int
searchLength int

Returns

nint

Remarks

A call whose target is already replaced is not found. FindCallSite(GameFunction, GameOffset) finds it.

Exceptions

ArgumentOutOfRangeException

occurrence is negative, or searchLength is less than 1 or greater than 0x2000.

InvalidOperationException

The address library has no identifier for the caller or for the callee, or the searched bytes contain no such call.

FindCallSite(GameFunction, GameOffset)

Returns the address of the call instruction offset bytes from the entry of caller.

nint FindCallSite(GameFunction caller, GameOffset offset)

Parameters

caller GameFunction
offset GameOffset

Returns

nint

Remarks

An offset is valid for one game build only. A call already replaced, through this service or through SKSE's trampoline, is found as well.

Exceptions

InvalidOperationException

The address library has no identifier for the caller, or the offset does not address a call to the game's code or to an absolute jump stub, as SKSE's trampoline and this service write.

ReplaceCallTarget(nint, nint)

Replaces the target of the call instruction at callSite with replacement and returns the target it displaced.

nint ReplaceCallTarget(nint callSite, nint replacement)

Parameters

callSite nint
replacement nint

Returns

nint

Remarks

A call an earlier hook replaced, through this service, a call patch, or SKSE's trampoline in another SKSE plugin, can be replaced again. Calling the displaced target then runs that hook's replacement.

Exceptions

ArgumentOutOfRangeException

callSite or replacement is 0.

InvalidOperationException

The address is not a call instruction in the executable code of a module SkyrimForge patches, the call overlaps a recorded hook or patch at another address, no earlier hook replaced the call and its target is neither code in the same section nor an absolute jump stub, or no memory within reach of the call could be allocated.

ReplaceVirtualTableEntry(string, int, nint)

Replaces one entry of a game type's virtual method table and returns the entry it displaced.

nint ReplaceVirtualTableEntry(string typeName, int index, nint replacement)

Parameters

typeName string

The game type's CommonLib name, which a view's summary states after RE::: "TESNPC" for ActorBase.

index int

The entry's index in the game's table.

replacement nint

The replacement's address, subject to the remarks on IFunctionHooks.

Returns

nint

Remarks

Only calls through this type's table, on objects of exactly this type, reach the replacement. An index of the wrong entry replaces that entry without an error.

Exceptions

ArgumentNullException

typeName is null.

ArgumentOutOfRangeException

index is negative or greater than 0x1000, or replacement is 0.

InvalidOperationException

No table of the name has an address in the running game version, or the entry neither addresses code in a loaded module nor was replaced by an earlier hook, as when the index lies past the table's end.