mirror of
https://github.com/vcmi/vcmi.git
synced 2025-08-10 22:31:40 +02:00
Remove no longer needed support for gamestate vectors serialization
This commit is contained in:
@@ -224,7 +224,6 @@ set(lib_MAIN_SRCS
|
||||
serializer/CMemorySerializer.cpp
|
||||
serializer/Connection.cpp
|
||||
serializer/CSaveFile.cpp
|
||||
serializer/CSerializer.cpp
|
||||
serializer/CTypeList.cpp
|
||||
serializer/JsonDeserializer.cpp
|
||||
serializer/JsonSerializeFormat.cpp
|
||||
|
@@ -202,23 +202,6 @@ public:
|
||||
return;
|
||||
}
|
||||
|
||||
if(reader->smartVectorMembersSerialization)
|
||||
{
|
||||
typedef typename std::remove_const_t<typename std::remove_pointer_t<T>> TObjectType; //eg: const CGHeroInstance * => CGHeroInstance
|
||||
typedef typename VectorizedTypeFor<TObjectType>::type VType; //eg: CGHeroInstance -> CGobjectInstance
|
||||
typedef typename VectorizedIDType<TObjectType>::type IDType;
|
||||
if(const auto *info = reader->getVectorizedTypeInfo<VType, IDType>())
|
||||
{
|
||||
IDType id;
|
||||
load(id);
|
||||
if(id != IDType(-1))
|
||||
{
|
||||
data = static_cast<T>(reader->getVectorItemFromId<VType, IDType>(*info, id));
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t pid = 0xffffffff; //pointer id (or maybe rather pointee id)
|
||||
if(trackSerializedPointers)
|
||||
{
|
||||
|
@@ -155,22 +155,6 @@ public:
|
||||
if(data == nullptr)
|
||||
return;
|
||||
|
||||
typedef typename std::remove_const_t<typename std::remove_pointer_t<T>> TObjectType;
|
||||
|
||||
if(writer->smartVectorMembersSerialization)
|
||||
{
|
||||
typedef typename VectorizedTypeFor<TObjectType>::type VType;
|
||||
typedef typename VectorizedIDType<TObjectType>::type IDType;
|
||||
|
||||
if(const auto *info = writer->getVectorizedTypeInfo<VType, IDType>())
|
||||
{
|
||||
IDType id = writer->getIdFromVectorItem<VType>(*info, data);
|
||||
save(id);
|
||||
if(id != IDType(-1)) //vector id is enough
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(trackSerializedPointers)
|
||||
{
|
||||
// We might have an object that has multiple inheritance and store it via the non-first base pointer.
|
||||
|
@@ -1,34 +0,0 @@
|
||||
/*
|
||||
* CSerializer.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 "CSerializer.h"
|
||||
|
||||
#include "../entities/hero/CHero.h"
|
||||
#include "../gameState/CGameState.h"
|
||||
#include "../mapping/CMap.h"
|
||||
#include "../mapObjects/CGHeroInstance.h"
|
||||
#include "../mapObjects/CQuest.h"
|
||||
|
||||
VCMI_LIB_NAMESPACE_BEGIN
|
||||
|
||||
//must be instantiated in .cpp file for access to complete types of all member fields
|
||||
CSerializer::~CSerializer() = default;
|
||||
|
||||
void CSerializer::addStdVecItems(CGameState *gs, GameLibrary *lib)
|
||||
{
|
||||
registerVectoredType<CGObjectInstance, ObjectInstanceID>(&gs->getMap().objects,
|
||||
[](const CGObjectInstance &obj){ return obj.id; });
|
||||
registerVectoredType<CArtifactInstance, ArtifactInstanceID>(&gs->getMap().artInstances,
|
||||
[](const CArtifactInstance &artInst){ return artInst.getId(); });
|
||||
|
||||
smartVectorMembersSerialization = true;
|
||||
}
|
||||
|
||||
VCMI_LIB_NAMESPACE_END
|
@@ -23,107 +23,12 @@ class CGameState;
|
||||
class GameLibrary;
|
||||
extern DLL_LINKAGE GameLibrary * LIBRARY;
|
||||
|
||||
struct TypeComparer
|
||||
{
|
||||
bool operator()(const std::type_info *a, const std::type_info *b) const
|
||||
{
|
||||
//#ifndef __APPLE__
|
||||
// return a->before(*b);
|
||||
//#else
|
||||
return strcmp(a->name(), b->name()) < 0;
|
||||
//#endif
|
||||
}
|
||||
};
|
||||
|
||||
template <typename ObjType, typename IdType>
|
||||
struct VectorizedObjectInfo
|
||||
{
|
||||
const std::vector<std::shared_ptr<ObjType> > *vector; //pointer to the appropriate vector
|
||||
std::function<IdType(const ObjType &)> idRetriever;
|
||||
|
||||
VectorizedObjectInfo(const std::vector< std::shared_ptr<ObjType> > *Vector, std::function<IdType(const ObjType &)> IdGetter)
|
||||
:vector(Vector), idRetriever(IdGetter)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
/// Base class for serializers capable of reading or writing data
|
||||
class DLL_LINKAGE CSerializer : boost::noncopyable
|
||||
{
|
||||
template<typename Numeric, std::enable_if_t<std::is_arithmetic_v<Numeric>, bool> = true>
|
||||
static int32_t idToNumber(const Numeric &t)
|
||||
{
|
||||
return t;
|
||||
}
|
||||
|
||||
template<typename IdentifierType, std::enable_if_t<std::is_base_of_v<IdentifierBase, IdentifierType>, bool> = true>
|
||||
static int32_t idToNumber(const IdentifierType &t)
|
||||
{
|
||||
return t.getNum();
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
void registerVectoredType(const std::vector<T*> *Vector, const std::function<U(const T&)> &idRetriever)
|
||||
{
|
||||
vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
|
||||
}
|
||||
template <typename T, typename U>
|
||||
void registerVectoredType(const std::vector<std::shared_ptr<T> > *Vector, const std::function<U(const T&)> &idRetriever)
|
||||
{
|
||||
vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
|
||||
}
|
||||
|
||||
using TTypeVecMap = std::map<const std::type_info *, std::any, TypeComparer>;
|
||||
TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
|
||||
|
||||
public:
|
||||
bool smartVectorMembersSerialization = false;
|
||||
|
||||
~CSerializer();
|
||||
|
||||
virtual ~CSerializer() = default;
|
||||
virtual void reportState(vstd::CLoggerBase * out){};
|
||||
|
||||
template <typename T, typename U>
|
||||
const VectorizedObjectInfo<T, U> *getVectorizedTypeInfo()
|
||||
{
|
||||
const std::type_info *myType = nullptr;
|
||||
|
||||
myType = &typeid(T);
|
||||
|
||||
auto i = vectors.find(myType);
|
||||
if(i == vectors.end())
|
||||
return nullptr;
|
||||
else
|
||||
{
|
||||
assert(i->second.has_value());
|
||||
#ifndef __APPLE__
|
||||
assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
|
||||
#endif
|
||||
auto *ret = std::any_cast<VectorizedObjectInfo<T, U>>(&i->second);
|
||||
return ret;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
T* getVectorItemFromId(const VectorizedObjectInfo<T, U> &oInfo, U id) const
|
||||
{
|
||||
si32 idAsNumber = idToNumber(id);
|
||||
|
||||
assert(oInfo.vector);
|
||||
assert(static_cast<si32>(oInfo.vector->size()) > idAsNumber);
|
||||
return const_cast<T*>((*oInfo.vector)[idAsNumber].get());
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
U getIdFromVectorItem(const VectorizedObjectInfo<T, U> &oInfo, const T* obj) const
|
||||
{
|
||||
if(!obj)
|
||||
return U(-1);
|
||||
|
||||
return oInfo.idRetriever(*obj);
|
||||
}
|
||||
|
||||
void addStdVecItems(CGameState *gs, GameLibrary *lib = LIBRARY);
|
||||
};
|
||||
|
||||
/// Helper to detect classes with user-provided serialize(S&, int version) method
|
||||
@@ -139,24 +44,6 @@ struct is_serializeable
|
||||
static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference_t<typename std::remove_cv_t<T>>*)nullptr));
|
||||
};
|
||||
|
||||
template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
|
||||
struct VectorizedTypeFor
|
||||
{
|
||||
using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, CGObjectInstance, T>;
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct VectorizedIDType
|
||||
{
|
||||
using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, ObjectInstanceID, int32_t>;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct VectorizedIDType<CArtifactInstance>
|
||||
{
|
||||
using type = ArtifactInstanceID;
|
||||
};
|
||||
|
||||
/// Base class for deserializers
|
||||
class DLL_LINKAGE IBinaryReader : public virtual CSerializer
|
||||
{
|
||||
|
@@ -123,7 +123,6 @@ void CConnection::enterLobbyConnectionMode()
|
||||
{
|
||||
deserializer->loadedPointers.clear();
|
||||
serializer->savedPointers.clear();
|
||||
disableSmartVectorMemberSerialization();
|
||||
}
|
||||
|
||||
void CConnection::setCallback(IGameCallback * cb)
|
||||
@@ -134,19 +133,6 @@ void CConnection::setCallback(IGameCallback * cb)
|
||||
void CConnection::enterGameplayConnectionMode(CGameState * gs)
|
||||
{
|
||||
setCallback(gs->callback);
|
||||
enableSmartVectorMemberSerializatoin(gs);
|
||||
}
|
||||
|
||||
void CConnection::disableSmartVectorMemberSerialization()
|
||||
{
|
||||
packReader->smartVectorMembersSerialization = false;
|
||||
packWriter->smartVectorMembersSerialization = false;
|
||||
}
|
||||
|
||||
void CConnection::enableSmartVectorMemberSerializatoin(CGameState * gs)
|
||||
{
|
||||
packWriter->addStdVecItems(gs);
|
||||
packReader->addStdVecItems(gs);
|
||||
}
|
||||
|
||||
void CConnection::setSerializationVersion(ESerializationVersion version)
|
||||
|
@@ -36,9 +36,6 @@ class DLL_LINKAGE CConnection : boost::noncopyable
|
||||
|
||||
std::mutex writeMutex;
|
||||
|
||||
void disableSmartVectorMemberSerialization();
|
||||
void enableSmartVectorMemberSerializatoin(CGameState * gs);
|
||||
|
||||
public:
|
||||
bool isMyConnection(const std::shared_ptr<INetworkConnection> & otherConnection) const;
|
||||
std::shared_ptr<INetworkConnection> getConnection();
|
||||
|
Reference in New Issue
Block a user