BsPrefabUtility.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439
  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. Vector<std::pair<HSceneObject, HSceneObject>> newPrefabInstances;
  55. // Need to do this bottom up to ensure I don't destroy the parents before children
  56. for (auto iter = prefabInstanceRoots.rbegin(); iter != prefabInstanceRoots.rend(); ++iter)
  57. {
  58. HSceneObject current = *iter;
  59. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->mPrefabLinkUUID, false, false));
  60. if (prefabLink != nullptr && prefabLink->getHash() != current->mPrefabHash)
  61. {
  62. // Save IDs, destroy original, create new, apply diff, restore IDs
  63. SceneObjectProxy soProxy;
  64. UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
  65. recordInstanceData(current, soProxy, linkedInstanceData);
  66. PrefabDiffPtr prefabDiff = current->mPrefabDiff;
  67. HSceneObject parent = current->getParent();
  68. current->destroy(true);
  69. HSceneObject newInstance = prefabLink->instantiate();
  70. if (prefabDiff != nullptr)
  71. prefabDiff->apply(newInstance);
  72. restoreLinkedInstanceData(newInstance, soProxy, linkedInstanceData);
  73. restoreUnlinkedInstanceData(newInstance, soProxy);
  74. newPrefabInstances.push_back({ newInstance, parent });
  75. }
  76. }
  77. // Once everything is instantiated, restore old parents
  78. for (auto& newInstanceData : newPrefabInstances)
  79. {
  80. newInstanceData.first->mParent = newInstanceData.second;
  81. }
  82. gResources().unloadAllUnused();
  83. }
  84. void PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject)
  85. {
  86. Vector<HGameObject> objectsToId;
  87. Set<INT32> existingIds;
  88. Stack<HSceneObject> todo;
  89. todo.push(sceneObject);
  90. while (!todo.empty())
  91. {
  92. HSceneObject currentSO = todo.top();
  93. todo.pop();
  94. for (auto& component : currentSO->mComponents)
  95. {
  96. if (component->getLinkId() == -1)
  97. objectsToId.push_back(component);
  98. else
  99. existingIds.insert(component->getLinkId());
  100. }
  101. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  102. for (UINT32 i = 0; i < numChildren; i++)
  103. {
  104. HSceneObject child = currentSO->getChild(i);
  105. if (!child->hasFlag(SOF_DontSave))
  106. {
  107. if (child->getLinkId() == -1)
  108. objectsToId.push_back(child);
  109. else
  110. existingIds.insert(child->getLinkId());
  111. if(child->mPrefabLinkUUID.empty())
  112. todo.push(currentSO->getChild(i));
  113. }
  114. }
  115. }
  116. auto setIter = existingIds.begin();
  117. INT32 nextId = 0;
  118. for (auto& object : objectsToId)
  119. {
  120. INT32 freeId = -1;
  121. for (; setIter != existingIds.end(); ++setIter)
  122. {
  123. if (nextId < (*setIter))
  124. {
  125. freeId = nextId++;
  126. break;
  127. }
  128. else
  129. nextId++;
  130. }
  131. if (freeId == -1)
  132. freeId = nextId++;
  133. object->mLinkId = freeId;
  134. }
  135. }
  136. void PrefabUtility::clearPrefabIds(const HSceneObject& sceneObject, bool recursive)
  137. {
  138. Stack<HSceneObject> todo;
  139. todo.push(sceneObject);
  140. while (!todo.empty())
  141. {
  142. HSceneObject currentSO = todo.top();
  143. todo.pop();
  144. for (auto& component : currentSO->mComponents)
  145. component->mLinkId = -1;
  146. if (recursive)
  147. {
  148. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  149. for (UINT32 i = 0; i < numChildren; i++)
  150. {
  151. HSceneObject child = currentSO->getChild(i);
  152. child->mLinkId = -1;
  153. if (child->mPrefabLinkUUID.empty())
  154. todo.push(child);
  155. }
  156. }
  157. }
  158. }
  159. void PrefabUtility::recordPrefabDiff(const HSceneObject& sceneObject)
  160. {
  161. HSceneObject topLevelObject = sceneObject;
  162. while (topLevelObject != nullptr)
  163. {
  164. if (!topLevelObject->mPrefabLinkUUID.empty())
  165. break;
  166. if (topLevelObject->mParent != nullptr)
  167. topLevelObject = topLevelObject->mParent;
  168. else
  169. topLevelObject = nullptr;
  170. }
  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 = -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. const Vector<HComponent>& components = current->getComponents();
  246. for (auto& component : components)
  247. {
  248. if (component->getLinkId() != -1)
  249. {
  250. auto iterFind = linkedInstanceData.find(component->getLinkId());
  251. if (iterFind != linkedInstanceData.end())
  252. component->_setInstanceData(iterFind->second);
  253. }
  254. }
  255. UINT32 numChildren = current->getNumChildren();
  256. for (UINT32 i = 0; i < numChildren; i++)
  257. {
  258. HSceneObject child = current->getChild(i);
  259. if (child->getLinkId() != -1)
  260. {
  261. auto iterFind = linkedInstanceData.find(child->getLinkId());
  262. if (iterFind != linkedInstanceData.end())
  263. child->_setInstanceData(iterFind->second);
  264. }
  265. if (child->mPrefabLinkUUID.empty())
  266. todo.push(child);
  267. }
  268. }
  269. }
  270. void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
  271. {
  272. struct StackEntry
  273. {
  274. HSceneObject so;
  275. SceneObjectProxy* proxy;
  276. };
  277. Stack<StackEntry> todo;
  278. todo.push(StackEntry());
  279. StackEntry& topEntry = todo.top();
  280. topEntry.so = so;
  281. topEntry.proxy = &proxy;
  282. while (!todo.empty())
  283. {
  284. StackEntry current = todo.top();
  285. todo.pop();
  286. if (current.proxy->linkId == -1)
  287. current.so->_setInstanceData(current.proxy->instanceData);
  288. const Vector<HComponent>& components = current.so->getComponents();
  289. UINT32 componentProxyIdx = 0;
  290. UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
  291. for (auto& component : components)
  292. {
  293. if (component->getLinkId() == -1)
  294. {
  295. bool foundInstanceData = false;
  296. for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
  297. {
  298. if (current.proxy->components[componentProxyIdx].linkId != -1)
  299. continue;
  300. component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
  301. foundInstanceData = true;
  302. break;
  303. }
  304. assert(foundInstanceData);
  305. }
  306. }
  307. UINT32 numChildren = current.so->getNumChildren();
  308. UINT32 childProxyIdx = 0;
  309. UINT32 numChildProxies = (UINT32)current.proxy->children.size();
  310. for (UINT32 i = 0; i < numChildren; i++)
  311. {
  312. HSceneObject child = current.so->getChild(i);
  313. if (child->getLinkId() == -1)
  314. {
  315. bool foundInstanceData = false;
  316. for (; childProxyIdx < numChildProxies; childProxyIdx++)
  317. {
  318. if (current.proxy->children[childProxyIdx].linkId != -1)
  319. continue;
  320. assert(current.proxy->children[childProxyIdx].linkId == -1);
  321. child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
  322. if (child->mPrefabLinkUUID.empty())
  323. {
  324. todo.push(StackEntry());
  325. StackEntry& newEntry = todo.top();
  326. newEntry.so = child;
  327. newEntry.proxy = &current.proxy->children[childProxyIdx];
  328. }
  329. foundInstanceData = true;
  330. break;
  331. }
  332. assert(foundInstanceData);
  333. }
  334. else
  335. {
  336. if (!child->mPrefabLinkUUID.empty())
  337. continue;
  338. for (UINT32 j = 0; j < numChildProxies; j++)
  339. {
  340. if (child->getLinkId() == current.proxy->children[j].linkId)
  341. {
  342. todo.push(StackEntry());
  343. StackEntry& newEntry = todo.top();
  344. newEntry.so = child;
  345. newEntry.proxy = &current.proxy->children[j];
  346. break;
  347. }
  348. }
  349. }
  350. }
  351. }
  352. }
  353. }