mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Use std::string in console commands/aliases registration, and std::map instead our sorted linked list (#9057)
* Codechange: Use std::string in console commands and aliases registration * Codechange: Use std::map to register console commands * Codechange: Use std::map to register console aliases * Cleanup: Remove now unused functionpull/9085/head
parent
d0e40ab314
commit
888389c28d
116
src/console.cpp
116
src/console.cpp
|
@ -24,8 +24,17 @@ static const uint ICON_TOKEN_COUNT = 20; ///< Maximum number of tokens in on
|
||||||
static const uint ICON_MAX_RECURSE = 10; ///< Maximum number of recursion
|
static const uint ICON_MAX_RECURSE = 10; ///< Maximum number of recursion
|
||||||
|
|
||||||
/* console parser */
|
/* console parser */
|
||||||
IConsoleCmd *_iconsole_cmds; ///< list of registered commands
|
/* static */ IConsole::CommandList &IConsole::Commands()
|
||||||
IConsoleAlias *_iconsole_aliases; ///< list of registered aliases
|
{
|
||||||
|
static IConsole::CommandList cmds;
|
||||||
|
return cmds;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* static */ IConsole::AliasList &IConsole::Aliases()
|
||||||
|
{
|
||||||
|
static IConsole::AliasList aliases;
|
||||||
|
return aliases;
|
||||||
|
}
|
||||||
|
|
||||||
FILE *_iconsole_output_file;
|
FILE *_iconsole_output_file;
|
||||||
|
|
||||||
|
@ -195,49 +204,13 @@ bool GetArgumentInteger(uint32 *value, const char *arg)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Add an item to an alphabetically sorted list.
|
* Creates a copy of a string with underscores removed from it
|
||||||
* @param base first item of the list
|
* @param name String to remove the underscores from.
|
||||||
* @param item_new the item to add
|
* @return A copy of \a name, without underscores.
|
||||||
*/
|
*/
|
||||||
template<class T>
|
static std::string RemoveUnderscores(std::string name)
|
||||||
void IConsoleAddSorted(T **base, T *item_new)
|
|
||||||
{
|
{
|
||||||
if (*base == nullptr) {
|
name.erase(std::remove(name.begin(), name.end(), '_'), name.end());
|
||||||
*base = item_new;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
T *item_before = nullptr;
|
|
||||||
T *item = *base;
|
|
||||||
/* The list is alphabetically sorted, insert the new item at the correct location */
|
|
||||||
while (item != nullptr) {
|
|
||||||
if (strcmp(item->name, item_new->name) > 0) break; // insert here
|
|
||||||
|
|
||||||
item_before = item;
|
|
||||||
item = item->next;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (item_before == nullptr) {
|
|
||||||
*base = item_new;
|
|
||||||
} else {
|
|
||||||
item_before->next = item_new;
|
|
||||||
}
|
|
||||||
|
|
||||||
item_new->next = item;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove underscores from a string; the string will be modified!
|
|
||||||
* @param[in,out] name String to remove the underscores from.
|
|
||||||
* @return \a name, with its contents modified.
|
|
||||||
*/
|
|
||||||
char *RemoveUnderscores(char *name)
|
|
||||||
{
|
|
||||||
char *q = name;
|
|
||||||
for (const char *p = name; *p != '\0'; p++) {
|
|
||||||
if (*p != '_') *q++ = *p;
|
|
||||||
}
|
|
||||||
*q = '\0';
|
|
||||||
return name;
|
return name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,15 +219,9 @@ char *RemoveUnderscores(char *name)
|
||||||
* @param name name of the command that will be used
|
* @param name name of the command that will be used
|
||||||
* @param proc function that will be called upon execution of command
|
* @param proc function that will be called upon execution of command
|
||||||
*/
|
*/
|
||||||
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook)
|
/* static */ void IConsole::CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook)
|
||||||
{
|
{
|
||||||
IConsoleCmd *item_new = MallocT<IConsoleCmd>(1);
|
IConsole::Commands().try_emplace(RemoveUnderscores(name), name, proc, hook);
|
||||||
item_new->name = RemoveUnderscores(stredup(name));
|
|
||||||
item_new->next = nullptr;
|
|
||||||
item_new->proc = proc;
|
|
||||||
item_new->hook = hook;
|
|
||||||
|
|
||||||
IConsoleAddSorted(&_iconsole_cmds, item_new);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -262,13 +229,10 @@ void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *
|
||||||
* @param name command to be found
|
* @param name command to be found
|
||||||
* @return return Cmdstruct of the found command, or nullptr on failure
|
* @return return Cmdstruct of the found command, or nullptr on failure
|
||||||
*/
|
*/
|
||||||
IConsoleCmd *IConsoleCmdGet(const char *name)
|
/* static */ IConsoleCmd *IConsole::CmdGet(const std::string &name)
|
||||||
{
|
{
|
||||||
IConsoleCmd *item;
|
auto item = IConsole::Commands().find(RemoveUnderscores(name));
|
||||||
|
if (item != IConsole::Commands().end()) return &item->second;
|
||||||
for (item = _iconsole_cmds; item != nullptr; item = item->next) {
|
|
||||||
if (strcmp(item->name, name) == 0) return item;
|
|
||||||
}
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -277,22 +241,10 @@ IConsoleCmd *IConsoleCmdGet(const char *name)
|
||||||
* @param name name of the alias that will be used
|
* @param name name of the alias that will be used
|
||||||
* @param cmd name of the command that 'name' will be alias of
|
* @param cmd name of the command that 'name' will be alias of
|
||||||
*/
|
*/
|
||||||
void IConsoleAliasRegister(const char *name, const char *cmd)
|
/* static */ void IConsole::AliasRegister(const std::string &name, const std::string &cmd)
|
||||||
{
|
{
|
||||||
if (IConsoleAliasGet(name) != nullptr) {
|
auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd);
|
||||||
IConsoleError("an alias with this name already exists; insertion aborted");
|
if (!result.second) IConsoleError("an alias with this name already exists; insertion aborted");
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
char *new_alias = RemoveUnderscores(stredup(name));
|
|
||||||
char *cmd_aliased = stredup(cmd);
|
|
||||||
IConsoleAlias *item_new = MallocT<IConsoleAlias>(1);
|
|
||||||
|
|
||||||
item_new->next = nullptr;
|
|
||||||
item_new->cmdline = cmd_aliased;
|
|
||||||
item_new->name = new_alias;
|
|
||||||
|
|
||||||
IConsoleAddSorted(&_iconsole_aliases, item_new);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -300,16 +252,13 @@ void IConsoleAliasRegister(const char *name, const char *cmd)
|
||||||
* @param name alias to be found
|
* @param name alias to be found
|
||||||
* @return return Aliasstruct of the found alias, or nullptr on failure
|
* @return return Aliasstruct of the found alias, or nullptr on failure
|
||||||
*/
|
*/
|
||||||
IConsoleAlias *IConsoleAliasGet(const char *name)
|
/* static */ IConsoleAlias *IConsole::AliasGet(const std::string &name)
|
||||||
{
|
{
|
||||||
IConsoleAlias *item;
|
auto item = IConsole::Aliases().find(RemoveUnderscores(name));
|
||||||
|
if (item != IConsole::Aliases().end()) return &item->second;
|
||||||
for (item = _iconsole_aliases; item != nullptr; item = item->next) {
|
|
||||||
if (strcmp(item->name, name) == 0) return item;
|
|
||||||
}
|
|
||||||
|
|
||||||
return nullptr;
|
return nullptr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An alias is just another name for a command, or for more commands
|
* An alias is just another name for a command, or for more commands
|
||||||
* Execute it as well.
|
* Execute it as well.
|
||||||
|
@ -329,7 +278,7 @@ static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const char *cmdptr = alias->cmdline; *cmdptr != '\0'; cmdptr++) {
|
for (const char *cmdptr = alias->cmdline.c_str(); *cmdptr != '\0'; cmdptr++) {
|
||||||
switch (*cmdptr) {
|
switch (*cmdptr) {
|
||||||
case '\'': // ' will double for ""
|
case '\'': // ' will double for ""
|
||||||
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
alias_stream = strecpy(alias_stream, "\"", lastof(alias_buffer));
|
||||||
|
@ -372,7 +321,7 @@ static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char
|
||||||
|
|
||||||
if (param < 0 || param >= tokencount) {
|
if (param < 0 || param >= tokencount) {
|
||||||
IConsoleError("too many or wrong amount of parameters passed to alias, aborting");
|
IConsoleError("too many or wrong amount of parameters passed to alias, aborting");
|
||||||
IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name, alias->cmdline);
|
IConsolePrintF(CC_WARNING, "Usage of alias '%s': %s", alias->name.c_str(), alias->cmdline.c_str());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -491,8 +440,7 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
|
||||||
* First try commands, then aliases. Execute
|
* First try commands, then aliases. Execute
|
||||||
* the found action taking into account its hooking code
|
* the found action taking into account its hooking code
|
||||||
*/
|
*/
|
||||||
RemoveUnderscores(tokens[0]);
|
IConsoleCmd *cmd = IConsole::CmdGet(tokens[0]);
|
||||||
IConsoleCmd *cmd = IConsoleCmdGet(tokens[0]);
|
|
||||||
if (cmd != nullptr) {
|
if (cmd != nullptr) {
|
||||||
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true));
|
||||||
switch (chr) {
|
switch (chr) {
|
||||||
|
@ -508,7 +456,7 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count)
|
||||||
}
|
}
|
||||||
|
|
||||||
t_index--;
|
t_index--;
|
||||||
IConsoleAlias *alias = IConsoleAliasGet(tokens[0]);
|
IConsoleAlias *alias = IConsole::AliasGet(tokens[0]);
|
||||||
if (alias != nullptr) {
|
if (alias != nullptr) {
|
||||||
IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
|
IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1);
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -1416,12 +1416,11 @@ DEF_CONSOLE_CMD(ConAlias)
|
||||||
|
|
||||||
if (argc < 3) return false;
|
if (argc < 3) return false;
|
||||||
|
|
||||||
alias = IConsoleAliasGet(argv[1]);
|
alias = IConsole::AliasGet(argv[1]);
|
||||||
if (alias == nullptr) {
|
if (alias == nullptr) {
|
||||||
IConsoleAliasRegister(argv[1], argv[2]);
|
IConsole::AliasRegister(argv[1], argv[2]);
|
||||||
} else {
|
} else {
|
||||||
free(alias->cmdline);
|
alias->cmdline = argv[2];
|
||||||
alias->cmdline = stredup(argv[2]);
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -1515,13 +1514,13 @@ DEF_CONSOLE_CMD(ConInfoCmd)
|
||||||
|
|
||||||
if (argc < 2) return false;
|
if (argc < 2) return false;
|
||||||
|
|
||||||
const IConsoleCmd *cmd = IConsoleCmdGet(argv[1]);
|
const IConsoleCmd *cmd = IConsole::CmdGet(argv[1]);
|
||||||
if (cmd == nullptr) {
|
if (cmd == nullptr) {
|
||||||
IConsoleError("the given command was not found");
|
IConsoleError("the given command was not found");
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
IConsolePrintF(CC_DEFAULT, "command name: %s", cmd->name);
|
IConsolePrintF(CC_DEFAULT, "command name: %s", cmd->name.c_str());
|
||||||
IConsolePrintF(CC_DEFAULT, "command proc: %p", cmd->proc);
|
IConsolePrintF(CC_DEFAULT, "command proc: %p", cmd->proc);
|
||||||
|
|
||||||
if (cmd->hook != nullptr) IConsoleWarning("command is hooked");
|
if (cmd->hook != nullptr) IConsoleWarning("command is hooked");
|
||||||
|
@ -1580,21 +1579,20 @@ DEF_CONSOLE_CMD(ConHelp)
|
||||||
const IConsoleCmd *cmd;
|
const IConsoleCmd *cmd;
|
||||||
const IConsoleAlias *alias;
|
const IConsoleAlias *alias;
|
||||||
|
|
||||||
RemoveUnderscores(argv[1]);
|
cmd = IConsole::CmdGet(argv[1]);
|
||||||
cmd = IConsoleCmdGet(argv[1]);
|
|
||||||
if (cmd != nullptr) {
|
if (cmd != nullptr) {
|
||||||
cmd->proc(0, nullptr);
|
cmd->proc(0, nullptr);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
alias = IConsoleAliasGet(argv[1]);
|
alias = IConsole::AliasGet(argv[1]);
|
||||||
if (alias != nullptr) {
|
if (alias != nullptr) {
|
||||||
cmd = IConsoleCmdGet(alias->cmdline);
|
cmd = IConsole::CmdGet(alias->cmdline);
|
||||||
if (cmd != nullptr) {
|
if (cmd != nullptr) {
|
||||||
cmd->proc(0, nullptr);
|
cmd->proc(0, nullptr);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
IConsolePrintF(CC_ERROR, "ERROR: alias is of special type, please see its execution-line: '%s'", alias->cmdline);
|
IConsolePrintF(CC_ERROR, "ERROR: alias is of special type, please see its execution-line: '%s'", alias->cmdline.c_str());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1621,9 +1619,10 @@ DEF_CONSOLE_CMD(ConListCommands)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const IConsoleCmd *cmd = _iconsole_cmds; cmd != nullptr; cmd = cmd->next) {
|
for (auto &it : IConsole::Commands()) {
|
||||||
if (argv[1] == nullptr || strstr(cmd->name, argv[1]) != nullptr) {
|
const IConsoleCmd *cmd = &it.second;
|
||||||
if (cmd->hook == nullptr || cmd->hook(false) != CHR_HIDE) IConsolePrintF(CC_DEFAULT, "%s", cmd->name);
|
if (argv[1] == nullptr || cmd->name.find(argv[1]) != std::string::npos) {
|
||||||
|
if (cmd->hook == nullptr || cmd->hook(false) != CHR_HIDE) IConsolePrintF(CC_DEFAULT, "%s", cmd->name.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1637,9 +1636,10 @@ DEF_CONSOLE_CMD(ConListAliases)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (const IConsoleAlias *alias = _iconsole_aliases; alias != nullptr; alias = alias->next) {
|
for (auto &it : IConsole::Aliases()) {
|
||||||
if (argv[1] == nullptr || strstr(alias->name, argv[1]) != nullptr) {
|
const IConsoleAlias *alias = &it.second;
|
||||||
IConsolePrintF(CC_DEFAULT, "%s => %s", alias->name, alias->cmdline);
|
if (argv[1] == nullptr || alias->name.find(argv[1]) != std::string::npos) {
|
||||||
|
IConsolePrintF(CC_DEFAULT, "%s => %s", alias->name.c_str(), alias->cmdline.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2133,9 +2133,9 @@ DEF_CONSOLE_CMD(ConNewGRFProfile)
|
||||||
|
|
||||||
static void IConsoleDebugLibRegister()
|
static void IConsoleDebugLibRegister()
|
||||||
{
|
{
|
||||||
IConsoleCmdRegister("resettile", ConResetTile);
|
IConsole::CmdRegister("resettile", ConResetTile);
|
||||||
IConsoleAliasRegister("dbg_echo", "echo %A; echo %B");
|
IConsole::AliasRegister("dbg_echo", "echo %A; echo %B");
|
||||||
IConsoleAliasRegister("dbg_echo2", "echo %!");
|
IConsole::AliasRegister("dbg_echo2", "echo %!");
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -2329,137 +2329,137 @@ DEF_CONSOLE_CMD(ConDumpInfo)
|
||||||
|
|
||||||
void IConsoleStdLibRegister()
|
void IConsoleStdLibRegister()
|
||||||
{
|
{
|
||||||
IConsoleCmdRegister("debug_level", ConDebugLevel);
|
IConsole::CmdRegister("debug_level", ConDebugLevel);
|
||||||
IConsoleCmdRegister("echo", ConEcho);
|
IConsole::CmdRegister("echo", ConEcho);
|
||||||
IConsoleCmdRegister("echoc", ConEchoC);
|
IConsole::CmdRegister("echoc", ConEchoC);
|
||||||
IConsoleCmdRegister("exec", ConExec);
|
IConsole::CmdRegister("exec", ConExec);
|
||||||
IConsoleCmdRegister("exit", ConExit);
|
IConsole::CmdRegister("exit", ConExit);
|
||||||
IConsoleCmdRegister("part", ConPart);
|
IConsole::CmdRegister("part", ConPart);
|
||||||
IConsoleCmdRegister("help", ConHelp);
|
IConsole::CmdRegister("help", ConHelp);
|
||||||
IConsoleCmdRegister("info_cmd", ConInfoCmd);
|
IConsole::CmdRegister("info_cmd", ConInfoCmd);
|
||||||
IConsoleCmdRegister("list_cmds", ConListCommands);
|
IConsole::CmdRegister("list_cmds", ConListCommands);
|
||||||
IConsoleCmdRegister("list_aliases", ConListAliases);
|
IConsole::CmdRegister("list_aliases", ConListAliases);
|
||||||
IConsoleCmdRegister("newgame", ConNewGame);
|
IConsole::CmdRegister("newgame", ConNewGame);
|
||||||
IConsoleCmdRegister("restart", ConRestart);
|
IConsole::CmdRegister("restart", ConRestart);
|
||||||
IConsoleCmdRegister("reload", ConReload);
|
IConsole::CmdRegister("reload", ConReload);
|
||||||
IConsoleCmdRegister("getseed", ConGetSeed);
|
IConsole::CmdRegister("getseed", ConGetSeed);
|
||||||
IConsoleCmdRegister("getdate", ConGetDate);
|
IConsole::CmdRegister("getdate", ConGetDate);
|
||||||
IConsoleCmdRegister("getsysdate", ConGetSysDate);
|
IConsole::CmdRegister("getsysdate", ConGetSysDate);
|
||||||
IConsoleCmdRegister("quit", ConExit);
|
IConsole::CmdRegister("quit", ConExit);
|
||||||
IConsoleCmdRegister("resetengines", ConResetEngines, ConHookNoNetwork);
|
IConsole::CmdRegister("resetengines", ConResetEngines, ConHookNoNetwork);
|
||||||
IConsoleCmdRegister("reset_enginepool", ConResetEnginePool, ConHookNoNetwork);
|
IConsole::CmdRegister("reset_enginepool", ConResetEnginePool, ConHookNoNetwork);
|
||||||
IConsoleCmdRegister("return", ConReturn);
|
IConsole::CmdRegister("return", ConReturn);
|
||||||
IConsoleCmdRegister("screenshot", ConScreenShot);
|
IConsole::CmdRegister("screenshot", ConScreenShot);
|
||||||
IConsoleCmdRegister("script", ConScript);
|
IConsole::CmdRegister("script", ConScript);
|
||||||
IConsoleCmdRegister("scrollto", ConScrollToTile);
|
IConsole::CmdRegister("scrollto", ConScrollToTile);
|
||||||
IConsoleCmdRegister("alias", ConAlias);
|
IConsole::CmdRegister("alias", ConAlias);
|
||||||
IConsoleCmdRegister("load", ConLoad);
|
IConsole::CmdRegister("load", ConLoad);
|
||||||
IConsoleCmdRegister("rm", ConRemove);
|
IConsole::CmdRegister("rm", ConRemove);
|
||||||
IConsoleCmdRegister("save", ConSave);
|
IConsole::CmdRegister("save", ConSave);
|
||||||
IConsoleCmdRegister("saveconfig", ConSaveConfig);
|
IConsole::CmdRegister("saveconfig", ConSaveConfig);
|
||||||
IConsoleCmdRegister("ls", ConListFiles);
|
IConsole::CmdRegister("ls", ConListFiles);
|
||||||
IConsoleCmdRegister("cd", ConChangeDirectory);
|
IConsole::CmdRegister("cd", ConChangeDirectory);
|
||||||
IConsoleCmdRegister("pwd", ConPrintWorkingDirectory);
|
IConsole::CmdRegister("pwd", ConPrintWorkingDirectory);
|
||||||
IConsoleCmdRegister("clear", ConClearBuffer);
|
IConsole::CmdRegister("clear", ConClearBuffer);
|
||||||
IConsoleCmdRegister("setting", ConSetting);
|
IConsole::CmdRegister("setting", ConSetting);
|
||||||
IConsoleCmdRegister("setting_newgame", ConSettingNewgame);
|
IConsole::CmdRegister("setting_newgame", ConSettingNewgame);
|
||||||
IConsoleCmdRegister("list_settings",ConListSettings);
|
IConsole::CmdRegister("list_settings", ConListSettings);
|
||||||
IConsoleCmdRegister("gamelog", ConGamelogPrint);
|
IConsole::CmdRegister("gamelog", ConGamelogPrint);
|
||||||
IConsoleCmdRegister("rescan_newgrf", ConRescanNewGRF);
|
IConsole::CmdRegister("rescan_newgrf", ConRescanNewGRF);
|
||||||
|
|
||||||
IConsoleAliasRegister("dir", "ls");
|
IConsole::AliasRegister("dir", "ls");
|
||||||
IConsoleAliasRegister("del", "rm %+");
|
IConsole::AliasRegister("del", "rm %+");
|
||||||
IConsoleAliasRegister("newmap", "newgame");
|
IConsole::AliasRegister("newmap", "newgame");
|
||||||
IConsoleAliasRegister("patch", "setting %+");
|
IConsole::AliasRegister("patch", "setting %+");
|
||||||
IConsoleAliasRegister("set", "setting %+");
|
IConsole::AliasRegister("set", "setting %+");
|
||||||
IConsoleAliasRegister("set_newgame", "setting_newgame %+");
|
IConsole::AliasRegister("set_newgame", "setting_newgame %+");
|
||||||
IConsoleAliasRegister("list_patches", "list_settings %+");
|
IConsole::AliasRegister("list_patches", "list_settings %+");
|
||||||
IConsoleAliasRegister("developer", "setting developer %+");
|
IConsole::AliasRegister("developer", "setting developer %+");
|
||||||
|
|
||||||
IConsoleCmdRegister("list_ai_libs", ConListAILibs);
|
IConsole::CmdRegister("list_ai_libs", ConListAILibs);
|
||||||
IConsoleCmdRegister("list_ai", ConListAI);
|
IConsole::CmdRegister("list_ai", ConListAI);
|
||||||
IConsoleCmdRegister("reload_ai", ConReloadAI);
|
IConsole::CmdRegister("reload_ai", ConReloadAI);
|
||||||
IConsoleCmdRegister("rescan_ai", ConRescanAI);
|
IConsole::CmdRegister("rescan_ai", ConRescanAI);
|
||||||
IConsoleCmdRegister("start_ai", ConStartAI);
|
IConsole::CmdRegister("start_ai", ConStartAI);
|
||||||
IConsoleCmdRegister("stop_ai", ConStopAI);
|
IConsole::CmdRegister("stop_ai", ConStopAI);
|
||||||
|
|
||||||
IConsoleCmdRegister("list_game", ConListGame);
|
IConsole::CmdRegister("list_game", ConListGame);
|
||||||
IConsoleCmdRegister("list_game_libs", ConListGameLibs);
|
IConsole::CmdRegister("list_game_libs", ConListGameLibs);
|
||||||
IConsoleCmdRegister("rescan_game", ConRescanGame);
|
IConsole::CmdRegister("rescan_game", ConRescanGame);
|
||||||
|
|
||||||
IConsoleCmdRegister("companies", ConCompanies);
|
IConsole::CmdRegister("companies", ConCompanies);
|
||||||
IConsoleAliasRegister("players", "companies");
|
IConsole::AliasRegister("players", "companies");
|
||||||
|
|
||||||
/* networking functions */
|
/* networking functions */
|
||||||
|
|
||||||
/* Content downloading is only available with ZLIB */
|
/* Content downloading is only available with ZLIB */
|
||||||
#if defined(WITH_ZLIB)
|
#if defined(WITH_ZLIB)
|
||||||
IConsoleCmdRegister("content", ConContent);
|
IConsole::CmdRegister("content", ConContent);
|
||||||
#endif /* defined(WITH_ZLIB) */
|
#endif /* defined(WITH_ZLIB) */
|
||||||
|
|
||||||
/*** Networking commands ***/
|
/*** Networking commands ***/
|
||||||
IConsoleCmdRegister("say", ConSay, ConHookNeedNetwork);
|
IConsole::CmdRegister("say", ConSay, ConHookNeedNetwork);
|
||||||
IConsoleCmdRegister("say_company", ConSayCompany, ConHookNeedNetwork);
|
IConsole::CmdRegister("say_company", ConSayCompany, ConHookNeedNetwork);
|
||||||
IConsoleAliasRegister("say_player", "say_company %+");
|
IConsole::AliasRegister("say_player", "say_company %+");
|
||||||
IConsoleCmdRegister("say_client", ConSayClient, ConHookNeedNetwork);
|
IConsole::CmdRegister("say_client", ConSayClient, ConHookNeedNetwork);
|
||||||
|
|
||||||
IConsoleCmdRegister("connect", ConNetworkConnect, ConHookClientOnly);
|
IConsole::CmdRegister("connect", ConNetworkConnect, ConHookClientOnly);
|
||||||
IConsoleCmdRegister("clients", ConNetworkClients, ConHookNeedNetwork);
|
IConsole::CmdRegister("clients", ConNetworkClients, ConHookNeedNetwork);
|
||||||
IConsoleCmdRegister("status", ConStatus, ConHookServerOnly);
|
IConsole::CmdRegister("status", ConStatus, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("server_info", ConServerInfo, ConHookServerOnly);
|
IConsole::CmdRegister("server_info", ConServerInfo, ConHookServerOnly);
|
||||||
IConsoleAliasRegister("info", "server_info");
|
IConsole::AliasRegister("info", "server_info");
|
||||||
IConsoleCmdRegister("reconnect", ConNetworkReconnect, ConHookClientOnly);
|
IConsole::CmdRegister("reconnect", ConNetworkReconnect, ConHookClientOnly);
|
||||||
IConsoleCmdRegister("rcon", ConRcon, ConHookNeedNetwork);
|
IConsole::CmdRegister("rcon", ConRcon, ConHookNeedNetwork);
|
||||||
|
|
||||||
IConsoleCmdRegister("join", ConJoinCompany, ConHookNeedNetwork);
|
IConsole::CmdRegister("join", ConJoinCompany, ConHookNeedNetwork);
|
||||||
IConsoleAliasRegister("spectate", "join 255");
|
IConsole::AliasRegister("spectate", "join 255");
|
||||||
IConsoleCmdRegister("move", ConMoveClient, ConHookServerOnly);
|
IConsole::CmdRegister("move", ConMoveClient, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("reset_company", ConResetCompany, ConHookServerOnly);
|
IConsole::CmdRegister("reset_company", ConResetCompany, ConHookServerOnly);
|
||||||
IConsoleAliasRegister("clean_company", "reset_company %A");
|
IConsole::AliasRegister("clean_company", "reset_company %A");
|
||||||
IConsoleCmdRegister("client_name", ConClientNickChange, ConHookServerOnly);
|
IConsole::CmdRegister("client_name", ConClientNickChange, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("kick", ConKick, ConHookServerOnly);
|
IConsole::CmdRegister("kick", ConKick, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("ban", ConBan, ConHookServerOnly);
|
IConsole::CmdRegister("ban", ConBan, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("unban", ConUnBan, ConHookServerOnly);
|
IConsole::CmdRegister("unban", ConUnBan, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("banlist", ConBanList, ConHookServerOnly);
|
IConsole::CmdRegister("banlist", ConBanList, ConHookServerOnly);
|
||||||
|
|
||||||
IConsoleCmdRegister("pause", ConPauseGame, ConHookServerOnly);
|
IConsole::CmdRegister("pause", ConPauseGame, ConHookServerOnly);
|
||||||
IConsoleCmdRegister("unpause", ConUnpauseGame, ConHookServerOnly);
|
IConsole::CmdRegister("unpause", ConUnpauseGame, ConHookServerOnly);
|
||||||
|
|
||||||
IConsoleCmdRegister("company_pw", ConCompanyPassword, ConHookNeedNetwork);
|
IConsole::CmdRegister("company_pw", ConCompanyPassword, ConHookNeedNetwork);
|
||||||
IConsoleAliasRegister("company_password", "company_pw %+");
|
IConsole::AliasRegister("company_password", "company_pw %+");
|
||||||
|
|
||||||
IConsoleAliasRegister("net_frame_freq", "setting frame_freq %+");
|
IConsole::AliasRegister("net_frame_freq", "setting frame_freq %+");
|
||||||
IConsoleAliasRegister("net_sync_freq", "setting sync_freq %+");
|
IConsole::AliasRegister("net_sync_freq", "setting sync_freq %+");
|
||||||
IConsoleAliasRegister("server_pw", "setting server_password %+");
|
IConsole::AliasRegister("server_pw", "setting server_password %+");
|
||||||
IConsoleAliasRegister("server_password", "setting server_password %+");
|
IConsole::AliasRegister("server_password", "setting server_password %+");
|
||||||
IConsoleAliasRegister("rcon_pw", "setting rcon_password %+");
|
IConsole::AliasRegister("rcon_pw", "setting rcon_password %+");
|
||||||
IConsoleAliasRegister("rcon_password", "setting rcon_password %+");
|
IConsole::AliasRegister("rcon_password", "setting rcon_password %+");
|
||||||
IConsoleAliasRegister("name", "setting client_name %+");
|
IConsole::AliasRegister("name", "setting client_name %+");
|
||||||
IConsoleAliasRegister("server_name", "setting server_name %+");
|
IConsole::AliasRegister("server_name", "setting server_name %+");
|
||||||
IConsoleAliasRegister("server_port", "setting server_port %+");
|
IConsole::AliasRegister("server_port", "setting server_port %+");
|
||||||
IConsoleAliasRegister("server_advertise", "setting server_advertise %+");
|
IConsole::AliasRegister("server_advertise", "setting server_advertise %+");
|
||||||
IConsoleAliasRegister("max_clients", "setting max_clients %+");
|
IConsole::AliasRegister("max_clients", "setting max_clients %+");
|
||||||
IConsoleAliasRegister("max_companies", "setting max_companies %+");
|
IConsole::AliasRegister("max_companies", "setting max_companies %+");
|
||||||
IConsoleAliasRegister("max_spectators", "setting max_spectators %+");
|
IConsole::AliasRegister("max_spectators", "setting max_spectators %+");
|
||||||
IConsoleAliasRegister("max_join_time", "setting max_join_time %+");
|
IConsole::AliasRegister("max_join_time", "setting max_join_time %+");
|
||||||
IConsoleAliasRegister("pause_on_join", "setting pause_on_join %+");
|
IConsole::AliasRegister("pause_on_join", "setting pause_on_join %+");
|
||||||
IConsoleAliasRegister("autoclean_companies", "setting autoclean_companies %+");
|
IConsole::AliasRegister("autoclean_companies", "setting autoclean_companies %+");
|
||||||
IConsoleAliasRegister("autoclean_protected", "setting autoclean_protected %+");
|
IConsole::AliasRegister("autoclean_protected", "setting autoclean_protected %+");
|
||||||
IConsoleAliasRegister("autoclean_unprotected", "setting autoclean_unprotected %+");
|
IConsole::AliasRegister("autoclean_unprotected", "setting autoclean_unprotected %+");
|
||||||
IConsoleAliasRegister("restart_game_year", "setting restart_game_year %+");
|
IConsole::AliasRegister("restart_game_year", "setting restart_game_year %+");
|
||||||
IConsoleAliasRegister("min_players", "setting min_active_clients %+");
|
IConsole::AliasRegister("min_players", "setting min_active_clients %+");
|
||||||
IConsoleAliasRegister("reload_cfg", "setting reload_cfg %+");
|
IConsole::AliasRegister("reload_cfg", "setting reload_cfg %+");
|
||||||
|
|
||||||
/* debugging stuff */
|
/* debugging stuff */
|
||||||
#ifdef _DEBUG
|
#ifdef _DEBUG
|
||||||
IConsoleDebugLibRegister();
|
IConsoleDebugLibRegister();
|
||||||
#endif
|
#endif
|
||||||
IConsoleCmdRegister("fps", ConFramerate);
|
IConsole::CmdRegister("fps", ConFramerate);
|
||||||
IConsoleCmdRegister("fps_wnd", ConFramerateWindow);
|
IConsole::CmdRegister("fps_wnd", ConFramerateWindow);
|
||||||
|
|
||||||
/* NewGRF development stuff */
|
/* NewGRF development stuff */
|
||||||
IConsoleCmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool);
|
IConsole::CmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool);
|
||||||
IConsoleCmdRegister("newgrf_profile", ConNewGRFProfile, ConHookNewGRFDeveloperTool);
|
IConsole::CmdRegister("newgrf_profile", ConNewGRFProfile, ConHookNewGRFDeveloperTool);
|
||||||
|
|
||||||
IConsoleCmdRegister("dump_info", ConDumpInfo);
|
IConsole::CmdRegister("dump_info", ConDumpInfo);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@
|
||||||
#define CONSOLE_INTERNAL_H
|
#define CONSOLE_INTERNAL_H
|
||||||
|
|
||||||
#include "gfx_type.h"
|
#include "gfx_type.h"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
static const uint ICON_CMDLN_SIZE = 1024; ///< maximum length of a typed in command
|
static const uint ICON_CMDLN_SIZE = 1024; ///< maximum length of a typed in command
|
||||||
static const uint ICON_MAX_STREAMSIZE = 2048; ///< maximum length of a totally expanded command
|
static const uint ICON_MAX_STREAMSIZE = 2048; ///< maximum length of a totally expanded command
|
||||||
|
@ -33,9 +34,9 @@ enum ConsoleHookResult {
|
||||||
typedef bool IConsoleCmdProc(byte argc, char *argv[]);
|
typedef bool IConsoleCmdProc(byte argc, char *argv[]);
|
||||||
typedef ConsoleHookResult IConsoleHook(bool echo);
|
typedef ConsoleHookResult IConsoleHook(bool echo);
|
||||||
struct IConsoleCmd {
|
struct IConsoleCmd {
|
||||||
char *name; ///< name of command
|
IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook) : name(name), proc(proc), hook(hook) {}
|
||||||
IConsoleCmd *next; ///< next command in list
|
|
||||||
|
|
||||||
|
std::string name; ///< name of command
|
||||||
IConsoleCmdProc *proc; ///< process executed when command is typed
|
IConsoleCmdProc *proc; ///< process executed when command is typed
|
||||||
IConsoleHook *hook; ///< any special trigger action that needs executing
|
IConsoleHook *hook; ///< any special trigger action that needs executing
|
||||||
};
|
};
|
||||||
|
@ -53,25 +54,31 @@ struct IConsoleCmd {
|
||||||
* - ";" allows for combining commands (see example 'ng')
|
* - ";" allows for combining commands (see example 'ng')
|
||||||
*/
|
*/
|
||||||
struct IConsoleAlias {
|
struct IConsoleAlias {
|
||||||
char *name; ///< name of the alias
|
IConsoleAlias(const std::string &name, const std::string &cmdline) : name(name), cmdline(cmdline) {}
|
||||||
IConsoleAlias *next; ///< next alias in list
|
|
||||||
|
|
||||||
char *cmdline; ///< command(s) that is/are being aliased
|
std::string name; ///< name of the alias
|
||||||
|
std::string cmdline; ///< command(s) that is/are being aliased
|
||||||
};
|
};
|
||||||
|
|
||||||
/* console parser */
|
struct IConsole
|
||||||
extern IConsoleCmd *_iconsole_cmds; ///< List of registered commands.
|
{
|
||||||
extern IConsoleAlias *_iconsole_aliases; ///< List of registered aliases.
|
typedef std::map<std::string, IConsoleCmd> CommandList;
|
||||||
|
typedef std::map<std::string, IConsoleAlias> AliasList;
|
||||||
|
|
||||||
|
/* console parser */
|
||||||
|
static CommandList &Commands();
|
||||||
|
static AliasList &Aliases();
|
||||||
|
|
||||||
|
/* Commands */
|
||||||
|
static void CmdRegister(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr);
|
||||||
|
static IConsoleCmd *CmdGet(const std::string &name);
|
||||||
|
static void AliasRegister(const std::string &name, const std::string &cmd);
|
||||||
|
static IConsoleAlias *AliasGet(const std::string &name);
|
||||||
|
};
|
||||||
|
|
||||||
/* console functions */
|
/* console functions */
|
||||||
void IConsoleClearBuffer();
|
void IConsoleClearBuffer();
|
||||||
|
|
||||||
/* Commands */
|
|
||||||
void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook *hook = nullptr);
|
|
||||||
void IConsoleAliasRegister(const char *name, const char *cmd);
|
|
||||||
IConsoleCmd *IConsoleCmdGet(const char *name);
|
|
||||||
IConsoleAlias *IConsoleAliasGet(const char *name);
|
|
||||||
|
|
||||||
/* console std lib (register ingame commands/aliases) */
|
/* console std lib (register ingame commands/aliases) */
|
||||||
void IConsoleStdLibRegister();
|
void IConsoleStdLibRegister();
|
||||||
|
|
||||||
|
@ -81,6 +88,5 @@ bool GetArgumentInteger(uint32 *value, const char *arg);
|
||||||
void IConsoleGUIInit();
|
void IConsoleGUIInit();
|
||||||
void IConsoleGUIFree();
|
void IConsoleGUIFree();
|
||||||
void IConsoleGUIPrint(TextColour colour_code, char *string);
|
void IConsoleGUIPrint(TextColour colour_code, char *string);
|
||||||
char *RemoveUnderscores(char *name);
|
|
||||||
|
|
||||||
#endif /* CONSOLE_INTERNAL_H */
|
#endif /* CONSOLE_INTERNAL_H */
|
||||||
|
|
|
@ -1142,7 +1142,7 @@ static void RegisterConsoleMidiCommands()
|
||||||
{
|
{
|
||||||
static bool registered = false;
|
static bool registered = false;
|
||||||
if (!registered) {
|
if (!registered) {
|
||||||
IConsoleCmdRegister("dumpsmf", CmdDumpSMF);
|
IConsole::CmdRegister("dumpsmf", CmdDumpSMF);
|
||||||
registered = true;
|
registered = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue