BsPrefabUtility.cpp 13 KB

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