BsPrefabUtility.cpp 12 KB

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