From 365eed533d0729f519e1185dfa79cb25fb93590b Mon Sep 17 00:00:00 2001 From: Rubidium Date: Sun, 20 Apr 2025 14:10:36 +0200 Subject: [PATCH] Codechange: use std::string_view for console commands --- src/console.cpp | 10 +- src/console_cmds.cpp | 490 ++++++++++++++++++++--------------------- src/console_internal.h | 8 +- src/music/midifile.cpp | 6 +- 4 files changed, 257 insertions(+), 257 deletions(-) diff --git a/src/console.cpp b/src/console.cpp index 11bc3c6aaf..e94f2acb9d 100644 --- a/src/console.cpp +++ b/src/console.cpp @@ -156,7 +156,7 @@ static std::string RemoveUnderscores(std::string name) * @param name name of the alias that will be used * @param cmd name of the command that 'name' will be alias of */ -/* static */ void IConsole::AliasRegister(const std::string &name, const std::string &cmd) +/* static */ void IConsole::AliasRegister(const std::string &name, std::string_view cmd) { auto result = IConsole::Aliases().try_emplace(RemoveUnderscores(name), name, cmd); if (!result.second) IConsolePrint(CC_ERROR, "An alias with the name '{}' already exists.", name); @@ -342,10 +342,10 @@ void IConsoleCmdExec(std::string_view command_string, const uint recurse_count) ConsoleHookResult chr = (cmd->hook == nullptr ? CHR_ALLOW : cmd->hook(true)); switch (chr) { case CHR_ALLOW: { - std::vector c_strings; - for (auto &token : tokens) c_strings.emplace_back(token.data()); - if (!cmd->proc(static_cast(tokens.size()), c_strings.data())) { // index started with 0 - cmd->proc(0, nullptr); // if command failed, give help + std::vector views; + for (auto &token : tokens) views.emplace_back(token); + if (!cmd->proc(views)) { // index started with 0 + cmd->proc({}); // if command failed, give help } return; } diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 5d15b0e79e..655430dabd 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -68,7 +68,7 @@ static IntervalTimer _scheduled_monthly_timer = {{TimerGameCa _scheduled_monthly_script.clear(); IConsolePrint(CC_DEFAULT, "Executing scheduled script file '{}'...", filename); - IConsoleCmdExec(std::string("exec") + " " + filename); + IConsoleCmdExec(fmt::format("exec {}", filename)); }}; @@ -251,9 +251,9 @@ static ConsoleHookResult ConHookNewGRFDeveloperTool(bool echo) * Reset status of all engines. * @return Will always succeed. */ -static bool ConResetEngines([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConResetEngines(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Reset status data of all engines. This might solve some issues with 'lost' engines. Usage: 'resetengines'."); return true; } @@ -267,9 +267,9 @@ static bool ConResetEngines([[maybe_unused]] uint8_t argc, [[maybe_unused]] char * @return Will always return true. * @note Resetting the pool only succeeds when there are no vehicles ingame. */ -static bool ConResetEnginePool([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConResetEnginePool(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Reset NewGRF allocations of engine slots. This will remove invalid engine definitions, and might make default engines available again."); return true; } @@ -293,15 +293,15 @@ static bool ConResetEnginePool([[maybe_unused]] uint8_t argc, [[maybe_unused]] c * param tile number. * @return True when the tile is reset or the help on usage was printed (0 or two parameters). */ -static bool ConResetTile([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConResetTile(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Reset a tile to bare land. Usage: 'resettile '."); IConsolePrint(CC_HELP, "Tile can be either decimal (34161) or hexadecimal (0x4a5B)."); return true; } - if (argc == 2) { + if (argv.size() == 2) { auto result = ParseInteger(argv[1], 0); if (result.has_value() && IsValidTile(*result)) { DoClearSquare(TileIndex{*result}); @@ -318,9 +318,9 @@ static bool ConResetTile([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *a * param level As defined by ZoomLevel and as limited by zoom_min/zoom_max from GUISettings. * @return True when either console help was shown or a proper amount of parameters given. */ -static bool ConZoomToLevel([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConZoomToLevel(std::span argv) { - switch (argc) { + switch (argv.size()) { case 0: IConsolePrint(CC_HELP, "Set the current zoom level of the main viewport."); IConsolePrint(CC_HELP, "Usage: 'zoomto '."); @@ -377,25 +377,25 @@ static bool ConZoomToLevel([[maybe_unused]] uint8_t argc, [[maybe_unused]] char * and y coordinates. * @return True when either console help was shown or a proper amount of parameters given. */ -static bool ConScrollToTile([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConScrollToTile(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Center the screen on a given tile."); IConsolePrint(CC_HELP, "Usage: 'scrollto [instant] ' or 'scrollto [instant] '."); IConsolePrint(CC_HELP, "Numbers can be either decimal (34161) or hexadecimal (0x4a5B)."); IConsolePrint(CC_HELP, "'instant' will immediately move and redraw viewport without smooth scrolling."); return true; } - if (argc < 2) return false; + if (argv.size() < 2) return false; uint32_t arg_index = 1; bool instant = false; - if (strcmp(argv[arg_index], "instant") == 0) { + if (argv[arg_index] == "instant") { ++arg_index; instant = true; } - switch (argc - arg_index) { + switch (argv.size() - arg_index) { case 1: { auto result = ParseInteger(argv[arg_index], 0); if (result.has_value()) { @@ -432,14 +432,14 @@ static bool ConScrollToTile([[maybe_unused]] uint8_t argc, [[maybe_unused]] char * param filename the filename to save the map to. * @return True when help was displayed or the file attempted to be saved. */ -static bool ConSave([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConSave(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Save the current game. Usage: 'save '."); return true; } - if (argc == 2) { + if (argv.size() == 2) { std::string filename = fmt::format("{}.sav", argv[1]); IConsolePrint(CC_DEFAULT, "Saving map..."); @@ -458,9 +458,9 @@ static bool ConSave([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[] * Explicitly save the configuration. * @return True. */ -static bool ConSaveConfig([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConSaveConfig(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Saves the configuration for new games to the configuration file, typically 'openttd.cfg'."); IConsolePrint(CC_HELP, "It does not save the configuration of the current game to the configuration file."); return true; @@ -471,14 +471,14 @@ static bool ConSaveConfig([[maybe_unused]] uint8_t argc, [[maybe_unused]] char * return true; } -static bool ConLoad([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConLoad(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Load a game by name or index. Usage: 'load '."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; std::string_view file = argv[1]; _console_file_list_savegame.ValidateFileList(); @@ -497,14 +497,14 @@ static bool ConLoad([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[] return true; } -static bool ConLoadScenario([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConLoadScenario(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Load a scenario by name or index. Usage: 'load_scenario '."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; std::string_view file = argv[1]; _console_file_list_scenario.ValidateFileList(); @@ -523,14 +523,14 @@ static bool ConLoadScenario([[maybe_unused]] uint8_t argc, [[maybe_unused]] char return true; } -static bool ConLoadHeightmap([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConLoadHeightmap(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Load a heightmap by name or index. Usage: 'load_heightmap '."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; std::string_view file = argv[1]; _console_file_list_heightmap.ValidateFileList(); @@ -549,14 +549,14 @@ static bool ConLoadHeightmap([[maybe_unused]] uint8_t argc, [[maybe_unused]] cha return true; } -static bool ConRemove([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConRemove(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Remove a savegame by name or index. Usage: 'rm '."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; std::string_view file = argv[1]; _console_file_list_savegame.ValidateFileList(); @@ -575,9 +575,9 @@ static bool ConRemove([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv /* List all the files in the current dir via console */ -static bool ConListFiles([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConListFiles(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "List all loadable savegames and directories in the current dir via console. Usage: 'ls | dir'."); return true; } @@ -591,9 +591,9 @@ static bool ConListFiles([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *a } /* List all the scenarios */ -static bool ConListScenarios([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConListScenarios(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "List all loadable scenarios. Usage: 'list_scenarios'."); return true; } @@ -607,9 +607,9 @@ static bool ConListScenarios([[maybe_unused]] uint8_t argc, [[maybe_unused]] cha } /* List all the heightmaps */ -static bool ConListHeightmaps([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConListHeightmaps(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "List all loadable heightmaps. Usage: 'list_heightmaps'."); return true; } @@ -623,14 +623,14 @@ static bool ConListHeightmaps([[maybe_unused]] uint8_t argc, [[maybe_unused]] ch } /* Change the dir via console */ -static bool ConChangeDirectory([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConChangeDirectory(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Change the dir via console. Usage: 'cd '."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; std::string_view file = argv[1]; _console_file_list_savegame.ValidateFileList(true); @@ -652,9 +652,9 @@ static bool ConChangeDirectory([[maybe_unused]] uint8_t argc, [[maybe_unused]] c return true; } -static bool ConPrintWorkingDirectory([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConPrintWorkingDirectory(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Print out the current working directory. Usage: 'pwd'."); return true; } @@ -667,9 +667,9 @@ static bool ConPrintWorkingDirectory([[maybe_unused]] uint8_t argc, [[maybe_unus return true; } -static bool ConClearBuffer([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConClearBuffer(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Clear the console buffer. Usage: 'clear'."); return true; } @@ -731,21 +731,21 @@ static bool ConKickOrBan(std::string_view arg, bool ban, const std::string_view return true; } -static bool ConKick([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConKick(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Kick a client from a network game. Usage: 'kick []'."); IConsolePrint(CC_HELP, "For client-id's, see the command 'clients'."); return true; } - if (argc != 2 && argc != 3) return false; + if (argv.size() != 2 && argv.size() != 3) return false; /* No reason supplied for kicking */ - if (argc == 2) return ConKickOrBan(argv[1], false, {}); + if (argv.size() == 2) return ConKickOrBan(argv[1], false, {}); /* Reason for kicking supplied */ - size_t kick_message_length = strlen(argv[2]); + size_t kick_message_length = argv[2].size(); if (kick_message_length >= 255) { IConsolePrint(CC_ERROR, "Maximum kick message length is 254 characters. You entered {} characters.", kick_message_length); return false; @@ -754,22 +754,22 @@ static bool ConKick([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[] } } -static bool ConBan([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConBan(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Ban a client from a network game. Usage: 'ban []'."); IConsolePrint(CC_HELP, "For client-id's, see the command 'clients'."); IConsolePrint(CC_HELP, "If the client is no longer online, you can still ban their IP."); return true; } - if (argc != 2 && argc != 3) return false; + if (argv.size() != 2 && argv.size() != 3) return false; /* No reason supplied for kicking */ - if (argc == 2) return ConKickOrBan(argv[1], true, {}); + if (argv.size() == 2) return ConKickOrBan(argv[1], true, {}); /* Reason for kicking supplied */ - size_t kick_message_length = strlen(argv[2]); + size_t kick_message_length = argv[2].size(); if (kick_message_length >= 255) { IConsolePrint(CC_ERROR, "Maximum kick message length is 254 characters. You entered {} characters.", kick_message_length); return false; @@ -778,15 +778,15 @@ static bool ConBan([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) } } -static bool ConUnBan([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConUnBan(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Unban a client from a network game. Usage: 'unban '."); IConsolePrint(CC_HELP, "For a list of banned IP's, see the command 'banlist'."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; /* Try by IP. */ uint index; @@ -810,9 +810,9 @@ static bool ConUnBan([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[ return true; } -static bool ConBanList([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConBanList(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "List the IP's of banned clients: Usage 'banlist'."); return true; } @@ -828,9 +828,9 @@ static bool ConBanList([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *arg return true; } -static bool ConPauseGame([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConPauseGame(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Pause a network game. Usage: 'pause'."); return true; } @@ -850,9 +850,9 @@ static bool ConPauseGame([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *a return true; } -static bool ConUnpauseGame([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConUnpauseGame(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Unpause a network game. Usage: 'unpause'."); return true; } @@ -876,16 +876,16 @@ static bool ConUnpauseGame([[maybe_unused]] uint8_t argc, [[maybe_unused]] char return true; } -static bool ConRcon([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConRcon(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Remote control the server from another client. Usage: 'rcon '."); IConsolePrint(CC_HELP, "Remember to enclose the command in quotes, otherwise only the first parameter is sent."); IConsolePrint(CC_HELP, "When your client's public key is in the 'authorized keys' for 'rcon', the password is not checked and may be '*'."); return true; } - if (argc < 3) return false; + if (argv.size() < 3) return false; if (_network_server) { IConsoleCmdExec(argv[2]); @@ -895,9 +895,9 @@ static bool ConRcon([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[] return true; } -static bool ConStatus([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConStatus(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "List the status of all clients connected to the server. Usage 'status'."); return true; } @@ -906,9 +906,9 @@ static bool ConStatus([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv return true; } -static bool ConServerInfo([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConServerInfo(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "List current and maximum client/company limits. Usage 'server_info'."); IConsolePrint(CC_HELP, "You can change these values by modifying settings 'network.max_clients' and 'network.max_companies'."); return true; @@ -922,9 +922,9 @@ static bool ConServerInfo([[maybe_unused]] uint8_t argc, [[maybe_unused]] char * return true; } -static bool ConClientNickChange([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConClientNickChange(std::span argv) { - if (argc != 3) { + if (argv.size() != 3) { IConsolePrint(CC_HELP, "Change the nickname of a connected client. Usage: 'client_name '."); IConsolePrint(CC_HELP, "For client-id's, see the command 'clients'."); return true; @@ -967,9 +967,9 @@ static std::optional ParseCompanyID(std::string_view arg) return company_id; } -static bool ConJoinCompany([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConJoinCompany(std::span argv) { - if (argc < 2) { + if (argv.size() < 2) { IConsolePrint(CC_HELP, "Request joining another company. Usage: 'join '."); IConsolePrint(CC_HELP, "For valid company-id see company list, use 255 for spectator."); return true; @@ -1018,9 +1018,9 @@ static bool ConJoinCompany([[maybe_unused]] uint8_t argc, [[maybe_unused]] char return true; } -static bool ConMoveClient([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConMoveClient(std::span argv) { - if (argc < 3) { + if (argv.size() < 3) { IConsolePrint(CC_HELP, "Move a client to another company. Usage: 'move '."); IConsolePrint(CC_HELP, "For valid client-id see 'clients', for valid company-id see 'companies', use 255 for moving to spectators."); return true; @@ -1071,15 +1071,15 @@ static bool ConMoveClient([[maybe_unused]] uint8_t argc, [[maybe_unused]] char * return true; } -static bool ConResetCompany([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConResetCompany(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Remove an idle company from the game. Usage: 'reset_company '."); IConsolePrint(CC_HELP, "For company-id's, see the list of companies from the dropdown menu. Company 1 is 1, etc."); return true; } - if (argc != 2) return false; + if (argv.size() != 2) return false; auto index = ParseCompanyID(argv[1]); if (!index.has_value()) { @@ -1116,9 +1116,9 @@ static bool ConResetCompany([[maybe_unused]] uint8_t argc, [[maybe_unused]] char return true; } -static bool ConNetworkClients([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConNetworkClients(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Get a list of connected clients including their ID, name, company-id, and IP. Usage: 'clients'."); return true; } @@ -1128,9 +1128,9 @@ static bool ConNetworkClients([[maybe_unused]] uint8_t argc, [[maybe_unused]] ch return true; } -static bool ConNetworkReconnect([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConNetworkReconnect(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Reconnect to server to which you were connected last time. Usage: 'reconnect []'."); IConsolePrint(CC_HELP, "Company 255 is spectator (default, if not specified), 254 means creating new company."); IConsolePrint(CC_HELP, "All others are a certain company with Company 1 being #1."); @@ -1138,7 +1138,7 @@ static bool ConNetworkReconnect([[maybe_unused]] uint8_t argc, [[maybe_unused]] } CompanyID playas = COMPANY_SPECTATOR; - if (argc >= 2) { + if (argv.size() >= 2) { auto company_id = ParseCompanyID(argv[1]); if (!company_id.has_value()) { IConsolePrint(CC_ERROR, "The given company-id is not a valid number."); @@ -1159,16 +1159,16 @@ static bool ConNetworkReconnect([[maybe_unused]] uint8_t argc, [[maybe_unused]] return NetworkClientConnectGame(_settings_client.network.last_joined, playas); } -static bool ConNetworkConnect([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConNetworkConnect(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Connect to a remote OTTD server and join the game. Usage: 'connect '."); IConsolePrint(CC_HELP, "IP can contain port and company: 'IP[:Port][#Company]', eg: 'server.ottd.org:443#2'."); IConsolePrint(CC_HELP, "Company #255 is spectator all others are a certain company with Company 1 being #1."); return true; } - if (argc < 2) return false; + if (argv.size() < 2) return false; return NetworkClientConnectGame(argv[1], COMPANY_NEW_COMPANY); } @@ -1177,20 +1177,20 @@ static bool ConNetworkConnect([[maybe_unused]] uint8_t argc, [[maybe_unused]] ch * script file console commands *********************************/ -static bool ConExec([[maybe_unused]] uint8_t argc, [[maybe_unused]] char *argv[]) +static bool ConExec(std::span argv) { - if (argc == 0) { + if (argv.empty()) { IConsolePrint(CC_HELP, "Execute a local script file. Usage: 'exec