2016-02-13 09:47:40 +02:00
|
|
|
/*
|
|
|
|
* JsonDeserializer.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "JsonDeserializer.h"
|
|
|
|
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
#include <vstd/StringUtils.h>
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
JsonDeserializer::JsonDeserializer(const IInstanceResolver * instanceResolver_, const JsonNode & root_):
|
Entities redesign and a few ERM features
* Made most Handlers derived from CHandlerBase and moved service API there.
* Declared existing Entity APIs.
* Added basic script context caching
* Started Lua script module
* Started Lua spell effect API
* Started script state persistence
* Started battle info callback binding
* CommitPackage removed
* Extracted spells::Caster to own header; Expanded Spell API.
* implemented !!MC:S, !!FU:E, !!FU:P, !!MA, !!VR:H, !!VR:C
* !!BU:C, !!BU:E, !!BU:G, !!BU:M implemented
* Allow use of "MC:S@varName@" to declare normal variable (technically v-variable with string key)
* Re-enabled VERM macros.
* !?GM0 added
* !?TM implemented
* Added !!MF:N
* Started !?OB, !!BM, !!HE, !!OW, !!UN
* Added basic support of w-variables
* Added support for ERM indirect variables
* Made !?FU regular trigger
* !!re (ERA loop receiver) implemented
* Fixed ERM receivers with zero args.
2018-03-17 16:58:30 +02:00
|
|
|
JsonTreeSerializer(instanceResolver_, &root_, false, false)
|
2016-02-13 09:47:40 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, boost::logic::tribool & value)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonNode & data = currentObject->operator[](fieldName);
|
2017-11-26 23:18:18 +02:00
|
|
|
if(data.getType() != JsonNode::JsonType::DATA_BOOL)
|
2016-11-13 12:38:42 +02:00
|
|
|
value = boost::logic::indeterminate;
|
|
|
|
else
|
|
|
|
value = data.Bool();
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const TDecoder & decoder, const TEncoder & encoder)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2016-11-13 12:38:42 +02:00
|
|
|
std::string identifier;
|
|
|
|
serializeString(fieldName, identifier);
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
value = defaultValue.value_or(0);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
2022-11-15 02:20:55 +02:00
|
|
|
if(!identifier.empty())
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
si32 rawId = decoder(identifier);
|
2020-12-13 02:33:28 +02:00
|
|
|
|
|
|
|
if(rawId < 0) //may be, user has installed the mod into another directory...
|
|
|
|
{
|
|
|
|
auto internalId = vstd::splitStringToPair(identifier, ':').second;
|
|
|
|
auto currentScope = getCurrent().meta;
|
|
|
|
auto actualId = currentScope.length() > 0 ? currentScope + ":" + internalId : internalId;
|
|
|
|
|
|
|
|
rawId = decoder(actualId);
|
|
|
|
|
|
|
|
if(rawId >= 0)
|
|
|
|
logMod->warn("Identifier %s has been resolved as %s instead of %s", internalId, actualId, identifier);
|
|
|
|
}
|
2016-11-13 12:38:42 +02:00
|
|
|
if(rawId >= 0)
|
|
|
|
value = rawId;
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, std::vector<si32> & value, const TDecoder & decoder, const TEncoder & encoder)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonVector & data = currentObject->operator[](fieldName).Vector();
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
value.clear();
|
|
|
|
value.reserve(data.size());
|
|
|
|
|
2022-05-19 14:14:50 +02:00
|
|
|
for(const JsonNode& elem : data)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
|
|
|
si32 rawId = decoder(elem.String());
|
|
|
|
|
|
|
|
if(rawId >= 0)
|
|
|
|
value.push_back(rawId);
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, double & value, const std::optional<double> & defaultValue)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonNode & data = currentObject->operator[](fieldName);
|
2016-02-14 13:22:46 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!data.isNumber())
|
2023-04-16 19:42:56 +02:00
|
|
|
value = defaultValue.value_or(0);//todo: report error on not null?
|
2016-02-14 13:22:46 +02:00
|
|
|
else
|
2016-11-13 12:38:42 +02:00
|
|
|
value = data.Float();
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, si64 & value, const std::optional<si64> & defaultValue)
|
2016-02-22 01:37:19 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonNode & data = currentObject->operator[](fieldName);
|
2016-02-22 01:37:19 +02:00
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
if(!data.isNumber())
|
2023-04-16 19:42:56 +02:00
|
|
|
value = defaultValue.value_or(0);//todo: report error on not null?
|
2016-11-13 12:38:42 +02:00
|
|
|
else
|
|
|
|
value = data.Integer();
|
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, si32 & value, const std::optional<si32> & defaultValue, const std::vector<std::string> & enumMap)
|
2016-11-13 12:38:42 +02:00
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const std::string & valueName = currentObject->operator[](fieldName).String();
|
2016-02-22 01:37:19 +02:00
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
const si32 actualOptional = defaultValue.value_or(0);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
si32 rawValue = vstd::find_pos(enumMap, valueName);
|
|
|
|
if(rawValue < 0)
|
|
|
|
value = actualOptional;
|
2016-02-22 01:37:19 +02:00
|
|
|
else
|
2016-11-13 12:38:42 +02:00
|
|
|
value = rawValue;
|
2016-02-22 01:37:19 +02:00
|
|
|
}
|
|
|
|
|
2023-09-04 21:23:20 +02:00
|
|
|
void JsonDeserializer::serializeInternal(const std::string & fieldName, std::vector<std::string> & value)
|
|
|
|
{
|
|
|
|
const JsonVector & data = currentObject->operator[](fieldName).Vector();
|
|
|
|
|
|
|
|
value.clear();
|
|
|
|
value.reserve(data.size());
|
|
|
|
|
|
|
|
for(const JsonNode& elem : data)
|
|
|
|
{
|
|
|
|
std::string rawId = elem.String();
|
|
|
|
value.push_back(rawId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 06:08:49 +02:00
|
|
|
void JsonDeserializer::serializeInternal(std::string & value)
|
|
|
|
{
|
|
|
|
value = currentObject->String();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JsonDeserializer::serializeInternal(int64_t & value)
|
|
|
|
{
|
|
|
|
value = currentObject->Integer();
|
|
|
|
}
|
|
|
|
|
2023-11-05 16:35:51 +02:00
|
|
|
void JsonDeserializer::serializeLIC(const std::string & fieldName, const TDecoder & decoder, const TEncoder & encoder, const std::set<int32_t> & standard, std::set<int32_t> & value)
|
2016-02-14 13:22:46 +02:00
|
|
|
{
|
2023-11-05 16:35:51 +02:00
|
|
|
LICSet lic(standard, decoder, encoder);
|
|
|
|
serializeLIC(fieldName, lic);
|
|
|
|
value = lic.any;
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-13 12:38:42 +02:00
|
|
|
void JsonDeserializer::serializeLIC(const std::string & fieldName, LICSet & value)
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
const JsonNode & field = currentObject->operator[](fieldName);
|
2016-11-13 12:38:42 +02:00
|
|
|
|
|
|
|
const JsonNode & anyOf = field["anyOf"];
|
|
|
|
const JsonNode & allOf = field["allOf"];
|
|
|
|
const JsonNode & noneOf = field["noneOf"];
|
|
|
|
|
|
|
|
value.all.clear();
|
|
|
|
value.none.clear();
|
|
|
|
|
|
|
|
if(anyOf.Vector().empty())
|
|
|
|
{
|
|
|
|
//permissive mode
|
|
|
|
value.any = value.standard;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//restrictive mode
|
|
|
|
value.any.clear();
|
|
|
|
readLICPart(anyOf, value.decoder, value.any);
|
|
|
|
|
|
|
|
for(si32 item : value.standard)
|
|
|
|
if(!vstd::contains(value.any, item))
|
|
|
|
value.none.insert(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
readLICPart(allOf, value.decoder, value.all);
|
|
|
|
readLICPart(noneOf, value.decoder, value.none);
|
|
|
|
|
|
|
|
//remove any banned from allowed and required
|
|
|
|
auto isBanned = [&value](const si32 item)->bool
|
|
|
|
{
|
|
|
|
return vstd::contains(value.none, item);
|
|
|
|
};
|
|
|
|
vstd::erase_if(value.all, isBanned);
|
|
|
|
vstd::erase_if(value.any, isBanned);
|
|
|
|
|
|
|
|
//add all required to allowed
|
|
|
|
for(si32 item : value.all)
|
|
|
|
{
|
|
|
|
value.any.insert(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 13:22:46 +02:00
|
|
|
void JsonDeserializer::serializeString(const std::string & fieldName, std::string & value)
|
|
|
|
{
|
2017-07-20 06:08:49 +02:00
|
|
|
value = currentObject->operator[](fieldName).String();
|
|
|
|
}
|
|
|
|
|
2023-04-16 19:42:56 +02:00
|
|
|
void JsonDeserializer::serializeRaw(const std::string & fieldName, JsonNode & value, const std::optional<std::reference_wrapper<const JsonNode>> defaultValue)
|
2017-07-20 06:08:49 +02:00
|
|
|
{
|
|
|
|
const JsonNode & data = currentObject->operator[](fieldName);
|
|
|
|
if(data.getType() == JsonNode::JsonType::DATA_NULL)
|
|
|
|
{
|
|
|
|
if(defaultValue)
|
2023-04-16 19:42:56 +02:00
|
|
|
value = defaultValue.value();
|
2017-07-20 06:08:49 +02:00
|
|
|
else
|
|
|
|
value.clear();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
value = data;
|
|
|
|
}
|
2016-02-14 13:22:46 +02:00
|
|
|
}
|
2016-02-13 09:47:40 +02:00
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_END
|