BsPrefabDiffRTTI.h 7.1 KB

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