1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-26 22:57:00 +02:00

Moved static methods outside of vcmi namespace

This commit is contained in:
Ivan Savenko 2024-02-14 20:45:14 +02:00
parent 757f77378d
commit 08deae4186
3 changed files with 112 additions and 108 deletions

View File

@ -22,7 +22,37 @@
#include "../constants/StringConstants.h"
#include "../modding/IdentifierStorage.h"
VCMI_LIB_NAMESPACE_BEGIN
VCMI_LIB_USING_NAMESPACE
template <typename T>
const T parseByMap(const std::map<std::string, T> & 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 <typename T>
const T parseByMapN(const std::map<std::string, T> & map, const JsonNode * val, const std::string & err)
{
if(val->isNumber())
return static_cast<T>(val->Integer());
else
return parseByMap<T>(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<GrowsWithLevelUpdater>();
const JsonVector param = updaterJson["parameters"].Vector();
updater->valPer20 = static_cast<int>(param[0].Integer());
if(param.size() > 1)
updater->stepSize = static_cast<int>(param[1].Integer());
return updater;
}
else if (updaterJson["type"].String() == "ARMY_MOVEMENT")
{
auto updater = std::make_shared<ArmyMovementUpdater>();
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<si32>(param.at(0).Integer());
updater->divider = static_cast<si32>(param.at(1).Integer());
updater->multiplier = static_cast<si32>(param.at(2).Integer());
updater->max = static_cast<si32>(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<Bonus> JsonUtils::parseBonus(const JsonVector & ability_vec)
{
auto b = std::make_shared<Bonus>();
@ -284,36 +385,6 @@ std::shared_ptr<Bonus> JsonUtils::parseBonus(const JsonVector & ability_vec)
return b;
}
template <typename T>
const T parseByMap(const std::map<std::string, T> & 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 <typename T>
const T parseByMapN(const std::map<std::string, T> & map, const JsonNode * val, const std::string & err)
{
if(val->isNumber())
return static_cast<T>(val->Integer());
else
return parseByMap<T>(map, val, err);
}
void JsonUtils::resolveAddInfo(CAddInfo & var, const JsonNode & node)
{
const JsonNode & value = node["addInfo"];
@ -567,75 +638,6 @@ std::shared_ptr<Bonus> 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<GrowsWithLevelUpdater>();
const JsonVector param = updaterJson["parameters"].Vector();
updater->valPer20 = static_cast<int>(param[0].Integer());
if(param.size() > 1)
updater->stepSize = static_cast<int>(param[1].Integer());
return updater;
}
else if (updaterJson["type"].String() == "ARMY_MOVEMENT")
{
auto updater = std::make_shared<ArmyMovementUpdater>();
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<si32>(param.at(0).Integer());
updater->divider = static_cast<si32>(param.at(1).Integer());
updater->multiplier = static_cast<si32>(param.at(2).Integer());
updater->max = static_cast<si32>(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;

View File

@ -21,13 +21,13 @@ class CAddInfo;
namespace JsonUtils
{
DLL_LINKAGE std::shared_ptr<Bonus> parseBonus(const JsonVector & ability_vec);
DLL_LINKAGE std::shared_ptr<Bonus> parseBonus(const JsonNode & ability);
DLL_LINKAGE std::shared_ptr<Bonus> 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<ILimiter> parseLimiter(const JsonNode & limiter);
DLL_LINKAGE CSelector parseSelector(const JsonNode &ability);
DLL_LINKAGE void resolveAddInfo(CAddInfo & var, const JsonNode & node);
std::shared_ptr<Bonus> parseBonus(const JsonVector & ability_vec);
std::shared_ptr<Bonus> parseBonus(const JsonNode & ability);
std::shared_ptr<Bonus> parseBuildingBonus(const JsonNode & ability, const FactionID & faction, const BuildingID & building, const std::string & description);
bool parseBonus(const JsonNode & ability, Bonus * placement);
std::shared_ptr<ILimiter> parseLimiter(const JsonNode & limiter);
CSelector parseSelector(const JsonNode &ability);
void resolveAddInfo(CAddInfo & var, const JsonNode & node);
}
VCMI_LIB_NAMESPACE_END

View File

@ -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));