BsPrefabDiff.cpp 14 KB

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