27 IConsole::CommandList &IConsole::Commands()
29 static IConsole::CommandList cmds;
33 IConsole::AliasList &IConsole::Aliases()
35 static IConsole::AliasList aliases;
39std::optional<FileHandle> _iconsole_output_file;
43 _iconsole_output_file = std::nullopt;
52static void IConsoleWriteToLogFile(
const std::string &
string)
54 if (_iconsole_output_file.has_value()) {
57 fmt::print(*_iconsole_output_file,
"{}{}\n",
GetLogPrefix(),
string);
58 }
catch (
const std::system_error &) {
59 _iconsole_output_file.reset();
65bool CloseConsoleLogIfActive()
67 if (_iconsole_output_file.has_value()) {
69 _iconsole_output_file.reset();
79 CloseConsoleLogIfActive();
114 IConsoleWriteToLogFile(str);
118 IConsoleWriteToLogFile(str);
129 name.erase(std::remove(name.begin(), name.end(),
'_'), name.end());
152 if (item != IConsole::Commands().end())
return &item->second;
163 auto result = IConsole::Aliases().try_emplace(
RemoveUnderscores(name), name, cmd);
175 if (item != IConsole::Aliases().end())
return &item->second;
201 if (!c.has_value()) {
221 for (
size_t i = 0; i < tokens.size(); ++i) {
222 if (i != 0) builder.
PutChar(
' ');
224 builder += tokens[i];
232 for (
size_t i = 0; i < tokens.size(); ++i) {
233 if (i != 0) builder.
PutChar(
' ');
234 builder += tokens[i];
241 size_t param = *c -
'A';
243 if (param >= tokens.size()) {
250 builder += tokens[param];
274 if (command_string[0] ==
'#')
return;
282 std::vector<std::string> tokens;
283 bool found_token =
false;
284 bool in_quotes =
false;
291 if (!c.has_value()) {
298 if (!found_token)
break;
305 tokens.emplace_back(std::move(buffer));
311 in_quotes = !in_quotes;
330 tokens.emplace_back(std::move(buffer));
334 for (
size_t i = 0; i < tokens.size(); i++) {
338 if (tokens.empty() || tokens[0].empty())
return;
344 if (cmd !=
nullptr) {
348 std::vector<std::string_view> views;
349 for (
auto &token : tokens) views.emplace_back(token);
350 if (!cmd->
proc(views)) {
362 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.
void IConsolePrint(ExtendedTextColour colour_code, const std::string &string)
Handle the printing of text entered into the console or redirected there by any other means.
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.
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
void IConsoleStdLibRegister()
Console command registration.
Console functions used outside of the console code.
bool IsValidConsoleColour(ExtendedTextColour c)
Check whether the given TextColour is valid for console usage.
void IConsoleGUIPrint(ExtendedTextColour 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(bool echo) IConsoleHook
Checks whether the command may be executed.
bool(std::span< std::string_view > argv) IConsoleCmdProc
Entrypoint of a console command.
ConsoleHookResult
Return values of console hooks (IConsoleHook).
@ Disallow
Disallow command execution.
@ Allow
Allow command execution.
@ Hide
Hide the existence of the command.
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(facility, severity, format_string,...)
Output a line of debugging information.
@ Console
Console message facility.
@ Debug2
Debug #2 - Low level debug messages.
@ Info
Info, information you might care about.
@ Trace2
Trace information #2.
Types for standard in/out file operations.
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 NetworkAdminConsole(std::string_view origin, std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
void NetworkServerSendAdminRcon(AdminID admin_index, ExtendedTextColour colour_code, std::string_view string)
Pass the rcon reply to the admin.
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, ExtendedTextColour colour_code, std::string_view string)
Send an rcon reply to the client.
@ Invalid
Client is not part of anything.
A number of safeguards to prevent using unsafe methods.
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.
Functions related to low-level strings.
Container for the text colour and some text colour related flags for drawing.
–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
–Commands– Commands are commands, or functions.
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.