1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-01-02 00:10:22 +02:00

vcmi: modernize lib/serializer

This commit is contained in:
Konstantin 2023-02-11 18:13:21 +03:00
parent 6362983db2
commit 8661496f6c
22 changed files with 155 additions and 176 deletions

View File

@ -125,6 +125,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/serializer/JsonSerializeFormat.cpp ${MAIN_LIB_DIR}/serializer/JsonSerializeFormat.cpp
${MAIN_LIB_DIR}/serializer/JsonSerializer.cpp ${MAIN_LIB_DIR}/serializer/JsonSerializer.cpp
${MAIN_LIB_DIR}/serializer/JsonUpdater.cpp ${MAIN_LIB_DIR}/serializer/JsonUpdater.cpp
${MAIN_LIB_DIR}/serializer/ILICReader.cpp
${MAIN_LIB_DIR}/spells/AbilityCaster.cpp ${MAIN_LIB_DIR}/spells/AbilityCaster.cpp
${MAIN_LIB_DIR}/spells/AdventureSpellMechanics.cpp ${MAIN_LIB_DIR}/spells/AdventureSpellMechanics.cpp
@ -361,6 +362,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE)
${MAIN_LIB_DIR}/serializer/JsonSerializeFormat.h ${MAIN_LIB_DIR}/serializer/JsonSerializeFormat.h
${MAIN_LIB_DIR}/serializer/JsonSerializer.h ${MAIN_LIB_DIR}/serializer/JsonSerializer.h
${MAIN_LIB_DIR}/serializer/JsonUpdater.h ${MAIN_LIB_DIR}/serializer/JsonUpdater.h
${MAIN_LIB_DIR}/serializer/ILICReader.h
${MAIN_LIB_DIR}/serializer/Cast.h ${MAIN_LIB_DIR}/serializer/Cast.h
${MAIN_LIB_DIR}/spells/AbilityCaster.h ${MAIN_LIB_DIR}/spells/AbilityCaster.h

View File

@ -24,13 +24,12 @@ CLoadFile::CLoadFile(const boost::filesystem::path & fname, int minimalVersion)
openNextFile(fname, minimalVersion); openNextFile(fname, minimalVersion);
} }
CLoadFile::~CLoadFile() //must be instantiated in .cpp file for access to complete types of all member fields
{ CLoadFile::~CLoadFile() = default;
}
int CLoadFile::read(void * data, unsigned size) int CLoadFile::read(void * data, unsigned size)
{ {
sfile->read((char*)data,size); sfile->read(reinterpret_cast<char *>(data), size);
return size; return size;
} }
@ -51,7 +50,7 @@ void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalV
//we can read //we can read
char buffer[4]; char buffer[4];
sfile->read(buffer, 4); sfile->read(buffer, 4);
if(std::memcmp(buffer,"VCMI",4)) if(std::memcmp(buffer, "VCMI", 4) != 0)
THROW_FORMAT("Error: not a VCMI file(%s)!", fName); THROW_FORMAT("Error: not a VCMI file(%s)!", fName);
serializer & serializer.fileVersion; serializer & serializer.fileVersion;
@ -62,7 +61,7 @@ void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalV
{ {
logGlobal->warn("Warning format version mismatch: found %d when current is %d! (file %s)\n", serializer.fileVersion, SERIALIZATION_VERSION , fName); logGlobal->warn("Warning format version mismatch: found %d when current is %d! (file %s)\n", serializer.fileVersion, SERIALIZATION_VERSION , fName);
auto versionptr = (char*)&serializer.fileVersion; auto * versionptr = reinterpret_cast<char *>(&serializer.fileVersion);
std::reverse(versionptr, versionptr + 4); std::reverse(versionptr, versionptr + 4);
logGlobal->warn("Version number reversed is %x, checking...", serializer.fileVersion); logGlobal->warn("Version number reversed is %x, checking...", serializer.fileVersion);
@ -99,7 +98,7 @@ void CLoadFile::clear()
void CLoadFile::checkMagicBytes(const std::string &text) void CLoadFile::checkMagicBytes(const std::string &text)
{ {
std::string loaded = text; std::string loaded = text;
read((void*)loaded.data(), (unsigned int)text.length()); read((void *)loaded.data(), static_cast<unsigned int>(text.length()));
if(loaded != text) if(loaded != text)
throw std::runtime_error("Magic bytes doesn't match!"); throw std::runtime_error("Magic bytes doesn't match!");
} }

View File

@ -192,7 +192,7 @@ public:
void load(T &data) void load(T &data)
{ {
unsigned length = sizeof(data); unsigned length = sizeof(data);
char* dataPtr = (char*)&data; char * dataPtr = reinterpret_cast<char *>(&data);
this->read(dataPtr,length); this->read(dataPtr,length);
if(reverseEndianess) if(reverseEndianess)
std::reverse(dataPtr, dataPtr + length); std::reverse(dataPtr, dataPtr + length);

View File

@ -24,9 +24,8 @@ CSaveFile::CSaveFile(const boost::filesystem::path &fname)
openNextFile(fname); openNextFile(fname);
} }
CSaveFile::~CSaveFile() //must be instantiated in .cpp file for access to complete types of all member fields
{ CSaveFile::~CSaveFile() = default;
}
int CSaveFile::write(const void * data, unsigned size) int CSaveFile::write(const void * data, unsigned size)
{ {
@ -73,7 +72,7 @@ void CSaveFile::clear()
void CSaveFile::putMagicBytes(const std::string &text) void CSaveFile::putMagicBytes(const std::string &text)
{ {
write(text.c_str(), (unsigned int)text.length()); write(text.c_str(), static_cast<unsigned int>(text.length()));
} }
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -40,7 +40,7 @@ int CLoadIntegrityValidator::read( void * data, unsigned size )
if(!foundDesync) if(!foundDesync)
{ {
controlFile->read(controlData.data(), size); controlFile->read(controlData.data(), size);
if(std::memcmp(data, controlData.data(), size)) if(std::memcmp(data, controlData.data(), size) != 0)
{ {
logGlobal->error("Desync found! Position: %d", primaryFile->sfile->tellg()); logGlobal->error("Desync found! Position: %d", primaryFile->sfile->tellg());
foundDesync = true; foundDesync = true;
@ -57,7 +57,7 @@ std::unique_ptr<CLoadFile> CLoadIntegrityValidator::decay()
return std::move(primaryFile); return std::move(primaryFile);
} }
void CLoadIntegrityValidator::checkMagicBytes( const std::string &text ) void CLoadIntegrityValidator::checkMagicBytes(const std::string & text) const
{ {
assert(primaryFile); assert(primaryFile);
assert(controlFile); assert(controlFile);

View File

@ -25,7 +25,7 @@ public:
CLoadIntegrityValidator(const boost::filesystem::path &primaryFileName, const boost::filesystem::path &controlFileName, int minimalVersion = SERIALIZATION_VERSION); //throws! CLoadIntegrityValidator(const boost::filesystem::path &primaryFileName, const boost::filesystem::path &controlFileName, int minimalVersion = SERIALIZATION_VERSION); //throws!
int read( void * data, unsigned size) override; //throws! int read( void * data, unsigned size) override; //throws!
void checkMagicBytes(const std::string &text); void checkMagicBytes(const std::string & text) const;
std::unique_ptr<CLoadFile> decay(); //returns primary file. CLoadIntegrityValidator stops being usable anymore std::unique_ptr<CLoadFile> decay(); //returns primary file. CLoadIntegrityValidator stops being usable anymore
}; };

View File

@ -32,13 +32,11 @@ int CMemorySerializer::write(const void * data, unsigned size)
return size; return size;
} }
CMemorySerializer::CMemorySerializer(): iser(this), oser(this) CMemorySerializer::CMemorySerializer(): iser(this), oser(this), readPos(0)
{ {
readPos = 0;
registerTypes(iser); registerTypes(iser);
registerTypes(oser); registerTypes(oser);
iser.fileVersion = SERIALIZATION_VERSION; iser.fileVersion = SERIALIZATION_VERSION;
} }
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -17,17 +17,8 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
CSerializer::~CSerializer() //must be instantiated in .cpp file for access to complete types of all member fields
{ CSerializer::~CSerializer() = default;
}
CSerializer::CSerializer()
{
smartVectorMembersSerialization = false;
sendStackInstanceByIds = false;
}
void CSerializer::addStdVecItems(CGameState *gs, LibClasses *lib) void CSerializer::addStdVecItems(CGameState *gs, LibClasses *lib)
{ {

View File

@ -80,10 +80,9 @@ class DLL_LINKAGE CSerializer
TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
public: public:
bool smartVectorMembersSerialization; bool smartVectorMembersSerialization = false;
bool sendStackInstanceByIds; bool sendStackInstanceByIds = false;
CSerializer();
~CSerializer(); ~CSerializer();
virtual void reportState(vstd::CLoggerBase * out){}; virtual void reportState(vstd::CLoggerBase * out){};

View File

@ -74,7 +74,7 @@ std::vector<CTypeList::TypeInfoPtr> CTypeList::castSequence(TypeInfoPtr from, Ty
std::map<TypeInfoPtr, TypeInfoPtr> previous; std::map<TypeInfoPtr, TypeInfoPtr> previous;
std::queue<TypeInfoPtr> q; std::queue<TypeInfoPtr> q;
q.push(to); q.push(to);
while(q.size()) while(!q.empty())
{ {
auto typeNode = q.front(); auto typeNode = q.front();
q.pop(); q.pop();

View File

@ -109,12 +109,7 @@ private:
return ptr; return ptr;
} }
CTypeList &operator=(CTypeList &) CTypeList & operator=(CTypeList &) = delete;
{
// As above.
assert(0);
return *this;
}
TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr
TypeInfoPtr registerType(const std::type_info *type); TypeInfoPtr registerType(const std::type_info *type);

View File

@ -21,16 +21,6 @@ VCMI_LIB_NAMESPACE_BEGIN
using namespace boost; using namespace boost;
using namespace boost::asio::ip; using namespace boost::asio::ip;
#if defined(__hppa__) || \
defined(__m68k__) || defined(mc68000) || defined(_M_M68K) || \
(defined(__MIPS__) && defined(__MISPEB__)) || \
defined(__ppc__) || defined(__POWERPC__) || defined(_M_PPC) || \
defined(__sparc__)
#define BIG_ENDIAN
#else
#define LIL_ENDIAN
#endif
struct ConnectionBuffers struct ConnectionBuffers
{ {
boost::asio::streambuf readBuffer; boost::asio::streambuf readBuffer;
@ -58,7 +48,7 @@ void CConnection::init()
disableStackSendingByID(); disableStackSendingByID();
registerTypes(iser); registerTypes(iser);
registerTypes(oser); registerTypes(oser);
#ifdef LIL_ENDIAN #ifndef VCMI_ENDIAN_BIG
myEndianess = true; myEndianess = true;
#else #else
myEndianess = false; myEndianess = false;
@ -75,15 +65,21 @@ void CConnection::init()
iser.fileVersion = SERIALIZATION_VERSION; iser.fileVersion = SERIALIZATION_VERSION;
} }
CConnection::CConnection(std::string host, ui16 port, std::string Name, std::string UUID) CConnection::CConnection(const std::string & host, ui16 port, std::string Name, std::string UUID):
: io_service(std::make_shared<asio::io_service>()), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0) io_service(std::make_shared<asio::io_service>()),
iser(this),
oser(this),
name(std::move(Name)),
uuid(std::move(UUID))
{ {
int i; int i = 0;
boost::system::error_code error = asio::error::host_not_found; boost::system::error_code error = asio::error::host_not_found;
socket = std::make_shared<tcp::socket>(*io_service); socket = std::make_shared<tcp::socket>(*io_service);
tcp::resolver resolver(*io_service); tcp::resolver resolver(*io_service);
tcp::resolver::iterator end, pom, endpoint_iterator = resolver.resolve(tcp::resolver::query(host, std::to_string(port)),error); tcp::resolver::iterator end;
tcp::resolver::iterator pom;
tcp::resolver::iterator endpoint_iterator = resolver.resolve(tcp::resolver::query(host, std::to_string(port)), error);
if(error) if(error)
{ {
logNetwork->error("Problem with resolving: \n%s", error.message()); logNetwork->error("Problem with resolving: \n%s", error.message());
@ -97,7 +93,6 @@ CConnection::CConnection(std::string host, ui16 port, std::string Name, std::str
logNetwork->error("Critical problem: No endpoints found!"); logNetwork->error("Critical problem: No endpoints found!");
goto connerror1; goto connerror1;
} }
i=0;
while(pom != end) while(pom != end)
{ {
logNetwork->info("\t%d:%s", i, (boost::asio::ip::tcp::endpoint&)*pom); logNetwork->info("\t%d:%s", i, (boost::asio::ip::tcp::endpoint&)*pom);
@ -129,13 +124,24 @@ connerror1:
logNetwork->error("No error info. "); logNetwork->error("No error info. ");
throw std::runtime_error("Can't establish connection :("); throw std::runtime_error("Can't establish connection :(");
} }
CConnection::CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID) CConnection::CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID):
: iser(this), oser(this), socket(Socket), name(Name), uuid(UUID), connectionID(0) iser(this),
oser(this),
socket(std::move(Socket)),
name(std::move(Name)),
uuid(std::move(UUID))
{ {
init(); init();
} }
CConnection::CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> io_service, std::string Name, std::string UUID) CConnection::CConnection(const std::shared_ptr<TAcceptor> & acceptor,
: io_service(io_service), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0) const std::shared_ptr<boost::asio::io_service> & io_service,
std::string Name,
std::string UUID):
io_service(io_service),
iser(this),
oser(this),
name(std::move(Name)),
uuid(std::move(UUID))
{ {
boost::system::error_code error = asio::error::host_not_found; boost::system::error_code error = asio::error::host_not_found;
socket = std::make_shared<tcp::socket>(*io_service); socket = std::make_shared<tcp::socket>(*io_service);
@ -181,8 +187,7 @@ int CConnection::write(const void * data, unsigned size)
return size; return size;
} }
int ret; int ret = static_cast<int>(asio::write(*socket, asio::const_buffers_1(asio::const_buffer(data, size))));
ret = static_cast<int>(asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size))));
return ret; return ret;
} }
catch(...) catch(...)

View File

@ -88,8 +88,8 @@ public:
int connectionID; int connectionID;
std::shared_ptr<boost::thread> handler; std::shared_ptr<boost::thread> handler;
CConnection(std::string host, ui16 port, std::string Name, std::string UUID); CConnection(const std::string & host, ui16 port, std::string Name, std::string UUID);
CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> Io_service, std::string Name, std::string UUID); CConnection(const std::shared_ptr<TAcceptor> & acceptor, const std::shared_ptr<boost::asio::io_service> & Io_service, std::string Name, std::string UUID);
CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID); //use immediately after accepting connection into socket
void close(); void close();

View File

@ -0,0 +1,49 @@
/*
* JsonTreeSerializer.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 "ILICReader.h"
#include "../JsonNode.h"
#include <vstd/StringUtils.h>
VCMI_LIB_NAMESPACE_BEGIN
void ILICReader::readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, bool val, std::vector<bool> & value) const
{
for(const auto & index : part.Vector())
{
const std::string & identifier = index.String();
const std::string type = typeid(decltype(this)).name();
const si32 rawId = decoder(identifier);
if(rawId >= 0)
{
if(rawId < value.size())
value[rawId] = val;
else
logGlobal->error("%s::serializeLIC: id out of bounds %d", type, rawId);
}
}
}
void ILICReader::readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, std::set<si32> & value) const
{
for(const auto & index : part.Vector())
{
const std::string & identifier = index.String();
const si32 rawId = decoder(identifier);
if(rawId != -1)
value.insert(rawId);
}
}
VCMI_LIB_NAMESPACE_END

View File

@ -0,0 +1,23 @@
/*
* ILICReader.h, 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
*
*/
#pragma once
#include "JsonTreeSerializer.h"
VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE ILICReader
{
protected:
void readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, bool val, std::vector<bool> & value) const;
void readLICPart(const JsonNode & part, const JsonSerializeFormat::TDecoder & decoder, std::set<si32> & value) const;
};
VCMI_LIB_NAMESPACE_END

View File

@ -254,34 +254,4 @@ void JsonDeserializer::serializeRaw(const std::string & fieldName, JsonNode & va
} }
} }
void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
{
for(size_t index = 0; index < part.Vector().size(); index++)
{
const std::string & identifier = part.Vector()[index].String();
const si32 rawId = decoder(identifier);
if(rawId >= 0)
{
if(rawId < value.size())
value[rawId] = val;
else
logGlobal->error("JsonDeserializer::serializeLIC: id out of bounds %d", rawId);
}
}
}
void JsonDeserializer::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
{
for(size_t index = 0; index < part.Vector().size(); index++)
{
const std::string & identifier = part.Vector()[index].String();
const si32 rawId = decoder(identifier);
if(rawId != -1)
value.insert(rawId);
}
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -9,11 +9,12 @@
*/ */
#pragma once #pragma once
#include "ILICReader.h"
#include "JsonTreeSerializer.h" #include "JsonTreeSerializer.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class DLL_LINKAGE JsonDeserializer: public JsonTreeSerializer<const JsonNode *> class DLL_LINKAGE JsonDeserializer: public JsonTreeSerializer<const JsonNode *>, public ILICReader
{ {
public: public:
JsonDeserializer(const IInstanceResolver * instanceResolver_, const JsonNode & root_); JsonDeserializer(const IInstanceResolver * instanceResolver_, const JsonNode & root_);
@ -35,10 +36,6 @@ protected:
void serializeInternal(std::string & value) override; void serializeInternal(std::string & value) override;
void serializeInternal(int64_t & value) override; void serializeInternal(int64_t & value) override;
private:
void readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value);
void readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value);
}; };
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -15,9 +15,7 @@
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
//JsonSerializeHelper //JsonSerializeHelper
JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other): JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other) noexcept: owner(other.owner), restoreState(false)
owner(other.owner),
restoreState(false)
{ {
std::swap(restoreState, other.restoreState); std::swap(restoreState, other.restoreState);
} }
@ -40,33 +38,17 @@ JsonSerializeHelper::JsonSerializeHelper(JsonSerializeFormat * owner_)
} }
//JsonStructSerializer //JsonStructSerializer
JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other) JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other) noexcept: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) {}
: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other)))
{
}
JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat * owner_) JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat * owner_)
: JsonSerializeHelper(owner_) : JsonSerializeHelper(owner_)
{ {
} }
JsonStructSerializer::~JsonStructSerializer()
{
}
//JsonArraySerializer //JsonArraySerializer
JsonArraySerializer::JsonArraySerializer(JsonArraySerializer && other) JsonArraySerializer::JsonArraySerializer(JsonArraySerializer && other) noexcept: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) {}
: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other)))
{
} JsonArraySerializer::JsonArraySerializer(JsonSerializeFormat * owner_): JsonSerializeHelper(owner_), thisNode(&owner->getCurrent()) {}
JsonArraySerializer::JsonArraySerializer(JsonSerializeFormat * owner_):
JsonSerializeHelper(owner_)
{
thisNode = &owner->getCurrent();
}
JsonStructSerializer JsonArraySerializer::enterStruct(const size_t index) JsonStructSerializer JsonArraySerializer::enterStruct(const size_t index)
{ {
@ -110,16 +92,20 @@ size_t JsonArraySerializer::size() const
} }
//JsonSerializeFormat::LIC //JsonSerializeFormat::LIC
JsonSerializeFormat::LIC::LIC(const std::vector<bool> & Standard, const TDecoder Decoder, const TEncoder Encoder): JsonSerializeFormat::LIC::LIC(const std::vector<bool> & Standard, TDecoder Decoder, TEncoder Encoder):
standard(Standard), decoder(Decoder), encoder(Encoder) standard(Standard),
decoder(std::move(Decoder)),
encoder(std::move(Encoder))
{ {
any.resize(standard.size(), false); any.resize(standard.size(), false);
all.resize(standard.size(), false); all.resize(standard.size(), false);
none.resize(standard.size(), false); none.resize(standard.size(), false);
} }
JsonSerializeFormat::LICSet::LICSet(const std::set<si32>& Standard, const TDecoder Decoder, const TEncoder Encoder): JsonSerializeFormat::LICSet::LICSet(const std::set<si32> & Standard, TDecoder Decoder, TEncoder Encoder):
standard(Standard), decoder(Decoder), encoder(Encoder) standard(Standard),
decoder(std::move(Decoder)),
encoder(std::move(Encoder))
{ {
} }

View File

@ -28,7 +28,7 @@ public:
class DLL_LINKAGE JsonSerializeHelper: public boost::noncopyable class DLL_LINKAGE JsonSerializeHelper: public boost::noncopyable
{ {
public: public:
JsonSerializeHelper(JsonSerializeHelper && other); JsonSerializeHelper(JsonSerializeHelper && other) noexcept;
virtual ~JsonSerializeHelper(); virtual ~JsonSerializeHelper();
JsonSerializeFormat * operator->(); JsonSerializeFormat * operator->();
@ -44,8 +44,8 @@ private:
class DLL_LINKAGE JsonStructSerializer: public JsonSerializeHelper class DLL_LINKAGE JsonStructSerializer: public JsonSerializeHelper
{ {
public: public:
JsonStructSerializer(JsonStructSerializer && other); JsonStructSerializer(JsonStructSerializer && other) noexcept;
~JsonStructSerializer();
protected: protected:
JsonStructSerializer(JsonSerializeFormat * owner_); JsonStructSerializer(JsonSerializeFormat * owner_);
@ -56,7 +56,7 @@ protected:
class DLL_LINKAGE JsonArraySerializer: public JsonSerializeHelper class DLL_LINKAGE JsonArraySerializer: public JsonSerializeHelper
{ {
public: public:
JsonArraySerializer(JsonArraySerializer && other); JsonArraySerializer(JsonArraySerializer && other) noexcept;
JsonStructSerializer enterStruct(const size_t index); JsonStructSerializer enterStruct(const size_t index);
JsonArraySerializer enterArray(const size_t index); JsonArraySerializer enterArray(const size_t index);
@ -102,17 +102,17 @@ class DLL_LINKAGE JsonSerializeFormat: public boost::noncopyable
public: public:
///user-provided callback to resolve string identifier ///user-provided callback to resolve string identifier
///returns resolved identifier or -1 on error ///returns resolved identifier or -1 on error
typedef std::function<si32(const std::string &)> TDecoder; using TDecoder = std::function<si32(const std::string &)>;
///user-provided callback to get string identifier ///user-provided callback to get string identifier
///may assume that object index is valid ///may assume that object index is valid
typedef std::function<std::string(si32)> TEncoder; using TEncoder = std::function<std::string(si32)>;
typedef std::function<void(JsonSerializeFormat &)> TSerialize; using TSerialize = std::function<void(JsonSerializeFormat &)>;
struct LIC struct LIC
{ {
LIC(const std::vector<bool> & Standard, const TDecoder Decoder, const TEncoder Encoder); LIC(const std::vector<bool> & Standard, TDecoder Decoder, TEncoder Encoder);
const std::vector<bool> & standard; const std::vector<bool> & standard;
const TDecoder decoder; const TDecoder decoder;
@ -122,7 +122,7 @@ public:
struct LICSet struct LICSet
{ {
LICSet(const std::set<si32> & Standard, const TDecoder Decoder, const TEncoder Encoder); LICSet(const std::set<si32> & Standard, TDecoder Decoder, TEncoder Encoder);
const std::set<si32> & standard; const std::set<si32> & standard;
const TDecoder decoder; const TDecoder decoder;

View File

@ -27,10 +27,9 @@ protected:
T currentObject; T currentObject;
std::vector<T> treeRoute; std::vector<T> treeRoute;
JsonTreeSerializer(const IInstanceResolver * instanceResolver_, T root, const bool saving_, const bool updating_) JsonTreeSerializer(const IInstanceResolver * instanceResolver_, T root, const bool saving_, const bool updating_):
: JsonSerializeFormat(instanceResolver_, saving_, updating_), JsonSerializeFormat(instanceResolver_, saving_, updating_),
currentObject(root), currentObject(root)
treeRoute()
{ {
} }

View File

@ -245,7 +245,7 @@ void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNo
if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR) if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR)
{ {
for(auto & item : toAdd.Vector()) for(const auto & item : toAdd.Vector())
{ {
auto b = JsonUtils::parseBonus(item); auto b = JsonUtils::parseBonus(item);
value->addNewBonus(b); value->addNewBonus(b);
@ -256,7 +256,7 @@ void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNo
if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR) if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR)
{ {
for(auto & item : toRemove.Vector()) for(const auto & item : toRemove.Vector())
{ {
auto mask = JsonUtils::parseBonus(item); auto mask = JsonUtils::parseBonus(item);
@ -280,34 +280,4 @@ void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNo
} }
} }
void JsonUpdater::readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value)
{
for(size_t index = 0; index < part.Vector().size(); index++)
{
const std::string & identifier = part.Vector()[index].String();
const si32 rawId = decoder(identifier);
if(rawId >= 0)
{
if(rawId < value.size())
value[rawId] = val;
else
logGlobal->error("JsonUpdater::serializeLIC: id out of bounds %d", rawId);
}
}
}
void JsonUpdater::readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value)
{
for(size_t index = 0; index < part.Vector().size(); index++)
{
const std::string & identifier = part.Vector()[index].String();
const si32 rawId = decoder(identifier);
if(rawId != -1)
value.insert(rawId);
}
}
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END

View File

@ -9,13 +9,14 @@
*/ */
#pragma once #pragma once
#include "ILICReader.h"
#include "JsonTreeSerializer.h" #include "JsonTreeSerializer.h"
VCMI_LIB_NAMESPACE_BEGIN VCMI_LIB_NAMESPACE_BEGIN
class CBonusSystemNode; class CBonusSystemNode;
class DLL_LINKAGE JsonUpdater: public JsonTreeSerializer<const JsonNode *> class DLL_LINKAGE JsonUpdater: public JsonTreeSerializer<const JsonNode *>, public ILICReader
{ {
public: public:
JsonUpdater(const IInstanceResolver * instanceResolver_, const JsonNode & root_); JsonUpdater(const IInstanceResolver * instanceResolver_, const JsonNode & root_);
@ -39,10 +40,6 @@ protected:
void serializeInternal(std::string & value) override; void serializeInternal(std::string & value) override;
void serializeInternal(int64_t & value) override; void serializeInternal(int64_t & value) override;
private:
void readLICPart(const JsonNode & part, const TDecoder & decoder, const bool val, std::vector<bool> & value);
void readLICPart(const JsonNode & part, const TDecoder & decoder, std::set<si32> & value);
}; };
VCMI_LIB_NAMESPACE_END VCMI_LIB_NAMESPACE_END