| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325 |
- #include "BsPrefabUtility.h"
- #include "BsPrefabDiff.h"
- #include "BsPrefab.h"
- #include "BsSceneObject.h"
- namespace BansheeEngine
- {
- void PrefabUtility::revertToPrefab(const HSceneObject& so)
- {
- HPrefab prefabLink = so->getPrefabLink();
- if (prefabLink == nullptr)
- return;
- // Save IDs, destroy original, create new, restore IDs
- SceneObjectProxy soProxy;
- UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
- recordInstanceData(so, soProxy, linkedInstanceData);
- so->destroy();
- HSceneObject newInstance = prefabLink->instantiate();
- restoreLinkedInstanceData(newInstance, linkedInstanceData);
- }
- void PrefabUtility::updateFromPrefab(const HSceneObject& so)
- {
- HPrefab prefabLink = so->getPrefabLink();
- if (prefabLink == nullptr)
- return;
- // Save IDs, destroy original, create new, apply diff, restore IDs
- SceneObjectProxy soProxy;
- UnorderedMap<UINT32, GameObjectInstanceDataPtr> linkedInstanceData;
- recordInstanceData(so, soProxy, linkedInstanceData);
- PrefabDiffPtr prefabDiff = so->mPrefabDiff;
- so->destroy();
- HSceneObject newInstance = prefabLink->instantiate();
- if (prefabDiff != nullptr)
- prefabDiff->apply(newInstance);
- restoreLinkedInstanceData(newInstance, linkedInstanceData);
- restoreUnlinkedInstanceData(newInstance, soProxy);
- }
- void PrefabUtility::generatePrefabIds(const HSceneObject& sceneObject)
- {
- Vector<HGameObject> objectsToId;
- Set<INT32> existingIds;
- Stack<HSceneObject> todo;
- todo.push(sceneObject);
- while (!todo.empty())
- {
- HSceneObject currentSO = todo.top();
- todo.pop();
- if (currentSO->mLinkId == -1)
- objectsToId.push_back(currentSO);
- else
- existingIds.insert(currentSO->mLinkId);
- for (auto& component : currentSO->mComponents)
- {
- if (component->mLinkId == -1)
- objectsToId.push_back(component);
- else
- existingIds.insert(component->mLinkId);
- }
- UINT32 numChildren = (UINT32)currentSO->getNumChildren();
- for (UINT32 i = 0; i < numChildren; i++)
- {
- HSceneObject child = currentSO->getChild(i);
- if (child->mPrefabLink == nullptr)
- todo.push(currentSO->getChild(i));
- }
- }
- auto setIter = existingIds.begin();
- INT32 nextId = 0;
- for (auto& object : objectsToId)
- {
- INT32 freeId = -1;
- for (; setIter != existingIds.end(); ++setIter)
- {
- if (nextId < (*setIter))
- freeId = nextId++;
- else
- nextId++;
- }
- if (freeId == -1)
- freeId = nextId++;
- object->mLinkId = freeId;
- }
- }
- void PrefabUtility::clearPrefabIds(const HSceneObject& sceneObject)
- {
- Stack<HSceneObject> todo;
- todo.push(sceneObject);
- while (!todo.empty())
- {
- HSceneObject currentSO = todo.top();
- todo.pop();
- currentSO->mLinkId = -1;
- for (auto& component : currentSO->mComponents)
- component->mLinkId = -1;
- UINT32 numChildren = (UINT32)currentSO->getNumChildren();
- for (UINT32 i = 0; i < numChildren; i++)
- {
- HSceneObject child = currentSO->getChild(i);
- if (child->mPrefabLink == nullptr)
- todo.push(child);
- }
- }
- }
- void PrefabUtility::recordInstanceData(const HSceneObject& so, SceneObjectProxy& output,
- UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
- {
- struct StackEntry
- {
- HSceneObject so;
- bool isPartOfPrefab;
- };
- Stack<StackEntry> todo;
- todo.push(StackEntry());
- StackEntry& topEntry = todo.top();
- topEntry.so = so;
- topEntry.isPartOfPrefab = true;
- while (!todo.empty())
- {
- StackEntry current = todo.top();
- todo.pop();
- output.instanceData = current.so->_getInstanceData();
- if (current.isPartOfPrefab)
- {
- output.linkId = current.so->getLinkId();
- linkedInstanceData[output.linkId] = output.instanceData;
- }
- else
- output.linkId = -1;
- const Vector<HComponent>& components = current.so->getComponents();
- for (auto& component : components)
- {
- output.components.push_back(ComponentProxy());
- ComponentProxy& componentProxy = output.components.back();
- componentProxy.instanceData = component->_getInstanceData();
- if (current.isPartOfPrefab)
- {
- componentProxy.linkId = component->getLinkId();
- linkedInstanceData[componentProxy.linkId] = componentProxy.instanceData;
- }
- else
- componentProxy.linkId = -1;
- }
- UINT32 numChildren = current.so->getNumChildren();
- for (UINT32 i = 0; i < numChildren; i++)
- {
- HSceneObject child = current.so->getChild(i);
- todo.push(StackEntry());
- StackEntry& newEntry = todo.top();
- newEntry.so = child;
- newEntry.isPartOfPrefab = current.isPartOfPrefab && (child->mPrefabLink == nullptr);
- }
- }
- }
- void PrefabUtility::restoreLinkedInstanceData(const HSceneObject& so, UnorderedMap<UINT32, GameObjectInstanceDataPtr>& linkedInstanceData)
- {
- Stack<HSceneObject> todo;
- todo.push(so);
- while (!todo.empty())
- {
- HSceneObject current = todo.top();
- todo.pop();
- if (current->getLinkId() != -1)
- {
- auto iterFind = linkedInstanceData.find(current->getLinkId());
- if (iterFind != linkedInstanceData.end())
- current->_setInstanceData(iterFind->second);
- }
- const Vector<HComponent>& components = current->getComponents();
- for (auto& component : components)
- {
- if (component->getLinkId() != -1)
- {
- auto iterFind = linkedInstanceData.find(component->getLinkId());
- if (iterFind != linkedInstanceData.end())
- component->_setInstanceData(iterFind->second);
- }
- }
- UINT32 numChildren = current->getNumChildren();
- for (UINT32 i = 0; i < numChildren; i++)
- {
- HSceneObject child = current->getChild(i);
- if (child->mPrefabLink == nullptr)
- todo.push(child);
- }
- }
- }
- void PrefabUtility::restoreUnlinkedInstanceData(const HSceneObject& so, SceneObjectProxy& proxy)
- {
- struct StackEntry
- {
- HSceneObject so;
- SceneObjectProxy* proxy;
- };
- Stack<StackEntry> todo;
- todo.push(StackEntry());
- assert(so->getLinkId() == proxy.linkId);
-
- StackEntry& topEntry = todo.top();
- topEntry.so = so;
- topEntry.proxy = &proxy;
- while (!todo.empty())
- {
- StackEntry current = todo.top();
- todo.pop();
- if (current.proxy->linkId == -1)
- current.so->_setInstanceData(current.proxy->instanceData);
- const Vector<HComponent>& components = current.so->getComponents();
- UINT32 componentProxyIdx = 0;
- UINT32 numComponentProxies = (UINT32)current.proxy->components.size();
- for (auto& component : components)
- {
- if (component->getLinkId() == -1)
- {
- bool foundInstanceData = false;
- for (; componentProxyIdx < numComponentProxies; componentProxyIdx++)
- {
- if (current.proxy->components[componentProxyIdx].linkId != -1)
- continue;
- assert(current.proxy->components[componentProxyIdx].linkId == -1);
- component->_setInstanceData(current.proxy->components[componentProxyIdx].instanceData);
- foundInstanceData = true;
- break;
- }
- assert(foundInstanceData);
- }
- }
- UINT32 numChildren = current.so->getNumChildren();
- UINT32 childProxyIdx = 0;
- UINT32 numChildProxies = (UINT32)current.proxy->children.size();
- for (UINT32 i = 0; i < numChildren; i++)
- {
- HSceneObject child = current.so->getChild(i);
- if (child->getLinkId() == -1)
- {
- bool foundInstanceData = false;
- for (; childProxyIdx < numChildProxies; childProxyIdx++)
- {
- if (current.proxy->children[childProxyIdx].linkId != -1)
- continue;
- assert(current.proxy->children[childProxyIdx].linkId == -1);
- child->_setInstanceData(current.proxy->children[childProxyIdx].instanceData);
- todo.push(StackEntry());
- StackEntry& newEntry = todo.top();
- newEntry.so = child;
- newEntry.proxy = ¤t.proxy->children[childProxyIdx];
- foundInstanceData = true;
- break;
- }
- assert(foundInstanceData);
- }
- else
- {
- for (UINT32 j = 0; j < numChildProxies; j++)
- {
- if (child->getLinkId() == current.proxy->children[j].linkId)
- {
- todo.push(StackEntry());
- StackEntry& newEntry = todo.top();
- newEntry.so = child;
- newEntry.proxy = ¤t.proxy->children[j];
- break;
- }
- }
- }
- }
- }
- }
- }
|