BsPrefabUtility.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "Scene/BsPrefabUtility.h"
  4. #include "Scene/BsPrefabDiff.h"
  5. #include "Scene/BsPrefab.h"
  6. #include "Scene/BsSceneObject.h"
  7. #include "Resources/BsResources.h"
  8. namespace bs
  9. {
  10. void PrefabUtility::revertToPrefab(const HSceneObject& so)
  11. {
  12. UUID 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 if the top scene object is live (instantiated)
  115. if (topLevelObject->isInstantiated())
  116. {
  117. for (auto& entry : newPrefabInstanceData)
  118. entry.newInstance->_instantiate(true);
  119. }
  120. gResources().unloadAllUnused();
  121. }
  122. void PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject)
  123. {
  124. UINT32 startingId = 0;
  125. Stack<HSceneObject> todo;
  126. todo.push(sceneObject);
  127. while (!todo.empty())
  128. {
  129. HSceneObject currentSO = todo.top();
  130. todo.pop();
  131. for (auto& component : currentSO->mComponents)
  132. {
  133. if (component->getLinkId() != (UINT32)-1)
  134. startingId = std::max(component->mLinkId + 1, startingId);
  135. }
  136. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  137. for (UINT32 i = 0; i < numChildren; i++)
  138. {
  139. HSceneObject child = currentSO->getChild(i);
  140. if (!child->hasFlag(SOF_DontSave))
  141. {
  142. if (child->getLinkId() != (UINT32)-1)
  143. startingId = std::max(child->mLinkId + 1, startingId);
  144. if (child->mPrefabLinkUUID.empty())
  145. todo.push(currentSO->getChild(i));
  146. }
  147. }
  148. }
  149. UINT32 currentId = startingId;
  150. todo.push(sceneObject);
  151. while (!todo.empty())
  152. {
  153. HSceneObject currentSO = todo.top();
  154. todo.pop();
  155. for (auto& component : currentSO->mComponents)
  156. {
  157. if (component->getLinkId() == (UINT32)-1)
  158. component->mLinkId = currentId++;
  159. }
  160. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  161. for (UINT32 i = 0; i < numChildren; i++)
  162. {
  163. HSceneObject child = currentSO->getChild(i);
  164. if (!child->hasFlag(SOF_DontSave))
  165. {
  166. if (child->getLinkId() == (UINT32)-1)
  167. child->mLinkId = currentId++;
  168. if(child->mPrefabLinkUUID.empty())
  169. todo.push(currentSO->getChild(i));
  170. }
  171. }
  172. }
  173. if (currentId < startingId)
  174. {
  175. BS_EXCEPT(InternalErrorException, "Prefab ran out of IDs to assign. " \
  176. "Consider increasing the size of the prefab ID data type.");
  177. }
  178. }
  179. void PrefabUtility::clearPrefabIds(const HSceneObject& sceneObject, bool recursive, bool clearRoot)
  180. {
  181. Stack<HSceneObject> todo;
  182. todo.push(sceneObject);
  183. if (clearRoot)
  184. sceneObject->mLinkId = (UINT32)-1;
  185. while (!todo.empty())
  186. {
  187. HSceneObject currentSO = todo.top();
  188. todo.pop();
  189. for (auto& component : currentSO->mComponents)
  190. component->mLinkId = (UINT32)-1;
  191. if (recursive)
  192. {
  193. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  194. for (UINT32 i = 0; i < numChildren; i++)
  195. {
  196. HSceneObject child = currentSO->getChild(i);
  197. child->mLinkId = (UINT32)-1;
  198. if (child->mPrefabLinkUUID.empty())
  199. todo.push(child);
  200. }
  201. }
  202. }
  203. }
  204. void PrefabUtility::recordPrefabDiff(const HSceneObject& sceneObject)
  205. {
  206. HSceneObject topLevelObject = sceneObject;
  207. while (topLevelObject != nullptr)
  208. {
  209. if (!topLevelObject->mPrefabLinkUUID.empty())
  210. break;
  211. if (topLevelObject->mParent != nullptr)
  212. topLevelObject = topLevelObject->mParent;
  213. else
  214. topLevelObject = nullptr;
  215. }
  216. if (topLevelObject == nullptr)
  217. topLevelObject = sceneObject;
  218. Stack<HSceneObject> todo;
  219. todo.push(topLevelObject);
  220. while (!todo.empty())
  221. {
  222. HSceneObject current = todo.top();
  223. todo.pop();
  224. if (!current->mPrefabLinkUUID.empty())
  225. {
  226. current->mPrefabDiff = nullptr;
  227. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, ResourceLoadFlag::None));
  228. if (prefabLink.isLoaded(false))
  229. current->mPrefabDiff = PrefabDiff::create(prefabLink->_getRoot(), current->getHandle());
  230. }
  231. UINT32 childCount = current->getNumChildren();
  232. for (UINT32 i = 0; i < childCount; i++)
  233. {
  234. HSceneObject child = current->getChild(i);
  235. todo.push(child);
  236. }
  237. }
  238. gResources().unloadAllUnused();
  239. }
  240. void PrefabUtility::recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  241. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  242. {
  243. struct StackData
  244. {
  245. HSceneObject so;
  246. SceneObjectProxy* proxy;
  247. };
  248. Stack<StackData> todo;
  249. todo.push({so, &output});
  250. output.instanceData = so->_getInstanceData();
  251. output.linkId = (UINT32)-1;
  252. while (!todo.empty())
  253. {
  254. StackData curData = todo.top();
  255. todo.pop();
  256. const Vector<HComponent>& components = curData.so->getComponents();
  257. for (auto& component : components)
  258. {
  259. curData.proxy->components.push_back(ComponentProxy());
  260. ComponentProxy& componentProxy = curData.proxy->components.back();
  261. componentProxy.instanceData = component->_getInstanceData();
  262. componentProxy.linkId = component->getLinkId();
  263. linkedInstanceData[componentProxy.linkId] = componentProxy.instanceData;
  264. }
  265. UINT32 numChildren = curData.so->getNumChildren();
  266. curData.proxy->children.resize(numChildren);
  267. for (UINT32 i = 0; i < numChildren; i++)
  268. {
  269. HSceneObject child = curData.so->getChild(i);
  270. SceneObjectProxy& childProxy = curData.proxy->children[i];
  271. childProxy.instanceData = child->_getInstanceData();
  272. childProxy.linkId = child->getLinkId();
  273. linkedInstanceData[childProxy.linkId] = childProxy.instanceData;
  274. if (child->mPrefabLinkUUID.empty())
  275. {
  276. todo.push({ child, &curData.proxy->children[i] });
  277. }
  278. }
  279. }
  280. }
  281. void PrefabUtility::restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  282. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  283. {
  284. Stack<HSceneObject> todo;
  285. todo.push(so);
  286. while (!todo.empty())
  287. {
  288. HSceneObject current = todo.top();
  289. todo.pop();
  290. Vector<HComponent>& components = current->mComponents;
  291. for (auto& component : components)
  292. {
  293. if (component->getLinkId() != (UINT32)-1)
  294. {
  295. auto iterFind = linkedInstanceData.find(component->getLinkId());
  296. if (iterFind != linkedInstanceData.end())
  297. {
  298. component->_setInstanceData(iterFind->second);
  299. component._setHandleData(component.getInternalPtr());
  300. }
  301. }
  302. }
  303. UINT32 numChildren = current->getNumChildren();
  304. for (UINT32 i = 0; i < numChildren; i++)
  305. {
  306. HSceneObject child = current->getChild(i);
  307. if (child->getLinkId() != (UINT32)-1)
  308. {
  309. auto iterFind = linkedInstanceData.find(child->getLinkId());
  310. if (iterFind != linkedInstanceData.end())
  311. child->_setInstanceData(iterFind->second);
  312. }
  313. if (child->mPrefabLinkUUID.empty())
  314. todo.push(child);
  315. }
  316. }
  317. }
  318. void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
  319. {
  320. struct StackEntry
  321. {
  322. HSceneObject so;
  323. SceneObjectProxy* proxy;
  324. };
  325. Stack<StackEntry> todo;
  326. todo.push(StackEntry());
  327. StackEntry& topEntry = todo.top();
  328. topEntry.so = so;
  329. topEntry.proxy = &proxy;
  330. while (!todo.empty())
  331. {
  332. StackEntry current = todo.top();
  333. todo.pop();
  334. if (current.proxy->linkId == (UINT32)-1)
  335. current.so->_setInstanceData(current.proxy->instanceData);
  336. Vector<HComponent>& components = current.so->mComponents;
  337. UINT32 componentProxyIdx = 0;
  338. UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
  339. for (auto& component : components)
  340. {
  341. if (component->getLinkId() == (UINT32)-1)
  342. {
  343. bool foundInstanceData = false;
  344. for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
  345. {
  346. if (current.proxy->components[componentProxyIdx].linkId != (UINT32)-1)
  347. continue;
  348. component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
  349. component._setHandleData(component.getInternalPtr());
  350. foundInstanceData = true;
  351. break;
  352. }
  353. assert(foundInstanceData);
  354. }
  355. }
  356. UINT32 numChildren = current.so->getNumChildren();
  357. UINT32 childProxyIdx = 0;
  358. UINT32 numChildProxies = (UINT32)current.proxy->children.size();
  359. for (UINT32 i = 0; i < numChildren; i++)
  360. {
  361. HSceneObject child = current.so->getChild(i);
  362. if (child->getLinkId() == (UINT32)-1)
  363. {
  364. bool foundInstanceData = false;
  365. for (; childProxyIdx < numChildProxies; childProxyIdx++)
  366. {
  367. if (current.proxy->children[childProxyIdx].linkId != (UINT32)-1)
  368. continue;
  369. assert(current.proxy->children[childProxyIdx].linkId == (UINT32)-1);
  370. child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
  371. if (child->mPrefabLinkUUID.empty())
  372. {
  373. todo.push(StackEntry());
  374. StackEntry& newEntry = todo.top();
  375. newEntry.so = child;
  376. newEntry.proxy = &current.proxy->children[childProxyIdx];
  377. }
  378. foundInstanceData = true;
  379. break;
  380. }
  381. assert(foundInstanceData);
  382. }
  383. else
  384. {
  385. if (!child->mPrefabLinkUUID.empty())
  386. continue;
  387. for (UINT32 j = 0; j < numChildProxies; j++)
  388. {
  389. if (child->getLinkId() == current.proxy->children[j].linkId)
  390. {
  391. todo.push(StackEntry());
  392. StackEntry& newEntry = todo.top();
  393. newEntry.so = child;
  394. newEntry.proxy = &current.proxy->children[j];
  395. break;
  396. }
  397. }
  398. }
  399. }
  400. }
  401. }
  402. }