VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
Runtime.cpp
Go to the documentation of this file.
2
8#include <VoltMod/Runtime.hpp>
9#include <chrono>
10#include <cstdint>
11#include <map>
12#include <memory>
13#include <string>
14#include <string_view>
15#include <utility>
16
17namespace VoltMod
18{
19
31
37
39 : PluginName(host.Name()),
40 Version(host.Version()),
42 Unsafe(unsafe),
43 Movement(Unsafe.Bindings,
44 [&events = host.Events()](VoltMod::Movement& movement) { return ListenForCommands(events, movement); }),
45 Screens(Entities, Unsafe.Bindings, Slots, Visibility,
46 [&events = host.Events()](ScreenManager& screens) { return ListenForPresses(events, screens); }),
47 AddonManager(host.Addons()),
48 Exchange(host.Services()),
49 Commands(Policy, Translations, Players, Entities, Messages, host)
50{
51 Policy.HasPermission = [this, warned = false](int64_t steamId, std::string_view permission) mutable {
52 if (auto* permissions = Exchange.Get<IPermissions>())
53 {
54 return permissions->HasPermission(steamId, permission);
55 }
56 if (!warned)
57 {
58 warned = true;
59 Log::Warn("Denying '{}': no plugin publishes {}.", permission, IPermissions::InterfaceName);
60 }
61 return false;
62 };
63
64 CheckServices();
65 AddStatusSections();
66}
67
69{
70 // Stop HTTP workers and flush queued logs before removing frame hooks.
71 Http.Stop();
73}
74
75std::string Runtime::PluginFile(std::string_view relative) const
76{
78}
79
81{
82 auto menu = std::make_unique<PanoramaMenu>(PanoramaMenu::Services{.Scheduler = Scheduler,
83 .Slots = Slots,
84 .Freeze = Freeze,
85 .ChatInput = ChatInput,
86 .Translations = Translations,
87 .Policy = Policy,
88 .Screens = Screens,
89 .AddonManager = AddonManager},
90 layout);
92 // Stop routing to the menu before destroying it.
93 return Subscription([menu = std::move(menu), preferred = std::move(preferred)]() mutable {
94 preferred.Reset();
95 menu.reset();
96 });
97}
98
99void Runtime::CheckServices()
100{
101 LoadReport.Required("Messages", Messages.Available());
102 LoadReport.Optional("GameData", Unsafe.GameData);
103 LoadReport.Optional("Entities", Entities.Available());
104 LoadReport.Optional("ConVars", ConVars.Available());
105 LoadReport.Optional("GameEvents", GameEvents.Available());
106 LoadReport.Optional("ClientConVars", ClientConVars.Available());
107 LoadReport.Optional("Movement", Movement.Available());
108 LoadReport.Optional("Teleport", Teleport.Available());
109 LoadReport.Optional("TextMessages", TextMessages.Available());
110 LoadReport.Optional("Visibility", Visibility.Available());
112 LoadReport.Optional("Screens", Screens.Available());
113 LoadReport.Optional("Damage", Damage.Available());
114}
115
116void Runtime::AddStatusSections()
117{
118 // Plugins add status sections during Load; the runtime owns them for the load cycle.
119 Status.RegisterSection("build", [this] { return Json::Write(glz::obj{"name", PluginName, "version", Version}); });
120
121 Status.RegisterSection("load", [this] {
122 std::map<std::string, std::string> failed;
123 for (const FailedCheck& check : LoadReport.Failures())
124 {
125 failed.emplace(check.Name, check.Reason);
126 }
127 return Json::Write(glz::obj{"failed", failed});
128 });
129
130 Status.RegisterSection("uptime", [start = std::chrono::steady_clock::now()] {
131 const auto uptime = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - start);
132 return Json::Write(glz::obj{"seconds", uptime.count()});
133 });
134}
135
141
142} // namespace VoltMod
Asks a connected client what one of its own convars is set to.
Status Available() const
Definition ConVars.cpp:53
Every entity's damage, from CBaseEntity::TakeDamageOld, and a way to deal it.
Definition Damage.hpp:80
Status Available() const
Definition Damage.cpp:78
Creates, fires and listens to game events.
Status Available() const
static std::string Write(const T &value)
Serialize value as compact JSON.
Definition Json.hpp:96
bool Optional(std::string_view name, Status result)
Definition LoadReport.cpp:9
bool Required(std::string_view name, Status result)
Subscription Prefer(MenuSurface &surface)
Definition MenuRouter.cpp:9
Send chat, center print, center HTML, and alert messages.
Definition Messages.hpp:30
Status Available() const
Definition Messages.cpp:61
Each player's command from CCSPlayer_MovementServices::RunCommand.
Definition Movement.hpp:23
Status Available() const
Definition Movement.cpp:25
The screen built from the menu Panorama block, as PanoramaMenu draws on it.
std::function< bool(int64_t steamId, std::string_view permission)> HasPermission
Definition Policy.hpp:50
SlotEvents Slots
Definition Runtime.hpp:87
StatusService Status
Definition Runtime.hpp:85
MultiAddonManager AddonManager
Definition Runtime.hpp:160
HttpClient Http
Definition Runtime.hpp:182
const std::string PluginName
Definition Runtime.hpp:69
VoltMod::Translations Translations
Definition Runtime.hpp:95
VoltMod::LoadReport LoadReport
Definition Runtime.hpp:83
ServiceExchange Exchange
Definition Runtime.hpp:162
VoltMod::Policy Policy
Definition Runtime.hpp:108
VoltMod::MenuFreeze Freeze
Definition Runtime.hpp:165
VoltMod::Scheduler Scheduler
Definition Runtime.hpp:93
std::string PluginFile(std::string_view relative) const
Definition Runtime.cpp:75
EntitySystem Entities
Definition Runtime.hpp:101
MenuRouter Menus
Definition Runtime.hpp:177
const std::string Version
Definition Runtime.hpp:71
VoltMod::ScreenManager Screens
Definition Runtime.hpp:158
Runtime(IPluginContext &host, UnsafeServices &unsafe, PlayerLanguages &languages)
Definition Runtime.cpp:38
UnsafeServices & Unsafe
Definition Runtime.hpp:98
Subscription UsePanorama(PanoramaMenuLayout &layout)
Definition Runtime.cpp:80
VoltMod::ChatInput ChatInput
Definition Runtime.hpp:141
One-shot delays and repeating timers, run on the game thread.
Definition Scheduler.hpp:18
Create Screen objects and report their button presses.
void RegisterSection(std::string name, Provider provider)
Add a section, replacing any existing one with the same name.
Owns one registration and releases it on destruction.
Report player pawn moves through CBaseEntity::Teleport.
Definition Teleport.hpp:29
Status Available() const
Definition Teleport.cpp:31
Outgoing TextMsg user messages, the game's own lines included.
Status Available() const
runtime.Trace: line and box traces against the physics world. Game-thread only.
Definition Trace.hpp:50
Status Available() const
Definition Trace.cpp:67
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
Which clients receive which entities, filtered after CheckTransmit.
Status Available() const
Subscription HostSubscription(IPluginEvents &events, uint64_t token)
void DeliverPending()
Definition Logger.cpp:69
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
static Subscription ListenForPresses(IPluginEvents &events, ScreenManager &screens)
Definition Runtime.cpp:32
static Subscription ListenForCommands(IPluginEvents &events, Movement &movement)
Definition Runtime.cpp:20
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::string PluginFile(std::string_view plugin, std::string_view relative)
"addons/voltmod/plugins/<plugin>/<relative>".
Definition Paths.cpp:25
static constexpr std::string_view InterfaceName
VoltMod::Scheduler & Scheduler
Opt-in access to raw engine interfaces and typed gamedata bindings.