VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
EntitySystem.cpp
Go to the documentation of this file.
2
7#include <entity2/entityidentity.h>
8#include <entity2/entityinstance.h>
9#include <entity2/entitysystem.h>
10#include <entityhandle.h>
11#include <gameentitysystem.h>
12#include <interfaces/interfaces.h>
13#include <string>
14#include <string_view>
15#include <vector>
16
17namespace VoltMod
18{
19
20static constexpr std::string_view ControllerClass = "cs_player_controller";
21
22static_assert(MaxPlayers == ABSOLUTE_PLAYER_LIMIT);
23
25 : _interfaces(interfaces), _bindings(bindings)
26{
27 if (_interfaces.GameResourceService)
28 {
29 Raw();
30 }
31}
32
33CGameEntitySystem* EntitySystem::ReadEntitySystemPointer()
34{
35 // Retry until the pointer resolves; keep this path a plain read.
36 if (!_interfaces.GameResourceService || _isRealSystem == false)
37 {
38 return nullptr;
39 }
40
41 CGameEntitySystem* system = ::GameEntitySystem();
42 // A game update can move the field before the SDK follows; check the vtable before using it.
43 if (system && !_isRealSystem)
44 {
45 const void* table = FindVirtualTable("server", "CGameEntitySystem");
46 _isRealSystem = !table || HasVtable(system, table);
47 if (!*_isRealSystem)
48 {
50 "The resource service's entity system field is not a CGameEntitySystem; entity lookups are off.");
51 return nullptr;
52 }
53 }
54 return system;
55}
56
58{
59 // Each map creates a new system, so discard the old pointer before it is freed.
60 _interfaces.EntitySystem = nullptr;
61
62 if (Raw())
63 {
64 Log::Info("Entity system initialized.");
65 }
66 else
67 {
68 Log::Error("Entity system pointer could not be read from IGameResourceService.");
69 }
70}
71
72CGameEntitySystem* EntitySystem::Raw()
73{
74 // The cache stays empty until the first server starts and is cleared for each new map.
75 if (!_interfaces.EntitySystem)
76 {
77 _interfaces.EntitySystem = ReadEntitySystemPointer();
78 }
79
80 return _interfaces.EntitySystem;
81}
82
84{
85 auto* system = Raw();
86 if (!ref || !system)
87 {
88 return {};
89 }
90
91 // The identity's serial must match, since a destroyed entity leaves a dangling instance pointer.
92 return {*this, system->GetEntityInstance(CEntityHandle(ref.Handle))};
93}
94
95CEntityInstance* EntitySystem::RawController(int slot)
96{
97 auto* system = Raw();
98 if (!system || slot < 0 || slot >= MaxPlayers)
99 {
100 return nullptr;
101 }
102
103 // Controllers occupy indices 1..MaxPlayers; index 0 is worldspawn.
104 CEntityIdentity* identity = system->GetEntityIdentity(CEntityIndex(slot + 1));
105 if (!identity)
106 {
107 return nullptr;
108 }
109
110 // An index past the server's player limit holds an ordinary entity.
111 if (ControllerClass != identity->GetClassname())
112 {
113 return nullptr;
114 }
115 return identity->m_pInstance;
116}
117
119{
120 return {*this, RawController(slot), slot};
121}
122
124{
125 return Controller(slot).Pawn();
126}
127
129{
130 return Get(ref).AsPawn();
131}
132
133Entity EntitySystem::Find(std::string_view className)
134{
135 if (!Raw() || className.empty())
136 {
137 return {};
138 }
139
140 // Walk the active list: the SDK's CEntityClass layout is stale, so its per-class chain reads garbage.
141 const std::string name(className);
142 EntityInstanceByClassIter_t iter(nullptr, name.c_str());
143 return {*this, iter.Next()};
144}
145
146std::vector<Entity> EntitySystem::FindAll(std::string_view className)
147{
148 std::vector<Entity> found;
149 if (!Raw() || className.empty())
150 {
151 return found;
152 }
153
154 const std::string name(className);
155 EntityInstanceByClassIter_t iter(nullptr, name.c_str());
156 for (CEntityInstance* entity = iter.Next(); entity; entity = iter.Next())
157 {
158 found.emplace_back(*this, entity);
159 }
160 return found;
161}
162
163} // namespace VoltMod
A player's identity behind a slot: name, money, team, scoreboard row. It survives death and respawn; ...
VoltMod::Pawn Pawn() const
std::vector< Entity > FindAll(std::string_view className)
Entity Find(std::string_view className)
VoltMod::Controller Controller(int slot)
VoltMod::Pawn Pawn(int slot)
CGameEntitySystem * Raw()
Entity Get(EntityRef ref)
EntitySystem(VoltMod::Interfaces &interfaces, const VoltMod::Bindings &bindings)
A live entity, valid for this frame only.
Definition Entity.hpp:40
Pawn AsPawn() const
Definition Pawn.cpp:243
A player's body: health, armor, movement, aim, weapons. The engine replaces it on every spawn; the Co...
Definition Pawn.hpp:35
void Error(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:97
void Info(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:79
static constexpr std::string_view ControllerClass
constexpr int MaxPlayers
Definition Slot.hpp:7
void * FindVirtualTable(std::string_view moduleName, std::string_view className)
bool HasVtable(const void *object, const void *table)
Centralized holder for all SDK interface pointers.
IGameResourceService * GameResourceService