diff options
author | Loïc Guilloux <glx22@users.noreply.github.com> | 2021-04-24 15:19:57 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-04-24 15:19:57 +0200 |
commit | 888389c28d5f3ad06154e4d2e36de568b6f7966e (patch) | |
tree | b4b342d27ef45202fed571440846e5581a143edf | |
parent | d0e40ab31487cdf59c2fb638bcf553f166f8faf1 (diff) | |
download | openttd-888389c28d5f3ad06154e4d2e36de568b6f7966e.tar.xz |
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 function
-rw-r--r-- | src/console.cpp | 116 | ||||
-rw-r--r-- | src/console_cmds.cpp | 274 | ||||
-rw-r--r-- | src/console_internal.h | 36 | ||||
-rw-r--r-- | src/music/midifile.cpp | 2 |
4 files changed, 191 insertions, 237 deletions
diff --git a/src/console.cpp b/src/console.cpp index 3c782357d..fbd34c197 100644 --- a/src/console.cpp +++ b/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 /* console parser */ -IConsoleCmd *_iconsole_cmds; ///< list of registered commands -IConsoleAlias *_iconsole_aliases; ///< list of registered aliases +/* static */ IConsole::CommandList &IConsole::Commands() +{ + static IConsole::CommandList cmds; + return cmds; +} + +/* static */ IConsole::AliasList &IConsole::Aliases() +{ + static IConsole::AliasList aliases; + return aliases; +} FILE *_iconsole_output_file; @@ -195,49 +204,13 @@ bool GetArgumentInteger(uint32 *value, const char *arg) } /** - * Add an item to an alphabetically sorted list. - * @param base first item of the list - * @param item_new the item to add + * Creates a copy of a string with underscores removed from it + * @param name String to remove the underscores from. + * @return A copy of \a name, without underscores. */ -template<class T> -void IConsoleAddSorted(T **base, T *item_new) +static std::string RemoveUnderscores(std::string name) { - if (*base == nullptr) { - *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'; + name.erase(std::remove(name.begin(), name.end(), '_'), name.end()); return name; } @@ -246,15 +219,9 @@ char *RemoveUnderscores(char *name) * @param name name of the command that will be used * @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); - item_new->name = RemoveUnderscores(stredup(name)); - item_new->next = nullptr; - item_new->proc = proc; - item_new->hook = hook; - - IConsoleAddSorted(&_iconsole_cmds, item_new); + IConsole::Commands().try_emplace(RemoveUnderscores(name), name, proc, hook); } /** @@ -262,13 +229,10 @@ void IConsoleCmdRegister(const char *name, IConsoleCmdProc *proc, IConsoleHook * * @param name command to be found * @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; - - for (item = _iconsole_cmds; item != nullptr; item = item->next) { - if (strcmp(item->name, name) == 0) return item; - } + auto item = IConsole::Commands().find(RemoveUnderscores(name)); + if (item != IConsole::Commands().end()) return &item->second; return nullptr; } @@ -277,22 +241,10 @@ IConsoleCmd *IConsoleCmdGet(const char *name) * @param name name of the alias that will be used * @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) { - 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); + auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd); + if (!result.second) IConsoleError("an alias with this name already exists; insertion aborted"); } /** @@ -300,16 +252,13 @@ void IConsoleAliasRegister(const char *name, const char *cmd) * @param name alias to be found * @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; - - for (item = _iconsole_aliases; item != nullptr; item = item->next) { - if (strcmp(item->name, name) == 0) return item; - } - + auto item = IConsole::Aliases().find(RemoveUnderscores(name)); + if (item != IConsole::Aliases().end()) return &item->second; return nullptr; } + /** * An alias is just another name for a command, or for more commands * Execute it as well. @@ -329,7 +278,7 @@ static void IConsoleAliasExec(const IConsoleAlias *alias, byte tokencount, char return; } - for (const char *cmdptr = alias->cmdline; *cmdptr != '\0'; cmdptr++) { + for (const char *cmdptr = alias->cmdline.c_str(); *cmdptr != '\0'; cmdptr++) { switch (*cmdptr) { case '\'': // ' will double for "" 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) { 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; } @@ -491,8 +440,7 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count) * First try commands, then aliases. Execute * the found action taking into account its hooking code */ - RemoveUnderscores(tokens[0]); - IConsoleCmd *cmd = IConsoleCmdGet(tokens[0]); + IConsoleCmd *cmd = IConsole::CmdGet(tokens[0]); if (cmd != nullptr) { ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true)); switch (chr) { @@ -508,7 +456,7 @@ void IConsoleCmdExec(const char *cmdstr, const uint recurse_count) } t_index--; - IConsoleAlias *alias = IConsoleAliasGet(tokens[0]); + IConsoleAlias *alias = IConsole::AliasGet(tokens[0]); if (alias != nullptr) { IConsoleAliasExec(alias, t_index, &tokens[1], recurse_count + 1); return; diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 305444f8c..f8d9eb1b0 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1416,12 +1416,11 @@ DEF_CONSOLE_CMD(ConAlias) if (argc < 3) return false; - alias = IConsoleAliasGet(argv[1]); + alias = IConsole::AliasGet(argv[1]); if (alias == nullptr) { - IConsoleAliasRegister(argv[1], argv[2]); + IConsole::AliasRegister(argv[1], argv[2]); } else { - free(alias->cmdline); - alias->cmdline = stredup(argv[2]); + alias->cmdline = argv[2]; } return true; } @@ -1515,13 +1514,13 @@ DEF_CONSOLE_CMD(ConInfoCmd) if (argc < 2) return false; - const IConsoleCmd *cmd = IConsoleCmdGet(argv[1]); + const IConsoleCmd *cmd = IConsole::CmdGet(argv[1]); if (cmd == nullptr) { IConsoleError("the given command was not found"); 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); if (cmd->hook != nullptr) IConsoleWarning("command is hooked"); @@ -1580,21 +1579,20 @@ DEF_CONSOLE_CMD(ConHelp) const IConsoleCmd *cmd; const IConsoleAlias *alias; - RemoveUnderscores(argv[1]); - cmd = IConsoleCmdGet(argv[1]); + cmd = IConsole::CmdGet(argv[1]); if (cmd != nullptr) { cmd->proc(0, nullptr); return true; } - alias = IConsoleAliasGet(argv[1]); + alias = IConsole::AliasGet(argv[1]); if (alias != nullptr) { - cmd = IConsoleCmdGet(alias->cmdline); + cmd = IConsole::CmdGet(alias->cmdline); if (cmd != nullptr) { cmd->proc(0, nullptr); 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; } @@ -1621,9 +1619,10 @@ DEF_CONSOLE_CMD(ConListCommands) return true; } - for (const IConsoleCmd *cmd = _iconsole_cmds; cmd != nullptr; cmd = cmd->next) { - if (argv[1] == nullptr || strstr(cmd->name, argv[1]) != nullptr) { - if (cmd->hook == nullptr || cmd->hook(false) != CHR_HIDE) IConsolePrintF(CC_DEFAULT, "%s", cmd->name); + for (auto &it : IConsole::Commands()) { + const IConsoleCmd *cmd = &it.second; + 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; } - for (const IConsoleAlias *alias = _iconsole_aliases; alias != nullptr; alias = alias->next) { - if (argv[1] == nullptr || strstr(alias->name, argv[1]) != nullptr) { - IConsolePrintF(CC_DEFAULT, "%s => %s", alias->name, alias->cmdline); + for (auto &it : IConsole::Aliases()) { + const IConsoleAlias *alias = &it.second; + 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() { - IConsoleCmdRegister("resettile", ConResetTile); - IConsoleAliasRegister("dbg_echo", "echo %A; echo %B"); - IConsoleAliasRegister("dbg_echo2", "echo %!"); + IConsole::CmdRegister("resettile", ConResetTile); + IConsole::AliasRegister("dbg_echo", "echo %A; echo %B"); + IConsole::AliasRegister("dbg_echo2", "echo %!"); } #endif @@ -2329,137 +2329,137 @@ DEF_CONSOLE_CMD(ConDumpInfo) void IConsoleStdLibRegister() { - IConsoleCmdRegister("debug_level", ConDebugLevel); - IConsoleCmdRegister("echo", ConEcho); - IConsoleCmdRegister("echoc", ConEchoC); - IConsoleCmdRegister("exec", ConExec); - IConsoleCmdRegister("exit", ConExit); - IConsoleCmdRegister("part", ConPart); - IConsoleCmdRegister("help", ConHelp); - IConsoleCmdRegister("info_cmd", ConInfoCmd); - IConsoleCmdRegister("list_cmds", ConListCommands); - IConsoleCmdRegister("list_aliases", ConListAliases); - IConsoleCmdRegister("newgame", ConNewGame); - IConsoleCmdRegister("restart", ConRestart); - IConsoleCmdRegister("reload", ConReload); - IConsoleCmdRegister("getseed", ConGetSeed); - IConsoleCmdRegister("getdate", ConGetDate); - IConsoleCmdRegister("getsysdate", ConGetSysDate); - IConsoleCmdRegister("quit", ConExit); - IConsoleCmdRegister("resetengines", ConResetEngines, ConHookNoNetwork); - IConsoleCmdRegister("reset_enginepool", ConResetEnginePool, ConHookNoNetwork); - IConsoleCmdRegister("return", ConReturn); - IConsoleCmdRegister("screenshot", ConScreenShot); - IConsoleCmdRegister("script", ConScript); - IConsoleCmdRegister("scrollto", ConScrollToTile); - IConsoleCmdRegister("alias", ConAlias); - IConsoleCmdRegister("load", ConLoad); - IConsoleCmdRegister("rm", ConRemove); - IConsoleCmdRegister("save", ConSave); - IConsoleCmdRegister("saveconfig", ConSaveConfig); - IConsoleCmdRegister("ls", ConListFiles); - IConsoleCmdRegister("cd", ConChangeDirectory); - IConsoleCmdRegister("pwd", ConPrintWorkingDirectory); - IConsoleCmdRegister("clear", ConClearBuffer); - IConsoleCmdRegister("setting", ConSetting); - IConsoleCmdRegister("setting_newgame", ConSettingNewgame); - IConsoleCmdRegister("list_settings",ConListSettings); - IConsoleCmdRegister("gamelog", ConGamelogPrint); - IConsoleCmdRegister("rescan_newgrf", ConRescanNewGRF); - - IConsoleAliasRegister("dir", "ls"); - IConsoleAliasRegister("del", "rm %+"); - IConsoleAliasRegister("newmap", "newgame"); - IConsoleAliasRegister("patch", "setting %+"); - IConsoleAliasRegister("set", "setting %+"); - IConsoleAliasRegister("set_newgame", "setting_newgame %+"); - IConsoleAliasRegister("list_patches", "list_settings %+"); - IConsoleAliasRegister("developer", "setting developer %+"); - - IConsoleCmdRegister("list_ai_libs", ConListAILibs); - IConsoleCmdRegister("list_ai", ConListAI); - IConsoleCmdRegister("reload_ai", ConReloadAI); - IConsoleCmdRegister("rescan_ai", ConRescanAI); - IConsoleCmdRegister("start_ai", ConStartAI); - IConsoleCmdRegister("stop_ai", ConStopAI); - - IConsoleCmdRegister("list_game", ConListGame); - IConsoleCmdRegister("list_game_libs", ConListGameLibs); - IConsoleCmdRegister("rescan_game", ConRescanGame); - - IConsoleCmdRegister("companies", ConCompanies); - IConsoleAliasRegister("players", "companies"); + IConsole::CmdRegister("debug_level", ConDebugLevel); + IConsole::CmdRegister("echo", ConEcho); + IConsole::CmdRegister("echoc", ConEchoC); + IConsole::CmdRegister("exec", ConExec); + IConsole::CmdRegister("exit", ConExit); + IConsole::CmdRegister("part", ConPart); + IConsole::CmdRegister("help", ConHelp); + IConsole::CmdRegister("info_cmd", ConInfoCmd); + IConsole::CmdRegister("list_cmds", ConListCommands); + IConsole::CmdRegister("list_aliases", ConListAliases); + IConsole::CmdRegister("newgame", ConNewGame); + IConsole::CmdRegister("restart", ConRestart); + IConsole::CmdRegister("reload", ConReload); + IConsole::CmdRegister("getseed", ConGetSeed); + IConsole::CmdRegister("getdate", ConGetDate); + IConsole::CmdRegister("getsysdate", ConGetSysDate); + IConsole::CmdRegister("quit", ConExit); + IConsole::CmdRegister("resetengines", ConResetEngines, ConHookNoNetwork); + IConsole::CmdRegister("reset_enginepool", ConResetEnginePool, ConHookNoNetwork); + IConsole::CmdRegister("return", ConReturn); + IConsole::CmdRegister("screenshot", ConScreenShot); + IConsole::CmdRegister("script", ConScript); + IConsole::CmdRegister("scrollto", ConScrollToTile); + IConsole::CmdRegister("alias", ConAlias); + IConsole::CmdRegister("load", ConLoad); + IConsole::CmdRegister("rm", ConRemove); + IConsole::CmdRegister("save", ConSave); + IConsole::CmdRegister("saveconfig", ConSaveConfig); + IConsole::CmdRegister("ls", ConListFiles); + IConsole::CmdRegister("cd", ConChangeDirectory); + IConsole::CmdRegister("pwd", ConPrintWorkingDirectory); + IConsole::CmdRegister("clear", ConClearBuffer); + IConsole::CmdRegister("setting", ConSetting); + IConsole::CmdRegister("setting_newgame", ConSettingNewgame); + IConsole::CmdRegister("list_settings", ConListSettings); + IConsole::CmdRegister("gamelog", ConGamelogPrint); + IConsole::CmdRegister("rescan_newgrf", ConRescanNewGRF); + + IConsole::AliasRegister("dir", "ls"); + IConsole::AliasRegister("del", "rm %+"); + IConsole::AliasRegister("newmap", "newgame"); + IConsole::AliasRegister("patch", "setting %+"); + IConsole::AliasRegister("set", "setting %+"); + IConsole::AliasRegister("set_newgame", "setting_newgame %+"); + IConsole::AliasRegister("list_patches", "list_settings %+"); + IConsole::AliasRegister("developer", "setting developer %+"); + + IConsole::CmdRegister("list_ai_libs", ConListAILibs); + IConsole::CmdRegister("list_ai", ConListAI); + IConsole::CmdRegister("reload_ai", ConReloadAI); + IConsole::CmdRegister("rescan_ai", ConRescanAI); + IConsole::CmdRegister("start_ai", ConStartAI); + IConsole::CmdRegister("stop_ai", ConStopAI); + + IConsole::CmdRegister("list_game", ConListGame); + IConsole::CmdRegister("list_game_libs", ConListGameLibs); + IConsole::CmdRegister("rescan_game", ConRescanGame); + + IConsole::CmdRegister("companies", ConCompanies); + IConsole::AliasRegister("players", "companies"); /* networking functions */ /* Content downloading is only available with ZLIB */ #if defined(WITH_ZLIB) - IConsoleCmdRegister("content", ConContent); + IConsole::CmdRegister("content", ConContent); #endif /* defined(WITH_ZLIB) */ /*** Networking commands ***/ - IConsoleCmdRegister("say", ConSay, ConHookNeedNetwork); - IConsoleCmdRegister("say_company", ConSayCompany, ConHookNeedNetwork); - IConsoleAliasRegister("say_player", "say_company %+"); - IConsoleCmdRegister("say_client", ConSayClient, ConHookNeedNetwork); - - IConsoleCmdRegister("connect", ConNetworkConnect, ConHookClientOnly); - IConsoleCmdRegister("clients", ConNetworkClients, ConHookNeedNetwork); - IConsoleCmdRegister("status", ConStatus, ConHookServerOnly); - IConsoleCmdRegister("server_info", ConServerInfo, ConHookServerOnly); - IConsoleAliasRegister("info", "server_info"); - IConsoleCmdRegister("reconnect", ConNetworkReconnect, ConHookClientOnly); - IConsoleCmdRegister("rcon", ConRcon, ConHookNeedNetwork); - - IConsoleCmdRegister("join", ConJoinCompany, ConHookNeedNetwork); - IConsoleAliasRegister("spectate", "join 255"); - IConsoleCmdRegister("move", ConMoveClient, ConHookServerOnly); - IConsoleCmdRegister("reset_company", ConResetCompany, ConHookServerOnly); - IConsoleAliasRegister("clean_company", "reset_company %A"); - IConsoleCmdRegister("client_name", ConClientNickChange, ConHookServerOnly); - IConsoleCmdRegister("kick", ConKick, ConHookServerOnly); - IConsoleCmdRegister("ban", ConBan, ConHookServerOnly); - IConsoleCmdRegister("unban", ConUnBan, ConHookServerOnly); - IConsoleCmdRegister("banlist", ConBanList, ConHookServerOnly); - - IConsoleCmdRegister("pause", ConPauseGame, ConHookServerOnly); - IConsoleCmdRegister("unpause", ConUnpauseGame, ConHookServerOnly); - - IConsoleCmdRegister("company_pw", ConCompanyPassword, ConHookNeedNetwork); - IConsoleAliasRegister("company_password", "company_pw %+"); - - IConsoleAliasRegister("net_frame_freq", "setting frame_freq %+"); - IConsoleAliasRegister("net_sync_freq", "setting sync_freq %+"); - IConsoleAliasRegister("server_pw", "setting server_password %+"); - IConsoleAliasRegister("server_password", "setting server_password %+"); - IConsoleAliasRegister("rcon_pw", "setting rcon_password %+"); - IConsoleAliasRegister("rcon_password", "setting rcon_password %+"); - IConsoleAliasRegister("name", "setting client_name %+"); - IConsoleAliasRegister("server_name", "setting server_name %+"); - IConsoleAliasRegister("server_port", "setting server_port %+"); - IConsoleAliasRegister("server_advertise", "setting server_advertise %+"); - IConsoleAliasRegister("max_clients", "setting max_clients %+"); - IConsoleAliasRegister("max_companies", "setting max_companies %+"); - IConsoleAliasRegister("max_spectators", "setting max_spectators %+"); - IConsoleAliasRegister("max_join_time", "setting max_join_time %+"); - IConsoleAliasRegister("pause_on_join", "setting pause_on_join %+"); - IConsoleAliasRegister("autoclean_companies", "setting autoclean_companies %+"); - IConsoleAliasRegister("autoclean_protected", "setting autoclean_protected %+"); - IConsoleAliasRegister("autoclean_unprotected", "setting autoclean_unprotected %+"); - IConsoleAliasRegister("restart_game_year", "setting restart_game_year %+"); - IConsoleAliasRegister("min_players", "setting min_active_clients %+"); - IConsoleAliasRegister("reload_cfg", "setting reload_cfg %+"); + IConsole::CmdRegister("say", ConSay, ConHookNeedNetwork); + IConsole::CmdRegister("say_company", ConSayCompany, ConHookNeedNetwork); + IConsole::AliasRegister("say_player", "say_company %+"); + IConsole::CmdRegister("say_client", ConSayClient, ConHookNeedNetwork); + + IConsole::CmdRegister("connect", ConNetworkConnect, ConHookClientOnly); + IConsole::CmdRegister("clients", ConNetworkClients, ConHookNeedNetwork); + IConsole::CmdRegister("status", ConStatus, ConHookServerOnly); + IConsole::CmdRegister("server_info", ConServerInfo, ConHookServerOnly); + IConsole::AliasRegister("info", "server_info"); + IConsole::CmdRegister("reconnect", ConNetworkReconnect, ConHookClientOnly); + IConsole::CmdRegister("rcon", ConRcon, ConHookNeedNetwork); + + IConsole::CmdRegister("join", ConJoinCompany, ConHookNeedNetwork); + IConsole::AliasRegister("spectate", "join 255"); + IConsole::CmdRegister("move", ConMoveClient, ConHookServerOnly); + IConsole::CmdRegister("reset_company", ConResetCompany, ConHookServerOnly); + IConsole::AliasRegister("clean_company", "reset_company %A"); + IConsole::CmdRegister("client_name", ConClientNickChange, ConHookServerOnly); + IConsole::CmdRegister("kick", ConKick, ConHookServerOnly); + IConsole::CmdRegister("ban", ConBan, ConHookServerOnly); + IConsole::CmdRegister("unban", ConUnBan, ConHookServerOnly); + IConsole::CmdRegister("banlist", ConBanList, ConHookServerOnly); + + IConsole::CmdRegister("pause", ConPauseGame, ConHookServerOnly); + IConsole::CmdRegister("unpause", ConUnpauseGame, ConHookServerOnly); + + IConsole::CmdRegister("company_pw", ConCompanyPassword, ConHookNeedNetwork); + IConsole::AliasRegister("company_password", "company_pw %+"); + + IConsole::AliasRegister("net_frame_freq", "setting frame_freq %+"); + IConsole::AliasRegister("net_sync_freq", "setting sync_freq %+"); + IConsole::AliasRegister("server_pw", "setting server_password %+"); + IConsole::AliasRegister("server_password", "setting server_password %+"); + IConsole::AliasRegister("rcon_pw", "setting rcon_password %+"); + IConsole::AliasRegister("rcon_password", "setting rcon_password %+"); + IConsole::AliasRegister("name", "setting client_name %+"); + IConsole::AliasRegister("server_name", "setting server_name %+"); + IConsole::AliasRegister("server_port", "setting server_port %+"); + IConsole::AliasRegister("server_advertise", "setting server_advertise %+"); + IConsole::AliasRegister("max_clients", "setting max_clients %+"); + IConsole::AliasRegister("max_companies", "setting max_companies %+"); + IConsole::AliasRegister("max_spectators", "setting max_spectators %+"); + IConsole::AliasRegister("max_join_time", "setting max_join_time %+"); + IConsole::AliasRegister("pause_on_join", "setting pause_on_join %+"); + IConsole::AliasRegister("autoclean_companies", "setting autoclean_companies %+"); + IConsole::AliasRegister("autoclean_protected", "setting autoclean_protected %+"); + IConsole::AliasRegister("autoclean_unprotected", "setting autoclean_unprotected %+"); + IConsole::AliasRegister("restart_game_year", "setting restart_game_year %+"); + IConsole::AliasRegister("min_players", "setting min_active_clients %+"); + IConsole::AliasRegister("reload_cfg", "setting reload_cfg %+"); /* debugging stuff */ #ifdef _DEBUG IConsoleDebugLibRegister(); #endif - IConsoleCmdRegister("fps", ConFramerate); - IConsoleCmdRegister("fps_wnd", ConFramerateWindow); + IConsole::CmdRegister("fps", ConFramerate); + IConsole::CmdRegister("fps_wnd", ConFramerateWindow); /* NewGRF development stuff */ - IConsoleCmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool); - IConsoleCmdRegister("newgrf_profile", ConNewGRFProfile, ConHookNewGRFDeveloperTool); + IConsole::CmdRegister("reload_newgrfs", ConNewGRFReload, ConHookNewGRFDeveloperTool); + IConsole::CmdRegister("newgrf_profile", ConNewGRFProfile, ConHookNewGRFDeveloperTool); - IConsoleCmdRegister("dump_info", ConDumpInfo); + IConsole::CmdRegister("dump_info", ConDumpInfo); } diff --git a/src/console_internal.h b/src/console_internal.h index 630f69607..ca2f92193 100644 --- a/src/console_internal.h +++ b/src/console_internal.h @@ -11,6 +11,7 @@ #define CONSOLE_INTERNAL_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_MAX_STREAMSIZE = 2048; ///< maximum length of a totally expanded command @@ -33,9 +34,9 @@ enum ConsoleHookResult { typedef bool IConsoleCmdProc(byte argc, char *argv[]); typedef ConsoleHookResult IConsoleHook(bool echo); struct IConsoleCmd { - char *name; ///< name of command - IConsoleCmd *next; ///< next command in list + IConsoleCmd(const std::string &name, IConsoleCmdProc *proc, IConsoleHook *hook) : name(name), proc(proc), hook(hook) {} + std::string name; ///< name of command IConsoleCmdProc *proc; ///< process executed when command is typed IConsoleHook *hook; ///< any special trigger action that needs executing }; @@ -53,25 +54,31 @@ struct IConsoleCmd { * - ";" allows for combining commands (see example 'ng') */ struct IConsoleAlias { - char *name; ///< name of the alias - IConsoleAlias *next; ///< next alias in list + IConsoleAlias(const std::string &name, const std::string &cmdline) : name(name), cmdline(cmdline) {} - 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 */ -extern IConsoleCmd *_iconsole_cmds; ///< List of registered commands. -extern IConsoleAlias *_iconsole_aliases; ///< List of registered aliases. +struct IConsole +{ + 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 */ 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) */ void IConsoleStdLibRegister(); @@ -81,6 +88,5 @@ bool GetArgumentInteger(uint32 *value, const char *arg); void IConsoleGUIInit(); void IConsoleGUIFree(); void IConsoleGUIPrint(TextColour colour_code, char *string); -char *RemoveUnderscores(char *name); #endif /* CONSOLE_INTERNAL_H */ diff --git a/src/music/midifile.cpp b/src/music/midifile.cpp index 4bdd3e4e3..dcd2cefd8 100644 --- a/src/music/midifile.cpp +++ b/src/music/midifile.cpp @@ -1142,7 +1142,7 @@ static void RegisterConsoleMidiCommands() { static bool registered = false; if (!registered) { - IConsoleCmdRegister("dumpsmf", CmdDumpSMF); + IConsole::CmdRegister("dumpsmf", CmdDumpSMF); registered = true; } } |