BsPrefabDiff.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  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. // It's possible the prefab added the same component that is in the diff, in which case we replace the existing
  97. // component with the new one
  98. bool foundExisting = false;
  99. for (auto& entry : components)
  100. {
  101. if (entry->typeEquals(*component))
  102. {
  103. GameObjectInstanceDataPtr instanceData = entry->_getInstanceData();
  104. entry->destroy(true);
  105. component->_setInstanceData(instanceData);
  106. object->addComponentInternal(component);
  107. foundExisting = true;
  108. break;
  109. }
  110. }
  111. if (!foundExisting)
  112. {
  113. object->addComponentInternal(component);
  114. }
  115. }
  116. for (auto& addedChildData : diff->addedChildren)
  117. {
  118. BinarySerializer bs;
  119. SPtr<SceneObject> sceneObject = std::static_pointer_cast<SceneObject>(bs._decodeIntermediate(addedChildData));
  120. sceneObject->setParent(object);
  121. sceneObject->_instantiate();
  122. }
  123. for (auto& componentDiff : diff->componentDiffs)
  124. {
  125. for (auto& component : components)
  126. {
  127. if (componentDiff->id == component->getLinkId())
  128. {
  129. IDiff& diffHandler = component->getRTTI()->getDiffHandler();
  130. diffHandler.applyDiff(component.getInternalPtr(), componentDiff->data);
  131. break;
  132. }
  133. }
  134. }
  135. for (auto& childDiff : diff->childDiffs)
  136. {
  137. UINT32 childCount = object->getNumChildren();
  138. for (UINT32 i = 0; i < childCount; i++)
  139. {
  140. HSceneObject child = object->getChild(i);
  141. if (childDiff->id == child->getLinkId())
  142. {
  143. applyDiff(childDiff, child);
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. SPtr<PrefabObjectDiff> PrefabDiff::generateDiff(const HSceneObject& prefab, const HSceneObject& instance)
  150. {
  151. SPtr<PrefabObjectDiff> output;
  152. if (prefab->getName() != instance->getName())
  153. {
  154. if (output == nullptr)
  155. output = bs_shared_ptr_new<PrefabObjectDiff>();
  156. output->name = instance->getName();
  157. output->soFlags |= (UINT32)SceneObjectDiffFlags::Name;
  158. }
  159. if (prefab->getPosition() != instance->getPosition())
  160. {
  161. if (output == nullptr)
  162. output = bs_shared_ptr_new<PrefabObjectDiff>();
  163. output->position = instance->getPosition();
  164. output->soFlags |= (UINT32)SceneObjectDiffFlags::Position;
  165. }
  166. if (prefab->getRotation() != instance->getRotation())
  167. {
  168. if (output == nullptr)
  169. output = bs_shared_ptr_new<PrefabObjectDiff>();
  170. output->rotation = instance->getRotation();
  171. output->soFlags |= (UINT32)SceneObjectDiffFlags::Rotation;
  172. }
  173. if (prefab->getScale() != instance->getScale())
  174. {
  175. if (output == nullptr)
  176. output = bs_shared_ptr_new<PrefabObjectDiff>();
  177. output->scale = instance->getScale();
  178. output->soFlags |= (UINT32)SceneObjectDiffFlags::Scale;
  179. }
  180. if (prefab->getActive() != instance->getActive())
  181. {
  182. if (output == nullptr)
  183. output = bs_shared_ptr_new<PrefabObjectDiff>();
  184. output->isActive = instance->getActive();
  185. output->soFlags |= (UINT32)SceneObjectDiffFlags::Active;
  186. }
  187. UINT32 prefabChildCount = prefab->getNumChildren();
  188. UINT32 instanceChildCount = instance->getNumChildren();
  189. // Find modified and removed children
  190. for (UINT32 i = 0; i < prefabChildCount; i++)
  191. {
  192. HSceneObject prefabChild = prefab->getChild(i);
  193. SPtr<PrefabObjectDiff> childDiff;
  194. bool foundMatching = false;
  195. for (UINT32 j = 0; j < instanceChildCount; j++)
  196. {
  197. HSceneObject instanceChild = instance->getChild(j);
  198. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  199. {
  200. if (instanceChild->mPrefabLinkUUID.empty())
  201. childDiff = generateDiff(prefabChild, instanceChild);
  202. foundMatching = true;
  203. break;
  204. }
  205. }
  206. if (foundMatching)
  207. {
  208. if (childDiff != nullptr)
  209. {
  210. if (output == nullptr)
  211. output = bs_shared_ptr_new<PrefabObjectDiff>();
  212. output->childDiffs.push_back(childDiff);
  213. }
  214. }
  215. else
  216. {
  217. if (output == nullptr)
  218. output = bs_shared_ptr_new<PrefabObjectDiff>();
  219. output->removedChildren.push_back(prefabChild->getLinkId());
  220. }
  221. }
  222. // Find added children
  223. for (UINT32 i = 0; i < instanceChildCount; i++)
  224. {
  225. HSceneObject instanceChild = instance->getChild(i);
  226. if (instanceChild->hasFlag(SOF_DontSave))
  227. continue;
  228. bool foundMatching = false;
  229. if (instanceChild->getLinkId() != -1)
  230. {
  231. for (UINT32 j = 0; j < prefabChildCount; j++)
  232. {
  233. HSceneObject prefabChild = prefab->getChild(j);
  234. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  235. {
  236. foundMatching = true;
  237. break;
  238. }
  239. }
  240. }
  241. if (!foundMatching)
  242. {
  243. BinarySerializer bs;
  244. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceChild.get());
  245. if (output == nullptr)
  246. output = bs_shared_ptr_new<PrefabObjectDiff>();
  247. output->addedChildren.push_back(obj);
  248. }
  249. }
  250. const Vector<HComponent>& prefabComponents = prefab->getComponents();
  251. const Vector<HComponent>& instanceComponents = instance->getComponents();
  252. UINT32 prefabComponentCount = (UINT32)prefabComponents.size();
  253. UINT32 instanceComponentCount = (UINT32)instanceComponents.size();
  254. // Find modified and removed components
  255. for (UINT32 i = 0; i < prefabComponentCount; i++)
  256. {
  257. HComponent prefabComponent = prefabComponents[i];
  258. SPtr<PrefabComponentDiff> childDiff;
  259. bool foundMatching = false;
  260. for (UINT32 j = 0; j < instanceComponentCount; j++)
  261. {
  262. HComponent instanceComponent = instanceComponents[j];
  263. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  264. {
  265. BinarySerializer bs;
  266. SPtr<SerializedObject> encodedPrefab = bs._encodeIntermediate(prefabComponent.get());
  267. SPtr<SerializedObject> encodedInstance = bs._encodeIntermediate(instanceComponent.get());
  268. IDiff& diffHandler = prefabComponent->getRTTI()->getDiffHandler();
  269. SPtr<SerializedObject> diff = diffHandler.generateDiff(encodedPrefab, encodedInstance);
  270. if (diff != nullptr)
  271. {
  272. childDiff = bs_shared_ptr_new<PrefabComponentDiff>();
  273. childDiff->id = prefabComponent->getLinkId();
  274. childDiff->data = diff;
  275. }
  276. foundMatching = true;
  277. break;
  278. }
  279. }
  280. if (foundMatching)
  281. {
  282. if (childDiff != nullptr)
  283. {
  284. if (output == nullptr)
  285. output = bs_shared_ptr_new<PrefabObjectDiff>();
  286. output->componentDiffs.push_back(childDiff);
  287. }
  288. }
  289. else
  290. {
  291. if (output == nullptr)
  292. output = bs_shared_ptr_new<PrefabObjectDiff>();
  293. output->removedComponents.push_back(prefabComponent->getLinkId());
  294. }
  295. }
  296. // Find added components
  297. for (UINT32 i = 0; i < instanceComponentCount; i++)
  298. {
  299. HComponent instanceComponent = instanceComponents[i];
  300. bool foundMatching = false;
  301. if (instanceComponent->getLinkId() != -1)
  302. {
  303. for (UINT32 j = 0; j < prefabComponentCount; j++)
  304. {
  305. HComponent prefabComponent = prefabComponents[j];
  306. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  307. {
  308. foundMatching = true;
  309. break;
  310. }
  311. }
  312. }
  313. if (!foundMatching)
  314. {
  315. BinarySerializer bs;
  316. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceComponent.get());
  317. if (output == nullptr)
  318. output = bs_shared_ptr_new<PrefabObjectDiff>();
  319. output->addedComponents.push_back(obj);
  320. }
  321. }
  322. if (output != nullptr)
  323. output->id = instance->getLinkId();
  324. return output;
  325. }
  326. void PrefabDiff::renameInstanceIds(const HSceneObject& prefab, const HSceneObject& instance, Vector<RenamedGameObject>& output)
  327. {
  328. UnorderedMap<String, UnorderedMap<UINT32, UINT64>> linkToInstanceId;
  329. struct StackEntry
  330. {
  331. HSceneObject so;
  332. String uuid;
  333. };
  334. // When renaming it is important to rename the prefab and not the instance, since the diff will otherwise
  335. // contain prefab's IDs, but will be used for the instance.
  336. Stack<StackEntry> todo;
  337. todo.push({ instance, "root" });
  338. while (!todo.empty())
  339. {
  340. StackEntry current = todo.top();
  341. todo.pop();
  342. String childParentUUID;
  343. if (current.so->mPrefabLinkUUID.empty())
  344. childParentUUID = current.uuid;
  345. else
  346. childParentUUID = current.so->mPrefabLinkUUID;
  347. UnorderedMap<UINT32, UINT64>& idMap = linkToInstanceId[childParentUUID];
  348. const Vector<HComponent>& components = current.so->getComponents();
  349. for (auto& component : components)
  350. {
  351. if (component->getLinkId() != (UINT32)-1)
  352. idMap[component->getLinkId()] = component->getInstanceId();
  353. }
  354. UINT32 numChildren = current.so->getNumChildren();
  355. for (UINT32 i = 0; i < numChildren; i++)
  356. {
  357. HSceneObject child = current.so->getChild(i);
  358. if (child->getLinkId() != (UINT32)-1)
  359. idMap[child->getLinkId()] = child->getInstanceId();
  360. todo.push({ child, childParentUUID });
  361. }
  362. }
  363. // Root has link ID from its parent so we handle it separately
  364. {
  365. output.push_back(RenamedGameObject());
  366. RenamedGameObject& renamedGO = output.back();
  367. renamedGO.instanceData = instance->mInstanceData;
  368. renamedGO.originalId = instance->getInstanceId();
  369. prefab->mInstanceData->mInstanceId = instance->getInstanceId();
  370. }
  371. todo.push({ prefab, "root" });
  372. while (!todo.empty())
  373. {
  374. StackEntry current = todo.top();
  375. todo.pop();
  376. String childParentUUID;
  377. if (current.so->mPrefabLinkUUID.empty())
  378. childParentUUID = current.uuid;
  379. else
  380. childParentUUID = current.so->mPrefabLinkUUID;
  381. auto iterFind = linkToInstanceId.find(childParentUUID);
  382. if (iterFind != linkToInstanceId.end())
  383. {
  384. UnorderedMap<UINT32, UINT64>& idMap = iterFind->second;
  385. const Vector<HComponent>& components = current.so->getComponents();
  386. for (auto& component : components)
  387. {
  388. auto iterFind2 = idMap.find(component->getLinkId());
  389. if (iterFind2 != idMap.end())
  390. {
  391. output.push_back(RenamedGameObject());
  392. RenamedGameObject& renamedGO = output.back();
  393. renamedGO.instanceData = component->mInstanceData;
  394. renamedGO.originalId = component->getInstanceId();
  395. component->mInstanceData->mInstanceId = iterFind2->second;
  396. }
  397. }
  398. }
  399. UINT32 numChildren = current.so->getNumChildren();
  400. for (UINT32 i = 0; i < numChildren; i++)
  401. {
  402. HSceneObject child = current.so->getChild(i);
  403. if (iterFind != linkToInstanceId.end())
  404. {
  405. if (child->getLinkId() != -1)
  406. {
  407. UnorderedMap<UINT32, UINT64>& idMap = iterFind->second;
  408. auto iterFind2 = idMap.find(child->getLinkId());
  409. if (iterFind2 != idMap.end())
  410. {
  411. output.push_back(RenamedGameObject());
  412. RenamedGameObject& renamedGO = output.back();
  413. renamedGO.instanceData = child->mInstanceData;
  414. renamedGO.originalId = child->getInstanceId();
  415. child->mInstanceData->mInstanceId = iterFind2->second;
  416. }
  417. }
  418. }
  419. todo.push({ child, childParentUUID });
  420. }
  421. }
  422. }
  423. void PrefabDiff::restoreInstanceIds(const Vector<RenamedGameObject>& renamedObjects)
  424. {
  425. for (auto& renamedGO : renamedObjects)
  426. renamedGO.instanceData->mInstanceId = renamedGO.originalId;
  427. }
  428. RTTITypeBase* PrefabDiff::getRTTIStatic()
  429. {
  430. return PrefabDiffRTTI::instance();
  431. }
  432. RTTITypeBase* PrefabDiff::getRTTI() const
  433. {
  434. return PrefabDiff::getRTTIStatic();
  435. }
  436. }