26 IConsole::CommandList &IConsole::Commands()
28 static IConsole::CommandList cmds;
32 IConsole::AliasList &IConsole::Aliases()
34 static IConsole::AliasList aliases;
38std::optional<FileHandle> _iconsole_output_file;
42 _iconsole_output_file = std::nullopt;
48 IConsoleStdLibRegister();
51static void IConsoleWriteToLogFile(
const std::string &
string)
53 if (_iconsole_output_file.has_value()) {
56 fmt::print(*_iconsole_output_file,
"{}{}\n",
GetLogPrefix(),
string);
57 }
catch (
const std::system_error &) {
58 _iconsole_output_file.reset();
64bool CloseConsoleLogIfActive()
66 if (_iconsole_output_file.has_value()) {
68 _iconsole_output_file.reset();
78 CloseConsoleLogIfActive();
113 IConsoleWriteToLogFile(str);
117 IConsoleWriteToLogFile(str);
128 name.erase(std::remove(name.begin(), name.end(),
'_'), name.end());
151 if (item != IConsole::Commands().end())
return &item->second;
162 auto result = IConsole::Aliases().try_emplace(
RemoveUnderscores(name), name, cmd);
174 if (item != IConsole::Aliases().end())
return &item->second;
187 Debug(console, 6,
"Requested command is an alias; parsing...");
200 if (!c.has_value()) {
220 for (
size_t i = 0; i < tokens.size(); ++i) {
221 if (i != 0) builder.
PutChar(
' ');
223 builder += tokens[i];
231 for (
size_t i = 0; i < tokens.size(); ++i) {
232 if (i != 0) builder.
PutChar(
' ');
233 builder += tokens[i];
240 size_t param = *c -
'A';
242 if (param >= tokens.size()) {
249 builder += tokens[param];
273 if (command_string[0] ==
'#')
return;
275 Debug(console, 4,
"Executing cmdline: '{}'", command_string);
281 std::vector<std::string> tokens;
282 bool found_token =
false;
283 bool in_quotes =
false;
290 if (!c.has_value()) {
297 if (!found_token)
break;
304 tokens.emplace_back(std::move(buffer));
310 in_quotes = !in_quotes;
329 tokens.emplace_back(std::move(buffer));
333 for (
size_t i = 0; i < tokens.size(); i++) {
334 Debug(console, 8,
"Token {} is: '{}'", i, tokens[i]);
337 if (tokens.empty() || tokens[0].empty())
return;
343 if (cmd !=
nullptr) {
347 std::vector<std::string_view> views;
348 for (
auto &token : tokens) views.emplace_back(token);
349 if (!cmd->
proc(views)) {
361 if (alias !=
nullptr) {
void PutUtf8(char32_t c)
Append UTF.8 char.
void PutChar(char c)
Append 8-bit char.
Compose data into a growing std::string.
std::string & GetString() noexcept
Get mutable already written data.
Parse data from a string / buffer.
char32_t ReadUtf8(char32_t def='?')
Read UTF-8 character, and advance reader.
bool AnyBytesLeft() const noexcept
Check whether any bytes left to read.
std::optional< char32_t > TryReadUtf8()
Try to read a UTF-8 character, and then advance reader.
bool ReadUtf8If(char32_t c)
Check whether the next UTF-8 char matches 'c', and skip it.
static std::string RemoveUnderscores(std::string name)
Creates a copy of a string with underscores removed from it.
void IConsoleCmdExec(std::string_view command_string, const uint recurse_count)
Execute a given command passed to us.
static void IConsoleAliasExec(const IConsoleAlias *alias, std::span< std::string > tokens, uint recurse_count)
An alias is just another name for a command, or for more commands Execute it as well.
void IConsolePrint(TextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
Console functions used outside of the console code.
bool IsValidConsoleColour(TextColour c)
Check whether the given TextColour is valid for console usage.
void IConsoleGUIPrint(TextColour colour_code, const std::string &str)
Handle the printing of text entered into the console or redirected there by any other means.
Internally used functions for the console.
ConsoleHookResult
Return values of console hooks (IConsoleHook).
@ CHR_HIDE
Hide the existence of the command.
@ CHR_DISALLOW
Disallow command execution.
@ CHR_ALLOW
Allow command execution.
bool(std::span< std::string_view >) IConsoleCmdProc
–Commands– Commands are commands, or functions.
static const TextColour CC_HELP
Colour for help lines.
static const TextColour CC_INFO
Colour for information lines.
static const TextColour CC_ERROR
Colour for error lines.
std::string GetLogPrefix(bool force)
Get the prefix for logs.
Functions related to debugging.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
TextColour
Colour of the strings, see _string_colourmap in table/string_colours.h or docs/ottd-colourtext-palett...
ClientID _redirect_console_to_client
If not invalid, redirect the console output to a client.
bool _network_dedicated
are we a dedicated server?
Basic functions/variables used all over the place.
void NetworkServerSendAdminRcon(AdminID admin_index, TextColour colour_code, std::string_view string)
Pass the rcon reply to the admin.
void NetworkAdminConsole(std::string_view origin, std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
AdminID _redirect_console_to_admin
Redirection of the (remote) console to the admin.
Server part of the admin network protocol.
Network functions used by other parts of OpenTTD.
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, std::string_view string)
Send an rcon reply to the client.
@ INVALID_CLIENT_ID
Client is not part of anything.
A number of safeguards to prevent using unsafe methods.
Types related to global configuration settings.
Definition of base types and functions in a cross-platform compatible way.
static void StrMakeValid(Builder &builder, StringConsumer &consumer, StringValidationSettings settings)
Copies the valid (UTF-8) characters from consumer to the builder.
Compose strings from textual and binary data.
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
std::string cmdline
command(s) that is/are being aliased
std::string name
name of the alias
IConsoleCmdProc * proc
process executed when command is typed
IConsoleHook * hook
any special trigger action that needs executing
static void AliasRegister(const std::string &name, std::string_view cmd)
Register a an alias for an already existing command in the console.
static IConsoleAlias * AliasGet(const std::string &name)
Find the alias pointed to by its string.
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook=nullptr)
Register a new command to be used in the console.
static IConsoleCmd * CmdGet(const std::string &name)
Find the command pointed to by its string.