VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
VoltCommand.cpp
Go to the documentation of this file.
2
3#include "BuildStamp.hpp"
4
8#include <optional>
9#include <tier1/convar.h>
10#include <vector>
11
12namespace VoltMod
13{
14
15static constexpr std::string_view CommandUsage =
16 "volt version | list | status [name] | load <name> | unload <name> | reload <name> | "
17 "log <name> <info|warn|error>";
18
20 : _registry(registry),
21 _loader(loader),
22 _command("volt", CommandUsage, [this](const CCommand& arguments, int) { Run(arguments); })
23{
24 _registry.RegisterHostCommand("volt");
25}
26
28
29void VoltCommand::Run(const CCommand& arguments)
30{
31 const std::string_view verb = arguments.ArgC() >= 2 ? arguments.Arg(1) : "";
32 const std::string_view target = arguments.ArgC() >= 3 ? arguments.Arg(2) : "";
33 const std::string_view value = arguments.ArgC() >= 4 ? arguments.Arg(3) : "";
34
35 if (verb == "version")
36 {
37 PrintVersion();
38 }
39 else if (verb == "list")
40 {
41 PrintLoaded();
42 }
43 else if (verb == "status")
44 {
45 PrintStatus(target);
46 }
47 else if (verb == "load" && !target.empty())
48 {
50 }
51 else if (verb == "unload" && !target.empty())
52 {
54 }
55 else if (verb == "reload" && !target.empty())
56 {
58 }
59 else if (verb == "log" && !target.empty() && !value.empty())
60 {
61 SetLogLevel(target, value);
62 }
63 else
64 {
65 Log::Info("Usage: {}", CommandUsage);
66 }
67}
68
69void VoltCommand::PrintVersion() const
70{
72}
73
74void VoltCommand::PrintLoaded() const
75{
76 PrintVersion();
77 const std::vector<LoadedPlugin>& loaded = _loader.LoadedPlugins();
78 if (loaded.empty())
79 {
80 Log::Info("No plugins are loaded.");
81 }
82 else
83 {
84 Log::Info("{} plugin(s), in load order:", loaded.size());
85 }
86 for (const LoadedPlugin& plugin : loaded)
87 {
88 const std::string_view description = plugin.Manifest.Description;
89 Log::Info(" {} v{} ({}){}{}", plugin.Manifest.Name, plugin.Manifest.Version, plugin.Descriptor->BuildStamp,
90 description.empty() ? "" : " - ", description);
91 }
92
93 const auto& refused = _loader.Refused();
94 if (!refused.empty())
95 {
96 Log::Info("{} refused:", refused.size());
97 }
98 for (const auto& [name, reason] : refused)
99 {
100 Log::Info(" {} - {}", name, reason);
101 }
102}
103
104void VoltCommand::PrintStatus(std::string_view name)
105{
106 const auto print = [](const LoadedPlugin& plugin) {
107 const char* status = plugin.Descriptor->Status();
108 Log::Info("{}: {}", plugin.Manifest.Name, status != nullptr ? status : "{}");
109 };
110
111 if (name.empty())
112 {
113 for (const LoadedPlugin& plugin : _loader.LoadedPlugins())
114 {
115 print(plugin);
116 }
117 return;
118 }
119
120 if (const LoadedPlugin* plugin = _loader.RequireLoaded(name); plugin != nullptr)
121 {
122 print(*plugin);
123 }
124}
125
126void VoltCommand::SetLogLevel(std::string_view name, std::string_view level)
127{
128 const std::optional<LogLevel> wanted = Parse<LogLevel>(level);
129 if (!wanted)
130 {
131 Log::Warn("'{}' is not a log level. Use info, warn or error.", level);
132 return;
133 }
134
135 PluginContext* plugin = _loader.RequireLoaded(name) != nullptr ? _registry.FindPlugin(name) : nullptr;
136 if (plugin == nullptr)
137 {
138 return;
139 }
140
142 Log::Info("{} now logs {} and above.", name, Strings::ToLower(Name(*wanted)));
143}
144
145} // namespace VoltMod
Enumerator names without a hand-written switch.
void SetMinLogLevel(LogLevel level)
Finds the installed plugins and loads the ones their dependencies allow.
LoadedPlugin * RequireLoaded(std::string_view name)
const std::map< std::string, std::string > & Refused() const
const std::vector< LoadedPlugin > & LoadedPlugins() const
void Defer(ActionKind kind, std::string_view name)
The loaded plugins and the engine events raised on them, one per process.
PluginContext * FindPlugin(std::string_view name)
static std::string ToLower(std::string_view str)
Definition Strings.cpp:100
VoltCommand(PluginRegistry &registry, PluginLoader &loader)
void Info(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:79
void Warn(std::format_string< Args... > fmt, Args &&... args)
Definition Log.hpp:88
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
static constexpr std::string_view CommandUsage