BsPrefabUtility.cpp 13 KB

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