CSerializer.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 = 832;
  15. const ui32 MINIMAL_SERIALIZATION_VERSION = 831;
  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<ConstTransitivePtr<T> > *Vector, const std::function<U(const T&)> &idRetriever)
  59. {
  60. vectors[&typeid(T)] = VectorizedObjectInfo<T, U>(Vector, idRetriever);
  61. }
  62. using TTypeVecMap = std::map<const std::type_info *, std::any, TypeComparer>;
  63. TTypeVecMap vectors; //entry must be a pointer to vector containing pointers to the objects of key type
  64. public:
  65. bool smartVectorMembersSerialization = false;
  66. bool sendStackInstanceByIds = false;
  67. ~CSerializer();
  68. virtual void reportState(vstd::CLoggerBase * out){};
  69. template <typename T, typename U>
  70. const VectorizedObjectInfo<T, U> *getVectorizedTypeInfo()
  71. {
  72. const std::type_info *myType = nullptr;
  73. myType = &typeid(T);
  74. auto i = vectors.find(myType);
  75. if(i == vectors.end())
  76. return nullptr;
  77. else
  78. {
  79. assert(i->second.has_value());
  80. #ifndef __APPLE__
  81. assert(i->second.type() == typeid(VectorizedObjectInfo<T, U>));
  82. #endif
  83. auto *ret = std::any_cast<VectorizedObjectInfo<T, U>>(&i->second);
  84. return ret;
  85. }
  86. }
  87. template <typename T, typename U>
  88. T* getVectorItemFromId(const VectorizedObjectInfo<T, U> &oInfo, U id) const
  89. {
  90. si32 idAsNumber = idToNumber(id);
  91. assert(oInfo.vector);
  92. assert(static_cast<si32>(oInfo.vector->size()) > idAsNumber);
  93. return const_cast<T*>((*oInfo.vector)[idAsNumber].get());
  94. }
  95. template <typename T, typename U>
  96. U getIdFromVectorItem(const VectorizedObjectInfo<T, U> &oInfo, const T* obj) const
  97. {
  98. if(!obj)
  99. return U(-1);
  100. return oInfo.idRetriever(*obj);
  101. }
  102. void addStdVecItems(CGameState *gs, LibClasses *lib = VLC);
  103. };
  104. /// Helper to detect classes with user-provided serialize(S&, int version) method
  105. template<class S, class T>
  106. struct is_serializeable
  107. {
  108. using Yes = char (&)[1];
  109. using No = char (&)[2];
  110. template<class U>
  111. static Yes test(U * data, S* arg1 = 0,
  112. typename std::enable_if<std::is_void<
  113. decltype(data->serialize(*arg1, int(0)))
  114. >::value>::type * = 0);
  115. static No test(...);
  116. static const bool value = sizeof(Yes) == sizeof(is_serializeable::test((typename std::remove_reference<typename std::remove_cv<T>::type>::type*)0));
  117. };
  118. template <typename T> //metafunction returning CGObjectInstance if T is its derivate or T elsewise
  119. struct VectorizedTypeFor
  120. {
  121. using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, CGObjectInstance, T>;
  122. };
  123. template <>
  124. struct VectorizedTypeFor<CGHeroInstance>
  125. {
  126. using type = CGHeroInstance;
  127. };
  128. template <typename T>
  129. struct VectorizedIDType
  130. {
  131. using type = std::conditional_t<std::is_base_of_v<CGObjectInstance, T>, ObjectInstanceID, int32_t>;
  132. };
  133. template <>
  134. struct VectorizedIDType<CArtifactInstance>
  135. {
  136. using type = ArtifactInstanceID;
  137. };
  138. template <>
  139. struct VectorizedIDType<CGHeroInstance>
  140. {
  141. using type = HeroTypeID;
  142. };
  143. /// Base class for deserializers
  144. class DLL_LINKAGE IBinaryReader : public virtual CSerializer
  145. {
  146. public:
  147. virtual int read(void * data, unsigned size) = 0;
  148. };
  149. /// Base class for serializers
  150. class DLL_LINKAGE IBinaryWriter : public virtual CSerializer
  151. {
  152. public:
  153. virtual int write(const void * data, unsigned size) = 0;
  154. };
  155. VCMI_LIB_NAMESPACE_END