VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CommandManager.hpp
Go to the documentation of this file.
1#pragma once
2
10#include <cstddef>
11#include <memory>
12#include <string>
13#include <string_view>
14#include <unordered_map>
15#include <vector>
16
17namespace VoltMod
18{
19
20/**
21 * @brief Typed commands for chat and the server console.
22 *
23 * The handler's parameters are the argument spec; dispatch authorizes, binds and replies:
24 *
25 * @code
26 * commands.Add("slap")
27 * .Permission("admin.control")
28 * .Run([&](Caller c, Args::Target t, Args::Opt<Args::Int> damage) {
29 * return c.Ok("cmd.slapped", {{"name", t->Name()}});
30 * });
31 * @endcode
32 *
33 * Commands live until the plugin unloads; the framework removes them before the plugin's state.
34 * `Anywhere()` and `ServerOnly()` also register a console command (see @ref commands_guide).
35 */
37{
38public:
39 /** Every argument must outlive the manager. Command names are shared with the other plugins
40 * on @p host. */
46
47 /** Start a command; `.Run(handler)` installs it. */
48 CommandBuilder Add(std::string_view name);
49
50 /** Run @p message when it is a `!` command. Framework only.
51 * @return true when it was one, so chat hides the line. */
52 bool HandleChatMessage(Player* caller, std::string_view message);
53
54 /** True when @p message is another plugin's command, which chat must pass on unfiltered. */
55 bool IsForeign(std::string_view message) const;
56
57 size_t Count() const;
58
59 /** Remove every command. Framework only, on unload. */
60 void RemoveAll();
61
62private:
63 void InstallConsoleCommand(const CommandDefinition& def);
64 /** Reply through `Policy::Reply`, else chat. */
65 void ReplyToPlayer(int slot, const std::string& line);
66
67 Policy& _policy;
68 PlayerManager& _players;
69 Messages& _messages;
70 std::unique_ptr<EngineArgBinder> _binder;
71 std::unique_ptr<CommandRouter> _router;
72 /** By lowercased name. Declared after the router, which their handlers use. */
73 std::unordered_map<std::string, ServerCommand> _consoleCommands;
74};
75
76} // namespace VoltMod
Fluent command registration, returned by CommandManager::Add.
Typed commands for chat and the server console.
CommandManager & operator=(const CommandManager &)=delete
bool HandleChatMessage(Player *caller, std::string_view message)
CommandBuilder Add(std::string_view name)
CommandManager(const CommandManager &)=delete
bool IsForeign(std::string_view message) const
runtime.Entities: finds and creates entities. Everything it returns is valid for this frame only; see...
Send chat, center print, center HTML, and alert messages.
Definition Messages.hpp:30
The roster: every connected player, and the signals for the connection lifecycle.
One connected player, owned by PlayerManager for the length of the connection.
Definition Player.hpp:23
The one permission and targeting gate, plus the plugin's reply callback.
Definition Policy.hpp:41
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
One command as the builder assembled it.