VoltMod
C++23 framework for CS2 server plugins
Loading...
Searching...
No Matches
CommandBuilder.cpp
Go to the documentation of this file.
2
3// The reply helpers, kept apart from the router so they read as one unit: every one of them
4// localizes for this caller's slot, which is what keeps English literals out of handlers.
5namespace VoltMod
6{
7
8std::string Caller::Text(std::string_view key, Tokens tokens) const
9{
10 return tokens.empty() ? Translations.Get(std::string(key), Slot) : Translations.Get(std::string(key), Slot, tokens);
11}
12
13Result<Reply> Caller::Ok(std::string_view key, Tokens tokens) const
14{
15 return Reply{Text(key, std::move(tokens))};
16}
17
18Result<Reply> Caller::Fail(std::string_view key, Tokens tokens) const
19{
20 // Localized now because Error has nowhere to hold `tokens` until reply time.
21 return std::unexpected(
22 Error{.Code = ErrorCode::Failed, .Key = std::string(key), .Text = Text(key, std::move(tokens))});
23}
24
25void Caller::Say(std::string_view key, Tokens tokens) const
26{
27 SayRaw(Text(key, std::move(tokens)));
28}
29
30void Caller::SayRaw(std::string_view line) const
31{
32 if (Send && !line.empty())
33 {
34 Send(std::string(line));
35 }
36}
37
38} // namespace VoltMod
Localization system. Loads one JSON file per language; nested objects flatten into dotted keys (categ...
std::string Get(std::string_view key) const
@ Text
Not selectable; a caption or a summary line.
@ Failed
Anything else; the default so a value-initialized Error is never a false success.
static std::string ReadFile(const std::filesystem::path &path)
Definition Loader.cpp:56
std::map< std::string, std::string > Tokens
Definition Strings.hpp:14
std::expected< T, Error > Result
Definition Result.hpp:64
void SayRaw(std::string_view line) const
Result< Reply > Ok(std::string_view key, Tokens tokens={}) const
std::function< void(const std::string &)> Send
std::string Text(std::string_view key, Tokens tokens={}) const
Result< Reply > Fail(std::string_view key, Tokens tokens={}) const
void Say(std::string_view key, Tokens tokens={}) const
One failure: a code to branch on, text for the log, and an optional translation key.
Definition Result.hpp:42
ErrorCode Code
Definition Result.hpp:43
What a command handler answers with.