BsPrefabDiffRTTI.h 7.4 KB

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