diff --git a/lib/CArtHandler.cpp b/lib/CArtHandler.cpp index 1edf30add..ad9941fb1 100644 --- a/lib/CArtHandler.cpp +++ b/lib/CArtHandler.cpp @@ -439,13 +439,13 @@ void CArtHandler::loadGrowingArt(CGrowingArtifact * art, const JsonNode & node) { for (auto b : node["growing"]["bonusesPerLevel"].Vector()) { - auto bonus = JsonUtils::parseBonus (b["bonus"]); - art->bonusesPerLevel.push_back (std::pair (b["level"].Float(), *bonus)); + art->bonusesPerLevel.push_back(std::pair (b["level"].Float(), Bonus())); + JsonUtils::parseBonus(b["bonus"], &art->bonusesPerLevel.back().second); } for (auto b : node["growing"]["thresholdBonuses"].Vector()) { - auto bonus = JsonUtils::parseBonus (b["bonus"]); - art->thresholdBonuses.push_back (std::pair (b["level"].Float(), *bonus)); + art->thresholdBonuses.push_back(std::pair (b["level"].Float(), Bonus())); + JsonUtils::parseBonus(b["bonus"], &art->thresholdBonuses.back().second); } } diff --git a/lib/CArtHandler.h b/lib/CArtHandler.h index d308cff6f..6e514f0dc 100644 --- a/lib/CArtHandler.h +++ b/lib/CArtHandler.h @@ -98,7 +98,7 @@ public: std::vector > bonusesPerLevel; //bonus given each n levels std::vector > thresholdBonuses; //after certain level they will be added once - void levelUpArtifact (CArtifactInstance * art) override; + void levelUpArtifact(CArtifactInstance * art) override; template void serialize(Handler &h, const int version) { diff --git a/lib/CModHandler.cpp b/lib/CModHandler.cpp index efe27e3ec..0a9b9d61f 100644 --- a/lib/CModHandler.cpp +++ b/lib/CModHandler.cpp @@ -54,8 +54,10 @@ void CIdentifierStorage::checkIdentifier(std::string & ID) } } -CIdentifierStorage::ObjectCallback::ObjectCallback(std::string localScope, std::string remoteScope, std::string type, - std::string name, const std::function & callback, bool optional): +CIdentifierStorage::ObjectCallback::ObjectCallback( + std::string localScope, std::string remoteScope, std::string type, + std::string name, const std::function & callback, + bool optional): localScope(localScope), remoteScope(remoteScope), type(type), diff --git a/lib/CModHandler.h b/lib/CModHandler.h index 1b908cbb4..0e26bc06f 100644 --- a/lib/CModHandler.h +++ b/lib/CModHandler.h @@ -41,7 +41,10 @@ class CIdentifierStorage std::function callback; bool optional; - ObjectCallback(std::string localScope, std::string remoteScope, std::string type, std::string name, const std::function & callback, bool optional); + ObjectCallback(std::string localScope, std::string remoteScope, + std::string type, std::string name, + const std::function & callback, + bool optional); }; struct ObjectData // entry created on ID registration diff --git a/lib/HeroBonus.cpp b/lib/HeroBonus.cpp index 2de66fd39..3d50f5eff 100644 --- a/lib/HeroBonus.cpp +++ b/lib/HeroBonus.cpp @@ -1110,10 +1110,6 @@ Bonus::Bonus() sid = 0; } -Bonus::~Bonus() -{ -} - std::shared_ptr Bonus::addPropagator(TPropagatorPtr Propagator) { propagator = Propagator; diff --git a/lib/HeroBonus.h b/lib/HeroBonus.h index 4cbd7b9d7..748c13b22 100644 --- a/lib/HeroBonus.h +++ b/lib/HeroBonus.h @@ -313,7 +313,6 @@ struct DLL_LINKAGE Bonus : public std::enable_shared_from_this Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, std::string Desc, si32 Subtype=-1); Bonus(ui16 Dur, BonusType Type, BonusSource Src, si32 Val, ui32 ID, si32 Subtype=-1, ValueType ValType = ADDITIVE_VALUE); Bonus(); - ~Bonus(); template void serialize(Handler &h, const int version) { diff --git a/lib/JsonNode.cpp b/lib/JsonNode.cpp index 29c050c57..026dcffbf 100644 --- a/lib/JsonNode.cpp +++ b/lib/JsonNode.cpp @@ -378,7 +378,7 @@ const T & parseByMap(const std::map & map, const JsonNode * val, return defaultValue; } -void JsonUtils::resolveIdentifier (si32 &var, const JsonNode &node, std::string name) +void JsonUtils::resolveIdentifier(si32 &var, const JsonNode &node, std::string name) { const JsonNode &value = node[name]; if (!value.isNull()) @@ -400,7 +400,7 @@ void JsonUtils::resolveIdentifier (si32 &var, const JsonNode &node, std::string } } -void JsonUtils::resolveIdentifier (const JsonNode &node, si32 &var) +void JsonUtils::resolveIdentifier(const JsonNode &node, si32 &var) { switch (node.getType()) { @@ -408,7 +408,7 @@ void JsonUtils::resolveIdentifier (const JsonNode &node, si32 &var) var = node.Float(); break; case JsonNode::DATA_STRING: - VLC->modh->identifiers.requestIdentifier (node, [&](si32 identifier) + VLC->modh->identifiers.requestIdentifier(node, [&](si32 identifier) { var = identifier; }); @@ -420,8 +420,16 @@ void JsonUtils::resolveIdentifier (const JsonNode &node, si32 &var) std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) { - auto b = std::make_shared(); + if (!parseBonus(ability, b.get())) + { + return nullptr; + } + return b; +} + +bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b) +{ const JsonNode *value; std::string type = ability["type"].String(); @@ -429,11 +437,11 @@ std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) if (it == bonusNameMap.end()) { logGlobal->errorStream() << "Error: invalid ability type " << type; - return b; + return false; } b->type = it->second; - resolveIdentifier (b->subtype, ability, "subtype"); + resolveIdentifier(b->subtype, ability, "subtype"); b->val = ability["val"].Float(); @@ -441,7 +449,7 @@ std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) if (!value->isNull()) b->valType = static_cast(parseByMap(bonusValueMap, value, "value type ")); - resolveIdentifier (b->additionalInfo, ability, "addInfo"); + resolveIdentifier(b->additionalInfo, ability, "addInfo"); b->turnsRemain = ability["turns"].Float(); @@ -499,7 +507,7 @@ std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) const JsonVector vec = limiter["parameters"].Vector(); VLC->modh->identifiers.requestIdentifier("creature", vec[0], [=](si32 creature) { - l2->setCreature (CreatureID(creature)); + l2->setCreature(CreatureID(creature)); }); if (vec.size() > 1) { @@ -516,7 +524,7 @@ std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) const JsonVector vec = limiter["parameters"].Vector(); std::string anotherBonusType = vec[0].String(); - auto it = bonusNameMap.find (anotherBonusType); + auto it = bonusNameMap.find(anotherBonusType); if (it == bonusNameMap.end()) { logGlobal->errorStream() << "Error: invalid ability type " << anotherBonusType; @@ -526,7 +534,7 @@ std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) if (vec.size() > 1 ) { - resolveIdentifier (vec[1], l2->subtype); + resolveIdentifier(vec[1], l2->subtype); l2->isSubtypeRelevant = true; } l = l2; @@ -542,7 +550,7 @@ std::shared_ptr JsonUtils::parseBonus(const JsonNode &ability) if (!value->isNull()) b->propagator = parseByMap(bonusPropagatorMap, value, "propagator type "); - return b; + return true; } //returns first Key with value equal to given one diff --git a/lib/JsonNode.h b/lib/JsonNode.h index e701c5854..14ac9d2cc 100644 --- a/lib/JsonNode.h +++ b/lib/JsonNode.h @@ -130,11 +130,12 @@ namespace JsonUtils DLL_LINKAGE void parseTypedBonusShort(const JsonVector &source, std::shared_ptr dest); /// - DLL_LINKAGE std::shared_ptr parseBonus (const JsonVector &ability_vec); - DLL_LINKAGE std::shared_ptr parseBonus (const JsonNode &bonus); + DLL_LINKAGE std::shared_ptr parseBonus(const JsonVector &ability_vec); + DLL_LINKAGE std::shared_ptr parseBonus(const JsonNode &ability); + DLL_LINKAGE bool parseBonus(const JsonNode &ability, Bonus *placement); DLL_LINKAGE void unparseBonus (JsonNode &node, const std::shared_ptr& bonus); - DLL_LINKAGE void resolveIdentifier (si32 &var, const JsonNode &node, std::string name); - DLL_LINKAGE void resolveIdentifier (const JsonNode &node, si32 &var); + DLL_LINKAGE void resolveIdentifier(si32 &var, const JsonNode &node, std::string name); + DLL_LINKAGE void resolveIdentifier(const JsonNode &node, si32 &var); /** * @brief recursively merges source into dest, replacing identical fields