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());
150 if (item != IConsole::Commands().end())
return &item->second;
161 auto result = IConsole::Aliases().try_emplace(
RemoveUnderscores(name), name, cmd);
173 if (item != IConsole::Aliases().end())
return &item->second;
186 Debug(console, 6,
"Requested command is an alias; parsing...");
197 while (consumer.AnyBytesLeft()) {
198 auto c = consumer.TryReadUtf8();
199 if (!c.has_value()) {
206 builder.PutChar(
'\"');
216 c = consumer.ReadUtf8();
219 for (
size_t i = 0; i < tokens.size(); ++i) {
220 if (i != 0) builder.PutChar(
' ');
221 builder.PutChar(
'\"');
222 builder += tokens[i];
223 builder.PutChar(
'\"');
229 builder.PutChar(
'\"');
230 for (
size_t i = 0; i < tokens.size(); ++i) {
231 if (i != 0) builder.PutChar(
' ');
232 builder += tokens[i];
234 builder.PutChar(
'\"');
239 size_t param = *c -
'A';
241 if (param >= tokens.size()) {
247 builder.PutChar(
'\"');
248 builder += tokens[param];
249 builder.PutChar(
'\"');
271 if (command_string[0] ==
'#')
return;
273 Debug(console, 4,
"Executing cmdline: '{}'", command_string);
279 std::vector<std::string> tokens;
280 bool found_token =
false;
281 bool in_quotes =
false;
286 while (consumer.AnyBytesLeft()) {
288 if (!c.has_value()) {
295 if (!found_token)
break;
302 tokens.emplace_back(std::move(buffer));
308 in_quotes = !in_quotes;
313 if (consumer.ReadUtf8If(
'"')) {
314 builder.PutUtf8(
'"');
327 tokens.emplace_back(std::move(buffer));
331 for (
size_t i = 0; i < tokens.size(); i++) {
332 Debug(console, 8,
"Token {} is: '{}'", i, tokens[i]);
335 if (tokens.empty() || tokens[0].empty())
return;
341 if (cmd !=
nullptr) {
345 std::vector<std::string_view> views;
346 for (
auto &token : tokens) views.emplace_back(token);
347 if (!cmd->
proc(views)) {
359 if (alias !=
nullptr) {
Compose data into a growing std::string.
Parse data from a string / buffer.
std::optional< char32_t > TryReadUtf8()
Try to read a UTF-8 character, and then advance reader.
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.