CmBinarySerializer.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. class CM_UTILITY_EXPORT BinarySerializer
  31. {
  32. public:
  33. BinarySerializer();
  34. /**
  35. * @brief Encodes all serializable fields provided by "object" into a binary format. Data is written in chunks.
  36. * Whenever a chunk is filled a callback is triggered that gives the user opportunity to expand or
  37. * empty the buffer (for example write the chunk to disk)
  38. *
  39. * @param [in] object Object to encode into binary format.
  40. * @param [out] buffer Preallocated buffer where the data will be stored.
  41. * @param bufferLength Length of the buffer, in bytes.
  42. * @param [out] bytesWritten Length of the data that was actually written to the buffer,
  43. * in bytes.
  44. * @param flushBufferCallback This callback will get called whenever the buffer gets full (Be careful to check the provided
  45. * "bytesRead" variable, as buffer might not be full completely). User must then
  46. * either create a new buffer or empty the existing one, and then return it by the callback.
  47. * If the returned buffer address is NULL, encoding is aborted.
  48. */
  49. void encode(IReflectable* object, UINT8* buffer, UINT32 bufferLength, int* bytesWritten,
  50. boost::function<UINT8*(UINT8* buffer, int bytesWritten, UINT32& newBufferSize)> flushBufferCallback);
  51. /**
  52. * @brief Decodes all of the fields of the provided object from a binary format.
  53. *
  54. * @param [in] object Object whos fields to decode.
  55. * @param [in] data Binary data to decode.
  56. * @param dataLength Length of the data.
  57. */
  58. void decode(std::shared_ptr<IReflectable> object, UINT8* data, UINT32 dataLength);
  59. private:
  60. struct ObjectToEncode
  61. {
  62. ObjectToEncode(UINT32 _objectId, std::shared_ptr<IReflectable> _object)
  63. :objectId(_objectId), object(_object)
  64. { }
  65. UINT32 objectId;
  66. std::shared_ptr<IReflectable> object;
  67. };
  68. /**
  69. * @brief Pointer fields get resolved after everything is loaded. Store their
  70. * temporary data here until then.
  71. */
  72. struct PtrToResolve
  73. {
  74. PtrToResolve()
  75. :field(nullptr), object(nullptr), id(0)
  76. { }
  77. PtrToResolve(RTTIReflectablePtrFieldBase* _field, std::shared_ptr<IReflectable> _object, UINT32 _id)
  78. :field(_field), object(_object), id(_id), arrIdx(0)
  79. { }
  80. PtrToResolve(RTTIReflectablePtrFieldBase* _field, std::shared_ptr<IReflectable> _object, UINT32 _id, UINT32 _arrIdx)
  81. :field(_field), object(_object), id(_id), arrIdx(_arrIdx)
  82. { }
  83. RTTIReflectablePtrFieldBase* field;
  84. UINT32 arrIdx;
  85. std::shared_ptr<IReflectable> object;
  86. UINT32 id;
  87. };
  88. std::unordered_map<UINT32, UINT32> mObjectAddrToId;
  89. UINT32 mLastUsedObjectId;
  90. std::vector<ObjectToEncode> mObjectsToEncode;
  91. int mTotalBytesWritten;
  92. std::vector<PtrToResolve> mPtrsToResolve;
  93. std::unordered_map<UINT32, std::shared_ptr<IReflectable>> mDecodedObjects;
  94. UINT32 getObjectSize(IReflectable* object);
  95. /**
  96. * @brief Encodes a single IReflectable object.
  97. */
  98. UINT8* encodeInternal(IReflectable* object, UINT32 objectId, UINT8* buffer, UINT32& bufferLength, int* bytesWritten,
  99. boost::function<UINT8*(UINT8* buffer, int bytesWritten, UINT32& newBufferSize)> flushBufferCallback);
  100. /**
  101. * @brief Decodes a single IReflectable object.
  102. */
  103. bool decodeInternal(std::shared_ptr<IReflectable> object, UINT8* data, UINT32 dataLength, UINT32& bytesRead);
  104. /**
  105. * @brief Encodes data required for representing a serialized field, into 4 bytes.
  106. */
  107. UINT32 encodeFieldMetaData(UINT16 id, UINT8 size, bool array, SerializableFieldType type, bool hasDynamicSize);
  108. /**
  109. * @brief Decode meta field that was encoded using encodeFieldMetaData.
  110. */
  111. void decodeFieldMetaData(UINT32 encodedData, UINT16& id, UINT8& size, bool& array, SerializableFieldType& type, bool& hasDynamicSize);
  112. /**
  113. * @brief Encodes data required for representing an object identifier, into 4 bytes.
  114. *
  115. * @note Id can be a maximum of 31 bits, as one bit is reserved.
  116. */
  117. UINT32 encodeObjectMetaData(UINT32 objId);
  118. /**
  119. * @brief Decode meta field that was encoded using encodeObjectMetaData.
  120. *
  121. * @note Id can be a maximum of 31 bits, as one bit is reserved.
  122. */
  123. void decodeObjectMetaData(UINT32 encodedData, UINT32& objId);
  124. /**
  125. * @brief Returns true if the provided encoded meta data represents object meta data.
  126. */
  127. bool isObjectMetaData(UINT32 encodedData);
  128. /**
  129. * @brief Helper method for encoding a complex object and copying its data to a buffer.
  130. */
  131. UINT8* complexTypeToBuffer(IReflectable* object, UINT8* buffer, UINT32& bufferLength, int* bytesWritten,
  132. boost::function<UINT8*(UINT8* buffer, int bytesWritten, UINT32& newBufferSize)> flushBufferCallback);
  133. /**
  134. * @brief Helper method for decoding a complex object from the provided data buffer.
  135. */
  136. std::shared_ptr<IReflectable> complexTypeFromBuffer(RTTIReflectableFieldBase* field, UINT8* data, int* complexTypeSize);
  137. /**
  138. * @brief Finds an existing, or creates a unique unique identifier for the specified object.
  139. */
  140. UINT32 findOrCreatePersistentId(IReflectable* object);
  141. /**
  142. * @brief Finds or creates an id for the provided object and returns it.
  143. * And it adds the object to a list of objects that need to be encoded,
  144. * if it's not already there.
  145. */
  146. UINT32 registerObjectPtr(std::shared_ptr<IReflectable> object);
  147. };
  148. }