1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-11-25 22:42:04 +02:00

vcmi: deprecated bonus converter

It converts almost all sorts of deprecated bonuses from mods
when loading json. It can print to console correct new variant
or bonus.

Also removed a bunch of deprecated bonuses from list.
It will break saves!!!
This commit is contained in:
Konstantin
2023-03-05 03:16:05 +03:00
parent 95503d0623
commit 930955f268
5 changed files with 318 additions and 30 deletions

View File

@@ -805,26 +805,82 @@ std::shared_ptr<Bonus> JsonUtils::parseBuildingBonus(const JsonNode &ability, Bu
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.toJson());
auto params = BonusParams(ability["type"].String(),
ability["subtype"].isString() ? ability["subtype"].String() : "",
ability["subtype"].isNumber() ? ability["subtype"].Integer() : -1);
if(params.isConverted)
{
if(!params.valRelevant) {
params.val = static_cast<si32>(ability["val"].Float());
params.valRelevant = true;
if(params.type == Bonus::SPECIFIC_SPELL_POWER) //First Aid value should be substracted by 10
params.val -= 10; //Base First Aid value
}
Bonus::ValueType valueType = Bonus::ADDITIVE_VALUE;
if(!ability["valueType"].isNull())
valueType = bonusValueMap.find(ability["valueType"].String())->second;
if(ability["type"].String() == "SECONDARY_SKILL_PREMY" && valueType == Bonus::PERCENT_TO_BASE) //assume secondary skill special
{
params.valueType = Bonus::PERCENT_TO_TARGET_TYPE;
params.targetType = Bonus::SECONDARY_SKILL;
params.targetTypeRelevant = true;
}
if(!params.valueTypeRelevant) {
params.valueType = valueType;
params.valueTypeRelevant = true;
}
logMod->warn("Please, use this bonus:\n%s\nConverted sucessfully!", params.toJson().toJson());
return params;
}
else
logMod->error("Cannot convert bonus!\n%s", ability.toJson());
}
BonusParams ret;
ret.isConverted = false;
return ret;
}
bool JsonUtils::parseBonus(const JsonNode &ability, Bonus *b)
{
const JsonNode *value;
std::string type = ability["type"].String();
auto it = bonusNameMap.find(type);
auto params = std::make_unique<BonusParams>(false);
if (it == bonusNameMap.end())
{
logMod->error("Error: invalid ability type %s.", type);
return false;
params = std::make_unique<BonusParams>(convertDeprecatedBonus(ability));
if(!params->isConverted)
{
logMod->error("Error: invalid ability type %s.", type);
return false;
}
b->type = params->type;
b->val = params->val;
b->valType = params->valueType;
if(params->targetTypeRelevant)
b->targetSourceType = params->targetType;
}
b->type = it->second;
else
b->type = it->second;
resolveIdentifier(b->subtype, ability, "subtype");
resolveIdentifier(b->subtype, params->isConverted ? params->toJson() : ability, "subtype");
b->val = static_cast<si32>(ability["val"].Float());
if(!params->isConverted)
{
b->val = static_cast<si32>(ability["val"].Float());
value = &ability["valueType"];
if (!value->isNull())
b->valType = static_cast<Bonus::ValueType>(parseByMapN(bonusValueMap, value, "value type "));
value = &ability["valueType"];
if (!value->isNull())
b->valType = static_cast<Bonus::ValueType>(parseByMapN(bonusValueMap, value, "value type "));
}
b->stacking = ability["stacking"].String();