From 08deae41865ccfaf05459175caf331b7ff1933af Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Wed, 14 Feb 2024 20:45:14 +0200 Subject: [PATCH] Moved static methods outside of vcmi namespace --- lib/json/JsonBonus.cpp | 202 +++++++++++++++++++++-------------------- lib/json/JsonBonus.h | 14 +-- lib/json/JsonUtils.cpp | 4 +- 3 files changed, 112 insertions(+), 108 deletions(-) diff --git a/lib/json/JsonBonus.cpp b/lib/json/JsonBonus.cpp index 76b5f77ab..b56de5786 100644 --- a/lib/json/JsonBonus.cpp +++ b/lib/json/JsonBonus.cpp @@ -22,7 +22,37 @@ #include "../constants/StringConstants.h" #include "../modding/IdentifierStorage.h" -VCMI_LIB_NAMESPACE_BEGIN +VCMI_LIB_USING_NAMESPACE + +template +const T parseByMap(const std::map & map, const JsonNode * val, const std::string & err) +{ + if (!val->isNull()) + { + const std::string & type = val->String(); + auto it = map.find(type); + if (it == map.end()) + { + logMod->error("Error: invalid %s%s.", err, type); + return {}; + } + else + { + return it->second; + } + } + else + return {}; +} + +template +const T parseByMapN(const std::map & map, const JsonNode * val, const std::string & err) +{ + if(val->isNumber()) + return static_cast(val->Integer()); + else + return parseByMap(map, val, err); +} static void loadBonusSubtype(BonusSubtypeID & subtype, BonusType type, const JsonNode & node) { @@ -264,6 +294,77 @@ static void loadBonusSourceInstance(BonusSourceID & sourceInstance, BonusSource } } +static BonusParams convertDeprecatedBonus(const JsonNode &ability) +{ + if(vstd::contains(deprecatedBonusSet, ability["type"].String())) + { + logMod->warn("There is deprecated bonus found:\n%s\nTrying to convert...", ability.toString()); + auto params = BonusParams(ability["type"].String(), + ability["subtype"].isString() ? ability["subtype"].String() : "", + ability["subtype"].isNumber() ? ability["subtype"].Integer() : -1); + if(params.isConverted) + { + if(ability["type"].String() == "SECONDARY_SKILL_PREMY" && bonusValueMap.find(ability["valueType"].String())->second == BonusValueType::PERCENT_TO_BASE) //assume secondary skill special + { + params.valueType = BonusValueType::PERCENT_TO_TARGET_TYPE; + params.targetType = BonusSource::SECONDARY_SKILL; + } + + logMod->warn("Please, use this bonus:\n%s\nConverted successfully!", params.toJson().toString()); + return params; + } + else + logMod->error("Cannot convert bonus!\n%s", ability.toString()); + } + BonusParams ret; + ret.isConverted = false; + return ret; +} + +static TUpdaterPtr parseUpdater(const JsonNode & updaterJson) +{ + switch(updaterJson.getType()) + { + case JsonNode::JsonType::DATA_STRING: + return parseByMap(bonusUpdaterMap, &updaterJson, "updater type "); + break; + case JsonNode::JsonType::DATA_STRUCT: + if(updaterJson["type"].String() == "GROWS_WITH_LEVEL") + { + auto updater = std::make_shared(); + const JsonVector param = updaterJson["parameters"].Vector(); + updater->valPer20 = static_cast(param[0].Integer()); + if(param.size() > 1) + updater->stepSize = static_cast(param[1].Integer()); + return updater; + } + else if (updaterJson["type"].String() == "ARMY_MOVEMENT") + { + auto updater = std::make_shared(); + if(updaterJson["parameters"].isVector()) + { + const auto & param = updaterJson["parameters"].Vector(); + if(param.size() < 4) + logMod->warn("Invalid ARMY_MOVEMENT parameters, using default!"); + else + { + updater->base = static_cast(param.at(0).Integer()); + updater->divider = static_cast(param.at(1).Integer()); + updater->multiplier = static_cast(param.at(2).Integer()); + updater->max = static_cast(param.at(3).Integer()); + } + return updater; + } + } + else + logMod->warn("Unknown updater type \"%s\"", updaterJson["type"].String()); + break; + } + return nullptr; +} + +VCMI_LIB_NAMESPACE_BEGIN + std::shared_ptr JsonUtils::parseBonus(const JsonVector & ability_vec) { auto b = std::make_shared(); @@ -284,36 +385,6 @@ std::shared_ptr JsonUtils::parseBonus(const JsonVector & ability_vec) return b; } -template -const T parseByMap(const std::map & map, const JsonNode * val, const std::string & err) -{ - if (!val->isNull()) - { - const std::string & type = val->String(); - auto it = map.find(type); - if (it == map.end()) - { - logMod->error("Error: invalid %s%s.", err, type); - return {}; - } - else - { - return it->second; - } - } - else - return {}; -} - -template -const T parseByMapN(const std::map & map, const JsonNode * val, const std::string & err) -{ - if(val->isNumber()) - return static_cast(val->Integer()); - else - return parseByMap(map, val, err); -} - void JsonUtils::resolveAddInfo(CAddInfo & var, const JsonNode & node) { const JsonNode & value = node["addInfo"]; @@ -567,75 +638,6 @@ std::shared_ptr JsonUtils::parseBuildingBonus(const JsonNode & ability, c return b; } -static BonusParams convertDeprecatedBonus(const JsonNode &ability) -{ - if(vstd::contains(deprecatedBonusSet, ability["type"].String())) - { - logMod->warn("There is deprecated bonus found:\n%s\nTrying to convert...", ability.toString()); - auto params = BonusParams(ability["type"].String(), - ability["subtype"].isString() ? ability["subtype"].String() : "", - ability["subtype"].isNumber() ? ability["subtype"].Integer() : -1); - if(params.isConverted) - { - if(ability["type"].String() == "SECONDARY_SKILL_PREMY" && bonusValueMap.find(ability["valueType"].String())->second == BonusValueType::PERCENT_TO_BASE) //assume secondary skill special - { - params.valueType = BonusValueType::PERCENT_TO_TARGET_TYPE; - params.targetType = BonusSource::SECONDARY_SKILL; - } - - logMod->warn("Please, use this bonus:\n%s\nConverted successfully!", params.toJson().toString()); - return params; - } - else - logMod->error("Cannot convert bonus!\n%s", ability.toString()); - } - BonusParams ret; - ret.isConverted = false; - return ret; -} - -static TUpdaterPtr parseUpdater(const JsonNode & updaterJson) -{ - switch(updaterJson.getType()) - { - case JsonNode::JsonType::DATA_STRING: - return parseByMap(bonusUpdaterMap, &updaterJson, "updater type "); - break; - case JsonNode::JsonType::DATA_STRUCT: - if(updaterJson["type"].String() == "GROWS_WITH_LEVEL") - { - auto updater = std::make_shared(); - const JsonVector param = updaterJson["parameters"].Vector(); - updater->valPer20 = static_cast(param[0].Integer()); - if(param.size() > 1) - updater->stepSize = static_cast(param[1].Integer()); - return updater; - } - else if (updaterJson["type"].String() == "ARMY_MOVEMENT") - { - auto updater = std::make_shared(); - if(updaterJson["parameters"].isVector()) - { - const auto & param = updaterJson["parameters"].Vector(); - if(param.size() < 4) - logMod->warn("Invalid ARMY_MOVEMENT parameters, using default!"); - else - { - updater->base = static_cast(param.at(0).Integer()); - updater->divider = static_cast(param.at(1).Integer()); - updater->multiplier = static_cast(param.at(2).Integer()); - updater->max = static_cast(param.at(3).Integer()); - } - return updater; - } - } - else - logMod->warn("Unknown updater type \"%s\"", updaterJson["type"].String()); - break; - } - return nullptr; -} - bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b) { const JsonNode * value = nullptr; diff --git a/lib/json/JsonBonus.h b/lib/json/JsonBonus.h index dd2b8fa12..db5ed492c 100644 --- a/lib/json/JsonBonus.h +++ b/lib/json/JsonBonus.h @@ -21,13 +21,13 @@ class CAddInfo; namespace JsonUtils { - DLL_LINKAGE std::shared_ptr parseBonus(const JsonVector & ability_vec); - DLL_LINKAGE std::shared_ptr parseBonus(const JsonNode & ability); - DLL_LINKAGE std::shared_ptr parseBuildingBonus(const JsonNode & ability, const FactionID & faction, const BuildingID & building, const std::string & description); - DLL_LINKAGE bool parseBonus(const JsonNode & ability, Bonus * placement); - DLL_LINKAGE std::shared_ptr parseLimiter(const JsonNode & limiter); - DLL_LINKAGE CSelector parseSelector(const JsonNode &ability); - DLL_LINKAGE void resolveAddInfo(CAddInfo & var, const JsonNode & node); + std::shared_ptr parseBonus(const JsonVector & ability_vec); + std::shared_ptr parseBonus(const JsonNode & ability); + std::shared_ptr parseBuildingBonus(const JsonNode & ability, const FactionID & faction, const BuildingID & building, const std::string & description); + bool parseBonus(const JsonNode & ability, Bonus * placement); + std::shared_ptr parseLimiter(const JsonNode & limiter); + CSelector parseSelector(const JsonNode &ability); + void resolveAddInfo(CAddInfo & var, const JsonNode & node); } VCMI_LIB_NAMESPACE_END diff --git a/lib/json/JsonUtils.cpp b/lib/json/JsonUtils.cpp index 9ddea0dfb..62d2fdebd 100644 --- a/lib/json/JsonUtils.cpp +++ b/lib/json/JsonUtils.cpp @@ -15,7 +15,7 @@ #include "../filesystem/Filesystem.h" -VCMI_LIB_NAMESPACE_BEGIN +VCMI_LIB_USING_NAMESPACE static const JsonNode nullNode; @@ -91,6 +91,8 @@ static void maximizeNode(JsonNode & node, const JsonNode & schema) eraseOptionalNodes(node, schema); } +VCMI_LIB_NAMESPACE_BEGIN + void JsonUtils::minimize(JsonNode & node, const std::string & schemaName) { minimizeNode(node, getSchema(schemaName));