From 4319d31036a243cce860511a1b543461c9677f41 Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Fri, 25 Dec 2020 15:22:40 +0100 Subject: [PATCH] Fix #6468: don't store version of AIs-started-via-console in name You can do: "startai myai.3", which starts version 3 of "myai". This is very useful for testing save/load code between different versions of your AI. However, when using this syntax, the AI got saved as "myai.3" as name of the AI, instead of "myai". This caused several problems, like indicating to the user the AI could not be found, but still load the AI. But in all cases, the AI never got the chance to load the saved data, making the whole reason this exists pointless. By splitting the name and version already in the console command, the code becomes simpler and AIs started this way now follow the normal flow after initialization. --- src/ai/ai_scanner.cpp | 16 +++++----------- src/console_cmds.cpp | 19 ++++++++++++++++++- src/game/game_scanner.cpp | 16 +++++----------- 3 files changed, 28 insertions(+), 23 deletions(-) diff --git a/src/ai/ai_scanner.cpp b/src/ai/ai_scanner.cpp index ee14fd2147..aeb5b50fae 100644 --- a/src/ai/ai_scanner.cpp +++ b/src/ai/ai_scanner.cpp @@ -101,20 +101,10 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo strecpy(ai_name, nameParam, lastof(ai_name)); strtolower(ai_name); - AIInfo *info = nullptr; - int version = -1; - if (versionParam == -1) { /* We want to load the latest version of this AI; so find it */ if (this->info_single_list.find(ai_name) != this->info_single_list.end()) return static_cast(this->info_single_list[ai_name]); - - /* If we didn't find a match AI, maybe the user included a version */ - char *e = strrchr(ai_name, '.'); - if (e == nullptr) return nullptr; - *e = '\0'; - e++; - versionParam = atoi(e); - /* Continue, like we were calling this function with a version. */ + return nullptr; } if (force_exact_match) { @@ -123,8 +113,12 @@ AIInfo *AIScannerInfo::FindInfo(const char *nameParam, int versionParam, bool fo seprintf(ai_name_tmp, lastof(ai_name_tmp), "%s.%d", ai_name, versionParam); strtolower(ai_name_tmp); if (this->info_list.find(ai_name_tmp) != this->info_list.end()) return static_cast(this->info_list[ai_name_tmp]); + return nullptr; } + AIInfo *info = nullptr; + int version = -1; + /* See if there is a compatible AI which goes by that name, with the highest * version which allows loading the requested version */ ScriptInfoList::iterator it = this->info_list.begin(); diff --git a/src/console_cmds.cpp b/src/console_cmds.cpp index 2f678b2fc7..e87232b365 100644 --- a/src/console_cmds.cpp +++ b/src/console_cmds.cpp @@ -1172,7 +1172,24 @@ DEF_CONSOLE_CMD(ConStartAI) AIConfig *config = AIConfig::GetConfig((CompanyID)n); if (argc >= 2) { - config->Change(argv[1], -1, true); + config->Change(argv[1], -1, false); + + /* If the name is not found, and there is a dot in the name, + * try again with the assumption everything right of the dot is + * the version the user wants to load. */ + if (!config->HasScript()) { + char *name = stredup(argv[1]); + char *e = strrchr(name, '.'); + if (e != nullptr) { + *e = '\0'; + e++; + + int version = atoi(e); + config->Change(name, version, true); + } + free(name); + } + if (!config->HasScript()) { IConsoleWarning("Failed to load the specified AI"); return true; diff --git a/src/game/game_scanner.cpp b/src/game/game_scanner.cpp index 68efa350c2..6bcba4455e 100644 --- a/src/game/game_scanner.cpp +++ b/src/game/game_scanner.cpp @@ -40,20 +40,10 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo strecpy(game_name, nameParam, lastof(game_name)); strtolower(game_name); - GameInfo *info = nullptr; - int version = -1; - if (versionParam == -1) { /* We want to load the latest version of this Game script; so find it */ if (this->info_single_list.find(game_name) != this->info_single_list.end()) return static_cast(this->info_single_list[game_name]); - - /* If we didn't find a match Game script, maybe the user included a version */ - char *e = strrchr(game_name, '.'); - if (e == nullptr) return nullptr; - *e = '\0'; - e++; - versionParam = atoi(e); - /* Continue like we were calling this function with a version. */ + return nullptr; } if (force_exact_match) { @@ -62,8 +52,12 @@ GameInfo *GameScannerInfo::FindInfo(const char *nameParam, int versionParam, boo seprintf(game_name_tmp, lastof(game_name_tmp), "%s.%d", game_name, versionParam); strtolower(game_name_tmp); if (this->info_list.find(game_name_tmp) != this->info_list.end()) return static_cast(this->info_list[game_name_tmp]); + return nullptr; } + GameInfo *info = nullptr; + int version = -1; + /* See if there is a compatible Game script which goes by that name, with the highest * version which allows loading the requested version */ ScriptInfoList::iterator it = this->info_list.begin();