2
0

BsPrefabDiff.cpp 14 KB

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