BsPrefabDiff.cpp 12 KB

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