BsPrefabDiff.cpp 15 KB

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