18 if (!error.
Text.empty())
22 if (!error.
Key.empty())
24 return translations.
Get(error.
Key, slot);
32 std::string key =
"cmd.usage.";
33 const std::string_view name =
Name(kind);
36 key +=
static_cast<char>(std::tolower(
static_cast<unsigned char>(name.front())));
37 key += name.substr(1);
48 Log::Error(
"A command was registered with no name - ignoring it.");
51 if (_commands.contains(name) || _aliases.contains(name))
53 Log::Error(
"Command '{}' is already registered - ignoring the second registration.", def.
Name);
56 if (_host && !_host->RegisterCommand(name))
58 Log::Error(
"Command '{}' is held by another plugin - ignoring this registration.", def.
Name);
63 for (
const std::string& alias : def.
Aliases)
66 if (key.empty() || key == name)
70 if (_commands.contains(key))
72 Log::Error(
"Command '{}' asks for alias '{}', which is already a command name - skipping the alias.",
76 if (
auto it = _aliases.find(key); it != _aliases.end())
78 Log::Error(
"Command '{}' asks for alias '{}', already taken by '{}' - skipping the alias.", def.
Name, alias,
82 if (_host && !_host->RegisterCommand(key))
84 Log::Error(
"Command '{}' asks for alias '{}', which another plugin owns - skipping the alias.", def.
Name,
88 _aliases.emplace(std::move(key), name);
92 stored = std::move(def);
106 if (
auto it = _commands.find(key); it != _commands.end())
111 if (
auto alias = _aliases.find(key); alias != _aliases.end())
113 if (
auto it = _commands.find(alias->second); it != _commands.end())
129 return static_cast<size_t>(
130 std::count_if(def.
Args.begin(), def.
Args.end(), [](
const ArgDesc& arg) { return !arg.Optional; }));
133bool CommandRouter::HasRest(
const CommandDefinition& def)
135 return std::any_of(def.Args.begin(), def.Args.end(), [](
const ArgDesc& arg) { return arg.Kind == ArgKind::Rest; });
144 for (
const ArgDesc& arg : def.Args)
151 args += arg.Optional ?
'[' :
'<';
153 args += arg.Optional ?
']' :
'>';
156 std::string line = prefix + def.Name;
162 return {{
"prefix", prefix}, {
"command", def.Name}, {
"args", args}, {
"usage", line}};
169 return _translations.Get(def.
UsageKey, slot);
171 return _translations.Get(
"cmd.usage", slot, UsageTokens(def, slot, origin));
175 Origin origin,
ArgBinder& binder,
const std::function<
void(
const std::string&)>& say)
const
178 const int slot = caller ? caller->
Slot() : -1;
179 const auto reply = [&say](
const std::string& line) {
189 auto authorized = _policy.Authorize(caller->
Ref(), std::nullopt, def.
PermissionName);
192 reply(_translations.Get(authorized.error().Key, slot));
198 if (!HasRest(def) && tokens.size() > def.
Args.size())
200 reply(_translations.Get(
"cmd.tooManyArgs", slot, UsageTokens(def, slot, origin)));
204 if (tokens.size() < RequiredArgs(def))
206 reply(
Usage(def, slot, origin));
210 auto bound =
BindArgs(def, tokens, caller, binder);
213 reply(_translations.Get(bound.error().Key, slot, bound.error().Vars));
222 const Caller who{.Player = caller, .
Slot = slot, .Translations = _translations, .Send = say};
223 auto result = def.
Invoke(who, *bound);
224 reply(result ? result->Text :
FailureLine(result.error(), _translations, slot));
How a chat line is spelled: the prefix, and splitting one into tokens.
Enumerator names without a hand-written switch.
The one thing command dispatch needs a running server for.
void Dispatch(const CommandDefinition &def, Player *caller, std::span< const std::string > tokens, Origin origin, ArgBinder &binder, const std::function< void(const std::string &)> &say) const
const CommandDefinition * Find(std::string_view name) const
const CommandDefinition * Add(CommandDefinition def)
std::string Usage(const CommandDefinition &def, int slot, Origin origin) const
bool IsForeign(std::string_view name) const
One connected player, owned by PlayerManager for the length of the connection.
int Slot() const noexcept
PlayerRef Ref() const noexcept
static std::string ToLower(std::string_view str)
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
std::string Get(std::string_view key) const
constexpr std::string_view ChatPrefix
void Error(std::format_string< Args... > fmt, Args &&... args)
std::expected< std::vector< BoundArg >, ArgError > BindArgs(const CommandDefinition &def, std::span< const std::string > tokens, Player *caller, ArgBinder &binder)
constexpr std::string_view Name(E value) noexcept
static std::string FailureLine(const Error &error, const Translations &translations, int slot)
static std::string UsagePlaceholderKey(ArgKind kind)
std::map< std::string, std::string > Tokens
ArgKind
Which binder runs for one declared argument.
Who invoked the command, and how to answer them.
One command as the builder assembled it.
std::string PermissionName
std::function< Result< Reply >(const Caller &, std::span< const BoundArg >)> Invoke
std::vector< std::string > Aliases
std::vector< ArgDesc > Args
One failure: a code to branch on, text for the log, and an optional translation key.