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 * `PlayersOnly()`, `PlayersAndServer()` and `ServerOnly()` also register a console command (see
35 * @ref commands_guide).
36 */
38{
39public:
40 /** Every argument must outlive the manager. Command names are shared with the other plugins
41 * on @p host. */
42 CommandManager(Policy& policy, Translations& translations, PlayerManager& players, EntitySystem& entities,
43 Messages& messages, IPluginContext& host);
47
48 /** Start a command; `.Run(handler)` installs it. */
49 CommandBuilder Add(std::string_view name);
50
51 /** Run @p message when it is a `!` command. Framework only.
52 * @return true when it was one, so chat hides the line. */
53 bool HandleChatMessage(Player* caller, std::string_view message);
54
55 /** True when @p message is another plugin's command, which chat must pass on unfiltered. */
56 bool IsForeign(std::string_view message) const;
57
58 size_t Count() const;
59
60 /** Remove every command. Framework only, on unload. */
61 void RemoveAll();
62
63private:
64 void InstallConsoleCommand(const CommandDefinition& def);
65 /** Reply through `Policy::Reply`, else chat. */
66 void ReplyToPlayer(int slot, const std::string& line);
67
68 Policy& _policy;
69 PlayerManager& _players;
70 Messages& _messages;
71 std::unique_ptr<EngineArgBinder> _binder;
72 std::unique_ptr<CommandRouter> _router;
73 /** By lowercased name. Declared after the router, which their handlers use. */
74 std::unordered_map<std::string, ServerCommand> _consoleCommands;
75};
76
77} // namespace VoltMod
Fluent command registration, returned by CommandManager::Add.
CommandManager(Policy &policy, Translations &translations, PlayerManager &players, EntitySystem &entities, Messages &messages, IPluginContext &host)
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...
One command as the builder assembled it.