VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
PluginContext.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <format>
5#include <utility>
6
7namespace VoltMod
8{
9
10PluginContext::PluginContext(SharedState& state, std::string name, std::string logTag, std::string version,
11 uint64_t order)
12 : _state(state), _name(std::move(name)), _logTag(std::move(logTag)), _version(std::move(version)), _order(order)
13{}
14
15std::string_view PluginContext::Name() const
16{
17 return _name;
18}
19
20std::string_view PluginContext::Version() const
21{
22 return _version;
23}
24
25void* PluginContext::EngineInterface(const char* version) const
26{
27 return _state.Start.EngineFactory(version, nullptr);
28}
29
30void* PluginContext::ServerInterface(const char* version) const
31{
32 return _state.Start.ServerFactory(version, nullptr);
33}
34
35std::string_view PluginContext::BaseDir() const
36{
37 return _state.Start.GameDir;
38}
39
40KHook::IKHook* PluginContext::HookDispatcher() const
41{
42 return _state.Start.HookDispatcher;
43}
44
46{
47 return *this;
48}
49
51{
52 return *this;
53}
54
56{
57 return _state.GameData;
58}
59
60bool PluginContext::RegisterCommand(std::string_view name)
61{
62 if (_state.Commands.Register(this, _name, name))
63 {
64 return true;
65 }
66
67 Log::Error("Command '{}' is already registered by {}, so {} cannot have it.", name, _state.Commands.OwnerName(name),
68 _name);
69 return false;
70}
71
72bool PluginContext::IsCommandRegistered(std::string_view name) const
73{
74 return !_state.Commands.OwnerName(name).empty();
75}
76
78{
79 return _state.Languages;
80}
81
83{
84 return *this;
85}
86
87bool PluginContext::Add(uint64_t addonId)
88{
89 if (!_state.Addons || !_state.Addons->Add(addonId))
90 {
91 return false;
92 }
93 _addons.push_back(addonId);
94 return true;
95}
96
97void PluginContext::Remove(uint64_t addonId)
98{
99 const auto held = std::ranges::find(_addons, addonId);
100 if (held != _addons.end())
101 {
102 _addons.erase(held);
103 _state.Addons->Remove(addonId);
104 }
105}
106
108{
109 return !_state.Addons || _state.Addons->IsReady(slot);
110}
111
112void PluginContext::WriteLog(uint8_t level, std::string_view text)
113{
114 const auto wanted = static_cast<LogLevel>(level);
115 if (wanted < _minLevel)
116 {
117 return;
118 }
119
120 // Every plugin prints through the host's own handler, so a server reads one stream.
121 Log::Emit(wanted, std::format("[{}] {}", _logTag, text));
122}
123
125{
126 return static_cast<uint8_t>(_minLevel);
127}
128
129Schema::LiveField PluginContext::FindSchemaField(std::string_view className, std::string_view field) const
130{
131 return _state.SchemaFields ? _state.SchemaFields(className, field) : Schema::LiveField{};
132}
133
134template <class Fn>
135uint64_t PluginContext::Subscribe(std::string_view event, CallbackList<Fn>& list, Fn callback, void* context)
136{
137 if (callback == nullptr)
138 {
139 return 0;
140 }
141
142 const uint64_t token = _state.NextToken++;
143 list.Add(token, _order, callback, context);
144 _subscriptions.push_back({.Token = token, .Event = event, .Remove = [&list, token] { list.Remove(token); }});
145 return token;
146}
147
148uint64_t PluginContext::OnFrame(FrameFn callback, void* context)
149{
150 return Subscribe("frame", _state.Frame, callback, context);
151}
152
153uint64_t PluginContext::OnServerStartup(ServerStartupFn callback, void* context)
154{
155 return Subscribe("server_startup", _state.ServerStartup, callback, context);
156}
157
159{
160 return Subscribe("client_connecting", _state.ClientConnecting, callback, context);
161}
162
164{
165 return Subscribe("client_connected", _state.ClientConnected, callback, context);
166}
167
169{
170 return Subscribe("client_disconnected", _state.ClientDisconnected, callback, context);
171}
172
174{
175 return Subscribe("client_fully_connected", _state.ClientFullyConnected, callback, context);
176}
177
179{
180 return Subscribe("client_settings_changed", _state.ClientSettingsChanged, callback, context);
181}
182
183uint64_t PluginContext::OnConsoleCommand(ConsoleCommandFn callback, void* context)
184{
185 return Subscribe("console_command", _state.ConsoleCommand, callback, context);
186}
187
188uint64_t PluginContext::OnCheckTransmit(CheckTransmitFn callback, void* context)
189{
190 return Subscribe("check_transmit", _state.CheckTransmit, callback, context);
191}
192
194{
195 return Subscribe("build_game_session_manifest", _state.BuildGameSessionManifest, callback, context);
196}
197
198uint64_t PluginContext::OnPlayerCommand(PlayerCommandFn callback, void* context)
199{
200 return Subscribe("player_command", _state.PlayerCommand, callback, context);
201}
202
204{
205 return Subscribe("player_command_done", _state.PlayerCommandDone, callback, context);
206}
207
208uint64_t PluginContext::OnButtonPress(ButtonPressFn callback, void* context)
209{
210 return Subscribe("button_press", _state.ButtonPress, callback, context);
211}
212
213void PluginContext::Publish(std::string_view name, void* implementation)
214{
215 _state.Services.Publish(this, name, implementation);
216}
217
218void PluginContext::Unpublish(std::string_view name)
219{
220 _state.Services.Unpublish(this, name);
221}
222
223void* PluginContext::Find(std::string_view name)
224{
225 return _state.Services.Find(name);
226}
227
229{
230 if (_state.CurrentMap.empty())
231 {
232 return;
233 }
234
235 _state.ServerStartup.DispatchTo(
236 _order, [&](ServerStartupFn callback, void* context) { callback(context, _state.CurrentMap); });
237 // Each slot checked before each event: a handler may kick, which empties it.
238 for (int slot = 0; slot < MaxPlayers; ++slot)
239 {
240 if (!_state.Clients[slot])
241 {
242 continue;
243 }
244 const ConnectedClient client = *_state.Clients[slot];
245 _state.ClientConnected.DispatchTo(_order, [&](ClientConnectedFn callback, void* context) {
246 callback(context, slot, client.SteamId, client.Name, client.Address);
247 });
248
249 if (_state.Clients[slot] && _state.Clients[slot]->FullyConnected)
250 {
251 _state.ClientFullyConnected.DispatchTo(
252 _order, [&](ClientFullyConnectedFn callback, void* context) { callback(context, slot); });
253 }
254 }
255}
256
258{
259 for (int slot = 0; slot < MaxPlayers; ++slot)
260 {
261 if (_state.Clients[slot])
262 {
263 _state.ClientDisconnected.DispatchTo(
264 _order, [&](ClientDisconnectedFn callback, void* context) { callback(context, slot); });
265 }
266 }
267}
268
269void PluginContext::Unsubscribe(uint64_t token)
270{
271 const auto held = std::ranges::find(_subscriptions, token, &Subscribed::Token);
272 if (held == _subscriptions.end())
273 {
274 return;
275 }
276
277 held->Remove();
278 _subscriptions.erase(held);
279}
280
282{
283 LeakReport leaks;
284 // Subscriptions first, so the plugin on its way out is not told about its own withdrawals.
285 for (const Subscribed& held : _subscriptions)
286 {
287 held.Remove();
288 leaks.Subscriptions.push_back(held.Event);
289 }
290 _subscriptions.clear();
291
292 leaks.Services = _state.Services.RemoveAll(this);
293 for (const uint64_t addonId : std::exchange(_addons, {}))
294 {
295 _state.Addons->Remove(addonId);
296 leaks.Addons.push_back(addonId);
297 }
298 _state.Commands.RemoveAll(this);
299 return leaks;
300}
301
302} // namespace VoltMod
bool Remove(uint64_t token)
void Add(uint64_t token, uint64_t order, Fn callback, void *context)
uint64_t OnButtonPress(ButtonPressFn callback, void *context) override
void * ServerInterface(const char *version) const override
IPluginGameData * GameData() const override
uint64_t OnClientSettingsChanged(ClientSettingsChangedFn callback, void *context) override
std::string_view BaseDir() const override
IPluginAddons & Addons() override
void * EngineInterface(const char *version) const override
uint64_t OnFrame(FrameFn callback, void *context) override
uint64_t OnClientDisconnected(ClientDisconnectedFn callback, void *context) override
void Unsubscribe(uint64_t token) override
uint64_t OnPlayerCommand(PlayerCommandFn callback, void *context) override
std::string_view Name() const override
bool IsReady(int slot) override
PluginContext(SharedState &state, std::string name, std::string logTag, std::string version, uint64_t order)
IPluginLanguages & Languages() override
uint64_t OnClientFullyConnected(ClientFullyConnectedFn callback, void *context) override
KHook::IKHook * HookDispatcher() const override
uint64_t OnClientConnected(ClientConnectedFn callback, void *context) override
bool IsCommandRegistered(std::string_view name) const override
bool RegisterCommand(std::string_view name) override
void * Find(std::string_view name) override
uint64_t OnCheckTransmit(CheckTransmitFn callback, void *context) override
uint8_t MinLogLevel() const override
void Publish(std::string_view name, void *implementation) override
uint64_t OnPlayerCommandDone(PlayerCommandFn callback, void *context) override
void Remove(uint64_t addonId) override
uint64_t OnConsoleCommand(ConsoleCommandFn callback, void *context) override
uint64_t OnClientConnecting(ClientConnectingFn callback, void *context) override
Schema::LiveField FindSchemaField(std::string_view className, std::string_view field) const override
void WriteLog(uint8_t level, std::string_view text) override
IPluginEvents & Events() override
std::string_view Version() const override
uint64_t OnServerStartup(ServerStartupFn callback, void *context) override
bool Add(uint64_t addonId) override
uint64_t OnBuildGameSessionManifest(BuildGameSessionManifestFn callback, void *context) override
void Unpublish(std::string_view name) override
IPluginServices & Services() override
void Error(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:97
void Emit(LogLevel level, std::string message)
Definition Logger.cpp:47
LogLevel
Definition Log.hpp:13
constexpr int MaxPlayers
Definition Slot.hpp:7
void(*)(void *context, int slot) ClientFullyConnectedFn
void(*)(void *context, int slot) ClientSettingsChangedFn
void(*)(void *context, int slot, std::string_view buttonId) ButtonPressFn
void(*)(void *context, std::string_view mapName) ServerStartupFn
bool(*)(void *context, std::string_view name, std::string_view arguments, int slot) ConsoleCommandFn
void(*)(void *context, int slot, int64_t steamId, std::string_view name, std::string_view address) ClientConnectedFn
void(*)(void *context, CCheckTransmitInfo **infoList, int infoCount) CheckTransmitFn
void(*)(void *context, IEntityResourceManifest *manifest) BuildGameSessionManifestFn
void(*)(void *context) FrameFn
void(*)(void *context, int slot, const PlayerInput &input) PlayerCommandFn
void(*)(void *context, int slot) ClientDisconnectedFn
bool(*)(void *context, int slot, int64_t steamId, std::string_view name, char *reason, size_t reasonSize) ClientConnectingFn
std::vector< std::string_view > Subscriptions
the event each was taken on
std::vector< std::string > Services
std::vector< uint64_t > Addons
uint64_t NextToken
unique across every event and the service table, never zero, never reused