BsPrefabDiff.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPrefabDiff.h"
  4. #include "BsPrefabDiffRTTI.h"
  5. #include "BsSceneObject.h"
  6. #include "BsMemorySerializer.h"
  7. #include "BsBinarySerializer.h"
  8. #include "BsBinaryDiff.h"
  9. namespace BansheeEngine
  10. {
  11. RTTITypeBase* PrefabComponentDiff::getRTTIStatic()
  12. {
  13. return PrefabComponentDiffRTTI::instance();
  14. }
  15. RTTITypeBase* PrefabComponentDiff::getRTTI() const
  16. {
  17. return PrefabComponentDiff::getRTTIStatic();
  18. }
  19. RTTITypeBase* PrefabObjectDiff::getRTTIStatic()
  20. {
  21. return PrefabObjectDiffRTTI::instance();
  22. }
  23. RTTITypeBase* PrefabObjectDiff::getRTTI() const
  24. {
  25. return PrefabObjectDiff::getRTTIStatic();
  26. }
  27. SPtr<PrefabDiff> PrefabDiff::create(const HSceneObject& prefab, const HSceneObject& instance)
  28. {
  29. if (prefab->mPrefabLinkUUID != instance->mPrefabLinkUUID)
  30. return nullptr;
  31. // Note: If this method is called multiple times in a row then renaming all objects every time is redundant, it
  32. // would be more efficient to do it once outside of this method. I'm keeping it this way for simplicity for now.
  33. // Rename instance objects so they share the same IDs as the prefab objects (if they link IDs match). This allows
  34. // game object handle diff to work properly, because otherwise handles that point to same objects would be
  35. // marked as different because the instance IDs of the two objects don't match (since one is in prefab and one
  36. // in instance).
  37. Vector<RenamedGameObject> renamedObjects;
  38. renameInstanceIds(prefab, instance, renamedObjects);
  39. SPtr<PrefabDiff> output = bs_shared_ptr_new<PrefabDiff>();
  40. output->mRoot = generateDiff(prefab, instance);
  41. restoreInstanceIds(renamedObjects);
  42. return output;
  43. }
  44. void PrefabDiff::apply(const HSceneObject& object)
  45. {
  46. if (mRoot == nullptr)
  47. return;
  48. GameObjectManager::instance().startDeserialization();
  49. applyDiff(mRoot, object);
  50. GameObjectManager::instance().endDeserialization();
  51. }
  52. void PrefabDiff::applyDiff(const SPtr<PrefabObjectDiff>& diff, const HSceneObject& object)
  53. {
  54. if ((diff->soFlags & (UINT32)SceneObjectDiffFlags::Name) != 0)
  55. object->setName(diff->name);
  56. if ((diff->soFlags & (UINT32)SceneObjectDiffFlags::Position) != 0)
  57. object->setPosition(diff->position);
  58. if ((diff->soFlags & (UINT32)SceneObjectDiffFlags::Rotation) != 0)
  59. object->setRotation(diff->rotation);
  60. if ((diff->soFlags & (UINT32)SceneObjectDiffFlags::Scale) != 0)
  61. object->setScale(diff->scale);
  62. if ((diff->soFlags & (UINT32)SceneObjectDiffFlags::Active) != 0)
  63. object->setActive(diff->isActive);
  64. // Note: It is important to remove objects and components first, before adding them.
  65. // Some systems rely on the fact that applyDiff added components/objects are
  66. // always at the end.
  67. const Vector<HComponent>& components = object->getComponents();
  68. for (auto& removedId : diff->removedComponents)
  69. {
  70. for (auto component : components)
  71. {
  72. if (removedId == component->getLinkId())
  73. {
  74. component->destroy();
  75. break;
  76. }
  77. }
  78. }
  79. for (auto& removedId : diff->removedChildren)
  80. {
  81. UINT32 childCount = object->getNumChildren();
  82. for (UINT32 i = 0; i < childCount; i++)
  83. {
  84. HSceneObject child = object->getChild(i);
  85. if (removedId == child->getLinkId())
  86. {
  87. child->destroy();
  88. break;
  89. }
  90. }
  91. }
  92. for (auto& addedComponentData : diff->addedComponents)
  93. {
  94. BinarySerializer bs;
  95. SPtr<Component> component = std::static_pointer_cast<Component>(bs._decodeIntermediate(addedComponentData));
  96. object->addComponentInternal(component);
  97. }
  98. for (auto& addedChildData : diff->addedChildren)
  99. {
  100. BinarySerializer bs;
  101. SPtr<SceneObject> sceneObject = std::static_pointer_cast<SceneObject>(bs._decodeIntermediate(addedChildData));
  102. sceneObject->setParent(object);
  103. sceneObject->_instantiate();
  104. }
  105. for (auto& componentDiff : diff->componentDiffs)
  106. {
  107. for (auto& component : components)
  108. {
  109. if (componentDiff->id == component->getLinkId())
  110. {
  111. IDiff& diffHandler = component->getRTTI()->getDiffHandler();
  112. diffHandler.applyDiff(component.getInternalPtr(), componentDiff->data);
  113. break;
  114. }
  115. }
  116. }
  117. for (auto& childDiff : diff->childDiffs)
  118. {
  119. UINT32 childCount = object->getNumChildren();
  120. for (UINT32 i = 0; i < childCount; i++)
  121. {
  122. HSceneObject child = object->getChild(i);
  123. if (childDiff->id == child->getLinkId())
  124. {
  125. applyDiff(childDiff, child);
  126. break;
  127. }
  128. }
  129. }
  130. }
  131. SPtr<PrefabObjectDiff> PrefabDiff::generateDiff(const HSceneObject& prefab, const HSceneObject& instance)
  132. {
  133. SPtr<PrefabObjectDiff> output;
  134. if (prefab->getName() != instance->getName())
  135. {
  136. if (output == nullptr)
  137. output = bs_shared_ptr_new<PrefabObjectDiff>();
  138. output->name = instance->getName();
  139. output->soFlags |= (UINT32)SceneObjectDiffFlags::Name;
  140. }
  141. if (prefab->getPosition() != instance->getPosition())
  142. {
  143. if (output == nullptr)
  144. output = bs_shared_ptr_new<PrefabObjectDiff>();
  145. output->position = instance->getPosition();
  146. output->soFlags |= (UINT32)SceneObjectDiffFlags::Position;
  147. }
  148. if (prefab->getRotation() != instance->getRotation())
  149. {
  150. if (output == nullptr)
  151. output = bs_shared_ptr_new<PrefabObjectDiff>();
  152. output->rotation = instance->getRotation();
  153. output->soFlags |= (UINT32)SceneObjectDiffFlags::Rotation;
  154. }
  155. if (prefab->getScale() != instance->getScale())
  156. {
  157. if (output == nullptr)
  158. output = bs_shared_ptr_new<PrefabObjectDiff>();
  159. output->scale = instance->getScale();
  160. output->soFlags |= (UINT32)SceneObjectDiffFlags::Scale;
  161. }
  162. if (prefab->getActive() != instance->getActive())
  163. {
  164. if (output == nullptr)
  165. output = bs_shared_ptr_new<PrefabObjectDiff>();
  166. output->isActive = instance->getActive();
  167. output->soFlags |= (UINT32)SceneObjectDiffFlags::Active;
  168. }
  169. UINT32 prefabChildCount = prefab->getNumChildren();
  170. UINT32 instanceChildCount = instance->getNumChildren();
  171. // Find modified and removed children
  172. for (UINT32 i = 0; i < prefabChildCount; i++)
  173. {
  174. HSceneObject prefabChild = prefab->getChild(i);
  175. SPtr<PrefabObjectDiff> childDiff;
  176. bool foundMatching = false;
  177. for (UINT32 j = 0; j < instanceChildCount; j++)
  178. {
  179. HSceneObject instanceChild = instance->getChild(j);
  180. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  181. {
  182. if (instanceChild->mPrefabLinkUUID.empty())
  183. childDiff = generateDiff(prefabChild, instanceChild);
  184. foundMatching = true;
  185. break;
  186. }
  187. }
  188. if (foundMatching)
  189. {
  190. if (childDiff != nullptr)
  191. {
  192. if (output == nullptr)
  193. output = bs_shared_ptr_new<PrefabObjectDiff>();
  194. output->childDiffs.push_back(childDiff);
  195. }
  196. }
  197. else
  198. {
  199. if (output == nullptr)
  200. output = bs_shared_ptr_new<PrefabObjectDiff>();
  201. output->removedChildren.push_back(prefabChild->getLinkId());
  202. }
  203. }
  204. // Find added children
  205. for (UINT32 i = 0; i < instanceChildCount; i++)
  206. {
  207. HSceneObject instanceChild = instance->getChild(i);
  208. if (instanceChild->hasFlag(SOF_DontSave))
  209. continue;
  210. bool foundMatching = false;
  211. if (instanceChild->getLinkId() != -1)
  212. {
  213. for (UINT32 j = 0; j < prefabChildCount; j++)
  214. {
  215. HSceneObject prefabChild = prefab->getChild(j);
  216. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  217. {
  218. foundMatching = true;
  219. break;
  220. }
  221. }
  222. }
  223. if (!foundMatching)
  224. {
  225. BinarySerializer bs;
  226. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceChild.get());
  227. if (output == nullptr)
  228. output = bs_shared_ptr_new<PrefabObjectDiff>();
  229. output->addedChildren.push_back(obj);
  230. }
  231. }
  232. const Vector<HComponent>& prefabComponents = prefab->getComponents();
  233. const Vector<HComponent>& instanceComponents = instance->getComponents();
  234. UINT32 prefabComponentCount = (UINT32)prefabComponents.size();
  235. UINT32 instanceComponentCount = (UINT32)instanceComponents.size();
  236. // Find modified and removed components
  237. for (UINT32 i = 0; i < prefabComponentCount; i++)
  238. {
  239. HComponent prefabComponent = prefabComponents[i];
  240. SPtr<PrefabComponentDiff> childDiff;
  241. bool foundMatching = false;
  242. for (UINT32 j = 0; j < instanceComponentCount; j++)
  243. {
  244. HComponent instanceComponent = instanceComponents[j];
  245. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  246. {
  247. BinarySerializer bs;
  248. SPtr<SerializedObject> encodedPrefab = bs._encodeIntermediate(prefabComponent.get());
  249. SPtr<SerializedObject> encodedInstance = bs._encodeIntermediate(instanceComponent.get());
  250. IDiff& diffHandler = prefabComponent->getRTTI()->getDiffHandler();
  251. SPtr<SerializedObject> diff = diffHandler.generateDiff(encodedPrefab, encodedInstance);
  252. if (diff != nullptr)
  253. {
  254. childDiff = bs_shared_ptr_new<PrefabComponentDiff>();
  255. childDiff->id = prefabComponent->getLinkId();
  256. childDiff->data = diff;
  257. }
  258. foundMatching = true;
  259. break;
  260. }
  261. }
  262. if (foundMatching)
  263. {
  264. if (childDiff != nullptr)
  265. {
  266. if (output == nullptr)
  267. output = bs_shared_ptr_new<PrefabObjectDiff>();
  268. output->componentDiffs.push_back(childDiff);
  269. }
  270. }
  271. else
  272. {
  273. if (output == nullptr)
  274. output = bs_shared_ptr_new<PrefabObjectDiff>();
  275. output->removedComponents.push_back(prefabComponent->getLinkId());
  276. }
  277. }
  278. // Find added components
  279. for (UINT32 i = 0; i < instanceComponentCount; i++)
  280. {
  281. HComponent instanceComponent = instanceComponents[i];
  282. bool foundMatching = false;
  283. if (instanceComponent->getLinkId() != -1)
  284. {
  285. for (UINT32 j = 0; j < prefabComponentCount; j++)
  286. {
  287. HComponent prefabComponent = prefabComponents[j];
  288. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  289. {
  290. foundMatching = true;
  291. break;
  292. }
  293. }
  294. }
  295. if (!foundMatching)
  296. {
  297. BinarySerializer bs;
  298. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceComponent.get());
  299. if (output == nullptr)
  300. output = bs_shared_ptr_new<PrefabObjectDiff>();
  301. output->addedComponents.push_back(obj);
  302. }
  303. }
  304. if (output != nullptr)
  305. output->id = instance->getLinkId();
  306. return output;
  307. }
  308. void PrefabDiff::renameInstanceIds(const HSceneObject& prefab, const HSceneObject& instance, Vector<RenamedGameObject>& output)
  309. {
  310. UnorderedMap<String, UnorderedMap<UINT32, UINT64>> linkToInstanceId;
  311. struct StackEntry
  312. {
  313. HSceneObject so;
  314. String uuid;
  315. };
  316. // When renaming it is important to rename the prefab and not the instance, since the diff will otherwise
  317. // contain prefab's IDs, but will be used for the instance.
  318. Stack<StackEntry> todo;
  319. todo.push({ instance, "root" });
  320. while (!todo.empty())
  321. {
  322. StackEntry current = todo.top();
  323. todo.pop();
  324. String childParentUUID;
  325. if (current.so->mPrefabLinkUUID.empty())
  326. childParentUUID = current.uuid;
  327. else
  328. childParentUUID = current.so->mPrefabLinkUUID;
  329. UnorderedMap<UINT32, UINT64>& idMap = linkToInstanceId[childParentUUID];
  330. const Vector<HComponent>& components = current.so->getComponents();
  331. for (auto& component : components)
  332. {
  333. if (component->getLinkId() != (UINT32)-1)
  334. idMap[component->getLinkId()] = component->getInstanceId();
  335. }
  336. UINT32 numChildren = current.so->getNumChildren();
  337. for (UINT32 i = 0; i < numChildren; i++)
  338. {
  339. HSceneObject child = current.so->getChild(i);
  340. if (child->getLinkId() != (UINT32)-1)
  341. idMap[child->getLinkId()] = child->getInstanceId();
  342. todo.push({ child, childParentUUID });
  343. }
  344. }
  345. // Root has link ID from its parent so we handle it separately
  346. {
  347. output.push_back(RenamedGameObject());
  348. RenamedGameObject& renamedGO = output.back();
  349. renamedGO.instanceData = instance->mInstanceData;
  350. renamedGO.originalId = instance->getInstanceId();
  351. prefab->mInstanceData->mInstanceId = instance->getInstanceId();
  352. }
  353. todo.push({ prefab, "root" });
  354. while (!todo.empty())
  355. {
  356. StackEntry current = todo.top();
  357. todo.pop();
  358. String childParentUUID;
  359. if (current.so->mPrefabLinkUUID.empty())
  360. childParentUUID = current.uuid;
  361. else
  362. childParentUUID = current.so->mPrefabLinkUUID;
  363. auto iterFind = linkToInstanceId.find(childParentUUID);
  364. if (iterFind != linkToInstanceId.end())
  365. {
  366. UnorderedMap<UINT32, UINT64>& idMap = iterFind->second;
  367. const Vector<HComponent>& components = current.so->getComponents();
  368. for (auto& component : components)
  369. {
  370. auto iterFind2 = idMap.find(component->getLinkId());
  371. if (iterFind2 != idMap.end())
  372. {
  373. output.push_back(RenamedGameObject());
  374. RenamedGameObject& renamedGO = output.back();
  375. renamedGO.instanceData = component->mInstanceData;
  376. renamedGO.originalId = component->getInstanceId();
  377. component->mInstanceData->mInstanceId = iterFind2->second;
  378. }
  379. }
  380. }
  381. UINT32 numChildren = current.so->getNumChildren();
  382. for (UINT32 i = 0; i < numChildren; i++)
  383. {
  384. HSceneObject child = current.so->getChild(i);
  385. if (iterFind != linkToInstanceId.end())
  386. {
  387. if (child->getLinkId() != -1)
  388. {
  389. UnorderedMap<UINT32, UINT64>& idMap = iterFind->second;
  390. auto iterFind2 = idMap.find(child->getLinkId());
  391. if (iterFind2 != idMap.end())
  392. {
  393. output.push_back(RenamedGameObject());
  394. RenamedGameObject& renamedGO = output.back();
  395. renamedGO.instanceData = child->mInstanceData;
  396. renamedGO.originalId = child->getInstanceId();
  397. child->mInstanceData->mInstanceId = iterFind2->second;
  398. }
  399. }
  400. }
  401. todo.push({ child, childParentUUID });
  402. }
  403. }
  404. }
  405. void PrefabDiff::restoreInstanceIds(const Vector<RenamedGameObject>& renamedObjects)
  406. {
  407. for (auto& renamedGO : renamedObjects)
  408. renamedGO.instanceData->mInstanceId = renamedGO.originalId;
  409. }
  410. RTTITypeBase* PrefabDiff::getRTTIStatic()
  411. {
  412. return PrefabDiffRTTI::instance();
  413. }
  414. RTTITypeBase* PrefabDiff::getRTTI() const
  415. {
  416. return PrefabDiff::getRTTIStatic();
  417. }
  418. }