From 235acd7e0ca82b263991db538aceaa61ec9400e2 Mon Sep 17 00:00:00 2001 From: Chris Beck Date: Sun, 15 Mar 2015 17:59:34 -0400 Subject: [PATCH] move update routine for addon reqs to class mp_game_settings This simplifies the code in saved_game.cpp which expands mp scenarios. --- src/mp_game_settings.cpp | 44 ++++++++++++++++++++++++++++++++++++++++ src/mp_game_settings.hpp | 4 ++++ src/saved_game.cpp | 31 +--------------------------- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/src/mp_game_settings.cpp b/src/mp_game_settings.cpp index 16fbfbeb6675..1de5fbcfb7cd 100644 --- a/src/mp_game_settings.cpp +++ b/src/mp_game_settings.cpp @@ -18,11 +18,18 @@ * Container for multiplayer game-creation parameters. */ +#include "log.hpp" #include "mp_game_settings.hpp" #include "formula_string_utils.hpp" #include +static lg::log_domain log_engine("engine"); +#define ERR_NG LOG_STREAM(err, log_engine) +#define WRN_NG LOG_STREAM(warn, log_engine) +#define LOG_NG LOG_STREAM(info, log_engine) +#define DBG_NG LOG_STREAM(debug, log_engine) + mp_game_settings::mp_game_settings() : savegame_config(), name(), @@ -163,3 +170,40 @@ void mp_game_settings::addon_version_info::write(config & cfg) const { cfg["min_version"] = *min_version; } } + +void mp_game_settings::update_addon_requirements(const config & cfg) { + if (cfg["id"].empty()) { + WRN_NG << "Tried to add add-on metadata to a game, missing mandatory id field... skipping.\n" << cfg.debug() << "\n"; + return; + } + + mp_game_settings::addon_version_info new_data(cfg); + + // Check if this add-on already has an entry as a dependency for this scenario. If so, try to reconcile their version info, + // by taking the larger of the min versions. The version should be the same for all WML from the same add-on... + std::map::iterator it = addons.find(cfg["id"].str()); + if (it != addons.end()) { + addon_version_info & addon = it->second; + + try { + if (new_data.version) { + if (!addon.version || (*addon.version != *new_data.version)) { + WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n"; + } + } + if (addon.version && !new_data.version) { + WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["id"].str() << "' addon.\n"; + } + if (new_data.min_version) { + if (!addon.min_version || (*new_data.min_version > *addon.min_version)) { + addon.min_version = *new_data.min_version; + } + } + } catch (version_info::not_sane_exception & e) { + WRN_NG << "Caught a version_info not_sane_exception when determining a scenario's add-on dependencies. addon_id = " << cfg["id"].str() << "\n"; + } + } else { + // Didn't find this addon-id in the map, so make a new entry. + addons.insert(std::make_pair(cfg["id"].str(), new_data)); + } +} diff --git a/src/mp_game_settings.hpp b/src/mp_game_settings.hpp index 1fd19b647c03..5bda02a4bb7c 100644 --- a/src/mp_game_settings.hpp +++ b/src/mp_game_settings.hpp @@ -85,6 +85,10 @@ struct mp_game_settings : public savegame::savegame_config }; std::map addons; // the key is the addon_id + + // Takes a config with addon metadata (id =, version =, min_version =), formatted similarly to how mp_game_settings is written that is, + // and adds this as a requirement, updating the min_version if there was already an entry for this addon_id. + void update_addon_requirements(const config & addon_cfg); }; MAKE_ENUM_STREAM_OPS2(mp_game_settings, RANDOM_FACTION_MODE) diff --git a/src/saved_game.cpp b/src/saved_game.cpp index 6b4c461d693b..0a95c5057db5 100644 --- a/src/saved_game.cpp +++ b/src/saved_game.cpp @@ -246,36 +246,7 @@ void saved_game::expand_mp_events() std::string require_attr = "require_" + mod.type; bool require_default = (mod.type == "era"); // By default, eras have "require_era = true", and mods have "require_modification = false" if (!cfg["addon_id"].empty() && cfg[require_attr].to_bool(require_default)) { - config addon_data = config_of("id",cfg["addon_id"])("version", cfg["addon_version"])("min_version", cfg["addon_min_version"]); - mp_game_settings::addon_version_info new_data(addon_data); - - // Check if this add-on already has an entry as a dependency for this scenario. If so, try to reconcile their version info, - // by taking the larger of the min versions. The version should be the same for all WML from the same add-on... - std::map::iterator it = mp_settings_.addons.find(cfg["addon_id"].str()); - if (it != mp_settings_.addons.end()) { - mp_game_settings::addon_version_info & addon = it->second; - - try { - if (new_data.version) { - if (!addon.version || (*addon.version != *new_data.version)) { - WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["addon_id"].str() << "' addon.\n"; - } - } - if (addon.version && !new_data.version) { - WRN_NG << "Addon version data mismatch -- not all local WML has same version of '" << cfg["addon_id"].str() << "' addon.\n"; - } - if (new_data.min_version) { - if (!addon.min_version || (*new_data.min_version > *addon.min_version)) { - addon.min_version = *new_data.min_version; - } - } - } catch (version_info::not_sane_exception & e) { - WRN_NG << "Caught a version_info not_sane_exception when determining a scenario's add-on dependencies. addon_id = " << cfg["addon_id"].str() << "\n"; - } - } else { - // Didn't find this addon-id in the map, so make a new entry. - mp_settings_.addons.insert(std::make_pair(cfg["addon_id"].str(), new_data)); - } + mp_settings_.update_addon_requirements(config_of("id",cfg["addon_id"])("version", cfg["addon_version"])("min_version", cfg["addon_min_version"])); } // Copy events