2
0

BsPrefabDiffRTTI.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. #include "BsCorePrerequisites.h"
  3. #include "BsRTTIType.h"
  4. #include "BsPrefabDiff.h"
  5. #include "BsSerializedObject.h"
  6. #include "BsGameObjectManager.h"
  7. #include "BsBinarySerializer.h"
  8. namespace BansheeEngine
  9. {
  10. /** @cond RTTI */
  11. /** @addtogroup RTTI-Impl-Core
  12. * @{
  13. */
  14. class BS_CORE_EXPORT PrefabComponentDiffRTTI : public RTTIType < PrefabComponentDiff, IReflectable, PrefabComponentDiffRTTI >
  15. {
  16. private:
  17. BS_PLAIN_MEMBER(id)
  18. BS_REFLPTR_MEMBER(data);
  19. public:
  20. PrefabComponentDiffRTTI()
  21. {
  22. BS_ADD_PLAIN_FIELD(id, 0);
  23. BS_ADD_REFLPTR_FIELD(data, 1);
  24. }
  25. const String& getRTTIName() override
  26. {
  27. static String name = "PrefabComponentDiff";
  28. return name;
  29. }
  30. UINT32 getRTTIId() override
  31. {
  32. return TID_PrefabComponentDiff;
  33. }
  34. std::shared_ptr<IReflectable> newRTTIObject() override
  35. {
  36. return bs_shared_ptr_new<PrefabComponentDiff>();
  37. }
  38. };
  39. class BS_CORE_EXPORT PrefabObjectDiffRTTI : public RTTIType < PrefabObjectDiff, IReflectable, PrefabObjectDiffRTTI >
  40. {
  41. private:
  42. BS_PLAIN_MEMBER(id)
  43. BS_PLAIN_MEMBER(name)
  44. BS_PLAIN_MEMBER(position);
  45. BS_PLAIN_MEMBER(rotation);
  46. BS_PLAIN_MEMBER(scale);
  47. BS_PLAIN_MEMBER(isActive);
  48. BS_PLAIN_MEMBER(soFlags);
  49. BS_REFLPTR_MEMBER_VEC(componentDiffs)
  50. BS_PLAIN_MEMBER_VEC(removedComponents)
  51. BS_REFLPTR_MEMBER_VEC(addedComponents)
  52. BS_REFLPTR_MEMBER_VEC(childDiffs)
  53. BS_PLAIN_MEMBER_VEC(removedChildren)
  54. BS_REFLPTR_MEMBER_VEC(addedChildren)
  55. public:
  56. PrefabObjectDiffRTTI()
  57. {
  58. BS_ADD_PLAIN_FIELD(id, 0);
  59. BS_ADD_PLAIN_FIELD(name, 1);
  60. BS_ADD_REFLPTR_FIELD_ARR(componentDiffs, 2);
  61. BS_ADD_PLAIN_FIELD_ARR(removedComponents, 3);
  62. BS_ADD_REFLPTR_FIELD_ARR(addedComponents, 4);
  63. BS_ADD_REFLPTR_FIELD_ARR(childDiffs, 5);
  64. BS_ADD_PLAIN_FIELD_ARR(removedChildren, 6);
  65. BS_ADD_REFLPTR_FIELD_ARR(addedChildren, 7);
  66. BS_ADD_PLAIN_FIELD(position, 8);
  67. BS_ADD_PLAIN_FIELD(rotation, 9);
  68. BS_ADD_PLAIN_FIELD(scale, 10);
  69. BS_ADD_PLAIN_FIELD(isActive, 11);
  70. BS_ADD_PLAIN_FIELD(soFlags, 12);
  71. }
  72. const String& getRTTIName() override
  73. {
  74. static String name = "PrefabObjectDiff";
  75. return name;
  76. }
  77. UINT32 getRTTIId() override
  78. {
  79. return TID_PrefabObjectDiff;
  80. }
  81. std::shared_ptr<IReflectable> newRTTIObject() override
  82. {
  83. return bs_shared_ptr_new<PrefabObjectDiff>();
  84. }
  85. };
  86. class BS_CORE_EXPORT PrefabDiffRTTI : public RTTIType < PrefabDiff, IReflectable, PrefabDiffRTTI >
  87. {
  88. /**
  89. * @brief Contains data about a game object handle serialized in a prefab diff.
  90. */
  91. struct SerializedHandle
  92. {
  93. SPtr<SerializedObject> object;
  94. SPtr<GameObjectHandleBase> handle;
  95. };
  96. private:
  97. BS_REFLPTR_MEMBER(mRoot);
  98. public:
  99. PrefabDiffRTTI()
  100. {
  101. BS_ADD_REFLPTR_FIELD(mRoot, 0);
  102. }
  103. void onDeserializationStarted(IReflectable* obj) override
  104. {
  105. PrefabDiff* prefabDiff = static_cast<PrefabDiff*>(obj);
  106. if (GameObjectManager::instance().isGameObjectDeserializationActive())
  107. GameObjectManager::instance().registerOnDeserializationEndCallback(std::bind(&PrefabDiffRTTI::delayedOnDeserializationEnded, prefabDiff));
  108. }
  109. void onDeserializationEnded(IReflectable* obj) override
  110. {
  111. assert(GameObjectManager::instance().isGameObjectDeserializationActive());
  112. // Make sure to deserialize all game object handles since their IDs need to be updated. Normally they are
  113. // updated automatically upon deserialization but since we store them in intermediate form we need to manually
  114. // deserialize and reserialize them in order to update their IDs.
  115. PrefabDiff* prefabDiff = static_cast<PrefabDiff*>(obj);
  116. Stack<SPtr<PrefabObjectDiff>> todo;
  117. if (prefabDiff->mRoot != nullptr)
  118. todo.push(prefabDiff->mRoot);
  119. UnorderedSet<SPtr<SerializedObject>> handleObjects;
  120. while (!todo.empty())
  121. {
  122. SPtr<PrefabObjectDiff> current = todo.top();
  123. todo.pop();
  124. for (auto& component : current->addedComponents)
  125. findGameObjectHandles(component, handleObjects);
  126. for (auto& child : current->addedChildren)
  127. findGameObjectHandles(child, handleObjects);
  128. for (auto& component : current->componentDiffs)
  129. findGameObjectHandles(component->data, handleObjects);
  130. for (auto& child : current->childDiffs)
  131. todo.push(child);
  132. }
  133. Vector<SerializedHandle> handleData(handleObjects.size());
  134. UINT32 idx = 0;
  135. BinarySerializer bs;
  136. for (auto& handleObject : handleObjects)
  137. {
  138. SerializedHandle& handle = handleData[idx];
  139. handle.object = handleObject;
  140. handle.handle = std::static_pointer_cast<GameObjectHandleBase>(bs._decodeIntermediate(handleObject));
  141. idx++;
  142. }
  143. prefabDiff->mRTTIData = handleData;
  144. }
  145. /**
  146. * @brief Decodes GameObjectHandles from their binary format, because during deserialization GameObjectManager
  147. * will update all object IDs and we want to keep the handles up to date.So we deserialize them
  148. * and allow them to be updated before storing them back into binary format.
  149. */
  150. static void delayedOnDeserializationEnded(PrefabDiff* prefabDiff)
  151. {
  152. Vector<SerializedHandle>& handleData = any_cast_ref<Vector<SerializedHandle>>(prefabDiff->mRTTIData);
  153. BinarySerializer bs;
  154. for (auto& serializedHandle : handleData)
  155. {
  156. if (serializedHandle.handle != nullptr)
  157. *serializedHandle.object = *bs._encodeIntermediate(serializedHandle.handle.get());
  158. }
  159. prefabDiff->mRTTIData = nullptr;
  160. }
  161. /**
  162. * @brief Scans the entire hierarchy and find all serialized GameObjectHandle objects.
  163. */
  164. static void findGameObjectHandles(const SPtr<SerializedObject>& serializedObject, UnorderedSet<SPtr<SerializedObject>>& handleObjects)
  165. {
  166. for (auto& subObject : serializedObject->subObjects)
  167. {
  168. RTTITypeBase* rtti = IReflectable::_getRTTIfromTypeId(subObject.typeId);
  169. if (rtti == nullptr)
  170. continue;
  171. if (rtti->getRTTIId() == TID_GameObjectHandleBase)
  172. {
  173. handleObjects.insert(serializedObject);
  174. return;
  175. }
  176. for (auto& child : subObject.entries)
  177. {
  178. RTTIField* curGenericField = rtti->findField(child.second.fieldId);
  179. if (curGenericField == nullptr)
  180. continue;
  181. SPtr<SerializedInstance> entryData = child.second.serialized;
  182. if (entryData == nullptr)
  183. continue;
  184. if (rtti_is_of_type<SerializedArray>(entryData))
  185. {
  186. SPtr<SerializedArray> arrayData = std::static_pointer_cast<SerializedArray>(entryData);
  187. for (auto& arrayElem : arrayData->entries)
  188. {
  189. if (arrayElem.second.serialized != nullptr && rtti_is_of_type<SerializedObject>(arrayElem.second.serialized))
  190. {
  191. SPtr<SerializedObject> arrayElemData = std::static_pointer_cast<SerializedObject>(arrayElem.second.serialized);
  192. findGameObjectHandles(arrayElemData, handleObjects);
  193. }
  194. }
  195. }
  196. else if(rtti_is_of_type<SerializedObject>(entryData))
  197. {
  198. SPtr<SerializedObject> fieldObjectData = std::static_pointer_cast<SerializedObject>(entryData);
  199. findGameObjectHandles(fieldObjectData, handleObjects);
  200. }
  201. }
  202. }
  203. }
  204. const String& getRTTIName() override
  205. {
  206. static String name = "PrefabDiff";
  207. return name;
  208. }
  209. UINT32 getRTTIId() override
  210. {
  211. return TID_PrefabDiff;
  212. }
  213. std::shared_ptr<IReflectable> newRTTIObject() override
  214. {
  215. return bs_shared_ptr_new<PrefabDiff>();
  216. }
  217. };
  218. /** @} */
  219. /** @endcond */
  220. }