BsPrefabUtility.cpp 11 KB

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