BsPrefabUtility.cpp 13 KB

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