CResourceStore: Make use of ranged for where applicable

Same behavior, less code.
This commit is contained in:
Lioncash 2020-06-18 06:47:00 -04:00
parent e9dd2c57a6
commit 1b71d91ea7
1 changed files with 58 additions and 53 deletions

View File

@ -117,13 +117,13 @@ bool CResourceStore::SerializeDatabaseCache(IArchive& rArc)
if (rArc.IsReader()) if (rArc.IsReader())
{ {
for (auto Iter = EmptyDirectories.begin(); Iter != EmptyDirectories.end(); Iter++) for (const auto& dir : EmptyDirectories)
{ {
// Don't create empty virtual directories that don't actually exist in the filesystem // Don't create empty virtual directories that don't actually exist in the filesystem
TString AbsPath = ResourcesDir() + *Iter; const TString AbsPath = ResourcesDir() + dir;
if (FileUtil::Exists(AbsPath)) if (FileUtil::Exists(AbsPath))
CreateVirtualDirectory(*Iter); CreateVirtualDirectory(dir);
} }
} }
@ -148,7 +148,10 @@ bool CResourceStore::LoadDatabaseCache()
if (!BuildFromDirectory(true)) if (!BuildFromDirectory(true))
return false; return false;
} }
else return false; else
{
return false;
}
} }
else else
{ {
@ -181,32 +184,34 @@ bool CResourceStore::SaveDatabaseCache()
void CResourceStore::ConditionalSaveStore() void CResourceStore::ConditionalSaveStore()
{ {
if (mDatabaseCacheDirty) SaveDatabaseCache(); if (mDatabaseCacheDirty)
SaveDatabaseCache();
} }
void CResourceStore::SetProject(CGameProject *pProj) void CResourceStore::SetProject(CGameProject *pProj)
{ {
if (mpProj == pProj) return; if (mpProj == pProj)
return;
if (mpProj) if (mpProj)
CloseProject(); CloseProject();
mpProj = pProj; mpProj = pProj;
if (mpProj) if (!mpProj)
{ return;
mDatabasePath = mpProj->ProjectRoot(); mDatabasePath = mpProj->ProjectRoot();
mpDatabaseRoot = new CVirtualDirectory(this); mpDatabaseRoot = new CVirtualDirectory(this);
mGame = mpProj->Game(); mGame = mpProj->Game();
// Clear deleted files from previous runs // Clear deleted files from previous runs
TString DeletedPath = DeletedResourcePath(); const TString DeletedPath = DeletedResourcePath();
if (FileUtil::Exists(DeletedPath)) if (FileUtil::Exists(DeletedPath))
{ {
FileUtil::ClearDirectory(DeletedPath); FileUtil::ClearDirectory(DeletedPath);
} }
}
} }
void CResourceStore::CloseProject() void CResourceStore::CloseProject()
@ -221,9 +226,9 @@ void CResourceStore::CloseProject()
{ {
warnf("%d resources still loaded on project close:", mLoadedResources.size()); warnf("%d resources still loaded on project close:", mLoadedResources.size());
for (auto Iter = mLoadedResources.begin(); Iter != mLoadedResources.end(); Iter++) for (const auto& entry : mLoadedResources)
{ {
CResourceEntry *pEntry = Iter->second; const CResourceEntry *pEntry = entry.second;
warnf("\t%s.%s", *pEntry->Name(), *pEntry->CookedExtension().ToString()); warnf("\t%s.%s", *pEntry->Name(), *pEntry->CookedExtension().ToString());
} }
@ -234,7 +239,7 @@ void CResourceStore::CloseProject()
mResourceEntries.clear(); mResourceEntries.clear();
// Clear deleted files from previous runs // Clear deleted files from previous runs
TString DeletedPath = DeletedResourcePath(); const TString DeletedPath = DeletedResourcePath();
if (FileUtil::Exists(DeletedPath)) if (FileUtil::Exists(DeletedPath))
{ {
@ -251,9 +256,10 @@ CVirtualDirectory* CResourceStore::GetVirtualDirectory(const TString& rkPath, bo
{ {
if (rkPath.IsEmpty()) if (rkPath.IsEmpty())
return mpDatabaseRoot; return mpDatabaseRoot;
else if (mpDatabaseRoot)
if (mpDatabaseRoot)
return mpDatabaseRoot->FindChildDirectory(rkPath, AllowCreate); return mpDatabaseRoot->FindChildDirectory(rkPath, AllowCreate);
else
return nullptr; return nullptr;
} }
@ -291,7 +297,7 @@ CResourceEntry* CResourceStore::FindEntry(const CAssetID& rkID) const
{ {
if (rkID.IsValid()) if (rkID.IsValid())
{ {
auto Found = mResourceEntries.find(rkID); const auto Found = mResourceEntries.find(rkID);
if (Found != mResourceEntries.cend()) if (Found != mResourceEntries.cend())
{ {
@ -307,7 +313,7 @@ CResourceEntry* CResourceStore::FindEntry(const CAssetID& rkID) const
CResourceEntry* CResourceStore::FindEntry(const TString& rkPath) const CResourceEntry* CResourceStore::FindEntry(const TString& rkPath) const
{ {
return (mpDatabaseRoot ? mpDatabaseRoot->FindChildResource(rkPath) : nullptr); return mpDatabaseRoot ? mpDatabaseRoot->FindChildResource(rkPath) : nullptr;
} }
bool CResourceStore::AreAllEntriesValid() const bool CResourceStore::AreAllEntriesValid() const
@ -329,8 +335,8 @@ void CResourceStore::ClearDatabase()
if (!mLoadedResources.empty()) if (!mLoadedResources.empty())
{ {
debugf("ERROR: Resources still loaded:"); debugf("ERROR: Resources still loaded:");
for (auto Iter = mLoadedResources.begin(); Iter != mLoadedResources.end(); Iter++) for (const auto& [asset, entry] : mLoadedResources)
debugf("\t[%s] %s", *Iter->first.ToString(), *Iter->second->CookedAssetPath(true)); debugf("\t[%s] %s", *asset.ToString(), *entry->CookedAssetPath(true));
ASSERT(false); ASSERT(false);
} }
@ -352,10 +358,9 @@ bool CResourceStore::BuildFromDirectory(bool ShouldGenerateCacheFile)
TStringList ResourceList; TStringList ResourceList;
FileUtil::GetDirectoryContents(ResDir, ResourceList); FileUtil::GetDirectoryContents(ResDir, ResourceList);
for (auto Iter = ResourceList.begin(); Iter != ResourceList.end(); Iter++) for (const auto& Path : ResourceList)
{ {
TString Path = *Iter; TString RelPath = Path.ChopFront(ResDir.Size());
TString RelPath = Path.ChopFront( ResDir.Size() );
if (FileUtil::IsFile(Path) && Path.EndsWith(".rsmeta")) if (FileUtil::IsFile(Path) && Path.EndsWith(".rsmeta"))
{ {
@ -363,11 +368,11 @@ bool CResourceStore::BuildFromDirectory(bool ShouldGenerateCacheFile)
TString DirPath = RelPath.GetFileDirectory(); TString DirPath = RelPath.GetFileDirectory();
TString CookedFilename = RelPath.GetFileName(false); // This call removes the .rsmeta extension TString CookedFilename = RelPath.GetFileName(false); // This call removes the .rsmeta extension
TString ResName = CookedFilename.GetFileName(false); // This call removes the cooked extension TString ResName = CookedFilename.GetFileName(false); // This call removes the cooked extension
ASSERT( IsValidResourcePath(DirPath, ResName) ); ASSERT(IsValidResourcePath(DirPath, ResName));
// Determine resource type // Determine resource type
TString CookedExtension = CookedFilename.GetFileExtension(); TString CookedExtension = CookedFilename.GetFileExtension();
CResTypeInfo *pTypeInfo = CResTypeInfo::TypeForCookedExtension( Game(), CFourCC(CookedExtension) ); CResTypeInfo* pTypeInfo = CResTypeInfo::TypeForCookedExtension(Game(), CFourCC(CookedExtension));
if (!pTypeInfo) if (!pTypeInfo)
{ {
@ -517,7 +522,7 @@ CResource* CResourceStore::LoadResource(const TString& rkPath)
if (pEntry) if (pEntry)
{ {
// Verify extension matches the entry + load resource // Verify extension matches the entry + load resource
TString Ext = rkPath.GetFileExtension(); const TString Ext = rkPath.GetFileExtension();
if (!Ext.IsEmpty()) if (!Ext.IsEmpty())
{ {
@ -541,7 +546,7 @@ void CResourceStore::TrackLoadedResource(CResourceEntry *pEntry)
{ {
ASSERT(pEntry->IsLoaded()); ASSERT(pEntry->IsLoaded());
ASSERT(mLoadedResources.find(pEntry->ID()) == mLoadedResources.end()); ASSERT(mLoadedResources.find(pEntry->ID()) == mLoadedResources.end());
mLoadedResources[pEntry->ID()] = pEntry; mLoadedResources.insert_or_assign(pEntry->ID(), pEntry);
} }
void CResourceStore::DestroyUnreferencedResources() void CResourceStore::DestroyUnreferencedResources()
@ -573,14 +578,14 @@ void CResourceStore::DestroyUnreferencedResources()
bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry) bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry)
{ {
CAssetID ID = pEntry->ID(); const CAssetID ID = pEntry->ID();
if (pEntry->IsLoaded()) if (pEntry->IsLoaded())
{ {
if (!pEntry->Unload()) if (!pEntry->Unload())
return false; return false;
auto It = mLoadedResources.find(ID); const auto It = mLoadedResources.find(ID);
ASSERT(It != mLoadedResources.end()); ASSERT(It != mLoadedResources.end());
mLoadedResources.erase(It); mLoadedResources.erase(It);
} }
@ -588,7 +593,7 @@ bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry)
if (pEntry->Directory()) if (pEntry->Directory())
pEntry->Directory()->RemoveChildResource(pEntry); pEntry->Directory()->RemoveChildResource(pEntry);
auto It = mResourceEntries.find(ID); const auto It = mResourceEntries.find(ID);
ASSERT(It != mResourceEntries.end()); ASSERT(It != mResourceEntries.end());
mResourceEntries.erase(It); mResourceEntries.erase(It);
@ -653,7 +658,7 @@ void CResourceStore::ImportNamesFromPakContentsTxt(const TString& rkTxtPath, boo
// Chop name to just after "x_rep" // Chop name to just after "x_rep"
uint32 RepStart = Path.IndexOfPhrase("_rep"); uint32 RepStart = Path.IndexOfPhrase("_rep");
if (RepStart != -1) if (RepStart != UINT32_MAX)
Path = Path.ChopFront(RepStart + 5); Path = Path.ChopFront(RepStart + 5);
// If the "x_rep" folder doesn't exist in this path for some reason, but this is still a path, then just chop off the drive letter. // If the "x_rep" folder doesn't exist in this path for some reason, but this is still a path, then just chop off the drive letter.
@ -661,24 +666,24 @@ void CResourceStore::ImportNamesFromPakContentsTxt(const TString& rkTxtPath, boo
else if (Path[1] == ':') else if (Path[1] == ':')
Path = Path.ChopFront(3); Path = Path.ChopFront(3);
PathMap[pEntry] = Path; PathMap.insert_or_assign(pEntry, std::move(Path));
} }
} }
fclose(pContentsFile); fclose(pContentsFile);
// Assign names // Assign names
for (auto Iter = PathMap.begin(); Iter != PathMap.end(); Iter++) for (auto& [entry, path] : PathMap)
{ {
CResourceEntry *pEntry = Iter->first; if (UnnamedOnly && entry->IsNamed())
if (UnnamedOnly && pEntry->IsNamed()) continue; continue;
TString Path = Iter->second; TString Dir = path.GetFileDirectory();
TString Dir = Path.GetFileDirectory(); TString Name = path.GetFileName(false);
TString Name = Path.GetFileName(false); if (Dir.IsEmpty())
if (Dir.IsEmpty()) Dir = pEntry->DirectoryPath(); Dir = entry->DirectoryPath();
pEntry->MoveAndRename(Dir, Name); entry->MoveAndRename(Dir, Name);
} }
// Save // Save
@ -689,13 +694,13 @@ bool CResourceStore::IsValidResourcePath(const TString& rkPath, const TString& r
{ {
// Path must not be an absolute path and must not go outside the project structure. // Path must not be an absolute path and must not go outside the project structure.
// Name must not be a path. // Name must not be a path.
return ( CVirtualDirectory::IsValidDirectoryPath(rkPath) && return CVirtualDirectory::IsValidDirectoryPath(rkPath) &&
FileUtil::IsValidName(rkName, false) && FileUtil::IsValidName(rkName, false) &&
!rkName.Contains('/') && !rkName.Contains('/') &&
!rkName.Contains('\\') ); !rkName.Contains('\\');
} }
TString CResourceStore::StaticDefaultResourceDirPath(EGame Game) TString CResourceStore::StaticDefaultResourceDirPath(EGame Game)
{ {
return (Game < EGame::CorruptionProto ? "Uncategorized/" : "uncategorized/"); return Game < EGame::CorruptionProto ? "Uncategorized/" : "uncategorized/";
} }