From f765f212bbc614d68041ea261e0334d89e010516 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Mon, 26 Aug 2024 20:52:37 +0000 Subject: [PATCH] Restore save compatibility --- lib/mapObjects/CGMarket.h | 20 ++++++++++++++++++-- lib/mapObjects/CGTownInstance.cpp | 8 ++++++++ lib/mapObjects/CGTownInstance.h | 8 +++++++- lib/mapObjects/IMarket.h | 14 ++++++++++++-- lib/serializer/ESerializationVersion.h | 3 ++- lib/serializer/RegisterTypes.h | 1 - lib/serializer/SerializerReflection.cpp | 10 ++++++++++ 7 files changed, 57 insertions(+), 7 deletions(-) diff --git a/lib/mapObjects/CGMarket.h b/lib/mapObjects/CGMarket.h index 19cb235c7..8f9f2eeef 100644 --- a/lib/mapObjects/CGMarket.h +++ b/lib/mapObjects/CGMarket.h @@ -35,6 +35,19 @@ public: h & static_cast(*this); h & static_cast(*this); h & marketEfficiency; + if (h.version < Handler::Version::NEW_MARKETS) + { + std::string speech; + std::string title; + h & speech; + h & title; + } + } + + template void serializeArtifactsAltar(Handler &h) + { + serialize(h); + IMarket::serializeArtifactsAltar(h); } }; @@ -71,8 +84,11 @@ public: { h & static_cast(*this); h & skills; - h & speech; - h & title; + if (h.version >= Handler::Version::NEW_MARKETS) + { + h & speech; + h & title; + } } }; diff --git a/lib/mapObjects/CGTownInstance.cpp b/lib/mapObjects/CGTownInstance.cpp index 1b8a60b28..30da917b1 100644 --- a/lib/mapObjects/CGTownInstance.cpp +++ b/lib/mapObjects/CGTownInstance.cpp @@ -946,6 +946,14 @@ void CGTownInstance::addBuilding(const BuildingID & buildingID) } } +void CGTownInstance::postDeserializeMarketFix() +{ + // re-add all buildings to recreate existing market modes + auto buildingsBak = builtBuildings; + for (auto building : buildingsBak) + addBuilding(building); +} + void CGTownInstance::removeBuilding(const BuildingID & buildingID) { if(!vstd::contains(builtBuildings, buildingID)) diff --git a/lib/mapObjects/CGTownInstance.h b/lib/mapObjects/CGTownInstance.h index 137ebb7fd..849ebb01b 100644 --- a/lib/mapObjects/CGTownInstance.h +++ b/lib/mapObjects/CGTownInstance.h @@ -77,7 +77,9 @@ public: template void serialize(Handler &h) { h & static_cast(*this); - h & static_cast(*this); + if (h.version >= Handler::Version::NEW_MARKETS) + h & static_cast(*this); + h & nameTextId; h & built; h & destroyed; @@ -116,6 +118,9 @@ public: town = faction ? faction->town : nullptr; } + if (!h.saving && h.version < Handler::Version::NEW_MARKETS) + postDeserializeMarketFix(); + h & townAndVis; BONUS_TREE_DESERIALIZATION_FIX @@ -135,6 +140,7 @@ public: void updateMoraleBonusFromArmy() override; void deserializationFix(); void postDeserialize(); + void postDeserializeMarketFix(); void recreateBuildingsBonuses(); void setVisitingHero(CGHeroInstance *h); void setGarrisonedHero(CGHeroInstance *h); diff --git a/lib/mapObjects/IMarket.h b/lib/mapObjects/IMarket.h index 3145a010b..4f06aad59 100644 --- a/lib/mapObjects/IMarket.h +++ b/lib/mapObjects/IMarket.h @@ -42,8 +42,18 @@ public: { h & marketModes; - if(h.loadingGamestate && vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP)) - altarArtifactsStorage = std::make_shared(); + if(vstd::contains(marketModes, EMarketMode::ARTIFACT_EXP)) + { + if (!h.saving) + altarArtifactsStorage = std::make_shared(); + + h & *altarArtifactsStorage; + } + } + + template void serializeArtifactsAltar(Handler & h) + { + h & *altarArtifactsStorage; } private: diff --git a/lib/serializer/ESerializationVersion.h b/lib/serializer/ESerializationVersion.h index e0e9002f8..d7c9ad4c1 100644 --- a/lib/serializer/ESerializationVersion.h +++ b/lib/serializer/ESerializationVersion.h @@ -65,6 +65,7 @@ enum class ESerializationVersion : int32_t EVENTS_PLAYER_SET, // 854 - map & town events use std::set instead of bitmask to store player list NEW_TOWN_BUILDINGS, // 855 - old bonusing buildings have been removed STATISTICS_SCREEN, // 856 - extent statistic functions + NEW_MARKETS, // 857 - reworked market classes - CURRENT = STATISTICS_SCREEN + CURRENT = NEW_MARKETS }; diff --git a/lib/serializer/RegisterTypes.h b/lib/serializer/RegisterTypes.h index 7b601db94..ecdad87ad 100644 --- a/lib/serializer/RegisterTypes.h +++ b/lib/serializer/RegisterTypes.h @@ -139,7 +139,6 @@ void registerTypes(Serializer &s) s.template registerType(78); s.template registerType(79); s.template registerType(80); - //s.template registerType(81); s.template registerType(82); s.template registerType(84); s.template registerType(85); diff --git a/lib/serializer/SerializerReflection.cpp b/lib/serializer/SerializerReflection.cpp index ce5048761..391a2ca60 100644 --- a/lib/serializer/SerializerReflection.cpp +++ b/lib/serializer/SerializerReflection.cpp @@ -63,6 +63,15 @@ class SerializerCompatibilityBonusingBuilding final : public SerializerCompatibi } }; +class SerializerCompatibilityArtifactsAltar final : public SerializerCompatibility +{ + void loadPtr(BinaryDeserializer &ar, IGameCallback * cb, Serializeable * data) const override + { + auto * realPtr = dynamic_cast(data); + realPtr->serializeArtifactsAltar(ar); + } +}; + template void CSerializationApplier::registerType(uint16_t ID) { @@ -76,6 +85,7 @@ CSerializationApplier::CSerializationApplier() apps[54].reset(new SerializerCompatibilityBonusingBuilding); apps[55].reset(new SerializerCompatibilityBonusingBuilding); + apps[81].reset(new SerializerCompatibilityArtifactsAltar); } CSerializationApplier & CSerializationApplier::getInstance()