CmBinarySerializer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. #pragma once
  2. #include <unordered_map>
  3. #include <boost/function.hpp>
  4. #include "CmPrerequisitesUtil.h"
  5. #include "CmRTTIField.h"
  6. namespace CamelotEngine
  7. {
  8. class IReflectable;
  9. struct RTTIReflectableFieldBase;
  10. struct RTTIReflectablePtrFieldBase;
  11. // TODO - Low priority. I will probably want to extract a generalized Serializer class so we can re-use the code
  12. // in text or other serializers
  13. // TODO - Low priority. Encode does a chunk-based encode so that we don't need to know the buffer size in advance,
  14. // and don't have to use a lot of memory for the buffer. Consider doing something similar for decode.
  15. // TODO - Low priority. Add a simple encode method that doesn't require a callback, instead it calls the callback internally
  16. // and creates the buffer internally.
  17. /**
  18. * @brief Encodes all the fields of the provided object into a binary format. Fields are
  19. * encoded using their unique IDs. Encoded data will remain compatible for decoding even
  20. * if you modify the encoded class, as long as you assign new unique field IDs to
  21. * added/modified fields.
  22. *
  23. * Like for any serializable class, fields are defined in RTTIType that each
  24. * IReflectable class must be able to return.
  25. *
  26. * Any data the object or its children are pointing to will also be serialized (unless the pointer isn't
  27. * registered in RTTIType). Upon decoding the pointer addresses will be set
  28. * to proper values.
  29. *
  30. * @note When deserializing make sure not to access any child members of a deserialized object in your RTTIType class.
  31. * Objects might be deserialized in an unknown order, so it is not guaranteed that child elements have been
  32. * deserialized before their parent.
  33. */
  34. class CM_UTILITY_EXPORT BinarySerializer
  35. {
  36. public:
  37. BinarySerializer();
  38. /**
  39. * @brief Encodes all serializable fields provided by "object" into a binary format. Data is written in chunks.
  40. * Whenever a chunk is filled a callback is triggered that gives the user opportunity to expand or
  41. * empty the buffer (for example write the chunk to disk)
  42. *
  43. * @param [in] object Object to encode into binary format.
  44. * @param [out] buffer Preallocated buffer where the data will be stored.
  45. * @param bufferLength Length of the buffer, in bytes.
  46. * @param [out] bytesWritten Length of the data that was actually written to the buffer,
  47. * in bytes.
  48. * @param flushBufferCallback This callback will get called whenever the buffer gets full (Be careful to check the provided
  49. * "bytesRead" variable, as buffer might not be full completely). User must then
  50. * either create a new buffer or empty the existing one, and then return it by the callback.
  51. * If the returned buffer address is NULL, encoding is aborted.
  52. */
  53. void encode(IReflectable* object, UINT8* buffer, UINT32 bufferLength, int* bytesWritten,
  54. boost::function<UINT8*(UINT8* buffer, int bytesWritten, UINT32& newBufferSize)> flushBufferCallback);
  55. /**
  56. * @brief Decodes an object from a binary format.
  57. *
  58. * @param [in] data Binary data to decode.
  59. * @param dataLength Length of the data.
  60. */
  61. std::shared_ptr<IReflectable> decode(UINT8* data, UINT32 dataLength);
  62. private:
  63. struct ObjectToEncode
  64. {
  65. ObjectToEncode(UINT32 _objectId, std::shared_ptr<IReflectable> _object)
  66. :objectId(_objectId), object(_object)
  67. { }
  68. UINT32 objectId;
  69. std::shared_ptr<IReflectable> object;
  70. };
  71. struct ObjectMetaData
  72. {
  73. UINT32 objectMeta;
  74. UINT32 typeId;
  75. };
  76. struct PtrFieldToSet
  77. {
  78. UINT32 objectId;
  79. boost::function<void(std::shared_ptr<IReflectable>)> func;
  80. };
  81. std::unordered_map<void*, UINT32> mObjectAddrToId;
  82. UINT32 mLastUsedObjectId;
  83. std::vector<ObjectToEncode> mObjectsToEncode;
  84. int mTotalBytesWritten;
  85. std::unordered_map<UINT32, std::shared_ptr<IReflectable>> mObjectMap;
  86. std::vector<std::shared_ptr<IReflectable>> mObjectsToDecode;
  87. std::vector<std::shared_ptr<IReflectable>> mDecodedObjects;
  88. std::vector<PtrFieldToSet> mPtrFieldsToSet;
  89. UINT32 getObjectSize(IReflectable* object);
  90. /**
  91. * @brief Encodes a single IReflectable object.
  92. */
  93. UINT8* encodeInternal(IReflectable* object, UINT32 objectId, UINT8* buffer, UINT32& bufferLength, int* bytesWritten,
  94. boost::function<UINT8*(UINT8* buffer, int bytesWritten, UINT32& newBufferSize)> flushBufferCallback);
  95. /**
  96. * @brief Decodes a single IReflectable object.
  97. */
  98. bool decodeInternal(std::shared_ptr<IReflectable> object, UINT8* data, UINT32 dataLength, UINT32& bytesRead);
  99. /**
  100. * @brief Encodes data required for representing a serialized field, into 4 bytes.
  101. */
  102. UINT32 encodeFieldMetaData(UINT16 id, UINT8 size, bool array, SerializableFieldType type, bool hasDynamicSize);
  103. /**
  104. * @brief Decode meta field that was encoded using encodeFieldMetaData.
  105. */
  106. void decodeFieldMetaData(UINT32 encodedData, UINT16& id, UINT8& size, bool& array, SerializableFieldType& type, bool& hasDynamicSize);
  107. /**
  108. * @brief Encodes data required for representing an object identifier, into 8 bytes.
  109. *
  110. * @note Id can be a maximum of 30 bits, as two bits are reserved.
  111. *
  112. * @param objId Unique ID of the object instance.
  113. * @param objTypeId Unique ID of the object type.
  114. * @param isBaseClass true if this object is base class (i.e. just a part of a larger object).
  115. */
  116. ObjectMetaData encodeObjectMetaData(UINT32 objId, UINT32 objTypeId, bool isBaseClass);
  117. /**
  118. * @brief Decode meta field that was encoded using encodeObjectMetaData.
  119. */
  120. void decodeObjectMetaData(ObjectMetaData encodedData, UINT32& objId, UINT32& objTypeId, bool& isBaseClass);
  121. /**
  122. * @brief Returns true if the provided encoded meta data represents object meta data.
  123. */
  124. bool isObjectMetaData(UINT32 encodedData);
  125. /**
  126. * @brief Helper method for encoding a complex object and copying its data to a buffer.
  127. */
  128. UINT8* complexTypeToBuffer(IReflectable* object, UINT8* buffer, UINT32& bufferLength, int* bytesWritten,
  129. boost::function<UINT8*(UINT8* buffer, int bytesWritten, UINT32& newBufferSize)> flushBufferCallback);
  130. /**
  131. * @brief Helper method for decoding a complex object from the provided data buffer.
  132. */
  133. std::shared_ptr<IReflectable> complexTypeFromBuffer(RTTIReflectableFieldBase* field, UINT8* data, int* complexTypeSize);
  134. /**
  135. * @brief Finds an existing, or creates a unique unique identifier for the specified object.
  136. */
  137. UINT32 findOrCreatePersistentId(IReflectable* object);
  138. /**
  139. * @brief Finds or creates an id for the provided object and returns it.
  140. * And it adds the object to a list of objects that need to be encoded,
  141. * if it's not already there.
  142. */
  143. UINT32 registerObjectPtr(std::shared_ptr<IReflectable> object);
  144. };
  145. }