2016-09-10 02:28:11 +02:00
|
|
|
/*
|
|
|
|
* CSerializer.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 "../ConstTransitivePtr.h"
|
|
|
|
#include "../GameConstants.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2016-09-10 02:28:11 +02:00
|
|
|
const std::string SAVEGAME_MAGIC = "VCMISVG";
|
|
|
|
|
|
|
|
class CHero;
|
|
|
|
class CGHeroInstance;
|
|
|
|
class CGObjectInstance;
|
|
|
|
|
|
|
|
class CGameState;
|
|
|
|
class LibClasses;
|
|
|
|
extern DLL_LINKAGE LibClasses * VLC;
|
|
|
|
|
|
|
|
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<ConstTransitivePtr<ObjType> > *vector; //pointer to the appropriate vector
|
|
|
|
std::function<IdType(const ObjType &)> idRetriever;
|
|
|
|
|
|
|
|
VectorizedObjectInfo(const std::vector< ConstTransitivePtr<ObjType> > *Vector, std::function<IdType(const ObjType &)> IdGetter)
|
|
|
|
:vector(Vector), idRetriever(IdGetter)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Base class for serializers capable of reading or writing data
|
2023-11-18 16:34:18 +02:00
|
|
|
class DLL_LINKAGE CSerializer : boost::noncopyable
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
2023-11-02 16:31:12 +02:00
|
|
|
template<typename Numeric, std::enable_if_t<std::is_arithmetic_v<Numeric>, bool> = true>
|
|
|
|
static int32_t idToNumber(const Numeric &t)
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2023-11-02 16:31:12 +02:00
|
|
|
template<typename IdentifierType, std::enable_if_t<std::is_base_of_v<IdentifierBase, IdentifierType>, bool> = true>
|
|
|
|
static int32_t idToNumber(const IdentifierType &t)
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
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<ConstTransitivePtr<T> > *Vector, const std::function<U(const T&)> &idRetriever)
|
|
|
|
{
|
|
|
|
vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
|
|
|
|
}
|
|
|
|
|
2023-04-17 23:11:16 +02:00
|
|
|
using TTypeVecMap = std::map<const std::type_info *, std::any, TypeComparer>;
|
2016-09-10 02:28:11 +02:00
|
|
|
TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
|
|
|
|
|
|
|
|
public:
|
2023-02-11 17:13:21 +02:00
|
|
|
bool smartVectorMembersSerialization = false;
|
|
|
|
bool sendStackInstanceByIds = false;
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
~CSerializer();
|
|
|
|
|
2017-08-11 19:03:05 +02:00
|
|
|
virtual void reportState(vstd::CLoggerBase * out){};
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
template <typename T, typename U>
|
|
|
|
const VectorizedObjectInfo<T, U> *getVectorizedTypeInfo()
|
|
|
|
{
|
|
|
|
const std::type_info *myType = nullptr;
|
|
|
|
|
|
|
|
myType = &typeid(T);
|
|
|
|
|
2023-04-17 23:11:16 +02:00
|
|
|
auto i = vectors.find(myType);
|
2016-09-10 02:28:11 +02:00
|
|
|
if(i == vectors.end())
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
{
|
2023-04-19 12:00:51 +02:00
|
|
|
assert(i->second.has_value());
|
2018-08-13 23:48:00 +02:00
|
|
|
#ifndef __APPLE__
|
2016-09-10 02:28:11 +02:00
|
|
|
assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
|
2018-08-13 23:48:00 +02:00
|
|
|
#endif
|
2023-04-18 22:29:47 +02:00
|
|
|
auto *ret = std::any_cast<VectorizedObjectInfo<T, U>>(&i->second);
|
2016-09-10 02:28:11 +02:00
|
|
|
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, LibClasses *lib = VLC);
|
|
|
|
};
|
|
|
|
|
|
|
|
/// Helper to detect classes with user-provided serialize(S&, int version) method
|
|
|
|
template<class S, class T>
|
|
|
|
struct is_serializeable
|
|
|
|
{
|
2023-04-17 23:11:16 +02:00
|
|
|
using Yes = char (&)[1];
|
|
|
|
using No = char (&)[2];
|
2016-09-10 02:28:11 +02:00
|
|
|
|
|
|
|
template<class U>
|
2024-01-20 23:26:04 +02:00
|
|
|
static Yes test(U * data, S* arg1 = nullptr, typename std::enable_if_t<std::is_void_v<decltype(data->serialize(*arg1))>> * = nullptr);
|
2016-09-10 02:28:11 +02:00
|
|
|
static No test(...);
|
2024-03-02 13:48:17 +02:00
|
|
|
static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference_t<typename std::remove_cv_t<T>>*)nullptr));
|
2016-09-10 02:28:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
|
|
|
|
struct VectorizedTypeFor
|
|
|
|
{
|
2023-11-04 16:21:54 +02:00
|
|
|
using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, CGObjectInstance, T>;
|
2016-09-10 02:28:11 +02:00
|
|
|
};
|
2023-11-04 16:21:54 +02:00
|
|
|
|
|
|
|
template <>
|
|
|
|
struct VectorizedTypeFor<CGHeroInstance>
|
|
|
|
{
|
|
|
|
using type = CGHeroInstance;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T>
|
2016-09-10 02:28:11 +02:00
|
|
|
struct VectorizedIDType
|
|
|
|
{
|
2023-11-04 16:21:54 +02:00
|
|
|
using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, ObjectInstanceID, int32_t>;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct VectorizedIDType<CArtifactInstance>
|
|
|
|
{
|
|
|
|
using type = ArtifactInstanceID;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct VectorizedIDType<CGHeroInstance>
|
|
|
|
{
|
|
|
|
using type = HeroTypeID;
|
2016-09-10 02:28:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Base class for deserializers
|
2019-01-19 12:52:02 +02:00
|
|
|
class DLL_LINKAGE IBinaryReader : public virtual CSerializer
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
public:
|
2024-02-02 02:36:57 +02:00
|
|
|
virtual int read(std::byte * data, unsigned size) = 0;
|
2016-09-10 02:28:11 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/// Base class for serializers
|
2019-01-19 12:52:02 +02:00
|
|
|
class DLL_LINKAGE IBinaryWriter : public virtual CSerializer
|
2016-09-10 02:28:11 +02:00
|
|
|
{
|
|
|
|
public:
|
2024-02-02 02:36:57 +02:00
|
|
|
virtual int write(const std::byte * data, unsigned size) = 0;
|
2016-09-10 02:28:11 +02:00
|
|
|
};
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|