BsPrefabDiffRTTI.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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. private:
  75. BS_REFLPTR_MEMBER(mRoot);
  76. public:
  77. PrefabDiffRTTI()
  78. {
  79. BS_ADD_REFLPTR_FIELD(mRoot, 0);
  80. }
  81. virtual void onDeserializationStarted(IReflectable* obj) override
  82. {
  83. PrefabDiff* prefabDiff = static_cast<PrefabDiff*>(obj);
  84. if (GameObjectManager::instance().isGameObjectDeserializationActive())
  85. GameObjectManager::instance().registerOnDeserializationEndCallback(std::bind(&PrefabDiffRTTI::delayedOnDeserializationEnded, prefabDiff));
  86. }
  87. /**
  88. * @brief Decodes GameObjectHandles from their binary format, because during deserialization GameObjectManager
  89. * will update all object IDs and we want to keep the handles up to date.So we deserialize them
  90. * and allow them to be updated before storing them back into binary format.
  91. */
  92. static void delayedOnDeserializationEnded(PrefabDiff* prefabDiff)
  93. {
  94. Stack<SPtr<PrefabObjectDiff>> todo;
  95. todo.push(prefabDiff->mRoot);
  96. UnorderedSet<SPtr<SerializedObject>> handleObjects;
  97. while (!todo.empty())
  98. {
  99. SPtr<PrefabObjectDiff> current = todo.top();
  100. todo.pop();
  101. for (auto& component : current->addedComponents)
  102. findGameObjectHandles(component, handleObjects);
  103. for (auto& child : current->addedChildren)
  104. findGameObjectHandles(child, handleObjects);
  105. for (auto& component : current->componentDiffs)
  106. findGameObjectHandles(component->data, handleObjects);
  107. for (auto& child : current->childDiffs)
  108. todo.push(child);
  109. }
  110. BinarySerializer bs;
  111. for (auto& serializedHandle : handleObjects)
  112. {
  113. SPtr<GameObjectHandleBase> handle = std::static_pointer_cast<GameObjectHandleBase>(bs._decodeIntermediate(serializedHandle));
  114. if (handle != nullptr)
  115. {
  116. UINT32 flags = GameObjectManager::instance().getDeserializationFlags();
  117. GameObjectManager::instance().resolveDeserializedHandle(*handle, flags | GODM_KeepMissing);
  118. *serializedHandle = *bs._encodeIntermediate(handle.get());
  119. }
  120. }
  121. }
  122. /**
  123. * @brief Scans the entire hierarchy and find all serialized GameObjectHandle objects.
  124. */
  125. static void findGameObjectHandles(const SPtr<SerializedObject>& serializedObject, UnorderedSet<SPtr<SerializedObject>>& handleObjects)
  126. {
  127. for (auto& subObject : serializedObject->subObjects)
  128. {
  129. RTTITypeBase* rtti = IReflectable::_getRTTIfromTypeId(subObject.typeId);
  130. if (rtti == nullptr)
  131. continue;
  132. if (rtti->getRTTIId() == TID_GameObjectHandleBase)
  133. {
  134. handleObjects.insert(serializedObject);
  135. return;
  136. }
  137. for (auto& child : subObject.entries)
  138. {
  139. RTTIField* curGenericField = rtti->getField(child.second.fieldId);
  140. if (curGenericField == nullptr)
  141. continue;
  142. SPtr<SerializedInstance> entryData = child.second.serialized;
  143. if (curGenericField->isArray())
  144. {
  145. SPtr<SerializedArray> arrayData = std::static_pointer_cast<SerializedArray>(entryData);
  146. switch (curGenericField->mType)
  147. {
  148. case SerializableFT_ReflectablePtr:
  149. case SerializableFT_Reflectable:
  150. {
  151. for (auto& arrayElem : arrayData->entries)
  152. {
  153. SPtr<SerializedObject> arrayElemData = std::static_pointer_cast<SerializedObject>(arrayElem.second.serialized);
  154. if (arrayElemData != nullptr)
  155. findGameObjectHandles(arrayElemData, handleObjects);
  156. }
  157. }
  158. break;
  159. }
  160. }
  161. else
  162. {
  163. switch (curGenericField->mType)
  164. {
  165. case SerializableFT_ReflectablePtr:
  166. case SerializableFT_Reflectable:
  167. {
  168. SPtr<SerializedObject> fieldObjectData = std::static_pointer_cast<SerializedObject>(entryData);
  169. if (fieldObjectData != nullptr)
  170. findGameObjectHandles(fieldObjectData, handleObjects);
  171. }
  172. break;
  173. }
  174. }
  175. }
  176. }
  177. }
  178. virtual const String& getRTTIName() override
  179. {
  180. static String name = "PrefabDiff";
  181. return name;
  182. }
  183. virtual UINT32 getRTTIId() override
  184. {
  185. return TID_PrefabDiff;
  186. }
  187. virtual std::shared_ptr<IReflectable> newRTTIObject() override
  188. {
  189. return bs_shared_ptr_new<PrefabDiff>();
  190. }
  191. };
  192. }