BsPrefabDiffRTTI.h 7.7 KB

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