BsPrefabUtility.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. UINT32 PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject, UINT32 startingId)
  120. {
  121. Stack<HSceneObject> todo;
  122. todo.push(sceneObject);
  123. while (!todo.empty())
  124. {
  125. HSceneObject currentSO = todo.top();
  126. todo.pop();
  127. for (auto& component : currentSO->mComponents)
  128. {
  129. if (component->getLinkId() == (UINT32)-1)
  130. component->mLinkId = startingId++;
  131. }
  132. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  133. for (UINT32 i = 0; i < numChildren; i++)
  134. {
  135. HSceneObject child = currentSO->getChild(i);
  136. if (!child->hasFlag(SOF_DontSave))
  137. {
  138. if (child->getLinkId() == (UINT32)-1)
  139. child->mLinkId = startingId++;
  140. if(child->mPrefabLinkUUID.empty())
  141. todo.push(currentSO->getChild(i));
  142. }
  143. }
  144. }
  145. return startingId;
  146. }
  147. void PrefabUtility::clearPrefabIds(const HSceneObject& sceneObject, bool recursive, bool clearRoot)
  148. {
  149. Stack<HSceneObject> todo;
  150. todo.push(sceneObject);
  151. if (clearRoot)
  152. sceneObject->mLinkId = (UINT32)-1;
  153. while (!todo.empty())
  154. {
  155. HSceneObject currentSO = todo.top();
  156. todo.pop();
  157. for (auto& component : currentSO->mComponents)
  158. component->mLinkId = (UINT32)-1;
  159. if (recursive)
  160. {
  161. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  162. for (UINT32 i = 0; i < numChildren; i++)
  163. {
  164. HSceneObject child = currentSO->getChild(i);
  165. child->mLinkId = (UINT32)-1;
  166. if (child->mPrefabLinkUUID.empty())
  167. todo.push(child);
  168. }
  169. }
  170. }
  171. }
  172. void PrefabUtility::recordPrefabDiff(const HSceneObject& sceneObject)
  173. {
  174. HSceneObject topLevelObject = sceneObject;
  175. while (topLevelObject != nullptr)
  176. {
  177. if (!topLevelObject->mPrefabLinkUUID.empty())
  178. break;
  179. if (topLevelObject->mParent != nullptr)
  180. topLevelObject = topLevelObject->mParent;
  181. else
  182. topLevelObject = nullptr;
  183. }
  184. if (topLevelObject == nullptr)
  185. topLevelObject = sceneObject;
  186. Stack<HSceneObject> todo;
  187. todo.push(topLevelObject);
  188. while (!todo.empty())
  189. {
  190. HSceneObject current = todo.top();
  191. todo.pop();
  192. if (!current->mPrefabLinkUUID.empty())
  193. {
  194. current->mPrefabDiff = nullptr;
  195. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, ResourceLoadFlag::None));
  196. if (prefabLink.isLoaded(false))
  197. current->mPrefabDiff = PrefabDiff::create(prefabLink->_getRoot(), current->getHandle());
  198. }
  199. UINT32 childCount = current->getNumChildren();
  200. for (UINT32 i = 0; i < childCount; i++)
  201. {
  202. HSceneObject child = current->getChild(i);
  203. todo.push(child);
  204. }
  205. }
  206. gResources().unloadAllUnused();
  207. }
  208. void PrefabUtility::recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  209. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  210. {
  211. struct StackData
  212. {
  213. HSceneObject so;
  214. SceneObjectProxy* proxy;
  215. };
  216. Stack<StackData> todo;
  217. todo.push({so, &output});
  218. output.instanceData = so->_getInstanceData();
  219. output.linkId = (UINT32)-1;
  220. while (!todo.empty())
  221. {
  222. StackData curData = todo.top();
  223. todo.pop();
  224. const Vector<HComponent>& components = curData.so->getComponents();
  225. for (auto& component : components)
  226. {
  227. curData.proxy->components.push_back(ComponentProxy());
  228. ComponentProxy& componentProxy = curData.proxy->components.back();
  229. componentProxy.instanceData = component->_getInstanceData();
  230. componentProxy.linkId = component->getLinkId();
  231. linkedInstanceData[componentProxy.linkId] = componentProxy.instanceData;
  232. }
  233. UINT32 numChildren = curData.so->getNumChildren();
  234. curData.proxy->children.resize(numChildren);
  235. for (UINT32 i = 0; i < numChildren; i++)
  236. {
  237. HSceneObject child = curData.so->getChild(i);
  238. SceneObjectProxy& childProxy = curData.proxy->children[i];
  239. childProxy.instanceData = child->_getInstanceData();
  240. childProxy.linkId = child->getLinkId();
  241. linkedInstanceData[childProxy.linkId] = childProxy.instanceData;
  242. if (child->mPrefabLinkUUID.empty())
  243. {
  244. todo.push({ child, &curData.proxy->children[i] });
  245. }
  246. }
  247. }
  248. }
  249. void PrefabUtility::restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  250. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  251. {
  252. Stack<HSceneObject> todo;
  253. todo.push(so);
  254. while (!todo.empty())
  255. {
  256. HSceneObject current = todo.top();
  257. todo.pop();
  258. Vector<HComponent>& components = current->mComponents;
  259. for (auto& component : components)
  260. {
  261. if (component->getLinkId() != (UINT32)-1)
  262. {
  263. auto iterFind = linkedInstanceData.find(component->getLinkId());
  264. if (iterFind != linkedInstanceData.end())
  265. {
  266. component->_setInstanceData(iterFind->second);
  267. component._setHandleData(component.getInternalPtr());
  268. }
  269. }
  270. }
  271. UINT32 numChildren = current->getNumChildren();
  272. for (UINT32 i = 0; i < numChildren; i++)
  273. {
  274. HSceneObject child = current->getChild(i);
  275. if (child->getLinkId() != (UINT32)-1)
  276. {
  277. auto iterFind = linkedInstanceData.find(child->getLinkId());
  278. if (iterFind != linkedInstanceData.end())
  279. child->_setInstanceData(iterFind->second);
  280. }
  281. if (child->mPrefabLinkUUID.empty())
  282. todo.push(child);
  283. }
  284. }
  285. }
  286. void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
  287. {
  288. struct StackEntry
  289. {
  290. HSceneObject so;
  291. SceneObjectProxy* proxy;
  292. };
  293. Stack<StackEntry> todo;
  294. todo.push(StackEntry());
  295. StackEntry& topEntry = todo.top();
  296. topEntry.so = so;
  297. topEntry.proxy = &proxy;
  298. while (!todo.empty())
  299. {
  300. StackEntry current = todo.top();
  301. todo.pop();
  302. if (current.proxy->linkId == -1)
  303. current.so->_setInstanceData(current.proxy->instanceData);
  304. Vector<HComponent>& components = current.so->mComponents;
  305. UINT32 componentProxyIdx = 0;
  306. UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
  307. for (auto& component : components)
  308. {
  309. if (component->getLinkId() == (UINT32)-1)
  310. {
  311. bool foundInstanceData = false;
  312. for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
  313. {
  314. if (current.proxy->components[componentProxyIdx].linkId != -1)
  315. continue;
  316. component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
  317. component._setHandleData(component.getInternalPtr());
  318. foundInstanceData = true;
  319. break;
  320. }
  321. assert(foundInstanceData);
  322. }
  323. }
  324. UINT32 numChildren = current.so->getNumChildren();
  325. UINT32 childProxyIdx = 0;
  326. UINT32 numChildProxies = (UINT32)current.proxy->children.size();
  327. for (UINT32 i = 0; i < numChildren; i++)
  328. {
  329. HSceneObject child = current.so->getChild(i);
  330. if (child->getLinkId() == (UINT32)-1)
  331. {
  332. bool foundInstanceData = false;
  333. for (; childProxyIdx < numChildProxies; childProxyIdx++)
  334. {
  335. if (current.proxy->children[childProxyIdx].linkId != -1)
  336. continue;
  337. assert(current.proxy->children[childProxyIdx].linkId == -1);
  338. child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
  339. if (child->mPrefabLinkUUID.empty())
  340. {
  341. todo.push(StackEntry());
  342. StackEntry& newEntry = todo.top();
  343. newEntry.so = child;
  344. newEntry.proxy = &current.proxy->children[childProxyIdx];
  345. }
  346. foundInstanceData = true;
  347. break;
  348. }
  349. assert(foundInstanceData);
  350. }
  351. else
  352. {
  353. if (!child->mPrefabLinkUUID.empty())
  354. continue;
  355. for (UINT32 j = 0; j < numChildProxies; j++)
  356. {
  357. if (child->getLinkId() == current.proxy->children[j].linkId)
  358. {
  359. todo.push(StackEntry());
  360. StackEntry& newEntry = todo.top();
  361. newEntry.so = child;
  362. newEntry.proxy = &current.proxy->children[j];
  363. break;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. }
  370. }