mirror of
				https://github.com/vcmi/vcmi.git
				synced 2025-10-31 00:07:39 +02:00 
			
		
		
		
	vcmi: modernize lib/serializer
This commit is contained in:
		| @@ -125,6 +125,7 @@ macro(add_main_lib TARGET_NAME LIBRARY_TYPE) | ||||
| 		${MAIN_LIB_DIR}/serializer/JsonSerializeFormat.cpp | ||||
| 		${MAIN_LIB_DIR}/serializer/JsonSerializer.cpp | ||||
| 		${MAIN_LIB_DIR}/serializer/JsonUpdater.cpp | ||||
| 		${MAIN_LIB_DIR}/serializer/ILICReader.cpp | ||||
|  | ||||
| 		${MAIN_LIB_DIR}/spells/AbilityCaster.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/JsonSerializer.h | ||||
| 		${MAIN_LIB_DIR}/serializer/JsonUpdater.h | ||||
| 		${MAIN_LIB_DIR}/serializer/ILICReader.h | ||||
| 		${MAIN_LIB_DIR}/serializer/Cast.h | ||||
|  | ||||
| 		${MAIN_LIB_DIR}/spells/AbilityCaster.h | ||||
|   | ||||
| @@ -24,13 +24,12 @@ CLoadFile::CLoadFile(const boost::filesystem::path & fname, int 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) | ||||
| { | ||||
| 	sfile->read((char*)data,size); | ||||
| 	sfile->read(reinterpret_cast<char *>(data), size); | ||||
| 	return size; | ||||
| } | ||||
|  | ||||
| @@ -51,7 +50,7 @@ void CLoadFile::openNextFile(const boost::filesystem::path & fname, int minimalV | ||||
| 		//we can read | ||||
| 		char 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); | ||||
|  | ||||
| 		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); | ||||
|  | ||||
| 			auto versionptr = (char*)&serializer.fileVersion; | ||||
| 			auto * versionptr = reinterpret_cast<char *>(&serializer.fileVersion); | ||||
| 			std::reverse(versionptr, versionptr + 4); | ||||
| 			logGlobal->warn("Version number reversed is %x, checking...", serializer.fileVersion); | ||||
|  | ||||
| @@ -99,7 +98,7 @@ void CLoadFile::clear() | ||||
| void CLoadFile::checkMagicBytes(const std::string &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) | ||||
| 		throw std::runtime_error("Magic bytes doesn't match!"); | ||||
| } | ||||
|   | ||||
| @@ -192,7 +192,7 @@ public: | ||||
| 	void load(T &data) | ||||
| 	{ | ||||
| 		unsigned length = sizeof(data); | ||||
| 		char* dataPtr = (char*)&data; | ||||
| 		char * dataPtr = reinterpret_cast<char *>(&data); | ||||
| 		this->read(dataPtr,length); | ||||
| 		if(reverseEndianess) | ||||
| 			std::reverse(dataPtr, dataPtr + length); | ||||
|   | ||||
| @@ -24,9 +24,8 @@ CSaveFile::CSaveFile(const boost::filesystem::path &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) | ||||
| { | ||||
| @@ -73,7 +72,7 @@ void CSaveFile::clear() | ||||
|  | ||||
| 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 | ||||
|   | ||||
| @@ -40,7 +40,7 @@ int CLoadIntegrityValidator::read( void * data, unsigned size ) | ||||
| 	if(!foundDesync) | ||||
| 	{ | ||||
| 		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()); | ||||
| 			foundDesync = true; | ||||
| @@ -57,7 +57,7 @@ std::unique_ptr<CLoadFile> CLoadIntegrityValidator::decay() | ||||
| 	return std::move(primaryFile); | ||||
| } | ||||
|  | ||||
| void CLoadIntegrityValidator::checkMagicBytes( const std::string &text ) | ||||
| void CLoadIntegrityValidator::checkMagicBytes(const std::string & text) const | ||||
| { | ||||
| 	assert(primaryFile); | ||||
| 	assert(controlFile); | ||||
|   | ||||
| @@ -25,7 +25,7 @@ public: | ||||
| 	CLoadIntegrityValidator(const boost::filesystem::path &primaryFileName, const boost::filesystem::path &controlFileName, int minimalVersion = SERIALIZATION_VERSION); //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 | ||||
| }; | ||||
|   | ||||
| @@ -32,13 +32,11 @@ int CMemorySerializer::write(const void * data, unsigned size) | ||||
| 	return size; | ||||
| } | ||||
|  | ||||
| CMemorySerializer::CMemorySerializer(): iser(this), oser(this) | ||||
| CMemorySerializer::CMemorySerializer(): iser(this), oser(this), readPos(0) | ||||
| { | ||||
| 	readPos = 0; | ||||
| 	registerTypes(iser); | ||||
| 	registerTypes(oser); | ||||
| 	iser.fileVersion = SERIALIZATION_VERSION; | ||||
| } | ||||
|  | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_END | ||||
|   | ||||
| @@ -17,17 +17,8 @@ | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| CSerializer::~CSerializer() | ||||
| { | ||||
|  | ||||
| } | ||||
|  | ||||
| CSerializer::CSerializer() | ||||
| { | ||||
| 	smartVectorMembersSerialization = false; | ||||
| 	sendStackInstanceByIds = false; | ||||
| } | ||||
|  | ||||
| //must be instantiated in .cpp file for access to complete types of all member fields | ||||
| CSerializer::~CSerializer() = default; | ||||
|  | ||||
| void CSerializer::addStdVecItems(CGameState *gs, LibClasses *lib) | ||||
| { | ||||
|   | ||||
| @@ -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 | ||||
|  | ||||
| public: | ||||
| 	bool smartVectorMembersSerialization; | ||||
| 	bool sendStackInstanceByIds; | ||||
| 	bool smartVectorMembersSerialization = false; | ||||
| 	bool sendStackInstanceByIds = false; | ||||
|  | ||||
| 	CSerializer(); | ||||
| 	~CSerializer(); | ||||
|  | ||||
| 	virtual void reportState(vstd::CLoggerBase * out){}; | ||||
|   | ||||
| @@ -74,7 +74,7 @@ std::vector<CTypeList::TypeInfoPtr> CTypeList::castSequence(TypeInfoPtr from, Ty | ||||
| 		std::map<TypeInfoPtr, TypeInfoPtr> previous; | ||||
| 		std::queue<TypeInfoPtr> q; | ||||
| 		q.push(to); | ||||
| 		while(q.size()) | ||||
| 		while(!q.empty()) | ||||
| 		{ | ||||
| 			auto typeNode = q.front(); | ||||
| 			q.pop(); | ||||
|   | ||||
| @@ -109,12 +109,7 @@ private: | ||||
|  | ||||
| 		return ptr; | ||||
| 	} | ||||
| 	CTypeList &operator=(CTypeList &) | ||||
| 	{ | ||||
| 		// As above. | ||||
| 		assert(0); | ||||
| 		return *this; | ||||
| 	} | ||||
| 	CTypeList & operator=(CTypeList &) = delete; | ||||
|  | ||||
| 	TypeInfoPtr getTypeDescriptor(const std::type_info *type, bool throws = true) const; //if not throws, failure returns nullptr | ||||
| 	TypeInfoPtr registerType(const std::type_info *type); | ||||
|   | ||||
| @@ -21,16 +21,6 @@ VCMI_LIB_NAMESPACE_BEGIN | ||||
| using namespace boost; | ||||
| 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 | ||||
| { | ||||
| 	boost::asio::streambuf readBuffer; | ||||
| @@ -58,7 +48,7 @@ void CConnection::init() | ||||
| 	disableStackSendingByID(); | ||||
| 	registerTypes(iser); | ||||
| 	registerTypes(oser); | ||||
| #ifdef LIL_ENDIAN | ||||
| #ifndef VCMI_ENDIAN_BIG | ||||
| 	myEndianess = true; | ||||
| #else | ||||
| 	myEndianess = false; | ||||
| @@ -75,15 +65,21 @@ void CConnection::init() | ||||
| 	iser.fileVersion = SERIALIZATION_VERSION; | ||||
| } | ||||
|  | ||||
| CConnection::CConnection(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) | ||||
| 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(std::move(Name)), | ||||
| 	uuid(std::move(UUID)) | ||||
| { | ||||
| 	int i; | ||||
| 	int i = 0; | ||||
| 	boost::system::error_code error = asio::error::host_not_found; | ||||
| 	socket = std::make_shared<tcp::socket>(*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) | ||||
| 	{ | ||||
| 		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!"); | ||||
| 		goto connerror1; | ||||
| 	} | ||||
| 	i=0; | ||||
| 	while(pom != end) | ||||
| 	{ | ||||
| 		logNetwork->info("\t%d:%s", i, (boost::asio::ip::tcp::endpoint&)*pom); | ||||
| @@ -129,13 +124,24 @@ connerror1: | ||||
| 		logNetwork->error("No error info. "); | ||||
| 	throw std::runtime_error("Can't establish connection :("); | ||||
| } | ||||
| 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) | ||||
| CConnection::CConnection(std::shared_ptr<TSocket> Socket, std::string Name, std::string UUID): | ||||
| 	iser(this), | ||||
| 	oser(this), | ||||
| 	socket(std::move(Socket)), | ||||
| 	name(std::move(Name)), | ||||
| 	uuid(std::move(UUID)) | ||||
| { | ||||
| 	init(); | ||||
| } | ||||
| CConnection::CConnection(std::shared_ptr<TAcceptor> acceptor, std::shared_ptr<boost::asio::io_service> io_service, std::string Name, std::string UUID) | ||||
| 	: io_service(io_service), iser(this), oser(this), name(Name), uuid(UUID), connectionID(0) | ||||
| CConnection::CConnection(const std::shared_ptr<TAcceptor> & acceptor, | ||||
| 						 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; | ||||
| 	socket = std::make_shared<tcp::socket>(*io_service); | ||||
| @@ -181,8 +187,7 @@ int CConnection::write(const void * data, unsigned size) | ||||
| 			return size; | ||||
| 		} | ||||
|  | ||||
| 		int ret; | ||||
| 		ret = static_cast<int>(asio::write(*socket,asio::const_buffers_1(asio::const_buffer(data,size)))); | ||||
| 		int ret = static_cast<int>(asio::write(*socket, asio::const_buffers_1(asio::const_buffer(data, size)))); | ||||
| 		return ret; | ||||
| 	} | ||||
| 	catch(...) | ||||
|   | ||||
| @@ -88,8 +88,8 @@ public: | ||||
| 	int connectionID; | ||||
| 	std::shared_ptr<boost::thread> handler; | ||||
|  | ||||
| 	CConnection(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::string & host, ui16 port, 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 | ||||
|  | ||||
| 	void close(); | ||||
|   | ||||
							
								
								
									
										49
									
								
								lib/serializer/ILICReader.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										49
									
								
								lib/serializer/ILICReader.cpp
									
									
									
									
									
										Normal 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 | ||||
							
								
								
									
										23
									
								
								lib/serializer/ILICReader.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										23
									
								
								lib/serializer/ILICReader.h
									
									
									
									
									
										Normal 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 | ||||
| @@ -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 | ||||
|   | ||||
| @@ -9,11 +9,12 @@ | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include  "JsonTreeSerializer.h" | ||||
| #include "ILICReader.h" | ||||
| #include "JsonTreeSerializer.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| class DLL_LINKAGE JsonDeserializer: public JsonTreeSerializer<const JsonNode *> | ||||
| class DLL_LINKAGE JsonDeserializer: public JsonTreeSerializer<const JsonNode *>, public ILICReader | ||||
| { | ||||
| public: | ||||
| 	JsonDeserializer(const IInstanceResolver * instanceResolver_, const JsonNode & root_); | ||||
| @@ -35,10 +36,6 @@ protected: | ||||
|  | ||||
| 	void serializeInternal(std::string & 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 | ||||
|   | ||||
| @@ -15,9 +15,7 @@ | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| //JsonSerializeHelper | ||||
| JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other): | ||||
| 	owner(other.owner), | ||||
| 	restoreState(false) | ||||
| JsonSerializeHelper::JsonSerializeHelper(JsonSerializeHelper && other) noexcept: owner(other.owner), restoreState(false) | ||||
| { | ||||
| 	std::swap(restoreState, other.restoreState); | ||||
| } | ||||
| @@ -40,33 +38,17 @@ JsonSerializeHelper::JsonSerializeHelper(JsonSerializeFormat * owner_) | ||||
| } | ||||
|  | ||||
| //JsonStructSerializer | ||||
| JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other) | ||||
| 	: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) | ||||
| { | ||||
|  | ||||
| } | ||||
| JsonStructSerializer::JsonStructSerializer(JsonStructSerializer && other) noexcept: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) {} | ||||
|  | ||||
| JsonStructSerializer::JsonStructSerializer(JsonSerializeFormat * owner_) | ||||
| 	: JsonSerializeHelper(owner_) | ||||
| { | ||||
| } | ||||
|  | ||||
| JsonStructSerializer::~JsonStructSerializer() | ||||
| { | ||||
| } | ||||
|  | ||||
| //JsonArraySerializer | ||||
| JsonArraySerializer::JsonArraySerializer(JsonArraySerializer && other) | ||||
| 	: JsonSerializeHelper(std::move(static_cast<JsonSerializeHelper &>(other))) | ||||
| { | ||||
| JsonArraySerializer::JsonArraySerializer(JsonArraySerializer && other) noexcept: 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) | ||||
| { | ||||
| @@ -110,16 +92,20 @@ size_t JsonArraySerializer::size() const | ||||
| } | ||||
|  | ||||
| //JsonSerializeFormat::LIC | ||||
| JsonSerializeFormat::LIC::LIC(const std::vector<bool> & Standard, const TDecoder Decoder, const TEncoder Encoder): | ||||
| 	standard(Standard), decoder(Decoder), encoder(Encoder) | ||||
| JsonSerializeFormat::LIC::LIC(const std::vector<bool> & Standard, TDecoder Decoder, TEncoder Encoder): | ||||
| 	standard(Standard), | ||||
| 	decoder(std::move(Decoder)), | ||||
| 	encoder(std::move(Encoder)) | ||||
| { | ||||
| 	any.resize(standard.size(), false); | ||||
| 	all.resize(standard.size(), false); | ||||
| 	none.resize(standard.size(), false); | ||||
| } | ||||
|  | ||||
| JsonSerializeFormat::LICSet::LICSet(const std::set<si32>& Standard, const TDecoder Decoder, const TEncoder Encoder): | ||||
| 	standard(Standard), decoder(Decoder), encoder(Encoder) | ||||
| JsonSerializeFormat::LICSet::LICSet(const std::set<si32> & Standard, TDecoder Decoder, TEncoder Encoder): | ||||
| 	standard(Standard), | ||||
| 	decoder(std::move(Decoder)), | ||||
| 	encoder(std::move(Encoder)) | ||||
| { | ||||
|  | ||||
| } | ||||
|   | ||||
| @@ -28,7 +28,7 @@ public: | ||||
| class DLL_LINKAGE JsonSerializeHelper: public boost::noncopyable | ||||
| { | ||||
| public: | ||||
| 	JsonSerializeHelper(JsonSerializeHelper && other); | ||||
| 	JsonSerializeHelper(JsonSerializeHelper && other) noexcept; | ||||
| 	virtual ~JsonSerializeHelper(); | ||||
|  | ||||
| 	JsonSerializeFormat * operator->(); | ||||
| @@ -44,8 +44,8 @@ private: | ||||
| class DLL_LINKAGE JsonStructSerializer: public JsonSerializeHelper | ||||
| { | ||||
| public: | ||||
| 	JsonStructSerializer(JsonStructSerializer && other); | ||||
| 	~JsonStructSerializer(); | ||||
| 	JsonStructSerializer(JsonStructSerializer && other) noexcept; | ||||
|  | ||||
| protected: | ||||
| 	JsonStructSerializer(JsonSerializeFormat * owner_); | ||||
|  | ||||
| @@ -56,7 +56,7 @@ protected: | ||||
| class DLL_LINKAGE JsonArraySerializer: public JsonSerializeHelper | ||||
| { | ||||
| public: | ||||
| 	JsonArraySerializer(JsonArraySerializer && other); | ||||
| 	JsonArraySerializer(JsonArraySerializer && other) noexcept; | ||||
|  | ||||
| 	JsonStructSerializer enterStruct(const size_t index); | ||||
| 	JsonArraySerializer enterArray(const size_t index); | ||||
| @@ -102,17 +102,17 @@ class DLL_LINKAGE JsonSerializeFormat: public boost::noncopyable | ||||
| public: | ||||
| 	///user-provided callback to resolve string identifier | ||||
| 	///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 | ||||
| 	///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 | ||||
| 	{ | ||||
| 		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 TDecoder decoder; | ||||
| @@ -122,7 +122,7 @@ public: | ||||
|  | ||||
| 	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 TDecoder decoder; | ||||
|   | ||||
| @@ -27,10 +27,9 @@ protected: | ||||
| 	T currentObject; | ||||
| 	std::vector<T> treeRoute; | ||||
|  | ||||
| 	JsonTreeSerializer(const IInstanceResolver * instanceResolver_, T root, const bool saving_, const bool updating_) | ||||
| 		: JsonSerializeFormat(instanceResolver_, saving_, updating_), | ||||
| 		currentObject(root), | ||||
| 		treeRoute() | ||||
| 	JsonTreeSerializer(const IInstanceResolver * instanceResolver_, T root, const bool saving_, const bool updating_): | ||||
| 		JsonSerializeFormat(instanceResolver_, saving_, updating_), | ||||
| 		currentObject(root) | ||||
| 	{ | ||||
| 	} | ||||
|  | ||||
|   | ||||
| @@ -245,7 +245,7 @@ void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNo | ||||
|  | ||||
| 	if(toAdd.getType() == JsonNode::JsonType::DATA_VECTOR) | ||||
| 	{ | ||||
| 		for(auto & item : toAdd.Vector()) | ||||
| 		for(const auto & item : toAdd.Vector()) | ||||
| 		{ | ||||
| 			auto b = JsonUtils::parseBonus(item); | ||||
| 			value->addNewBonus(b); | ||||
| @@ -256,7 +256,7 @@ void JsonUpdater::serializeBonuses(const std::string & fieldName, CBonusSystemNo | ||||
|  | ||||
| 	if(toRemove.getType() == JsonNode::JsonType::DATA_VECTOR) | ||||
| 	{ | ||||
| 		for(auto & item : toRemove.Vector()) | ||||
| 		for(const auto & item : toRemove.Vector()) | ||||
| 		{ | ||||
| 			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 | ||||
|   | ||||
| @@ -9,13 +9,14 @@ | ||||
|  */ | ||||
| #pragma once | ||||
|  | ||||
| #include  "JsonTreeSerializer.h" | ||||
| #include "ILICReader.h" | ||||
| #include "JsonTreeSerializer.h" | ||||
|  | ||||
| VCMI_LIB_NAMESPACE_BEGIN | ||||
|  | ||||
| class CBonusSystemNode; | ||||
|  | ||||
| class DLL_LINKAGE JsonUpdater: public JsonTreeSerializer<const JsonNode *> | ||||
| class DLL_LINKAGE JsonUpdater: public JsonTreeSerializer<const JsonNode *>, public ILICReader | ||||
| { | ||||
| public: | ||||
| 	JsonUpdater(const IInstanceResolver * instanceResolver_, const JsonNode & root_); | ||||
| @@ -39,10 +40,6 @@ protected: | ||||
|  | ||||
| 	void serializeInternal(std::string & 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 | ||||
|   | ||||
		Reference in New Issue
	
	Block a user