BsPrefabDiffRTTI.h 7.5 KB

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