VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Host.cpp
Go to the documentation of this file.
1#include "BuildStamp.hpp"
16
18#include <VoltMod/Core/Log.hpp>
24#include <cstddef>
25#include <memory>
26#include <string_view>
27
28namespace KHook
29{
30KHook::IKHook* __exported__khook = nullptr;
31}
32
33namespace VoltMod
34{
35
36static constexpr std::string_view GameDataPath = "addons/voltmod/gamedata/gamedata.jsonc";
37
38/** The host's services for the process, built at start and torn down at stop. */
39class Host
40{
41public:
42 ~Host() { Stop(); }
43
45 {
48 SetBaseDir(start.GameDir);
49
50 // Before any plugin, so a broken signature logs once; KHook reads slots another hook holds.
51 _gameData = std::make_unique<GameDataTable>(GameDataPath, ReadOriginalSlot, FindScriptBinding);
52
54 if (!engine)
55 {
56 Stop();
57 return std::unexpected(engine.error());
58 }
59
60 GameDataTable* gameData = _gameData->Ready() ? _gameData.get() : nullptr;
61
62 // The host's own hooks share one pass; each service checks the members it needs.
63 _bindings = {};
64 if (gameData)
65 {
66 (void)_bindings.Bind([gameData](GameDataSection sections, std::string_view name) {
67 return gameData->Lookup(sections, name);
68 });
69 }
70
71 _downloads = std::make_unique<WorkshopDownloads>(_bindings, *engine);
72 _registry = std::make_unique<PluginRegistry>(start, gameData, _downloads.get());
73 _commands = std::make_unique<PlayerCommands>(_bindings, *_registry);
74 _presses = std::make_unique<ButtonPresses>(_bindings, *engine, *_registry);
75 _schema = std::make_unique<SchemaCheck>(*_registry, engine->Schema);
76 _assets = std::make_unique<ServerAssets>(engine->Files);
77 _plugins = std::make_unique<PluginLoader>(*_registry, *_assets);
78
79 _hooks = std::make_unique<EngineHooks>(
80 *_registry, *engine,
81 [this] {
82 _plugins->RunPending();
83 _downloads->OnFrame();
84 _presses->OnFrame();
85 },
86 [this] { _schema->OnServerStartup(); }, [this](int64_t steamId) { _downloads->OnClientConnected(steamId); },
87 [this](int64_t steamId) { _downloads->OnClientDisconnected(steamId); });
88
89 // Before the plugins load, so a plugin registering `volt` is refused rather than racing it.
90 _command = std::make_unique<VoltCommand>(*_registry, *_plugins);
91 _plugins->LoadAll();
92
93 Log::Info("VoltMod host {} ({}) loaded.", VOLTMOD_VERSION, VOLTMOD_BUILD_STAMP);
94 return {};
95 }
96
97 void Stop()
98 {
99 // Plugins first: their teardown runs while the host's events and services are still there.
100 if (_plugins)
101 {
102 _plugins->UnloadAll();
103 }
104
105 _hooks.reset();
106 _command.reset();
107 _plugins.reset();
108 _assets.reset();
109 _schema.reset();
110 _presses.reset();
111 _commands.reset();
112 _registry.reset();
113 _downloads.reset();
114 _gameData.reset();
115 }
116
117private:
118 std::unique_ptr<GameDataTable> _gameData;
119 Bindings _bindings;
120 std::unique_ptr<WorkshopDownloads> _downloads;
121 std::unique_ptr<PluginRegistry> _registry;
122 std::unique_ptr<PlayerCommands> _commands;
123 std::unique_ptr<ButtonPresses> _presses;
124 std::unique_ptr<SchemaCheck> _schema;
125 std::unique_ptr<ServerAssets> _assets;
126 std::unique_ptr<PluginLoader> _plugins;
127 std::unique_ptr<VoltCommand> _command;
128 std::unique_ptr<EngineHooks> _hooks;
129};
130
132
133} // namespace VoltMod
134
135extern "C" VOLTMOD_EXPORT bool VoltMod_HostStart(const VoltMod::LoaderHandoff* start, char* error, size_t errorSize)
136{
137 VoltMod::Status started = VoltMod::g_host.Start(*start);
138 if (!started)
139 {
140 VoltMod::Strings::CopyToBuffer(error, errorSize, started.error().Detail);
141 }
142 return started.has_value();
143}
144
146{
147 VoltMod::g_host.Stop();
148}
VOLTMOD_EXPORT void VoltMod_HostStop()
Definition Host.cpp:145
VOLTMOD_EXPORT bool VoltMod_HostStart(const VoltMod::LoaderHandoff *start, char *error, size_t errorSize)
Definition Host.cpp:135
#define VOLTMOD_EXPORT
void Stop()
Definition Host.cpp:97
Status Start(const LoaderHandoff &start)
Definition Host.cpp:44
static void CopyToBuffer(char *buffer, std::size_t size, std::string_view text) noexcept
Definition Strings.cpp:257
KHook::IKHook * __exported__khook
Definition Host.cpp:30
void Info(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:79
void SetHandler(Handler handler)
Definition Logger.cpp:26
Log::Handler MakeConsoleHandler(std::string prefix)
The default handler: tier0 ConColorMsg/Msg, one colour per level.
void SetBaseDir(const std::filesystem::path &baseDir)
Set the base directory for path resolution (the host's csgo directory). Must be called during initial...
Definition Paths.cpp:9
std::expected< void, Error > Status
Definition Result.hpp:67
Result< ScriptTarget > FindScriptBinding(void *classTable, std::string_view name)
std::expected< T, Error > Result
Definition Result.hpp:64
static Host g_host
Definition Host.cpp:131
Result< EngineInterfaces > ResolveEngineInterfaces(const LoaderHandoff &start)
const void * ReadOriginalSlot(void **vtable, int index)
Definition Detours.hpp:21
static constexpr std::string_view GameDataPath
Definition Host.cpp:36
const char * GameDir
the absolute csgo directory
KHook::IKHook * HookDispatcher
the loader's KHook, which every module's hooks go through