VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
BindingTypes.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <bit>
4#include <cstdint>
5#include <cstring>
6#include <utility>
7
8namespace VoltMod
9{
10
11/** Opaque address for an ABI declared only in its implementation file. */
13{
14public:
15 Address() = default;
16 explicit Address(void* address) noexcept : _address(address) {}
17
18 explicit operator bool() const noexcept { return _address != nullptr; }
19 void* Ptr() const noexcept { return _address; }
20
21private:
22 void* _address = nullptr;
23};
24
25/** A class vtable found through RTTI. A hook counts its slot from the SDK's declaration. */
27{
28public:
29 ClassTable() = default;
30 explicit ClassTable(void* table) noexcept : _table(table) {}
31
32 explicit operator bool() const noexcept { return _table != nullptr; }
33 void* Ptr() const noexcept { return _table; }
34
35private:
36 void* _table = nullptr;
37};
38
39/** Typed engine function. Calling an unbound function is undefined. */
40template <class Sig>
41class Fn;
42
43template <class Ret, class... Args>
44class Fn<Ret(Args...)>
45{
46public:
47 Fn() = default;
48 explicit Fn(void* address) noexcept : _address(address) {}
49
50 explicit operator bool() const noexcept { return _address != nullptr; }
51 void* Ptr() const noexcept { return _address; }
52
53 Ret operator()(Args... args) const
54 {
55 return std::bit_cast<Ret (*)(Args...)>(_address)(std::forward<Args>(args)...);
56 }
57
58private:
59 void* _address = nullptr;
60};
61
62/** Typed virtual function called through the object's own vtable; the first parameter is the
63 * object. Calling an unbound function is undefined. */
64template <class Sig>
66
67template <class Ret, class Object, class... Args>
68class VirtualFn<Ret(Object*, Args...)>
69{
70public:
71 VirtualFn() = default;
72 VirtualFn(int index, void* table) noexcept : _index(index), _table(table) {}
73
74 explicit operator bool() const noexcept { return _index >= 0 && _table != nullptr; }
75 int Index() const noexcept { return _index; }
76 void* Table() const noexcept { return _table; }
77
78 Ret operator()(Object* object, Args... args) const
79 {
80 auto* vtable = *reinterpret_cast<void***>(object);
81 return std::bit_cast<Ret (*)(Object*, Args...)>(vtable[_index])(object, std::forward<Args>(args)...);
82 }
83
84private:
85 int _index = -1;
86 void* _table = nullptr;
87};
88
89/** Typed byte offset. Reads and writes allow unaligned fields and do nothing while unbound. */
90template <class T>
91class Offset
92{
93public:
94 Offset() = default;
95 explicit constexpr Offset(int value) noexcept : _value(value) {}
96
97 explicit constexpr operator bool() const noexcept { return _value >= 0; }
98 constexpr int Value() const noexcept { return _value; }
99
100 T Read(const void* base) const
101 {
102 T out{};
103 if (_value >= 0 && base)
104 {
105 std::memcpy(&out, static_cast<const uint8_t*>(base) + _value, sizeof(T));
106 }
107 return out;
108 }
109
110 void Write(void* base, const T& value) const
111 {
112 if (_value >= 0 && base)
113 {
114 std::memcpy(static_cast<uint8_t*>(base) + _value, &value, sizeof(T));
115 }
116 }
117
118private:
119 int _value = -1;
120};
121
122/** Byte offset for an embedded type declared only in an implementation file. */
123template <>
124class Offset<void>
125{
126public:
127 Offset() = default;
128 explicit constexpr Offset(int value) noexcept : _value(value) {}
129
130 explicit constexpr operator bool() const noexcept { return _value >= 0; }
131 constexpr int Value() const noexcept { return _value; }
132
133 const void* Ptr(const void* base) const
134 {
135 if (_value < 0 || !base)
136 {
137 return nullptr;
138 }
139 return static_cast<const uint8_t*>(base) + _value;
140 }
141
142private:
143 int _value = -1;
144};
145
146} // namespace VoltMod
Address()=default
void * Ptr() const noexcept
Address(void *address) noexcept
ClassTable(void *table) noexcept
void * Ptr() const noexcept
Ret operator()(Args... args) const
void * Ptr() const noexcept
Fn(void *address) noexcept
constexpr Offset(int value) noexcept
const void * Ptr(const void *base) const
constexpr int Value() const noexcept
constexpr int Value() const noexcept
constexpr Offset(int value) noexcept
T Read(const void *base) const
void Write(void *base, const T &value) const
Offset()=default
VirtualFn(int index, void *table) noexcept
Ret operator()(Object *object, Args... args) const
The argument types a command handler's parameter list is written in.
Definition Args.hpp:57