VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
ScriptBindings.cpp
Go to the documentation of this file.
2
4
5#include <cstdint>
6#include <cstring>
7#include <entity2/entityinstance.h>
8#include <format>
9#include <khook.hpp>
10#include <vscript/ivscript.h>
11
12namespace VoltMod
13{
14
15static const ScriptFunctionBinding_t* FindBinding(const ScriptClassDesc_t* description, std::string_view name)
16{
17 for (; description; description = description->m_pBaseDesc)
18 {
19 const auto& bindings = description->m_FunctionBindings;
20 for (int i = 0; i < bindings.Count(); ++i)
21 {
22 const char* scriptName = bindings[i].m_desc.m_pszScriptName;
23 if (scriptName && name == scriptName)
24 {
25 return &bindings[i];
26 }
27 }
28 }
29 return nullptr;
30}
31
32/** The vtable index @p function dispatches through, or -1 when it is not virtual. */
33static int VirtualIndex(const void* function)
34{
35#ifdef _WIN32
36 // MSVC points a virtual member at a thunk: mov rax, [rcx]; jmp [rax + disp].
37 const auto* code = static_cast<const uint8_t*>(function);
38 if (!IsReadableAddress(code, 9) || code[0] != 0x48 || code[1] != 0x8B || code[2] != 0x01 || code[3] != 0xFF)
39 {
40 return -1;
41 }
42 switch (code[4])
43 {
44 case 0x20:
45 return 0;
46 case 0x60:
47 return static_cast<int8_t>(code[5]) / static_cast<int>(sizeof(void*));
48 case 0xA0:
49 {
50 int32_t displacement = 0;
51 std::memcpy(&displacement, code + 5, sizeof(displacement));
52 return displacement / static_cast<int>(sizeof(void*));
53 }
54 default:
55 return -1;
56 }
57#else
58 // Itanium stores a virtual member as one plus its byte offset in the table.
59 const auto value = reinterpret_cast<uintptr_t>(function);
60 return (value & 1) != 0 ? static_cast<int>((value - 1) / sizeof(void*)) : -1;
61#endif
62}
63
64Result<ScriptTarget> FindScriptBinding(void* classTable, std::string_view name)
65{
66 // Call the slot, not a virtual on a fake object: GCC drops that as undefined. `this` is ignored.
67 using GetScriptDescFn = void* (*)(void* self);
68 const int slot = KHook::GetVtableIndex(&CEntityInstance::GetScriptDesc);
69 if (slot < 0)
70 {
71 return std::unexpected(Error::NotFound("no vtable slot for GetScriptDesc"));
72 }
73 const auto getScriptDesc = reinterpret_cast<GetScriptDescFn>(static_cast<void**>(classTable)[slot]);
74 const auto* description = static_cast<const ScriptClassDesc_t*>(getScriptDesc(&classTable));
75 if (!description)
76 {
77 return std::unexpected(Error::NotFound("the class has no VScript description"));
78 }
79
80 const ScriptFunctionBinding_t* binding = FindBinding(description, name);
81 if (!binding || !binding->m_pFunction)
82 {
83 return std::unexpected(Error::NotFound(std::format("no VScript binding '{}'", name)));
84 }
85
86 const int index = VirtualIndex(binding->m_pFunction);
87 return index >= 0 ? ScriptTarget{.Index = index} : ScriptTarget{.Address = binding->m_pFunction};
88}
89
90} // namespace VoltMod
static int VirtualIndex(const void *function)
bool IsReadableAddress(const void *address, size_t bytes)
Result< ScriptTarget > FindScriptBinding(void *classTable, std::string_view name)
std::expected< T, Error > Result
Definition Result.hpp:64
static const ScriptFunctionBinding_t * FindBinding(const ScriptClassDesc_t *description, std::string_view name)
static Error NotFound(std::string detail)
Definition Result.hpp:49