BsPrefabDiffRTTI.h 7.6 KB

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