CSerializer.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*
  2. * CSerializer.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../ConstTransitivePtr.h"
  12. #include "../GameConstants.h"
  13. VCMI_LIB_NAMESPACE_BEGIN
  14. const ui32 SERIALIZATION_VERSION = 830;
  15. const ui32 MINIMAL_SERIALIZATION_VERSION = 830;
  16. const std::string SAVEGAME_MAGIC = "VCMISVG";
  17. class CHero;
  18. class CGHeroInstance;
  19. class CGObjectInstance;
  20. class CGameState;
  21. class LibClasses;
  22. extern DLL_LINKAGE LibClasses * VLC;
  23. struct TypeComparer
  24. {
  25. bool operator()(const std::type_info *a, const std::type_info *b) const
  26. {
  27. //#ifndef __APPLE__
  28. // return a->before(*b);
  29. //#else
  30. return strcmp(a->name(), b->name()) < 0;
  31. //#endif
  32. }
  33. };
  34. template <typename ObjType, typename IdType>
  35. struct VectorizedObjectInfo
  36. {
  37. const std::vector<ConstTransitivePtr<ObjType> > *vector; //pointer to the appropriate vector
  38. std::function<IdType(const ObjType &)> idRetriever;
  39. VectorizedObjectInfo(const std::vector< ConstTransitivePtr<ObjType> > *Vector, std::function<IdType(const ObjType &)> IdGetter)
  40. :vector(Vector), idRetriever(IdGetter)
  41. {
  42. }
  43. };
  44. /// Base class for serializers capable of reading or writing data
  45. class DLL_LINKAGE CSerializer
  46. {
  47. template<typename Numeric, std::enable_if_t<std::is_arithmetic_v<Numeric>, bool> = true>
  48. static int32_t idToNumber(const Numeric &t)
  49. {
  50. return t;
  51. }
  52. template<typename IdentifierType, std::enable_if_t<std::is_base_of_v<IdentifierBase, IdentifierType>, bool> = true>
  53. static int32_t idToNumber(const IdentifierType &t)
  54. {
  55. return t.getNum();
  56. }
  57. template <typename T, typename U>
  58. void registerVectoredType(const std::vector<T*> *Vector, const std::function<U(const T&)> &idRetriever)
  59. {
  60. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  61. }
  62. template <typename T, typename U>
  63. void registerVectoredType(const std::vector<ConstTransitivePtr<T> > *Vector, const std::function<U(const T&)> &idRetriever)
  64. {
  65. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  66. }
  67. using TTypeVecMap = std::map<const std::type_info *, std::any, TypeComparer>;
  68. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  69. public:
  70. bool smartVectorMembersSerialization = false;
  71. bool sendStackInstanceByIds = false;
  72. ~CSerializer();
  73. virtual void reportState(vstd::CLoggerBase * out){};
  74. template <typename T, typename U>
  75. const VectorizedObjectInfo<T, U> *getVectorizedTypeInfo()
  76. {
  77. const std::type_info *myType = nullptr;
  78. myType = &typeid(T);
  79. auto i = vectors.find(myType);
  80. if(i == vectors.end())
  81. return nullptr;
  82. else
  83. {
  84. assert(i->second.has_value());
  85. #ifndef __APPLE__
  86. assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
  87. #endif
  88. auto *ret = std::any_cast<VectorizedObjectInfo<T, U>>(&i->second);
  89. return ret;
  90. }
  91. }
  92. template <typename T, typename U>
  93. T* getVectorItemFromId(const VectorizedObjectInfo<T, U> &oInfo, U id) const
  94. {
  95. si32 idAsNumber = idToNumber(id);
  96. assert(oInfo.vector);
  97. assert(static_cast<si32>(oInfo.vector->size()) > idAsNumber);
  98. return const_cast<T*>((*oInfo.vector)[idAsNumber].get());
  99. }
  100. template <typename T, typename U>
  101. U getIdFromVectorItem(const VectorizedObjectInfo<T, U> &oInfo, const T* obj) const
  102. {
  103. if(!obj)
  104. return U(-1);
  105. return oInfo.idRetriever(*obj);
  106. }
  107. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  108. };
  109. /// Helper to detect classes with user-provided serialize(S&, int version) method
  110. template<class S, class T>
  111. struct is_serializeable
  112. {
  113. using Yes = char (&)[1];
  114. using No = char (&)[2];
  115. template<class U>
  116. static Yes test(U * data, S* arg1 = 0,
  117. typename std::enable_if<std::is_void<
  118. decltype(data->serialize(*arg1, int(0)))
  119. >::value>::type * = 0);
  120. static No test(...);
  121. static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference<typename std::remove_cv<T>::type>::type*)0));
  122. };
  123. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  124. struct VectorizedTypeFor
  125. {
  126. using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, CGObjectInstance, T>;
  127. };
  128. template <>
  129. struct VectorizedTypeFor<CGHeroInstance>
  130. {
  131. using type = CGHeroInstance;
  132. };
  133. template <typename T>
  134. struct VectorizedIDType
  135. {
  136. using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, ObjectInstanceID, int32_t>;
  137. };
  138. template <>
  139. struct VectorizedIDType<CArtifactInstance>
  140. {
  141. using type = ArtifactInstanceID;
  142. };
  143. template <>
  144. struct VectorizedIDType<CGHeroInstance>
  145. {
  146. using type = HeroTypeID;
  147. };
  148. /// Base class for deserializers
  149. class DLL_LINKAGE IBinaryReader : public virtual CSerializer
  150. {
  151. public:
  152. virtual int read(void * data, unsigned size) = 0;
  153. };
  154. /// Base class for serializers
  155. class DLL_LINKAGE IBinaryWriter : public virtual CSerializer
  156. {
  157. public:
  158. virtual int write(const void * data, unsigned size) = 0;
  159. };
  160. VCMI_LIB_NAMESPACE_END