BsPrefabUtility.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  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. Vector<HSceneObject> prefabInstanceRoots;
  41. while (!todo.empty())
  42. {
  43. HSceneObject current = todo.top();
  44. todo.pop();
  45. if (!current->mPrefabLinkUUID.empty())
  46. prefabInstanceRoots.push_back(current);
  47. UINT32 childCount = current->getNumChildren();
  48. for (UINT32 i = 0; i < childCount; i++)
  49. {
  50. HSceneObject child = current->getChild(i);
  51. todo.push(child);
  52. }
  53. }
  54. struct RestoredPrefabInstance
  55. {
  56. HSceneObject newInstance;
  57. HSceneObject originalParent;
  58. UINT32 originalLinkId;
  59. };
  60. Vector<RestoredPrefabInstance> newPrefabInstances;
  61. // Need to do this bottom up to ensure I don't destroy the parents before children
  62. for (auto iter = prefabInstanceRoots.rbegin(); iter != prefabInstanceRoots.rend(); ++iter)
  63. {
  64. HSceneObject current = *iter;
  65. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, false));
  66. if (prefabLink != nullptr && prefabLink->getHash() != current->mPrefabHash)
  67. {
  68. // Save IDs, destroy original, create new, apply diff, restore IDs
  69. SceneObjectProxy soProxy;
  70. UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
  71. recordInstanceData(current, soProxy, linkedInstanceData);
  72. PrefabDiffPtr prefabDiff = current->mPrefabDiff;
  73. HSceneObject parent = current->getParent();
  74. current->destroy(true);
  75. HSceneObject newInstance = prefabLink->instantiate();
  76. if (prefabDiff != nullptr)
  77. prefabDiff->apply(newInstance);
  78. restoreLinkedInstanceData(newInstance, soProxy, linkedInstanceData);
  79. restoreUnlinkedInstanceData(newInstance, soProxy);
  80. newPrefabInstances.push_back({ newInstance, parent, newInstance->getLinkId() });
  81. }
  82. }
  83. // Once everything is instantiated, restore old parents & link IDs for root
  84. for (auto& newInstanceData : newPrefabInstances)
  85. {
  86. newInstanceData.newInstance->setParent(newInstanceData.originalParent);
  87. newInstanceData.newInstance->mLinkId = newInstanceData.originalLinkId;
  88. }
  89. gResources().unloadAllUnused();
  90. }
  91. UINT32 PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject, UINT32 startingId)
  92. {
  93. Stack<HSceneObject> todo;
  94. todo.push(sceneObject);
  95. while (!todo.empty())
  96. {
  97. HSceneObject currentSO = todo.top();
  98. todo.pop();
  99. for (auto& component : currentSO->mComponents)
  100. {
  101. if (component->getLinkId() == -1)
  102. component->mLinkId = startingId++;
  103. }
  104. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  105. for (UINT32 i = 0; i < numChildren; i++)
  106. {
  107. HSceneObject child = currentSO->getChild(i);
  108. if (!child->hasFlag(SOF_DontSave))
  109. {
  110. if (child->getLinkId() == -1)
  111. child->mLinkId = startingId++;
  112. if(child->mPrefabLinkUUID.empty())
  113. todo.push(currentSO->getChild(i));
  114. }
  115. }
  116. }
  117. return startingId;
  118. }
  119. void PrefabUtility::clearPrefabIds(const HSceneObject& sceneObject, bool recursive)
  120. {
  121. Stack<HSceneObject> todo;
  122. todo.push(sceneObject);
  123. while (!todo.empty())
  124. {
  125. HSceneObject currentSO = todo.top();
  126. todo.pop();
  127. for (auto& component : currentSO->mComponents)
  128. component->mLinkId = -1;
  129. if (recursive)
  130. {
  131. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  132. for (UINT32 i = 0; i < numChildren; i++)
  133. {
  134. HSceneObject child = currentSO->getChild(i);
  135. child->mLinkId = -1;
  136. if (child->mPrefabLinkUUID.empty())
  137. todo.push(child);
  138. }
  139. }
  140. }
  141. }
  142. void PrefabUtility::recordPrefabDiff(const HSceneObject& sceneObject)
  143. {
  144. HSceneObject topLevelObject = sceneObject;
  145. while (topLevelObject != nullptr)
  146. {
  147. if (!topLevelObject->mPrefabLinkUUID.empty())
  148. break;
  149. if (topLevelObject->mParent != nullptr)
  150. topLevelObject = topLevelObject->mParent;
  151. else
  152. topLevelObject = nullptr;
  153. }
  154. if (topLevelObject == nullptr)
  155. return;
  156. Stack<HSceneObject> todo;
  157. todo.push(topLevelObject);
  158. while (!todo.empty())
  159. {
  160. HSceneObject current = todo.top();
  161. todo.pop();
  162. if (!current->mPrefabLinkUUID.empty())
  163. {
  164. current->mPrefabDiff = nullptr;
  165. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, false));
  166. if (prefabLink != nullptr)
  167. current->mPrefabDiff = PrefabDiff::create(prefabLink->_getRoot(), current->getHandle());
  168. }
  169. UINT32 childCount = current->getNumChildren();
  170. for (UINT32 i = 0; i < childCount; i++)
  171. {
  172. HSceneObject child = current->getChild(i);
  173. todo.push(child);
  174. }
  175. }
  176. gResources().unloadAllUnused();
  177. }
  178. void PrefabUtility::recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  179. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  180. {
  181. struct StackData
  182. {
  183. HSceneObject so;
  184. SceneObjectProxy* proxy;
  185. };
  186. Stack<StackData> todo;
  187. todo.push({so, &output});
  188. output.instanceData = so->_getInstanceData();
  189. output.linkId = -1;
  190. while (!todo.empty())
  191. {
  192. StackData curData = todo.top();
  193. todo.pop();
  194. const Vector<HComponent>& components = curData.so->getComponents();
  195. for (auto& component : components)
  196. {
  197. curData.proxy->components.push_back(ComponentProxy());
  198. ComponentProxy& componentProxy = curData.proxy->components.back();
  199. componentProxy.instanceData = component->_getInstanceData();
  200. componentProxy.linkId = component->getLinkId();
  201. linkedInstanceData[componentProxy.linkId] = componentProxy.instanceData;
  202. }
  203. UINT32 numChildren = curData.so->getNumChildren();
  204. curData.proxy->children.resize(numChildren);
  205. for (UINT32 i = 0; i < numChildren; i++)
  206. {
  207. HSceneObject child = curData.so->getChild(i);
  208. SceneObjectProxy& childProxy = curData.proxy->children[i];
  209. childProxy.instanceData = child->_getInstanceData();
  210. childProxy.linkId = child->getLinkId();
  211. linkedInstanceData[childProxy.linkId] = childProxy.instanceData;
  212. if (child->mPrefabLinkUUID.empty())
  213. {
  214. todo.push({ child, &curData.proxy->children[i] });
  215. }
  216. }
  217. }
  218. }
  219. void PrefabUtility::restoreLinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy,
  220. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  221. {
  222. Stack<HSceneObject> todo;
  223. todo.push(so);
  224. // Root is not in the instance data map because its link ID belongs to the parent prefab, if any
  225. so->_setInstanceData(proxy.instanceData);
  226. while (!todo.empty())
  227. {
  228. HSceneObject current = todo.top();
  229. todo.pop();
  230. Vector<HComponent>& components = current->mComponents;
  231. for (auto& component : components)
  232. {
  233. if (component->getLinkId() != -1)
  234. {
  235. auto iterFind = linkedInstanceData.find(component->getLinkId());
  236. if (iterFind != linkedInstanceData.end())
  237. {
  238. component->_setInstanceData(iterFind->second);
  239. component._setHandleData(component.getInternalPtr());
  240. }
  241. }
  242. }
  243. UINT32 numChildren = current->getNumChildren();
  244. for (UINT32 i = 0; i < numChildren; i++)
  245. {
  246. HSceneObject child = current->getChild(i);
  247. if (child->getLinkId() != -1)
  248. {
  249. auto iterFind = linkedInstanceData.find(child->getLinkId());
  250. if (iterFind != linkedInstanceData.end())
  251. child->_setInstanceData(iterFind->second);
  252. }
  253. if (child->mPrefabLinkUUID.empty())
  254. todo.push(child);
  255. }
  256. }
  257. }
  258. void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
  259. {
  260. struct StackEntry
  261. {
  262. HSceneObject so;
  263. SceneObjectProxy* proxy;
  264. };
  265. Stack<StackEntry> todo;
  266. todo.push(StackEntry());
  267. StackEntry& topEntry = todo.top();
  268. topEntry.so = so;
  269. topEntry.proxy = &proxy;
  270. while (!todo.empty())
  271. {
  272. StackEntry current = todo.top();
  273. todo.pop();
  274. if (current.proxy->linkId == -1)
  275. current.so->_setInstanceData(current.proxy->instanceData);
  276. Vector<HComponent>& components = current.so->mComponents;
  277. UINT32 componentProxyIdx = 0;
  278. UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
  279. for (auto& component : components)
  280. {
  281. if (component->getLinkId() == -1)
  282. {
  283. bool foundInstanceData = false;
  284. for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
  285. {
  286. if (current.proxy->components[componentProxyIdx].linkId != -1)
  287. continue;
  288. component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
  289. component._setHandleData(component.getInternalPtr());
  290. foundInstanceData = true;
  291. break;
  292. }
  293. assert(foundInstanceData);
  294. }
  295. }
  296. UINT32 numChildren = current.so->getNumChildren();
  297. UINT32 childProxyIdx = 0;
  298. UINT32 numChildProxies = (UINT32)current.proxy->children.size();
  299. for (UINT32 i = 0; i < numChildren; i++)
  300. {
  301. HSceneObject child = current.so->getChild(i);
  302. if (child->getLinkId() == -1)
  303. {
  304. bool foundInstanceData = false;
  305. for (; childProxyIdx < numChildProxies; childProxyIdx++)
  306. {
  307. if (current.proxy->children[childProxyIdx].linkId != -1)
  308. continue;
  309. assert(current.proxy->children[childProxyIdx].linkId == -1);
  310. child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
  311. if (child->mPrefabLinkUUID.empty())
  312. {
  313. todo.push(StackEntry());
  314. StackEntry& newEntry = todo.top();
  315. newEntry.so = child;
  316. newEntry.proxy = &current.proxy->children[childProxyIdx];
  317. }
  318. foundInstanceData = true;
  319. break;
  320. }
  321. assert(foundInstanceData);
  322. }
  323. else
  324. {
  325. if (!child->mPrefabLinkUUID.empty())
  326. continue;
  327. for (UINT32 j = 0; j < numChildProxies; j++)
  328. {
  329. if (child->getLinkId() == current.proxy->children[j].linkId)
  330. {
  331. todo.push(StackEntry());
  332. StackEntry& newEntry = todo.top();
  333. newEntry.so = child;
  334. newEntry.proxy = &current.proxy->children[j];
  335. break;
  336. }
  337. }
  338. }
  339. }
  340. }
  341. }
  342. }