BsPrefabUtility.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsPrefabUtility.h"
  4. #include "BsPrefabDiff.h"
  5. #include "BsPrefab.h"
  6. #include "BsSceneObject.h"
  7. #include "BsResources.h"
  8. namespace BansheeEngine
  9. {
  10. void PrefabUtility::revertToPrefab(const HSceneObject& so)
  11. {
  12. String prefabLinkUUID = so->getPrefabLink();
  13. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(prefabLinkUUID, false, ResourceLoadFlag::None));
  14. if (!prefabLink.isLoaded(false))
  15. return;
  16. // Save IDs, destroy original, create new, restore IDs
  17. SceneObjectProxy soProxy;
  18. UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
  19. recordInstanceData(so, soProxy, linkedInstanceData);
  20. HSceneObject parent = so->getParent();
  21. // This will destroy the object but keep it in the parent's child list
  22. HSceneObject currentSO = so;
  23. so->destroyInternal(currentSO, true);
  24. HSceneObject newInstance = prefabLink->instantiate();
  25. // Remove default parent, and replace with original one
  26. newInstance->mParent->removeChild(newInstance);
  27. newInstance->mParent = parent;
  28. restoreLinkedInstanceData(newInstance, soProxy, linkedInstanceData);
  29. }
  30. void PrefabUtility::updateFromPrefab(const HSceneObject& so)
  31. {
  32. HSceneObject topLevelObject = so;
  33. while (topLevelObject != nullptr)
  34. {
  35. if (!topLevelObject->mPrefabLinkUUID.empty())
  36. break;
  37. if (topLevelObject->mParent != nullptr)
  38. topLevelObject = topLevelObject->mParent;
  39. else
  40. topLevelObject = nullptr;
  41. }
  42. if (topLevelObject == nullptr)
  43. topLevelObject = so;
  44. Stack<HSceneObject> todo;
  45. todo.push(topLevelObject);
  46. // Find any prefab instances
  47. Vector<HSceneObject> prefabInstanceRoots;
  48. while (!todo.empty())
  49. {
  50. HSceneObject current = todo.top();
  51. todo.pop();
  52. if (!current->mPrefabLinkUUID.empty())
  53. prefabInstanceRoots.push_back(current);
  54. UINT32 childCount = current->getNumChildren();
  55. for (UINT32 i = 0; i < childCount; i++)
  56. {
  57. HSceneObject child = current->getChild(i);
  58. todo.push(child);
  59. }
  60. }
  61. // Stores data about the new prefab instance and its original parent and link id
  62. // (as those aren't stored in the prefab diff)
  63. struct RestoredPrefabInstance
  64. {
  65. HSceneObject newInstance;
  66. HSceneObject originalParent;
  67. SPtr<PrefabDiff> diff;
  68. UINT32 originalLinkId;
  69. };
  70. Vector<RestoredPrefabInstance> newPrefabInstanceData;
  71. // For each prefab instance load its reference prefab from the disk and check if it changed. If it has changed
  72. // instantiate the prefab and destroy the current instance. Then apply instance specific changes stored in a
  73. // prefab diff, if any, as well as restore the original parent and link id (link id of the root prefab instance
  74. // belongs to the parent prefab if any). Finally fix any handles pointing to the old objects so that they now point
  75. // to the newly instantiated objects. To the outside world it should be transparent that we just destroyed and then
  76. // re-created from scratch the entire hierarchy.
  77. // Need to do this bottom up to ensure I don't destroy the parents before children
  78. for (auto iter = prefabInstanceRoots.rbegin(); iter != prefabInstanceRoots.rend(); ++iter)
  79. {
  80. HSceneObject current = *iter;
  81. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, ResourceLoadFlag::None));
  82. if (prefabLink.isLoaded(false) && prefabLink->getHash() != current->mPrefabHash)
  83. {
  84. // Save IDs, destroy original, create new, restore IDs
  85. SceneObjectProxy soProxy;
  86. UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
  87. recordInstanceData(current, soProxy, linkedInstanceData);
  88. HSceneObject parent = current->getParent();
  89. SPtr<PrefabDiff> prefabDiff = current->mPrefabDiff;
  90. current->destroy(true);
  91. HSceneObject newInstance = prefabLink->_clone();
  92. // When restoring instance IDs it is important to make all the new handles point to the old GameObjectInstanceData.
  93. // This is because old handles will have different GameObjectHandleData and we have no easy way of accessing it to
  94. // change to which GameObjectInstanceData it points. But the GameObjectManager ensures that all handles deserialized
  95. // at once (i.e. during the ::_clone() call above) will share GameObjectHandleData so we can simply replace
  96. // to what they point to, affecting all of the handles to that object. (In another words, we can modify the
  97. // new handles at this point, but old ones must keep referencing what they already were.)
  98. restoreLinkedInstanceData(newInstance, soProxy, linkedInstanceData);
  99. restoreUnlinkedInstanceData(newInstance, soProxy);
  100. newPrefabInstanceData.push_back({ newInstance, parent, prefabDiff, newInstance->getLinkId() });
  101. }
  102. }
  103. // Once everything is cloned, apply diffs, restore old parents & link IDs for root.
  104. for (auto& entry : newPrefabInstanceData)
  105. {
  106. // Diffs must be applied after everything is instantiated and instance data restored since it may contain
  107. // game object handles within or external to its prefab instance.
  108. if (entry.diff != nullptr)
  109. entry.diff->apply(entry.newInstance);
  110. entry.newInstance->mPrefabDiff = entry.diff;
  111. entry.newInstance->setParent(entry.originalParent, false);
  112. entry.newInstance->mLinkId = entry.originalLinkId;
  113. }
  114. // Finally, instantiate everything
  115. for (auto& entry : newPrefabInstanceData)
  116. entry.newInstance->_instantiate(true);
  117. gResources().unloadAllUnused();
  118. }
  119. void PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject)
  120. {
  121. UINT32 startingId = 0;
  122. Stack<HSceneObject> todo;
  123. todo.push(sceneObject);
  124. while (!todo.empty())
  125. {
  126. HSceneObject currentSO = todo.top();
  127. todo.pop();
  128. for (auto& component : currentSO->mComponents)
  129. {
  130. if (component->getLinkId() != (UINT32)-1)
  131. startingId = std::max(component->mLinkId + 1, startingId);
  132. }
  133. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  134. for (UINT32 i = 0; i < numChildren; i++)
  135. {
  136. HSceneObject child = currentSO->getChild(i);
  137. if (!child->hasFlag(SOF_DontSave))
  138. {
  139. if (child->getLinkId() != (UINT32)-1)
  140. startingId = std::max(child->mLinkId + 1, startingId);
  141. if (child->mPrefabLinkUUID.empty())
  142. todo.push(currentSO->getChild(i));
  143. }
  144. }
  145. }
  146. UINT32 currentId = startingId;
  147. todo.push(sceneObject);
  148. while (!todo.empty())
  149. {
  150. HSceneObject currentSO = todo.top();
  151. todo.pop();
  152. for (auto& component : currentSO->mComponents)
  153. {
  154. if (component->getLinkId() == (UINT32)-1)
  155. component->mLinkId = currentId++;
  156. }
  157. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  158. for (UINT32 i = 0; i < numChildren; i++)
  159. {
  160. HSceneObject child = currentSO->getChild(i);
  161. if (!child->hasFlag(SOF_DontSave))
  162. {
  163. if (child->getLinkId() == (UINT32)-1)
  164. child->mLinkId = currentId++;
  165. if(child->mPrefabLinkUUID.empty())
  166. todo.push(currentSO->getChild(i));
  167. }
  168. }
  169. }
  170. if (currentId < startingId)
  171. {
  172. BS_EXCEPT(InternalErrorException, "Prefab ran out of IDs to assign. " \
  173. "Consider increasing the size of the prefab ID data type.");
  174. }
  175. }
  176. void PrefabUtility::clearPrefabIds(const HSceneObject& sceneObject, bool recursive, bool clearRoot)
  177. {
  178. Stack<HSceneObject> todo;
  179. todo.push(sceneObject);
  180. if (clearRoot)
  181. sceneObject->mLinkId = (UINT32)-1;
  182. while (!todo.empty())
  183. {
  184. HSceneObject currentSO = todo.top();
  185. todo.pop();
  186. for (auto& component : currentSO->mComponents)
  187. component->mLinkId = (UINT32)-1;
  188. if (recursive)
  189. {
  190. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  191. for (UINT32 i = 0; i < numChildren; i++)
  192. {
  193. HSceneObject child = currentSO->getChild(i);
  194. child->mLinkId = (UINT32)-1;
  195. if (child->mPrefabLinkUUID.empty())
  196. todo.push(child);
  197. }
  198. }
  199. }
  200. }
  201. void PrefabUtility::recordPrefabDiff(const HSceneObject& sceneObject)
  202. {
  203. HSceneObject topLevelObject = sceneObject;
  204. while (topLevelObject != nullptr)
  205. {
  206. if (!topLevelObject->mPrefabLinkUUID.empty())
  207. break;
  208. if (topLevelObject->mParent != nullptr)
  209. topLevelObject = topLevelObject->mParent;
  210. else
  211. topLevelObject = nullptr;
  212. }
  213. if (topLevelObject == nullptr)
  214. topLevelObject = sceneObject;
  215. Stack<HSceneObject> todo;
  216. todo.push(topLevelObject);
  217. while (!todo.empty())
  218. {
  219. HSceneObject current = todo.top();
  220. todo.pop();
  221. if (!current->mPrefabLinkUUID.empty())
  222. {
  223. current->mPrefabDiff = nullptr;
  224. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, ResourceLoadFlag::None));
  225. if (prefabLink.isLoaded(false))
  226. current->mPrefabDiff = PrefabDiff::create(prefabLink->_getRoot(), current->getHandle());
  227. }
  228. UINT32 childCount = current->getNumChildren();
  229. for (UINT32 i = 0; i < childCount; i++)
  230. {
  231. HSceneObject child = current->getChild(i);
  232. todo.push(child);
  233. }
  234. }
  235. gResources().unloadAllUnused();
  236. }
  237. void PrefabUtility::recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  238. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  239. {
  240. struct StackData
  241. {
  242. HSceneObject so;
  243. SceneObjectProxy* proxy;
  244. };
  245. Stack<StackData> todo;
  246. todo.push({so, &output});
  247. output.instanceData = so->_getInstanceData();
  248. output.linkId = (UINT32)-1;
  249. while (!todo.empty())
  250. {
  251. StackData curData = todo.top();
  252. todo.pop();
  253. const Vector<HComponent>& components = curData.so->getComponents();
  254. for (auto& component : components)
  255. {
  256. curData.proxy->components.push_back(ComponentProxy());
  257. ComponentProxy& componentProxy = curData.proxy->components.back();
  258. componentProxy.instanceData = component->_getInstanceData();
  259. componentProxy.linkId = component->getLinkId();
  260. linkedInstanceData[componentProxy.linkId] = componentProxy.instanceData;
  261. }
  262. UINT32 numChildren = curData.so->getNumChildren();
  263. curData.proxy->children.resize(numChildren);
  264. for (UINT32 i = 0; i < numChildren; i++)
  265. {
  266. HSceneObject child = curData.so->getChild(i);
  267. SceneObjectProxy& childProxy = curData.proxy->children[i];
  268. childProxy.instanceData = child->_getInstanceData();
  269. childProxy.linkId = child->getLinkId();
  270. linkedInstanceData[childProxy.linkId] = childProxy.instanceData;
  271. if (child->mPrefabLinkUUID.empty())
  272. {
  273. todo.push({ child, &curData.proxy->children[i] });
  274. }
  275. }
  276. }
  277. }
  278. void PrefabUtility::restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  279. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  280. {
  281. Stack<HSceneObject> todo;
  282. todo.push(so);
  283. while (!todo.empty())
  284. {
  285. HSceneObject current = todo.top();
  286. todo.pop();
  287. Vector<HComponent>& components = current->mComponents;
  288. for (auto& component : components)
  289. {
  290. if (component->getLinkId() != (UINT32)-1)
  291. {
  292. auto iterFind = linkedInstanceData.find(component->getLinkId());
  293. if (iterFind != linkedInstanceData.end())
  294. {
  295. component->_setInstanceData(iterFind->second);
  296. component._setHandleData(component.getInternalPtr());
  297. }
  298. }
  299. }
  300. UINT32 numChildren = current->getNumChildren();
  301. for (UINT32 i = 0; i < numChildren; i++)
  302. {
  303. HSceneObject child = current->getChild(i);
  304. if (child->getLinkId() != (UINT32)-1)
  305. {
  306. auto iterFind = linkedInstanceData.find(child->getLinkId());
  307. if (iterFind != linkedInstanceData.end())
  308. child->_setInstanceData(iterFind->second);
  309. }
  310. if (child->mPrefabLinkUUID.empty())
  311. todo.push(child);
  312. }
  313. }
  314. }
  315. void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
  316. {
  317. struct StackEntry
  318. {
  319. HSceneObject so;
  320. SceneObjectProxy* proxy;
  321. };
  322. Stack<StackEntry> todo;
  323. todo.push(StackEntry());
  324. StackEntry& topEntry = todo.top();
  325. topEntry.so = so;
  326. topEntry.proxy = &proxy;
  327. while (!todo.empty())
  328. {
  329. StackEntry current = todo.top();
  330. todo.pop();
  331. if (current.proxy->linkId == -1)
  332. current.so->_setInstanceData(current.proxy->instanceData);
  333. Vector<HComponent>& components = current.so->mComponents;
  334. UINT32 componentProxyIdx = 0;
  335. UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
  336. for (auto& component : components)
  337. {
  338. if (component->getLinkId() == (UINT32)-1)
  339. {
  340. bool foundInstanceData = false;
  341. for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
  342. {
  343. if (current.proxy->components[componentProxyIdx].linkId != -1)
  344. continue;
  345. component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
  346. component._setHandleData(component.getInternalPtr());
  347. foundInstanceData = true;
  348. break;
  349. }
  350. assert(foundInstanceData);
  351. }
  352. }
  353. UINT32 numChildren = current.so->getNumChildren();
  354. UINT32 childProxyIdx = 0;
  355. UINT32 numChildProxies = (UINT32)current.proxy->children.size();
  356. for (UINT32 i = 0; i < numChildren; i++)
  357. {
  358. HSceneObject child = current.so->getChild(i);
  359. if (child->getLinkId() == (UINT32)-1)
  360. {
  361. bool foundInstanceData = false;
  362. for (; childProxyIdx < numChildProxies; childProxyIdx++)
  363. {
  364. if (current.proxy->children[childProxyIdx].linkId != -1)
  365. continue;
  366. assert(current.proxy->children[childProxyIdx].linkId == -1);
  367. child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
  368. if (child->mPrefabLinkUUID.empty())
  369. {
  370. todo.push(StackEntry());
  371. StackEntry& newEntry = todo.top();
  372. newEntry.so = child;
  373. newEntry.proxy = &current.proxy->children[childProxyIdx];
  374. }
  375. foundInstanceData = true;
  376. break;
  377. }
  378. assert(foundInstanceData);
  379. }
  380. else
  381. {
  382. if (!child->mPrefabLinkUUID.empty())
  383. continue;
  384. for (UINT32 j = 0; j < numChildProxies; j++)
  385. {
  386. if (child->getLinkId() == current.proxy->children[j].linkId)
  387. {
  388. todo.push(StackEntry());
  389. StackEntry& newEntry = todo.top();
  390. newEntry.so = child;
  391. newEntry.proxy = &current.proxy->children[j];
  392. break;
  393. }
  394. }
  395. }
  396. }
  397. }
  398. }
  399. }