Schema
Schema system utilities for accessing and modifying entity fields.
Provides:
- Runtime field offset resolution (via SchemaSystem)
- Safe access to entity properties
- Automatic network state updates
- Compile-time hashing for fast lookup
Header source2mod/schema/schema.h
Types
CEntityNameString
typedef void (*CEntityNameString)(void);CGameSoundEventName
using CGameSoundEventName = CGlobalSymbol;RotationVector
using RotationVector = void*;schema_identity_t
template <typename T> using schema_identity_t = typename schema_identity<T>::type;schema_bits_t
template <typename T> using schema_bits_t = typename schema_bits<T>::type;CGlobalSymbol
Get
const char* Get() const;Set
void Set(const char* value);| Parameter | Type |
|---|---|
value | const char* |
SchemaKey
Represents schema offset information.
Fields
| Field | Type | Description |
|---|---|---|
offset | int32 | Offset of the field |
networked | bool | True if field is networked |
manipulator | SchemaCollectionManipulatorFn_t | For an atomic collection field, the engine's own element accessor. |
valid | bool | False when the field is not in the running game's schema (renamed, removed). Reads then come from zeroed scratch memory and writes go there, so the plugin keeps running with that one field doing nothing -- an error line says which, once. |
CStrongHandle
Lightweight wrapper for entity pointers.
Get
T* Get() const;Set
void Set(void* pPtr);| Parameter | Type |
|---|---|
pPtr | void* |
IsValid
bool IsValid() const;CStrongHandleCopyable
Get
T* Get() const;Set
void Set(void* pPtr);| Parameter | Type |
|---|---|
pPtr | void* |
IsValid
bool IsValid() const;CWeakHandle
Get
T* Get() const;Set
void Set(void* pPtr);| Parameter | Type |
|---|---|
pPtr | void* |
IsValid
bool IsValid() const;schema_identity
Puts a parameter in a non-deduced context. * The field setters are templates only so enable_if can pick an overload; T * must stay the field's own type. Deduced from the argument, m_nByte = 1 * made T an int and wrote four bytes over a one-byte field.
type
using type = T;schema_bits
The integer type a bitwise operator computes in: the underlying type of an enum, otherwise T.
type
using type = T;Functions
GetGameEventManager
IGameEventManager2* GetGameEventManager();Global accessors for engine interfaces.
GetGlobalVars
CGlobalVars* GetGlobalVars();GetCVar
ICvar* GetCVar();GetSource2Server
ISource2Server* GetSource2Server();GetEngineServer
IVEngineServer2* GetEngineServer();GetGameEventSystem
IGameEventSystem* GetGameEventSystem();GetNetworkMessages
INetworkMessages* GetNetworkMessages();GetNetworkServerService
INetworkServerService* GetNetworkServerService();GetEntitySystem
CGameEntitySystem* GetEntitySystem();GetSchemaSystem
CSchemaSystem* GetSchemaSystem();EntityNetworkStateChanged
void EntityNetworkStateChanged(uintptr_t pEntity, uint nOffset);| Parameter | Type |
|---|---|
pEntity | uintptr_t |
nOffset | uint |
ChainNetworkStateChanged
void ChainNetworkStateChanged(uintptr_t pNetworkVarChainer, uint nOffset);| Parameter | Type |
|---|---|
pNetworkVarChainer | uintptr_t |
nOffset | uint |
NetworkVarStateChanged
void NetworkVarStateChanged(uintptr_t pNetworkVar, uint32_t nOffset, uint32 nNetworkStateChangedOffset);| Parameter | Type |
|---|---|
pNetworkVar | uintptr_t |
nOffset | uint32_t |
nNetworkStateChangedOffset | uint32 |
FindChainOffset
int16_t FindChainOffset(const char* className, uint32_t classNameHash);
int16_t FindChainOffset(const char* className);
template <typename T> int16_t FindChainOffset();
template <typename T> int16_t FindChainOffset(const T*);Finds chain offset for a class.
| Parameter | Type |
|---|---|
className | const char* |
classNameHash | uint32_t |
— | const T* |
GetOffset
SchemaKey GetOffset(const char* className, uint32_t classKey, const char* memberName, uint32_t memberKey);
template <typename T> SchemaKey GetOffset(const char* memberName);
template <typename T> SchemaKey GetOffset(const T*, const char* memberName);Gets schema offset for a field.
| Parameter | Type |
|---|---|
className | const char* |
classKey | uint32_t |
memberName | const char* |
memberKey | uint32_t |
— | const T* |
MissingFieldStorage
void* MissingFieldStorage(size_t size);Scratch memory a missing field reads from and writes to; zeroed, one block for every such field in the plugin.
| Parameter | Type |
|---|---|
size | size_t |
MissingField
template <typename T> T& MissingField();GetServerOffset
int32_t GetServerOffset(const char* pszClassName, const char* pszPropName);
template <typename T> int32_t GetServerOffset(const char* pszPropName);
template <typename T> int32_t GetServerOffset(const T*, const char* pszPropName);Gets server offset (fallback).
| Parameter | Type |
|---|---|
pszClassName | const char* |
pszPropName | const char* |
— | const T* |
GetClassSize
int32_t GetClassSize(const char* className);
template <typename T> int32_t GetClassSize();
template <typename T> int32_t GetClassSize(const T*);Gets class size.
| Parameter | Type |
|---|---|
className | const char* |
— | const T* |
SetStateChanged
void SetStateChanged(CEntityInstance* entity, const char* className, const char* propName);
template <typename T> void SetStateChanged(T* entity, const char* propName);Marks field as changed (network update).
SetStateChanged(entity, "m_iHealth"), the class taken from the entity's type.
| Parameter | Type |
|---|---|
entity | CEntityInstance* |
className | const char* |
propName | const char* |
entity | T* |
hash_32_fnv1a_const
inline constexpr uint32_t hash_32_fnv1a_const(const char* const str, const uint32_t value = 0x811c9dc5) noexcept;FNV-1a 32-bit hash (constexpr).
| Parameter | Type | Default |
|---|---|---|
str | const char* const | |
value | const uint32_t | 0x811c9dc5 |
hash_64_fnv1a_const
inline constexpr uint64_t hash_64_fnv1a_const(const char* const str, const uint64_t value = 0xcbf29ce484222325) noexcept;FNV-1a 64-bit hash (constexpr).
| Parameter | Type | Default |
|---|---|---|
str | const char* const | |
value | const uint64_t | 0xcbf29ce484222325 |
ClassName
template <typename T> constexpr std::string_view ClassName();The C++ name of T at compile time ("CChicken"); schema class names are the C++ ones.
ClassNameCStr
template <typename T> const char* ClassNameCStr();ClassName<T>() as a null-terminated string.
Macros
SCHEMA_FIELD
#define SCHEMA_FIELD(type, varName) SCHEMA_FIELD_OFFSET(type, varName, 0)Declares schema field (auto offset + network update).
SCHEMA_FIELD_POINTER
#define SCHEMA_FIELD_POINTER(type, varName) SCHEMA_FIELD_POINTER_OFFSET(type, varName, 0)Declares pointer schema field. * Use this when you want a pointer to a member
DECLARE_SCHEMA_CLASS_BASE
#define DECLARE_SCHEMA_CLASS_BASE(ClassName, offset) \
private:
typedef ClassName ThisClass;
static constexpr const char* m_className = #ClassName;
static constexpr uint32_t m_classNameHash = hash_32_fnv1a_const(#ClassName);
static constexpr int m_networkStateChangedOffset = offset;
public:Declares schema-enabled class. * offset != 0 marks a non-entity (embedded) class; the value itself is not used any more
DECLARE_SCHEMA_CLASS_INLINE
#define DECLARE_SCHEMA_CLASS_INLINE(className) DECLARE_SCHEMA_CLASS_BASE(className, 1)Declares inline schema class (non-entity). * Use this for non-entity classes such as CCollisionProperty or CGlowProperty * Their NetworkStateChanged is a virtual of the NetworkVar_<field> wrapper the engine embeds them in, not CEntityInstance's; * its vtable slot is found at runtime (NetworkVarStateChanged), so no index is kept per class * Though some classes like CGameRules will instead use their CNetworkVarChainer as a link back to the parent entity
Other macros
| Macro | Expands to |
|---|---|
SCHEMA_FIELD_OFFSET(type, varName, extra_offset) | class varName##_prop { public: /*The deleted copy ctor below is user-declared, which suppresses the*/ /*imp... |
SCHEMA_FIELD_POINTER_OFFSET(type, varName, extra_offset) | class varName##_prop { public: /*The deleted copy ctor below is user-declared, which suppresses the*/ /*imp... |
SCHEMA_FIELD_OLD(type, className, propName) | std::add_lvalue_reference_t<type> propName() { static const int32_t offset = [] { const int32_t nOffset = s... |
DECLARE_SCHEMA_CLASS(className) | DECLARE_SCHEMA_CLASS_BASE(className, 0) |