BsPrefabDiffRTTI.h 6.8 KB

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