diff --git a/src/saveload/ai_sl.cpp b/src/saveload/ai_sl.cpp index 29f759361c..3a0d74a04e 100644 --- a/src/saveload/ai_sl.cpp +++ b/src/saveload/ai_sl.cpp @@ -27,18 +27,21 @@ static std::string _ai_saveload_name; static int _ai_saveload_version; static std::string _ai_saveload_settings; static bool _ai_saveload_is_random; +static bool _ai_saveload_force_exact_match; static const SaveLoad _ai_company_desc[] = { SLEG_SSTR("name", _ai_saveload_name, SLE_STR), SLEG_SSTR("settings", _ai_saveload_settings, SLE_STR), SLEG_CONDVAR("version", _ai_saveload_version, SLE_UINT32, SLV_108, SL_MAX_VERSION), SLEG_CONDVAR("is_random", _ai_saveload_is_random, SLE_BOOL, SLV_136, SLV_AI_LOCAL_CONFIG), + SLEG_CONDVAR("force_exact_match", _ai_saveload_force_exact_match, SLE_BOOL, SLV_SCRIPT_FORCE_EXACT_MATCH, SL_MAX_VERSION), }; static const SaveLoad _ai_running_desc[] = { SLEG_CONDSSTR("running_name", _ai_saveload_name, SLE_STR, SLV_AI_LOCAL_CONFIG, SL_MAX_VERSION), SLEG_CONDSSTR("running_settings", _ai_saveload_settings, SLE_STR, SLV_AI_LOCAL_CONFIG, SL_MAX_VERSION), SLEG_CONDVAR("running_version", _ai_saveload_version, SLE_UINT32, SLV_AI_LOCAL_CONFIG, SL_MAX_VERSION), + SLEG_CONDVAR("running_force_exact_match", _ai_saveload_force_exact_match, SLE_BOOL, SLV_SCRIPT_FORCE_EXACT_MATCH, SL_MAX_VERSION), }; static void SaveReal_AIPL(int arg) @@ -56,6 +59,7 @@ static void SaveReal_AIPL(int arg) } _ai_saveload_settings = config->SettingsToString(); + _ai_saveload_force_exact_match = config->GetForceExactMatch(); SlObject(nullptr, _ai_company_desc); @@ -66,10 +70,10 @@ static void SaveReal_AIPL(int arg) _ai_saveload_name = config->GetName(); _ai_saveload_version = config->GetVersion(); _ai_saveload_settings = config->SettingsToString(); + _ai_saveload_force_exact_match = config->GetForceExactMatch(); SlObject(nullptr, _ai_running_desc); AI::Save(index); - } struct AIPLChunkHandler : ChunkHandler { @@ -90,6 +94,7 @@ struct AIPLChunkHandler : ChunkHandler { _ai_saveload_is_random = false; _ai_saveload_version = -1; + _ai_saveload_force_exact_match = false; SlObject(nullptr, slt); if (_game_mode == GM_MENU || (_networking && !_network_server)) { @@ -105,19 +110,27 @@ struct AIPLChunkHandler : ChunkHandler { /* A random AI. */ config->Change(std::nullopt, -1, false); } else { - config->Change(_ai_saveload_name, _ai_saveload_version, false); + if (_ai_saveload_force_exact_match) config->Change(_ai_saveload_name, _ai_saveload_version, true); if (!config->HasScript()) { - /* No version of the AI available. Try to configure the - * latest version of the AI instead. */ - config->Change(_ai_saveload_name, -1, false); + /* Exact version of the AI is not available. Try to + * configure the highest compatible version. */ + config->Change(_ai_saveload_name, _ai_saveload_version, false); if (!config->HasScript()) { - if (_ai_saveload_name.compare("%_dummy") != 0) { - Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); - Debug(script, 0, "Configuration switched to Random AI."); + /* No version of the AI available. Try to configure the + * latest version of the AI instead. */ + config->Change(_ai_saveload_name, -1, false); + if (!config->HasScript()) { + if (_ai_saveload_name.compare("%_dummy") != 0) { + Debug(script, 0, "The savegame has an AI by the name '{}', {}version {} which is no longer available.", _ai_saveload_name, _ai_saveload_force_exact_match ? "forcing " : "", _ai_saveload_version); + Debug(script, 0, "Configuration switched to Random AI."); + } + } else { + Debug(script, 0, "The savegame has an AI by the name '{}', {}version {} which is no longer available.", _ai_saveload_name, _ai_saveload_force_exact_match ? "forcing " : "", _ai_saveload_version); + Debug(script, 0, "The latest version of that AI has been configured instead."); } - } else { - Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); - Debug(script, 0, "The latest version of that AI has been configured instead"); + } else if (_ai_saveload_force_exact_match) { + Debug(script, 0, "The savegame has an AI by the name '{}', forcing version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); + Debug(script, 0, "The highest compatible version of that AI has been configured instead."); } } } @@ -130,26 +143,34 @@ struct AIPLChunkHandler : ChunkHandler { Company::Get(index)->ai_config = std::make_unique(); config = Company::Get(index)->ai_config.get(); - config->Change(_ai_saveload_name, _ai_saveload_version, false); + if (_ai_saveload_force_exact_match) config->Change(_ai_saveload_name, _ai_saveload_version, true); if (!config->HasScript()) { - /* No version of the AI available that can load the data. Try to load the - * latest version of the AI instead. */ - config->Change(_ai_saveload_name, -1, false); + /* Exact version of the AI is not available. Try to load the highest + * compatible version of the AI instead that can load the data. */ + config->Change(_ai_saveload_name, _ai_saveload_version, false); if (!config->HasScript()) { - if (_ai_saveload_name.compare("%_dummy") != 0) { - Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); - Debug(script, 0, "A random other AI will be loaded in its place."); + /* No version of the AI available that can load the data. Try to load the + * latest version of the AI instead. */ + config->Change(_ai_saveload_name, -1, false); + if (!config->HasScript()) { + if (_ai_saveload_name.compare("%_dummy") != 0) { + Debug(script, 0, "The savegame has an AI by the name '{}', {}version {} which is no longer available.", _ai_saveload_name, _ai_saveload_force_exact_match ? "forcing " : "", _ai_saveload_version); + Debug(script, 0, "A random other AI will be loaded in its place."); + } else { + Debug(script, 0, "The savegame had no AIs available at the time of saving."); + Debug(script, 0, "A random available AI will be loaded now."); + } } else { - Debug(script, 0, "The savegame had no AIs available at the time of saving."); - Debug(script, 0, "A random available AI will be loaded now."); + Debug(script, 0, "The savegame has an AI by the name '{}', {}version {} which is no longer available.", _ai_saveload_name, _ai_saveload_force_exact_match ? "forcing " : "", _ai_saveload_version); + Debug(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible."); } - } else { - Debug(script, 0, "The savegame has an AI by the name '{}', version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); - Debug(script, 0, "The latest version of that AI has been loaded instead, but it'll not get the savegame data as it's incompatible."); + /* Make sure the AI doesn't get the saveload data, as it was not the + * writer of the saveload data in the first place */ + _ai_saveload_version = -1; + } else if (_ai_saveload_force_exact_match) { + Debug(script, 0, "The savegame has an AI by the name '{}', forcing version {} which is no longer available.", _ai_saveload_name, _ai_saveload_version); + Debug(script, 0, "The highest compatible version of that AI has been loaded instead."); } - /* Make sure the AI doesn't get the saveload data, as it was not the - * writer of the saveload data in the first place */ - _ai_saveload_version = -1; } config->StringToSettings(_ai_saveload_settings); config->SetToLoadData(AIInstance::Load(_ai_saveload_version)); diff --git a/src/saveload/game_sl.cpp b/src/saveload/game_sl.cpp index d6a35ffa20..63514e80df 100644 --- a/src/saveload/game_sl.cpp +++ b/src/saveload/game_sl.cpp @@ -25,11 +25,13 @@ static std::string _game_saveload_name; static int _game_saveload_version; static std::string _game_saveload_settings; +static bool _game_saveload_force_exact_match; static const SaveLoad _game_script_desc[] = { SLEG_SSTR("name", _game_saveload_name, SLE_STR), SLEG_SSTR("settings", _game_saveload_settings, SLE_STR), SLEG_VAR("version", _game_saveload_version, SLE_UINT32), + SLEG_CONDVAR("force_exact_match", _game_saveload_force_exact_match, SLE_BOOL, SLV_SCRIPT_FORCE_EXACT_MATCH, SL_MAX_VERSION), }; static void SaveReal_GSDT(int) @@ -46,6 +48,7 @@ static void SaveReal_GSDT(int) } _game_saveload_settings = config->SettingsToString(); + _game_saveload_force_exact_match = config->GetForceExactMatch(); SlObject(nullptr, _game_script_desc); Game::Save(); @@ -64,6 +67,7 @@ struct GSDTChunkHandler : ChunkHandler { if (SlIterateArray() == -1) return; _game_saveload_version = -1; + _game_saveload_force_exact_match = false; SlObject(nullptr, slt); if (_game_mode == GM_MENU || (_networking && !_network_server)) { @@ -74,26 +78,34 @@ struct GSDTChunkHandler : ChunkHandler { GameConfig *config = GameConfig::GetConfig(GameConfig::SSS_FORCE_GAME); if (!_game_saveload_name.empty()) { - config->Change(_game_saveload_name, _game_saveload_version, false); + if (_game_saveload_force_exact_match) config->Change(_game_saveload_name, _game_saveload_version, true); if (!config->HasScript()) { - /* No version of the GameScript available that can load the data. Try to load the - * latest version of the GameScript instead. */ - config->Change(_game_saveload_name, -1, false); + /* Exact version of the GameScript is not available. Try to load the highest + * compatible version of the AI instead that can load the data. */ + config->Change(_game_saveload_name, _game_saveload_version, false); if (!config->HasScript()) { - if (_game_saveload_name.compare("%_dummy") != 0) { - Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version); - Debug(script, 0, "This game will continue to run without GameScript."); + /* No version of the GameScript available that can load the data. Try to load the + * latest version of the GameScript instead. */ + config->Change(_game_saveload_name, -1, false); + if (!config->HasScript()) { + if (_game_saveload_name.compare("%_dummy") != 0) { + Debug(script, 0, "The savegame has a GameScript by the name '{}', {}version {} which is no longer available.", _game_saveload_name, _game_saveload_force_exact_match ? "forcing " : "", _game_saveload_version); + Debug(script, 0, "This game will continue to run without GameScript."); + } else { + Debug(script, 0, "The savegame had no GameScript available at the time of saving."); + Debug(script, 0, "This game will continue to run without GameScript."); + } } else { - Debug(script, 0, "The savegame had no GameScript available at the time of saving."); - Debug(script, 0, "This game will continue to run without GameScript."); + Debug(script, 0, "The savegame has a GameScript by the name '{}', {}version {} which is no longer available.", _game_saveload_name, _game_saveload_force_exact_match ? "forcing " : "", _game_saveload_version); + Debug(script, 0, "The latest version of that GameScript has been loaded instead, but it'll not get the savegame data as it's incompatible."); } - } else { - Debug(script, 0, "The savegame has an GameScript by the name '{}', version {} which is no longer available.", _game_saveload_name, _game_saveload_version); - Debug(script, 0, "The latest version of that GameScript has been loaded instead, but it'll not get the savegame data as it's incompatible."); + /* Make sure the GameScript doesn't get the saveload data, as it was not the + * writer of the saveload data in the first place */ + _game_saveload_version = -1; + } else if (_game_saveload_force_exact_match) { + Debug(script, 0, "The savegame has a GameScript by the name '{}', forcing version {} which is no longer available.", _game_saveload_name, _game_saveload_version); + Debug(script, 0, "The highest compatible version of that GameScript has been loaded instead."); } - /* Make sure the GameScript doesn't get the saveload data, as it was not the - * writer of the saveload data in the first place */ - _game_saveload_version = -1; } } diff --git a/src/saveload/saveload.h b/src/saveload/saveload.h index ec78153db1..f543e10411 100644 --- a/src/saveload/saveload.h +++ b/src/saveload/saveload.h @@ -403,6 +403,8 @@ enum SaveLoadVersion : uint16_t { SLV_FIX_SCC_ENCODED_NEGATIVE, ///< 353 PR#14049 Fix encoding of negative parameters. SLV_ORDERS_OWNED_BY_ORDERLIST, ///< 354 PR#13948 Orders stored in OrderList, pool removed. + SLV_SCRIPT_FORCE_EXACT_MATCH, ///< 355 PR#13565 Store script force exact match in savegames. + SL_MAX_VERSION, ///< Highest possible saveload version }; diff --git a/src/script/script_config.cpp b/src/script/script_config.cpp index a599a7e30a..856d223ea4 100644 --- a/src/script/script_config.cpp +++ b/src/script/script_config.cpp @@ -29,6 +29,7 @@ void ScriptConfig::Change(std::optional name, int version, boo this->info = nullptr; } this->version = (info == nullptr) ? -1 : info->GetVersion(); + this->force_exact_match = (info == nullptr) ? false : force_exact_match; this->config_list.reset(); this->to_load_data.reset(); @@ -40,6 +41,7 @@ ScriptConfig::ScriptConfig(const ScriptConfig &config) this->name = config.name; this->info = config.info; this->version = config.version; + this->force_exact_match = config.force_exact_match; this->to_load_data.reset(); for (const auto &item : config.settings) { @@ -140,6 +142,11 @@ int ScriptConfig::GetVersion() const return this->version; } +bool ScriptConfig::GetForceExactMatch() const +{ + return this->force_exact_match; +} + void ScriptConfig::StringToSettings(std::string_view value) { std::string_view to_process = value; diff --git a/src/script/script_config.hpp b/src/script/script_config.hpp index 76da137bcf..1059985c04 100644 --- a/src/script/script_config.hpp +++ b/src/script/script_config.hpp @@ -150,6 +150,11 @@ public: */ int GetVersion() const; + /** + * Whether the config is forcing the exact version of the Script? + */ + bool GetForceExactMatch() const; + /** * Convert a string which is stored in the config file or savegames to * custom settings of this Script. @@ -176,6 +181,7 @@ public: protected: std::string name; ///< Name of the Script int version; ///< Version of the Script + bool force_exact_match; ///< Force exact match for the version class ScriptInfo *info; ///< ScriptInfo object for related to this Script version SettingValueList settings; ///< List with all setting=>value pairs that are configure for this Script std::unique_ptr config_list; ///< List with all settings defined by this Script