2
0

BsPrefabDiff.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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->getPrefabLink() != instance->getPrefabLink() || prefab->getLinkId() != instance->getLinkId())
  28. return nullptr;
  29. Vector<RenamedGameObject> renamedObjects;
  30. renameInstanceIds(prefab, instance, renamedObjects);
  31. SPtr<PrefabDiff> output = bs_shared_ptr_new<PrefabDiff>();
  32. output->mRoot = generateDiff(prefab, instance);
  33. restoreInstanceIds(renamedObjects);
  34. return output;
  35. }
  36. void PrefabDiff::apply(const HSceneObject& object)
  37. {
  38. if (mRoot == nullptr)
  39. return;
  40. GameObjectManager::instance().startDeserialization();
  41. applyDiff(mRoot, object);
  42. GameObjectManager::instance().endDeserialization();
  43. }
  44. void PrefabDiff::applyDiff(const SPtr<PrefabObjectDiff>& diff, const HSceneObject& object)
  45. {
  46. if (diff->id != object->getLinkId())
  47. return;
  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. childDiff = generateDiff(prefabChild, instanceChild);
  138. foundMatching = true;
  139. break;
  140. }
  141. }
  142. if (foundMatching)
  143. {
  144. if (childDiff != nullptr)
  145. {
  146. if (output == nullptr)
  147. output = bs_shared_ptr_new<PrefabObjectDiff>();
  148. output->childDiffs.push_back(childDiff);
  149. }
  150. }
  151. else
  152. {
  153. if (output == nullptr)
  154. output = bs_shared_ptr_new<PrefabObjectDiff>();
  155. output->removedChildren.push_back(prefabChild->getLinkId());
  156. }
  157. }
  158. // Find added children
  159. for (UINT32 i = 0; i < instanceChildCount; i++)
  160. {
  161. HSceneObject instanceChild = instance->getChild(i);
  162. bool foundMatching = false;
  163. if (instanceChild->getLinkId() != -1)
  164. {
  165. for (UINT32 j = 0; j < prefabChildCount; j++)
  166. {
  167. HSceneObject prefabChild = prefab->getChild(j);
  168. if (prefabChild->getLinkId() == instanceChild->getLinkId())
  169. {
  170. foundMatching = true;
  171. break;
  172. }
  173. }
  174. }
  175. if (!foundMatching)
  176. {
  177. BinarySerializer bs;
  178. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceChild.get());
  179. if (output == nullptr)
  180. output = bs_shared_ptr_new<PrefabObjectDiff>();
  181. output->addedChildren.push_back(obj);
  182. }
  183. }
  184. const Vector<HComponent>& prefabComponents = prefab->getComponents();
  185. const Vector<HComponent>& instanceComponents = instance->getComponents();
  186. UINT32 prefabComponentCount = (UINT32)prefabComponents.size();
  187. UINT32 instanceComponentCount = (UINT32)instanceComponents.size();
  188. // Find modified and removed components
  189. for (UINT32 i = 0; i < prefabComponentCount; i++)
  190. {
  191. HComponent prefabComponent = prefabComponents[i];
  192. SPtr<PrefabComponentDiff> childDiff;
  193. bool foundMatching = false;
  194. for (UINT32 j = 0; j < instanceComponentCount; j++)
  195. {
  196. HComponent instanceComponent = instanceComponents[j];
  197. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  198. {
  199. BinarySerializer bs;
  200. SPtr<SerializedObject> encodedPrefab = bs._encodeIntermediate(prefabComponent.get());
  201. SPtr<SerializedObject> encodedInstance = bs._encodeIntermediate(instanceComponent.get());
  202. IDiff& diffHandler = prefabComponent->getRTTI()->getDiffHandler();
  203. SPtr<SerializedObject> diff = diffHandler.generateDiff(encodedPrefab, encodedInstance);
  204. if (diff != nullptr)
  205. {
  206. childDiff = bs_shared_ptr_new<PrefabComponentDiff>();
  207. childDiff->id = prefabComponent->getLinkId();
  208. childDiff->data = diff;
  209. }
  210. foundMatching = true;
  211. break;
  212. }
  213. }
  214. if (foundMatching)
  215. {
  216. if (childDiff != nullptr)
  217. {
  218. if (output == nullptr)
  219. output = bs_shared_ptr_new<PrefabObjectDiff>();
  220. output->componentDiffs.push_back(childDiff);
  221. }
  222. }
  223. else
  224. {
  225. if (output == nullptr)
  226. output = bs_shared_ptr_new<PrefabObjectDiff>();
  227. output->removedComponents.push_back(prefabComponent->getLinkId());
  228. }
  229. }
  230. // Find added components
  231. for (UINT32 i = 0; i < instanceComponentCount; i++)
  232. {
  233. HComponent instanceComponent = instanceComponents[i];
  234. bool foundMatching = false;
  235. if (instanceComponent->getLinkId() != -1)
  236. {
  237. for (UINT32 j = 0; j < prefabComponentCount; j++)
  238. {
  239. HComponent prefabComponent = prefabComponents[j];
  240. if (prefabComponent->getLinkId() == instanceComponent->getLinkId())
  241. {
  242. foundMatching = true;
  243. break;
  244. }
  245. }
  246. }
  247. if (!foundMatching)
  248. {
  249. BinarySerializer bs;
  250. SPtr<SerializedObject> obj = bs._encodeIntermediate(instanceComponent.get());
  251. if (output == nullptr)
  252. output = bs_shared_ptr_new<PrefabObjectDiff>();
  253. output->addedComponents.push_back(obj);
  254. }
  255. }
  256. if (output != nullptr)
  257. {
  258. output->name = instance->getName();
  259. output->id = instance->getLinkId();
  260. }
  261. return output;
  262. }
  263. void PrefabDiff::renameInstanceIds(const HSceneObject& prefab, const HSceneObject& instance, Vector<RenamedGameObject>& output)
  264. {
  265. UnorderedMap<UINT32, UINT64> linkToInstanceId;
  266. Stack<HSceneObject> todo;
  267. todo.push(prefab);
  268. while (!todo.empty())
  269. {
  270. HSceneObject current = todo.top();
  271. todo.pop();
  272. linkToInstanceId[current->getLinkId()] = current->getInstanceId();
  273. const Vector<HComponent>& components = current->getComponents();
  274. for (auto& component : components)
  275. linkToInstanceId[component->getLinkId()] = component->getInstanceId();
  276. UINT32 numChildren = current->getNumChildren();
  277. for (UINT32 i = 0; i < numChildren; i++)
  278. {
  279. HSceneObject child = current->getChild(i);
  280. if (child->mPrefabLinkUUID.empty())
  281. todo.push(child);
  282. }
  283. }
  284. todo.push(instance);
  285. while (!todo.empty())
  286. {
  287. HSceneObject current = todo.top();
  288. todo.pop();
  289. if (current->getLinkId() != -1)
  290. {
  291. auto iterFind = linkToInstanceId.find(current->getLinkId());
  292. if (iterFind != linkToInstanceId.end())
  293. {
  294. output.push_back(RenamedGameObject());
  295. RenamedGameObject& renamedGO = output.back();
  296. renamedGO.instanceData = current->mInstanceData;
  297. renamedGO.originalId = current->getInstanceId();
  298. current->mInstanceData->mInstanceId = iterFind->second;
  299. }
  300. }
  301. const Vector<HComponent>& components = current->getComponents();
  302. for (auto& component : components)
  303. {
  304. auto iterFind = linkToInstanceId.find(component->getLinkId());
  305. if (iterFind != linkToInstanceId.end())
  306. {
  307. output.push_back(RenamedGameObject());
  308. RenamedGameObject& renamedGO = output.back();
  309. renamedGO.instanceData = component->mInstanceData;
  310. renamedGO.originalId = component->getInstanceId();
  311. component->mInstanceData->mInstanceId = iterFind->second;
  312. }
  313. }
  314. UINT32 numChildren = current->getNumChildren();
  315. for (UINT32 i = 0; i < numChildren; i++)
  316. {
  317. HSceneObject child = current->getChild(i);
  318. if (child->mPrefabLinkUUID.empty())
  319. todo.push(child);
  320. }
  321. }
  322. }
  323. void PrefabDiff::restoreInstanceIds(const Vector<RenamedGameObject>& renamedObjects)
  324. {
  325. for (auto& renamedGO : renamedObjects)
  326. renamedGO.instanceData->mInstanceId = renamedGO.originalId;
  327. }
  328. RTTITypeBase* PrefabDiff::getRTTIStatic()
  329. {
  330. return PrefabDiffRTTI::instance();
  331. }
  332. RTTITypeBase* PrefabDiff::getRTTI() const
  333. {
  334. return PrefabDiff::getRTTIStatic();
  335. }
  336. }