|
OpenTTD Source
14.0-beta1
|
Go to the documentation of this file.
25 IConsole::CommandList &IConsole::Commands()
27 static IConsole::CommandList cmds;
31 IConsole::AliasList &IConsole::Aliases()
33 static IConsole::AliasList aliases;
37 FILE *_iconsole_output_file;
41 _iconsole_output_file =
nullptr;
47 IConsoleStdLibRegister();
50 static void IConsoleWriteToLogFile(
const std::string &
string)
52 if (_iconsole_output_file !=
nullptr) {
55 fmt::print(_iconsole_output_file,
"{}{}\n",
GetLogPrefix(),
string);
56 }
catch (
const std::system_error &) {
57 fclose(_iconsole_output_file);
58 _iconsole_output_file =
nullptr;
64 bool CloseConsoleLogIfActive()
66 if (_iconsole_output_file !=
nullptr) {
68 fclose(_iconsole_output_file);
69 _iconsole_output_file =
nullptr;
79 CloseConsoleLogIfActive();
114 IConsoleWriteToLogFile(str);
118 IConsoleWriteToLogFile(str);
133 if (strcmp(arg,
"on") == 0 || strcmp(arg,
"true") == 0) {
137 if (strcmp(arg,
"off") == 0 || strcmp(arg,
"false") == 0) {
142 *value = std::strtoul(arg, &endptr, 0);
143 return arg != endptr;
153 name.erase(std::remove(name.begin(), name.end(),
'_'), name.end());
175 if (item != IConsole::Commands().end())
return &item->second;
186 auto result = IConsole::Aliases().try_emplace(
RemoveUnderscores(name), name, cmd);
198 if (item != IConsole::Aliases().end())
return &item->second;
211 std::string alias_buffer;
213 Debug(console, 6,
"Requested command is an alias; parsing...");
220 for (
const char *cmdptr = alias->
cmdline.c_str(); *cmdptr !=
'\0'; cmdptr++) {
223 alias_buffer +=
'\"';
229 alias_buffer.clear();
238 for (uint i = 0; i != tokencount; i++) {
239 if (i != 0) alias_buffer +=
' ';
240 alias_buffer +=
'\"';
241 alias_buffer += tokens[i];
242 alias_buffer +=
'\"';
248 alias_buffer +=
'\"';
249 for (uint i = 0; i != tokencount; i++) {
250 if (i != 0) alias_buffer +=
" ";
251 alias_buffer += tokens[i];
253 alias_buffer +=
'\"';
258 int param = *cmdptr -
'A';
260 if (param < 0 || param >= tokencount) {
266 alias_buffer +=
'\"';
267 alias_buffer += tokens[param];
268 alias_buffer +=
'\"';
275 alias_buffer += *cmdptr;
297 uint t_index, tstream_i;
299 bool longtoken =
false;
300 bool foundtoken =
false;
302 if (command_string[0] ==
'#')
return;
304 for (cmdptr = command_string.c_str(); *cmdptr !=
'\0'; cmdptr++) {
311 Debug(console, 4,
"Executing cmdline: '{}'", command_string);
313 memset(&tokens, 0,
sizeof(tokens));
314 memset(&tokenstream, 0,
sizeof(tokenstream));
319 for (cmdptr = command_string.c_str(), t_index = 0, tstream_i = 0; *cmdptr !=
'\0'; cmdptr++) {
320 if (tstream_i >=
lengthof(tokenstream)) {
327 if (!foundtoken)
break;
330 tokenstream[tstream_i] = *cmdptr;
332 tokenstream[tstream_i] =
'\0';
339 longtoken = !longtoken;
345 tokens[t_index++] = &tokenstream[tstream_i];
350 if (cmdptr[1] ==
'"' && tstream_i + 1 <
lengthof(tokenstream)) {
351 tokenstream[tstream_i++] = *++cmdptr;
356 tokenstream[tstream_i++] = *cmdptr;
363 tokens[t_index++] = &tokenstream[tstream_i - 1];
370 for (uint i = 0; i <
lengthof(tokens) && tokens[i] !=
nullptr; i++) {
371 Debug(console, 8,
"Token {} is: '{}'", i, tokens[i]);
380 if (cmd !=
nullptr) {
384 if (!cmd->
proc(t_index, tokens)) {
385 cmd->
proc(0,
nullptr);
396 if (alias !=
nullptr) {
static const TextColour CC_INFO
Colour for information lines.
IConsoleCmdProc * proc
process executed when command is typed
static void StrMakeValid(T &dst, const char *str, const char *last, StringValidationSettings settings)
Copies the valid (UTF-8) characters from str up to last to the dst.
static IConsoleAlias * AliasGet(const std::string &name)
Find the alias pointed to by its string.
bool GetArgumentInteger(uint32_t *value, const char *arg)
Change a string into its number representation.
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 StrEmpty(const char *s)
Check if a string buffer is empty.
std::string cmdline
command(s) that is/are being aliased
static const AdminIndex INVALID_ADMIN_ID
An invalid admin marker.
AdminIndex _redirect_console_to_admin
Redirection of the (remote) console to the admin.
#define Debug(category, level, format_string,...)
Ouptut a line of debugging information.
IConsoleHook * hook
any special trigger action that needs executing
void NetworkAdminConsole(const std::string_view origin, const std::string_view string)
Send console to the admin network (if they did opt in for the respective update).
static const uint ICON_TOKEN_COUNT
Maximum number of tokens in one command.
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook=nullptr)
Register a new command to be used in the console.
static const uint ICON_MAX_STREAMSIZE
maximum length of a totally expanded command
static const TextColour CC_HELP
Colour for help lines.
void IConsoleCmdExec(const std::string &command_string, const uint recurse_count)
Execute a given command passed to us.
@ CHR_ALLOW
Allow command execution.
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.
static const uint ICON_MAX_RECURSE
Maximum number of recursion.
bool _network_dedicated
are we a dedicated server?
@ CS_ALPHANUMERAL
Both numeric and alphabetic and spaces and stuff.
std::string GetLogPrefix(bool force)
Get the prefix for logs.
std::string name
name of the alias
bool IsValidChar(char32_t key, CharSetFilter afilter)
Only allow certain keys.
static std::string RemoveUnderscores(std::string name)
Creates a copy of a string with underscores removed from it.
static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char *tokens[ICON_TOKEN_COUNT], const uint recurse_count)
An alias is just another name for a command, or for more commands Execute it as well.
static void AliasRegister(const std::string &name, const std::string &cmd)
Register a an alias for an already existing command in the console.
void NetworkServerSendRcon(ClientID client_id, TextColour colour_code, const std::string &string)
Send an rcon reply to the client.
–Aliases– Aliases are like shortcuts for complex functions, variable assignments, etc.
@ INVALID_CLIENT_ID
Client is not part of anything.
static const TextColour CC_ERROR
Colour for error lines.
@ SVS_NONE
Allow nothing and replace nothing.
@ CHR_DISALLOW
Disallow command execution.
#define lengthof(x)
Return the length of an fixed size array.
@ CHR_HIDE
Hide the existence of the command.
bool IConsoleCmdProc(byte argc, char *argv[])
–Commands– Commands are commands, or functions.
ConsoleHookResult
Return values of console hooks (#IConsoleHook).
static IConsoleCmd * CmdGet(const std::string &name)
Find the command pointed to by its string.
void NetworkServerSendAdminRcon(AdminIndex admin_index, TextColour colour_code, const std::string_view string)
Pass the rcon reply to the admin.
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.