BsPrefabDiff.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. Vector<RenamedGameObject> renamedObjects;
  32. renameInstanceIds(prefab, instance, renamedObjects);
  33. SPtr<PrefabDiff> output = bs_shared_ptr_new<PrefabDiff>();
  34. output->mRoot = generateDiff(prefab, instance);
  35. restoreInstanceIds(renamedObjects);
  36. return output;
  37. }
  38. void PrefabDiff::apply(const HSceneObject& object)
  39. {
  40. if (mRoot == nullptr)
  41. return;
  42. GameObjectManager::instance().startDeserialization();
  43. applyDiff(mRoot, object);
  44. GameObjectManager::instance().endDeserialization();
  45. }
  46. void PrefabDiff::applyDiff(const SPtr<PrefabObjectDiff>& diff, const HSceneObject& object)
  47. {
  48. object->setName(diff->name);
  49. // Note: It is important to remove objects and components first, before adding them.
  50. // Some systems rely on the fact that applyDiff added components/objects are
  51. // always at the end.
  52. const Vector<HComponent>& components = object->getComponents();
  53. for (auto& removedId : diff->removedComponents)
  54. {
  55. for (auto component : components)
  56. {
  57. if (removedId == component->getLinkId())
  58. {
  59. component->destroy();
  60. break;
  61. }
  62. }
  63. }
  64. for (auto& removedId : diff->removedChildren)
  65. {
  66. UINT32 childCount = object->getNumChildren();
  67. for (UINT32 i = 0; i < childCount; i++)
  68. {
  69. HSceneObject child = object->getChild(i);
  70. if (removedId == child->getLinkId())
  71. {
  72. child->destroy();
  73. break;
  74. }
  75. }
  76. }
  77. for (auto& addedComponentData : diff->addedComponents)
  78. {
  79. BinarySerializer bs;
  80. SPtr<Component> component = std::static_pointer_cast<Component>(bs._decodeIntermediate(addedComponentData));
  81. object->addComponentInternal(component);
  82. }
  83. for (auto& addedChildData : diff->addedChildren)
  84. {
  85. BinarySerializer bs;
  86. SPtr<SceneObject> sceneObject = std::static_pointer_cast<SceneObject>(bs._decodeIntermediate(addedChildData));
  87. sceneObject->setParent(object);
  88. sceneObject->instantiate();
  89. }
  90. for (auto& componentDiff : diff->componentDiffs)
  91. {
  92. for (auto& component : components)
  93. {
  94. if (componentDiff->id == component->getLinkId())
  95. {
  96. IDiff& diffHandler = component->getRTTI()->getDiffHandler();
  97. diffHandler.applyDiff(component.getInternalPtr(), componentDiff->data);
  98. break;
  99. }
  100. }
  101. }
  102. for (auto& childDiff : diff->childDiffs)
  103. {
  104. UINT32 childCount = object->getNumChildren();
  105. for (UINT32 i = 0; i < childCount; i++)
  106. {
  107. HSceneObject child = object->getChild(i);
  108. if (childDiff->id == child->getLinkId())
  109. {
  110. applyDiff(childDiff, child);
  111. break;
  112. }
  113. }
  114. }
  115. }
  116. SPtr<PrefabObjectDiff> PrefabDiff::generateDiff(const HSceneObject& prefab, const HSceneObject& instance)
  117. {
  118. SPtr<PrefabObjectDiff> output;
  119. if (prefab->getName() != instance->getName())
  120. {
  121. if (output == nullptr)
  122. output = bs_shared_ptr_new<PrefabObjectDiff>();
  123. }
  124. UINT32 prefabChildCount = prefab->getNumChildren();
  125. UINT32 instanceChildCount = instance->getNumChildren();
  126. // Find modified and removed children
  127. for (UINT32 i = 0; i < prefabChildCount; i++)
  128. {
  129. HSceneObject prefabChild = prefab->getChild(i);
  130. SPtr<PrefabObjectDiff> childDiff;
  131. bool foundMatching = false;
  132. for (UINT32 j = 0; j < instanceChildCount; j++)
  133. {
  134. HSceneObject instanceChild = instance->getChild(j);
  135. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  136. {
  137. if (instanceChild->mPrefabLinkUUID.empty())
  138. childDiff = generateDiff(prefabChild, instanceChild);
  139. foundMatching = true;
  140. break;
  141. }
  142. }
  143. if (foundMatching)
  144. {
  145. if (childDiff != nullptr)
  146. {
  147. if (output == nullptr)
  148. output = bs_shared_ptr_new<PrefabObjectDiff>();
  149. output->childDiffs.push_back(childDiff);
  150. }
  151. }
  152. else
  153. {
  154. if (output == nullptr)
  155. output = bs_shared_ptr_new<PrefabObjectDiff>();
  156. output->removedChildren.push_back(prefabChild->getLinkId());
  157. }
  158. }
  159. // Find added children
  160. for (UINT32 i = 0; i < instanceChildCount; i++)
  161. {
  162. HSceneObject instanceChild = instance->getChild(i);
  163. if (instanceChild->hasFlag(SOF_DontSave))
  164. continue;
  165. bool foundMatching = false;
  166. if (instanceChild->getLinkId() != -1)
  167. {
  168. for (UINT32 j = 0; j < prefabChildCount; j++)
  169. {
  170. HSceneObject prefabChild = prefab->getChild(j);
  171. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  172. {
  173. foundMatching = true;
  174. break;
  175. }
  176. }
  177. }
  178. if (!foundMatching)
  179. {
  180. BinarySerializer bs;
  181. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceChild.get());
  182. if (output == nullptr)
  183. output = bs_shared_ptr_new<PrefabObjectDiff>();
  184. output->addedChildren.push_back(obj);
  185. }
  186. }
  187. const Vector<HComponent>& prefabComponents = prefab->getComponents();
  188. const Vector<HComponent>& instanceComponents = instance->getComponents();
  189. UINT32 prefabComponentCount = (UINT32)prefabComponents.size();
  190. UINT32 instanceComponentCount = (UINT32)instanceComponents.size();
  191. // Find modified and removed components
  192. for (UINT32 i = 0; i < prefabComponentCount; i++)
  193. {
  194. HComponent prefabComponent = prefabComponents[i];
  195. SPtr<PrefabComponentDiff> childDiff;
  196. bool foundMatching = false;
  197. for (UINT32 j = 0; j < instanceComponentCount; j++)
  198. {
  199. HComponent instanceComponent = instanceComponents[j];
  200. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  201. {
  202. BinarySerializer bs;
  203. SPtr<SerializedObject> encodedPrefab = bs._encodeIntermediate(prefabComponent.get());
  204. SPtr<SerializedObject> encodedInstance = bs._encodeIntermediate(instanceComponent.get());
  205. IDiff& diffHandler = prefabComponent->getRTTI()->getDiffHandler();
  206. SPtr<SerializedObject> diff = diffHandler.generateDiff(encodedPrefab, encodedInstance);
  207. if (diff != nullptr)
  208. {
  209. childDiff = bs_shared_ptr_new<PrefabComponentDiff>();
  210. childDiff->id = prefabComponent->getLinkId();
  211. childDiff->data = diff;
  212. }
  213. foundMatching = true;
  214. break;
  215. }
  216. }
  217. if (foundMatching)
  218. {
  219. if (childDiff != nullptr)
  220. {
  221. if (output == nullptr)
  222. output = bs_shared_ptr_new<PrefabObjectDiff>();
  223. output->componentDiffs.push_back(childDiff);
  224. }
  225. }
  226. else
  227. {
  228. if (output == nullptr)
  229. output = bs_shared_ptr_new<PrefabObjectDiff>();
  230. output->removedComponents.push_back(prefabComponent->getLinkId());
  231. }
  232. }
  233. // Find added components
  234. for (UINT32 i = 0; i < instanceComponentCount; i++)
  235. {
  236. HComponent instanceComponent = instanceComponents[i];
  237. bool foundMatching = false;
  238. if (instanceComponent->getLinkId() != -1)
  239. {
  240. for (UINT32 j = 0; j < prefabComponentCount; j++)
  241. {
  242. HComponent prefabComponent = prefabComponents[j];
  243. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  244. {
  245. foundMatching = true;
  246. break;
  247. }
  248. }
  249. }
  250. if (!foundMatching)
  251. {
  252. BinarySerializer bs;
  253. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceComponent.get());
  254. if (output == nullptr)
  255. output = bs_shared_ptr_new<PrefabObjectDiff>();
  256. output->addedComponents.push_back(obj);
  257. }
  258. }
  259. if (output != nullptr)
  260. {
  261. output->name = instance->getName();
  262. output->id = instance->getLinkId();
  263. }
  264. return output;
  265. }
  266. void PrefabDiff::renameInstanceIds(const HSceneObject& prefab, const HSceneObject& instance, Vector<RenamedGameObject>& output)
  267. {
  268. UnorderedMap<String, UnorderedMap<UINT32, UINT64>> linkToInstanceId;
  269. struct StackEntry
  270. {
  271. HSceneObject so;
  272. String uuid;
  273. };
  274. Stack<StackEntry> todo;
  275. todo.push({ prefab, "root" });
  276. while (!todo.empty())
  277. {
  278. StackEntry current = todo.top();
  279. todo.pop();
  280. String childParentUUID;
  281. if (current.so->mPrefabLinkUUID.empty())
  282. childParentUUID = current.uuid;
  283. else
  284. childParentUUID = current.so->mPrefabLinkUUID;
  285. UnorderedMap<UINT32, UINT64>& idMap = linkToInstanceId[childParentUUID];
  286. const Vector<HComponent>& components = current.so->getComponents();
  287. for (auto& component : components)
  288. idMap[component->getLinkId()] = component->getInstanceId();
  289. UINT32 numChildren = current.so->getNumChildren();
  290. for (UINT32 i = 0; i < numChildren; i++)
  291. {
  292. HSceneObject child = current.so->getChild(i);
  293. idMap[child->getLinkId()] = child->getInstanceId();
  294. todo.push({ child, childParentUUID });
  295. }
  296. }
  297. // Root has link ID from its parent so we handle it separately
  298. {
  299. output.push_back(RenamedGameObject());
  300. RenamedGameObject& renamedGO = output.back();
  301. renamedGO.instanceData = instance->mInstanceData;
  302. renamedGO.originalId = instance->getInstanceId();
  303. instance->mInstanceData->mInstanceId = prefab->getInstanceId();
  304. }
  305. todo.push({ instance, "root" });
  306. while (!todo.empty())
  307. {
  308. StackEntry current = todo.top();
  309. todo.pop();
  310. String childParentUUID;
  311. if (current.so->mPrefabLinkUUID.empty())
  312. childParentUUID = current.uuid;
  313. else
  314. childParentUUID = current.so->mPrefabLinkUUID;
  315. auto iterFind = linkToInstanceId.find(childParentUUID);
  316. if (iterFind != linkToInstanceId.end())
  317. {
  318. UnorderedMap<UINT32, UINT64>& idMap = iterFind->second;
  319. const Vector<HComponent>& components = current.so->getComponents();
  320. for (auto& component : components)
  321. {
  322. auto iterFind2 = idMap.find(component->getLinkId());
  323. if (iterFind2 != idMap.end())
  324. {
  325. output.push_back(RenamedGameObject());
  326. RenamedGameObject& renamedGO = output.back();
  327. renamedGO.instanceData = component->mInstanceData;
  328. renamedGO.originalId = component->getInstanceId();
  329. component->mInstanceData->mInstanceId = iterFind2->second;
  330. }
  331. }
  332. }
  333. UINT32 numChildren = current.so->getNumChildren();
  334. for (UINT32 i = 0; i < numChildren; i++)
  335. {
  336. HSceneObject child = current.so->getChild(i);
  337. if (iterFind != linkToInstanceId.end())
  338. {
  339. if (current.so->getLinkId() != -1)
  340. {
  341. UnorderedMap<UINT32, UINT64>& idMap = iterFind->second;
  342. auto iterFind2 = idMap.find(current.so->getLinkId());
  343. if (iterFind2 != idMap.end())
  344. {
  345. output.push_back(RenamedGameObject());
  346. RenamedGameObject& renamedGO = output.back();
  347. renamedGO.instanceData = current.so->mInstanceData;
  348. renamedGO.originalId = current.so->getInstanceId();
  349. current.so->mInstanceData->mInstanceId = iterFind2->second;
  350. }
  351. }
  352. }
  353. todo.push({ child, childParentUUID });
  354. }
  355. }
  356. }
  357. void PrefabDiff::restoreInstanceIds(const Vector<RenamedGameObject>& renamedObjects)
  358. {
  359. for (auto& renamedGO : renamedObjects)
  360. renamedGO.instanceData->mInstanceId = renamedGO.originalId;
  361. }
  362. RTTITypeBase* PrefabDiff::getRTTIStatic()
  363. {
  364. return PrefabDiffRTTI::instance();
  365. }
  366. RTTITypeBase* PrefabDiff::getRTTI() const
  367. {
  368. return PrefabDiff::getRTTIStatic();
  369. }
  370. }