BsPrefabDiff.cpp 14 KB

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