diff --git a/src/data/game.cpp b/src/data/game.cpp index 1c0be291..2cb28c1e 100644 --- a/src/data/game.cpp +++ b/src/data/game.cpp @@ -207,8 +207,10 @@ void Game::validate(Version v) { card_links_alt_names.emplace(linked_tr, linked_default); } } - // sort the update_cards_scripts from oldest to newest - std::sort(update_cards_scripts.begin(), update_cards_scripts.end()); + // sort the update_cards_scripts from oldest to newest + std::sort(update_cards_scripts.begin(), update_cards_scripts.end(), [](const auto& a, const auto& b) { + return *a < *b; + }); } void Game::initCardListColorScript() { diff --git a/src/data/set.cpp b/src/data/set.cpp index acd45ee8..a3d6ed28 100644 --- a/src/data/set.cpp +++ b/src/data/set.cpp @@ -207,40 +207,52 @@ void Set::validate(Version file_app_version) { script_manager->updateAll(); // build uid map buildUIDMap(); - // update cards with game update_cards_scripts - for (int j = 0; j < game->update_cards_scripts.size(); ++j) { - UpdateCardsScriptP& script = game->update_cards_scripts[j]; - if (game_version >= script->before_version) continue; + // update_cards_scripts + // first apply all the stylesheet scripts that are older than the first game script + // then apply the first game script + // then apply all the stylesheet scripts that are older than the second game script + // then apply the second game script, etc... + Version previous_cutoff = Version(); + Version current_cutoff = Version(); + for (int g = 0; g < game->update_cards_scripts.size() + 1; ++g) { + bool last_iteration = g == game->update_cards_scripts.size(); + UpdateCardsScriptP& game_script = last_iteration ? nullptr : game->update_cards_scripts[g]; + previous_cutoff = current_cutoff; + current_cutoff = last_iteration ? current_cutoff : game_script->before_version; + // Apply stylesheet scripts that are older than the current game script + for (int i = 0; i < cards.size(); ++i) { + CardP& card = cards[i]; + StyleSheetP stylesheet = card->stylesheet ? card->stylesheet : stylesheetForP(card); + Version stylesheet_version = card->stylesheet_version.isZero() ? this->stylesheet_version : card->stylesheet_version; + for (int j = 0; j < stylesheet->update_cards_scripts.size(); ++j) { + UpdateCardsScriptP& script = stylesheet->update_cards_scripts[j]; + if (script->before_version >= current_cutoff && !last_iteration) continue; + if (script->before_version < previous_cutoff) continue; + if (stylesheet_version >= script->before_version) continue; + vector new_cards = script->perform(*this, card); + if (!new_cards.empty()) { + FOR_EACH(new_card, new_cards) { + // Initialize the stylesheet_version if it wasn't defined, to prevent this script from applying again + if (stylesheet == stylesheetForP(new_card) && new_card->stylesheet_version < script->before_version) { + new_card->stylesheet_version = script->before_version; + } + } + --i; + break; + } + } + } + // Apply current game script + if (last_iteration || game_version >= current_cutoff) continue; size_t size = cards.size(); for (int i = 0; i < size; ++i) { CardP& card = cards[i]; - vector new_cards = script->perform(*this, card); + vector new_cards = game_script->perform(*this, card); if (!new_cards.empty()) { --i; --size; } } - } - // update cards with stylesheet update_cards_scripts - for (int i = 0; i < cards.size(); ++i) { - CardP& card = cards[i]; - StyleSheetP stylesheet = card->stylesheet ? card->stylesheet : stylesheetForP(card); - Version stylesheet_version = card->stylesheet_version.isZero() ? this->stylesheet_version : card->stylesheet_version; - for (int j = 0; j < stylesheet->update_cards_scripts.size(); ++j) { - UpdateCardsScriptP& script = stylesheet->update_cards_scripts[j]; - if (stylesheet_version >= script->before_version) continue; - vector new_cards = script->perform(*this, card); - if (!new_cards.empty()) { - FOR_EACH(new_card, new_cards) { - // Initialize the stylesheet_version if it wasn't defined, to prevent this script from applying again - if (stylesheet == stylesheetForP(new_card) && new_card->stylesheet_version < script->before_version) { - new_card->stylesheet_version = script->before_version; - } - } - --i; - break; - } - } } } diff --git a/src/script/functions/construction_helper.hpp b/src/script/functions/construction_helper.hpp index 652b2de6..50db58be 100644 --- a/src/script/functions/construction_helper.hpp +++ b/src/script/functions/construction_helper.hpp @@ -64,7 +64,7 @@ inline static void set_container(Value* container, ScriptValueP& value, String k else if (PackageChoiceValue* pvalue = dynamic_cast(container)) { String package_name = value->toString(); while (package_name.starts_with(_("/"))) package_name = package_name.substr(1); - pvalue->package_name = value->toString(); + pvalue->package_name = package_name; } else if (ColorValue* cvalue = dynamic_cast(container)) { cvalue->value = value->toColor();