BsPrefabUtility.cpp 11 KB

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