BsPrefabDiff.cpp 10 KB

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