BsPrefabDiffRTTI.h 7.5 KB

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