2
0

BsPrefabUtility.cpp 13 KB

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