BsPrefabUtility.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  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. so->destroy();
  19. HSceneObject newInstance = prefabLink->instantiate();
  20. restoreLinkedInstanceData(newInstance, linkedInstanceData);
  21. }
  22. void PrefabUtility::updateFromPrefab(const HSceneObject& so)
  23. {
  24. HSceneObject topLevelObject = so;
  25. while (topLevelObject != nullptr)
  26. {
  27. if (!topLevelObject->mPrefabLinkUUID.empty())
  28. break;
  29. if (topLevelObject->mParent != nullptr)
  30. topLevelObject = topLevelObject->mParent;
  31. else
  32. topLevelObject = nullptr;
  33. }
  34. Stack<HSceneObject> todo;
  35. todo.push(topLevelObject);
  36. Vector<HSceneObject> prefabInstanceRoots;
  37. while (!todo.empty())
  38. {
  39. HSceneObject current = todo.top();
  40. todo.pop();
  41. String prefabLinkUUID = current->getPrefabLink();
  42. if (!prefabLinkUUID.empty())
  43. prefabInstanceRoots.push_back(current);
  44. UINT32 childCount = current->getNumChildren();
  45. for (UINT32 i = 0; i < childCount; i++)
  46. {
  47. HSceneObject child = current->getChild(i);
  48. todo.push(child);
  49. }
  50. }
  51. // Need to do this bottom up to ensure I don't destroy the parents before children
  52. for (auto iter = prefabInstanceRoots.rbegin(); iter != prefabInstanceRoots.rend(); ++iter)
  53. {
  54. HSceneObject current = *iter;
  55. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->getPrefabLink(), false, false));
  56. if (prefabLink != nullptr && prefabLink->getHash() != current->mPrefabHash)
  57. {
  58. // Save IDs, destroy original, create new, apply diff, restore IDs
  59. SceneObjectProxy soProxy;
  60. UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
  61. recordInstanceData(current, soProxy, linkedInstanceData);
  62. PrefabDiffPtr prefabDiff = current->mPrefabDiff;
  63. current->destroy();
  64. HSceneObject newInstance = prefabLink->instantiate();
  65. if (prefabDiff != nullptr)
  66. prefabDiff->apply(newInstance);
  67. restoreLinkedInstanceData(newInstance, linkedInstanceData);
  68. restoreUnlinkedInstanceData(newInstance, soProxy);
  69. }
  70. }
  71. gResources().unloadAllUnused();
  72. }
  73. void PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject)
  74. {
  75. Vector<HGameObject> objectsToId;
  76. Set<INT32> existingIds;
  77. Stack<HSceneObject> todo;
  78. todo.push(sceneObject);
  79. while (!todo.empty())
  80. {
  81. HSceneObject currentSO = todo.top();
  82. todo.pop();
  83. if (currentSO->mLinkId == -1)
  84. objectsToId.push_back(currentSO);
  85. else
  86. existingIds.insert(currentSO->mLinkId);
  87. for (auto& component : currentSO->mComponents)
  88. {
  89. if (component->mLinkId == -1)
  90. objectsToId.push_back(component);
  91. else
  92. existingIds.insert(component->mLinkId);
  93. }
  94. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  95. for (UINT32 i = 0; i < numChildren; i++)
  96. {
  97. HSceneObject child = currentSO->getChild(i);
  98. if (child->mPrefabLinkUUID.empty())
  99. todo.push(currentSO->getChild(i));
  100. }
  101. }
  102. auto setIter = existingIds.begin();
  103. INT32 nextId = 0;
  104. for (auto& object : objectsToId)
  105. {
  106. INT32 freeId = -1;
  107. for (; setIter != existingIds.end(); ++setIter)
  108. {
  109. if (nextId < (*setIter))
  110. freeId = nextId++;
  111. else
  112. nextId++;
  113. }
  114. if (freeId == -1)
  115. freeId = nextId++;
  116. object->mLinkId = freeId;
  117. }
  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. currentSO->mLinkId = -1;
  128. for (auto& component : currentSO->mComponents)
  129. component->mLinkId = -1;
  130. if (recursive)
  131. {
  132. UINT32 numChildren = (UINT32)currentSO->getNumChildren();
  133. for (UINT32 i = 0; i < numChildren; i++)
  134. {
  135. HSceneObject child = currentSO->getChild(i);
  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. Stack<HSceneObject> todo;
  155. todo.push(topLevelObject);
  156. while (!todo.empty())
  157. {
  158. HSceneObject current = todo.top();
  159. todo.pop();
  160. if (!current->getPrefabLink().empty())
  161. {
  162. current->mPrefabDiff = nullptr;
  163. HPrefab prefabLink = static_resource_cast<Prefab>(gResources().loadFromUUID(current->getPrefabLink(), false, false));
  164. if (prefabLink != nullptr)
  165. current->mPrefabDiff = PrefabDiff::create(prefabLink->_getRoot(), current->getHandle());
  166. }
  167. UINT32 childCount = current->getNumChildren();
  168. for (UINT32 i = 0; i < childCount; i++)
  169. {
  170. HSceneObject child = current->getChild(i);
  171. todo.push(child);
  172. }
  173. }
  174. gResources().unloadAllUnused();
  175. }
  176. void PrefabUtility::recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
  177. UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  178. {
  179. Stack<HSceneObject> todo;
  180. todo.push(so);
  181. while (!todo.empty())
  182. {
  183. HSceneObject current = todo.top();
  184. todo.pop();
  185. output.instanceData = current->_getInstanceData();
  186. output.linkId = current->getLinkId();
  187. linkedInstanceData[output.linkId] = output.instanceData;
  188. const Vector<HComponent>& components = current->getComponents();
  189. for (auto& component : components)
  190. {
  191. output.components.push_back(ComponentProxy());
  192. ComponentProxy& componentProxy = output.components.back();
  193. componentProxy.instanceData = component->_getInstanceData();
  194. componentProxy.linkId = component->getLinkId();
  195. linkedInstanceData[componentProxy.linkId] = componentProxy.instanceData;
  196. }
  197. UINT32 numChildren = current->getNumChildren();
  198. for (UINT32 i = 0; i < numChildren; i++)
  199. {
  200. HSceneObject child = current->getChild(i);
  201. if (child->mPrefabLinkUUID.empty())
  202. todo.push(child);
  203. }
  204. }
  205. }
  206. void PrefabUtility::restoreLinkedInstanceData(const HSceneObject& so, UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
  207. {
  208. Stack<HSceneObject> todo;
  209. todo.push(so);
  210. while (!todo.empty())
  211. {
  212. HSceneObject current = todo.top();
  213. todo.pop();
  214. if (current->getLinkId() != -1)
  215. {
  216. auto iterFind = linkedInstanceData.find(current->getLinkId());
  217. if (iterFind != linkedInstanceData.end())
  218. current->_setInstanceData(iterFind->second);
  219. }
  220. const Vector<HComponent>& components = current->getComponents();
  221. for (auto& component : components)
  222. {
  223. if (component->getLinkId() != -1)
  224. {
  225. auto iterFind = linkedInstanceData.find(component->getLinkId());
  226. if (iterFind != linkedInstanceData.end())
  227. component->_setInstanceData(iterFind->second);
  228. }
  229. }
  230. UINT32 numChildren = current->getNumChildren();
  231. for (UINT32 i = 0; i < numChildren; i++)
  232. {
  233. HSceneObject child = current->getChild(i);
  234. if (child->mPrefabLinkUUID.empty())
  235. todo.push(child);
  236. }
  237. }
  238. }
  239. void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
  240. {
  241. struct StackEntry
  242. {
  243. HSceneObject so;
  244. SceneObjectProxy* proxy;
  245. };
  246. Stack<StackEntry> todo;
  247. todo.push(StackEntry());
  248. assert(so->getLinkId() == proxy.linkId);
  249. StackEntry& topEntry = todo.top();
  250. topEntry.so = so;
  251. topEntry.proxy = &proxy;
  252. while (!todo.empty())
  253. {
  254. StackEntry current = todo.top();
  255. todo.pop();
  256. if (current.proxy->linkId == -1)
  257. current.so->_setInstanceData(current.proxy->instanceData);
  258. const Vector<HComponent>& components = current.so->getComponents();
  259. UINT32 componentProxyIdx = 0;
  260. UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
  261. for (auto& component : components)
  262. {
  263. if (component->getLinkId() == -1)
  264. {
  265. bool foundInstanceData = false;
  266. for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
  267. {
  268. if (current.proxy->components[componentProxyIdx].linkId != -1)
  269. continue;
  270. assert(current.proxy->components[componentProxyIdx].linkId == -1);
  271. component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
  272. foundInstanceData = true;
  273. break;
  274. }
  275. assert(foundInstanceData);
  276. }
  277. }
  278. UINT32 numChildren = current.so->getNumChildren();
  279. UINT32 childProxyIdx = 0;
  280. UINT32 numChildProxies = (UINT32)current.proxy->children.size();
  281. for (UINT32 i = 0; i < numChildren; i++)
  282. {
  283. HSceneObject child = current.so->getChild(i);
  284. if (!child->mPrefabLinkUUID.empty())
  285. continue;
  286. if (child->getLinkId() == -1)
  287. {
  288. bool foundInstanceData = false;
  289. for (; childProxyIdx < numChildProxies; childProxyIdx++)
  290. {
  291. if (current.proxy->children[childProxyIdx].linkId != -1)
  292. continue;
  293. assert(current.proxy->children[childProxyIdx].linkId == -1);
  294. child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
  295. todo.push(StackEntry());
  296. StackEntry& newEntry = todo.top();
  297. newEntry.so = child;
  298. newEntry.proxy = &current.proxy->children[childProxyIdx];
  299. foundInstanceData = true;
  300. break;
  301. }
  302. assert(foundInstanceData);
  303. }
  304. else
  305. {
  306. for (UINT32 j = 0; j < numChildProxies; j++)
  307. {
  308. if (child->getLinkId() == current.proxy->children[j].linkId)
  309. {
  310. todo.push(StackEntry());
  311. StackEntry& newEntry = todo.top();
  312. newEntry.so = child;
  313. newEntry.proxy = &current.proxy->children[j];
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. }
  320. }
  321. }