Changeset View
Changeset View
Standalone View
Standalone View
source/graphics/ObjectManager.cpp
Show All 31 Lines | |||||
bool CObjectManager::ObjectKey::operator< (const CObjectManager::ObjectKey& a) const | bool CObjectManager::ObjectKey::operator< (const CObjectManager::ObjectKey& a) const | ||||
{ | { | ||||
if (ActorName < a.ActorName) | if (ActorName < a.ActorName) | ||||
return true; | return true; | ||||
else if (ActorName > a.ActorName) | else if (ActorName > a.ActorName) | ||||
return false; | return false; | ||||
else if (LOD < a.LOD) | |||||
return true; | |||||
else if (LOD > a.LOD) | |||||
return false; | |||||
else | else | ||||
return ActorVariation < a.ActorVariation; | return ActorVariation < a.ActorVariation; | ||||
} | } | ||||
static Status ReloadChangedFileCB(void* param, const VfsPath& path) | static Status ReloadChangedFileCB(void* param, const VfsPath& path) | ||||
{ | { | ||||
return static_cast<CObjectManager*>(param)->ReloadChangedFile(path); | return static_cast<CObjectManager*>(param)->ReloadChangedFile(path); | ||||
} | } | ||||
Show All 9 Lines | |||||
CObjectManager::~CObjectManager() | CObjectManager::~CObjectManager() | ||||
{ | { | ||||
UnloadObjects(); | UnloadObjects(); | ||||
UnregisterFileReloadFunc(ReloadChangedFileCB, this); | UnregisterFileReloadFunc(ReloadChangedFileCB, this); | ||||
} | } | ||||
CActorDef* CObjectManager::FindActorDef(const CStrW& actorName) | |||||
CObjectBase* CObjectManager::FindObjectBase(const CStrW& objectname) | |||||
{ | { | ||||
ENSURE(!objectname.empty()); | ENSURE(!actorName.empty()); | ||||
// See if the base type has been loaded yet: | decltype(m_ActorDefs)::iterator it = m_ActorDefs.find(actorName); | ||||
if (it != m_ActorDefs.end()) | |||||
return it->second.get(); | |||||
std::map<CStrW, CObjectBase*>::iterator it = m_ObjectBases.find(objectname); | std::unique_ptr<CActorDef> actor = std::make_unique<CActorDef>(*this); | ||||
if (it != m_ObjectBases.end()) | |||||
return it->second; | |||||
// Not already loaded, so try to load it: | VfsPath pathname = VfsPath("art/actors/") / actorName; | ||||
CObjectBase* obj = new CObjectBase(*this); | if (actor->Load(pathname)) | ||||
return m_ActorDefs.emplace(actorName, std::move(actor)).first->second.get(); | |||||
VfsPath pathname = VfsPath("art/actors/") / objectname; | LOGERROR("CObjectManager::FindActorDef(): Cannot find actor '%s'", utf8_from_wstring(actorName)); | ||||
if (obj->Load(pathname)) | return nullptr; | ||||
{ | |||||
m_ObjectBases[objectname] = obj; | |||||
return obj; | |||||
} | } | ||||
else | |||||
delete obj; | |||||
LOGERROR("CObjectManager::FindObjectBase(): Cannot find object '%s'", utf8_from_wstring(objectname)); | CObjectEntry* CObjectManager::FindObjectVariation(const CStrW& objname, u8 LODLevel, const std::vector<std::set<CStr> >& selections) | ||||
{ | |||||
CActorDef* base = FindActorDef(objname); | |||||
if (!base) | |||||
return nullptr; | |||||
return 0; | return FindObjectVariation(base, LODLevel, selections); | ||||
} | } | ||||
CObjectEntry* CObjectManager::FindObject(const CStrW& objname) | CObjectEntry* CObjectManager::FindObjectVariation(const CObjectEntry* entry, const std::vector<std::set<CStr>>& selections) | ||||
{ | { | ||||
std::vector<std::set<CStr> > selections; // TODO - should this really be empty? | return FindObjectVariation(&entry->m_Base, selections); | ||||
return FindObjectVariation(objname, selections); | |||||
} | } | ||||
CObjectEntry* CObjectManager::FindObjectVariation(const CStrW& objname, const std::vector<std::set<CStr> >& selections) | CObjectEntry* CObjectManager::FindObjectVariation(const CActorDef* actor, u8 LODLevel, const std::vector<std::set<CStr> >& selections) | ||||
{ | { | ||||
CObjectBase* base = FindObjectBase(objname); | return FindObjectVariation(actor->GetBase(LODLevel), selections); | ||||
if (! base) | |||||
return NULL; | |||||
return FindObjectVariation(base, selections); | |||||
} | } | ||||
CObjectEntry* CObjectManager::FindObjectVariation(CObjectBase* base, const std::vector<std::set<CStr> >& selections) | CObjectEntry* CObjectManager::FindObjectVariation(const CObjectBase* base, const std::vector<std::set<CStr> >& selections) | ||||
{ | { | ||||
PROFILE("object variation loading"); | PROFILE2("LOD_ObjectVariation"); | ||||
// Look to see whether this particular variation has already been loaded | // Look to see whether this particular variation has already been loaded | ||||
std::vector<u8> choices = base->CalculateVariationKey(selections); | std::vector<u8> choices = base->CalculateVariationKey(selections); | ||||
ObjectKey key (base->m_Pathname.string(), choices); | ObjectKey key (base->GetPathname(), base->m_LODLevel, choices); | ||||
decltype(m_Objects)::iterator it = m_Objects.find(key); | |||||
std::map<ObjectKey, CObjectEntry*>::iterator it = m_Objects.find(key); | |||||
if (it != m_Objects.end() && !it->second->m_Outdated) | if (it != m_Objects.end() && !it->second->m_Outdated) | ||||
Stan: Can't it be the loop condition? | |||||
Done Inline ActionsNo, the loop needs to run once anyways, but I need to check the pre-incremented value. It's plausible that I'm missing an obvious way to do this, but I didn't find an easier one. wraitii: No, the loop needs to run once anyways, but I need to check the pre-incremented value.
It's… | |||||
return it->second; | return it->second.get(); | ||||
// If it hasn't been loaded, load it now | |||||
// TODO: If there was an existing ObjectEntry, but it's outdated (due to hotloading), | // If it hasn't been loaded, load it now. | ||||
// we'll get a memory leak when replacing its entry in m_Objects. The problem is | |||||
// some CUnits might still have a pointer to the old ObjectEntry so we probably can't | |||||
// just delete it now. Probably we need to redesign the caching/hotloading system so it | |||||
// makes more sense (e.g. use shared_ptr); for now I'll just leak, to avoid making the logic | |||||
// more complex than it is already is, since this only matters for the rare case of hotloading. | |||||
CObjectEntry* obj = new CObjectEntry(base, m_Simulation); // TODO: type ? | std::unique_ptr<CObjectEntry> obj = std::make_unique<CObjectEntry>(*base, m_Simulation); | ||||
// TODO (for some efficiency): use the pre-calculated choices for this object, | // TODO (for some efficiency): use the pre-calculated choices for this object, | ||||
// which has already worked out what to do for props, instead of passing the | // which has already worked out what to do for props, instead of passing the | ||||
// selections into BuildVariation and having it recalculate the props' choices. | // selections into BuildVariation and having it recalculate the props' choices. | ||||
if (! obj->BuildVariation(selections, choices, *this)) | if (!obj->BuildVariation(selections, choices, *this)) | ||||
{ | return nullptr; | ||||
DeleteObject(obj); | |||||
return NULL; | |||||
} | |||||
m_Objects[key] = obj; | |||||
return obj; | return m_Objects.emplace(key, std::move(obj)).first->second.get(); | ||||
} | } | ||||
CTerrain* CObjectManager::GetTerrain() | CTerrain* CObjectManager::GetTerrain() | ||||
{ | { | ||||
CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY); | CmpPtr<ICmpTerrain> cmpTerrain(m_Simulation, SYSTEM_ENTITY); | ||||
if (!cmpTerrain) | if (!cmpTerrain) | ||||
return NULL; | return NULL; | ||||
return cmpTerrain->GetCTerrain(); | return cmpTerrain->GetCTerrain(); | ||||
} | } | ||||
void CObjectManager::DeleteObject(CObjectEntry* entry) | |||||
{ | |||||
std::function<bool(const std::pair<ObjectKey, CObjectEntry*>&)> second_equals = | |||||
[&entry](const std::pair<ObjectKey, CObjectEntry*>& a) { return a.second == entry; }; | |||||
std::map<ObjectKey, CObjectEntry*>::iterator it = m_Objects.begin(); | |||||
while (m_Objects.end() != (it = find_if(it, m_Objects.end(), second_equals))) | |||||
it = m_Objects.erase(it); | |||||
delete entry; | |||||
} | |||||
void CObjectManager::UnloadObjects() | void CObjectManager::UnloadObjects() | ||||
{ | { | ||||
for (const std::pair<const ObjectKey, CObjectEntry*>& p : m_Objects) | |||||
delete p.second; | |||||
m_Objects.clear(); | m_Objects.clear(); | ||||
m_ActorDefs.clear(); | |||||
for (const std::pair<const CStrW, CObjectBase*>& p : m_ObjectBases) | |||||
delete p.second; | |||||
m_ObjectBases.clear(); | |||||
} | } | ||||
Status CObjectManager::ReloadChangedFile(const VfsPath& path) | Status CObjectManager::ReloadChangedFile(const VfsPath& path) | ||||
{ | { | ||||
// TODO | |||||
/* | |||||
// Mark old entries as outdated so we don't reload them from the cache | // Mark old entries as outdated so we don't reload them from the cache | ||||
for (std::map<ObjectKey, CObjectEntry*>::iterator it = m_Objects.begin(); it != m_Objects.end(); ++it) | for (std::map<ObjectKey, CObjectEntry*>::iterator it = m_Objects.begin(); it != m_Objects.end(); ++it) | ||||
if (it->second->m_Base->UsesFile(path)) | if (it->second->m_Base->UsesFile(path)) | ||||
it->second->m_Outdated = true; | it->second->m_Outdated = true; | ||||
// Reload actors that use a changed object | // Reload actors that use a changed object | ||||
for (std::map<CStrW, CObjectBase*>::iterator it = m_ObjectBases.begin(); it != m_ObjectBases.end(); ++it) | for (std::map<CStrW, CObjectBase*>::iterator it = m_ObjectBases.begin(); it != m_ObjectBases.end(); ++it) | ||||
{ | { | ||||
if (it->second->UsesFile(path)) | if (it->second->UsesFile(path)) | ||||
{ | { | ||||
it->second->Reload(); | it->second->Reload(); | ||||
// Slightly ugly hack: The graphics system doesn't preserve enough information to regenerate the | // Slightly ugly hack: The graphics system doesn't preserve enough information to regenerate the | ||||
// object with all correct variations, and we don't want to waste space storing it just for the | // object with all correct variations, and we don't want to waste space storing it just for the | ||||
// rare occurrence of hotloading, so we'll tell the component (which does preserve the information) | // rare occurrence of hotloading, so we'll tell the component (which does preserve the information) | ||||
// to do the reloading itself | // to do the reloading itself | ||||
const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual); | const CSimulation2::InterfaceListUnordered& cmps = m_Simulation.GetEntitiesWithInterfaceUnordered(IID_Visual); | ||||
for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit) | for (CSimulation2::InterfaceListUnordered::const_iterator eit = cmps.begin(); eit != cmps.end(); ++eit) | ||||
static_cast<ICmpVisual*>(eit->second)->Hotload(it->first); | static_cast<ICmpVisual*>(eit->second)->Hotload(it->first); | ||||
} | } | ||||
} | } | ||||
*/ | |||||
return INFO::OK; | return INFO::OK; | ||||
} | } |
Wildfire Games · Phabricator
Can't it be the loop condition?