Utilities

utils/virtual.h

Virtual method table (VMT) utilities for calling virtual functions.

Provides low-level helpers for:

  • Retrieving virtual functions from objects
  • Calling virtual methods by index
This is unsafe low-level functionality. Incorrect usage may lead to crashes or undefined behavior.

Header source2mod/utils/virtual.h

Functions

GetVMethod

template <typename T = void*> inline T GetVMethod(uint32 uIndex, void* pClass);

Retrieves a virtual function pointer from an object's VTable.

No bounds checking is performed.
ParameterTypeDescription
uIndexuint32Index in virtual table
pClassvoid*Pointer to class instance

Returns T — Function pointer or nullptr if invalid

CallVirtual

template <typename T, typename... Args> inline T CallVirtual(uint32 uIndex, void* pClass, Args... args);

Calls a virtual function by index.

int result = vmt::CallVirtual<int>(10, entity, arg1);
Calling convention differs between platforms: - Windows: __thiscall - Linux: __cdecl
Index must match correct function signature.
ParameterTypeDescription
uIndexuint32Virtual table index
pClassvoid*Pointer to class instance
argsArgs...Arguments passed to function

Returns T — Function result or default value if failed

CallVirtualOverrideVTable

template <typename T = void, typename... Args> inline T CallVirtualOverrideVTable(uint32 uIndex, void** pVTable, void* pClass, Args... args);

Calls a virtual function through a caller-supplied vtable.

Same as CallVirtual, except the table is passed in rather than read out of the object. Needed when the instance does not carry the vtable you want to dispatch through -- e.g. a trace filter whose table was resolved from the server module by name.

ParameterTypeDescription
uIndexuint32Virtual table index
pVTablevoid**The vtable to dispatch through
pClassvoid*Pointer passed as the implicit this
argsArgs...Arguments passed to the function

Returns T — Function result, or a default-constructed T if the table or entry is null.

CallVirtualChain

template <typename T, typename... Args> inline T CallVirtualChain(uint32 uIndex, HookChain eChain, void* pClass, Args... args);

Calls a virtual function by index, running or bypassing the hooks on it as eChain says (see HookChain in ISource2ModTypes.h).

ParameterType
uIndexuint32
eChainHookChain
pClassvoid*
argsArgs...

Returns T — Function result, or a default-constructed T if the slot is null.

Macros

CALL_VIRTUAL

#define CALL_VIRTUAL(retType, idx, ...) vmt::CallVirtual<retType>(idx, __VA_ARGS__)

Convenience macro for calling virtual functions.

CALL_VIRTUAL(void, 5, entity, arg1, arg2);

CALL_VIRTUAL_CHAIN

#define CALL_VIRTUAL_CHAIN(retType, idx, eChain, ...) vmt::CallVirtualChain<retType>(idx, eChain, __VA_ARGS__)

CALL_VIRTUAL that runs or bypasses the hooks on the function.

CALL_VIRTUAL_CHAIN(void, offset, HookChain::Bypass, this, bExplode, bForce);

CALL_VIRTUAL_OVERRIDE_VTBL

#define CALL_VIRTUAL_OVERRIDE_VTBL(retType, idx, vtable, ...) vmt::CallVirtualOverrideVTable<retType>(idx, vtable, __VA_ARGS__)

Calls a virtual function through a vtable the caller supplies.

For objects whose vtable is not the one stored in the instance -- a filter whose table was looked up in the server module by name, say -- where CALL_VIRTUAL would dispatch through the wrong table.

CALL_VIRTUAL_OVERRIDE_VTBL(bool, offset, pVTable, this, pEntity);

On this page