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())
{
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
TString AbsPath = ResourcesDir() + *Iter;
const TString AbsPath = ResourcesDir() + dir;
if (FileUtil::Exists(AbsPath))
CreateVirtualDirectory(*Iter);
CreateVirtualDirectory(dir);
}
}
@ -148,7 +148,10 @@ bool CResourceStore::LoadDatabaseCache()
if (!BuildFromDirectory(true))
return false;
}
else return false;
else
{
return false;
}
}
else
{
@ -181,31 +184,33 @@ bool CResourceStore::SaveDatabaseCache()
void CResourceStore::ConditionalSaveStore()
{
if (mDatabaseCacheDirty) SaveDatabaseCache();
if (mDatabaseCacheDirty)
SaveDatabaseCache();
}
void CResourceStore::SetProject(CGameProject *pProj)
{
if (mpProj == pProj) return;
if (mpProj == pProj)
return;
if (mpProj)
CloseProject();
mpProj = pProj;
if (mpProj)
if (!mpProj)
return;
mDatabasePath = mpProj->ProjectRoot();
mpDatabaseRoot = new CVirtualDirectory(this);
mGame = mpProj->Game();
// Clear deleted files from previous runs
const TString DeletedPath = DeletedResourcePath();
if (FileUtil::Exists(DeletedPath))
{
mDatabasePath = mpProj->ProjectRoot();
mpDatabaseRoot = new CVirtualDirectory(this);
mGame = mpProj->Game();
// Clear deleted files from previous runs
TString DeletedPath = DeletedResourcePath();
if (FileUtil::Exists(DeletedPath))
{
FileUtil::ClearDirectory(DeletedPath);
}
FileUtil::ClearDirectory(DeletedPath);
}
}
@ -221,9 +226,9 @@ void CResourceStore::CloseProject()
{
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());
}
@ -234,7 +239,7 @@ void CResourceStore::CloseProject()
mResourceEntries.clear();
// Clear deleted files from previous runs
TString DeletedPath = DeletedResourcePath();
const TString DeletedPath = DeletedResourcePath();
if (FileUtil::Exists(DeletedPath))
{
@ -251,10 +256,11 @@ CVirtualDirectory* CResourceStore::GetVirtualDirectory(const TString& rkPath, bo
{
if (rkPath.IsEmpty())
return mpDatabaseRoot;
else if (mpDatabaseRoot)
if (mpDatabaseRoot)
return mpDatabaseRoot->FindChildDirectory(rkPath, AllowCreate);
else
return nullptr;
return nullptr;
}
void CResourceStore::CreateVirtualDirectory(const TString& rkPath)
@ -291,7 +297,7 @@ CResourceEntry* CResourceStore::FindEntry(const CAssetID& rkID) const
{
if (rkID.IsValid())
{
auto Found = mResourceEntries.find(rkID);
const auto Found = mResourceEntries.find(rkID);
if (Found != mResourceEntries.cend())
{
@ -307,7 +313,7 @@ CResourceEntry* CResourceStore::FindEntry(const CAssetID& rkID) const
CResourceEntry* CResourceStore::FindEntry(const TString& rkPath) const
{
return (mpDatabaseRoot ? mpDatabaseRoot->FindChildResource(rkPath) : nullptr);
return mpDatabaseRoot ? mpDatabaseRoot->FindChildResource(rkPath) : nullptr;
}
bool CResourceStore::AreAllEntriesValid() const
@ -329,8 +335,8 @@ void CResourceStore::ClearDatabase()
if (!mLoadedResources.empty())
{
debugf("ERROR: Resources still loaded:");
for (auto Iter = mLoadedResources.begin(); Iter != mLoadedResources.end(); Iter++)
debugf("\t[%s] %s", *Iter->first.ToString(), *Iter->second->CookedAssetPath(true));
for (const auto& [asset, entry] : mLoadedResources)
debugf("\t[%s] %s", *asset.ToString(), *entry->CookedAssetPath(true));
ASSERT(false);
}
@ -352,10 +358,9 @@ bool CResourceStore::BuildFromDirectory(bool ShouldGenerateCacheFile)
TStringList 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"))
{
@ -363,11 +368,11 @@ bool CResourceStore::BuildFromDirectory(bool ShouldGenerateCacheFile)
TString DirPath = RelPath.GetFileDirectory();
TString CookedFilename = RelPath.GetFileName(false); // This call removes the .rsmeta extension
TString ResName = CookedFilename.GetFileName(false); // This call removes the cooked extension
ASSERT( IsValidResourcePath(DirPath, ResName) );
ASSERT(IsValidResourcePath(DirPath, ResName));
// Determine resource type
TString CookedExtension = CookedFilename.GetFileExtension();
CResTypeInfo *pTypeInfo = CResTypeInfo::TypeForCookedExtension( Game(), CFourCC(CookedExtension) );
CResTypeInfo* pTypeInfo = CResTypeInfo::TypeForCookedExtension(Game(), CFourCC(CookedExtension));
if (!pTypeInfo)
{
@ -517,7 +522,7 @@ CResource* CResourceStore::LoadResource(const TString& rkPath)
if (pEntry)
{
// Verify extension matches the entry + load resource
TString Ext = rkPath.GetFileExtension();
const TString Ext = rkPath.GetFileExtension();
if (!Ext.IsEmpty())
{
@ -541,7 +546,7 @@ void CResourceStore::TrackLoadedResource(CResourceEntry *pEntry)
{
ASSERT(pEntry->IsLoaded());
ASSERT(mLoadedResources.find(pEntry->ID()) == mLoadedResources.end());
mLoadedResources[pEntry->ID()] = pEntry;
mLoadedResources.insert_or_assign(pEntry->ID(), pEntry);
}
void CResourceStore::DestroyUnreferencedResources()
@ -573,14 +578,14 @@ void CResourceStore::DestroyUnreferencedResources()
bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry)
{
CAssetID ID = pEntry->ID();
const CAssetID ID = pEntry->ID();
if (pEntry->IsLoaded())
{
if (!pEntry->Unload())
return false;
auto It = mLoadedResources.find(ID);
const auto It = mLoadedResources.find(ID);
ASSERT(It != mLoadedResources.end());
mLoadedResources.erase(It);
}
@ -588,7 +593,7 @@ bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry)
if (pEntry->Directory())
pEntry->Directory()->RemoveChildResource(pEntry);
auto It = mResourceEntries.find(ID);
const auto It = mResourceEntries.find(ID);
ASSERT(It != mResourceEntries.end());
mResourceEntries.erase(It);
@ -653,7 +658,7 @@ void CResourceStore::ImportNamesFromPakContentsTxt(const TString& rkTxtPath, boo
// Chop name to just after "x_rep"
uint32 RepStart = Path.IndexOfPhrase("_rep");
if (RepStart != -1)
if (RepStart != UINT32_MAX)
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.
@ -661,24 +666,24 @@ void CResourceStore::ImportNamesFromPakContentsTxt(const TString& rkTxtPath, boo
else if (Path[1] == ':')
Path = Path.ChopFront(3);
PathMap[pEntry] = Path;
PathMap.insert_or_assign(pEntry, std::move(Path));
}
}
fclose(pContentsFile);
// Assign names
for (auto Iter = PathMap.begin(); Iter != PathMap.end(); Iter++)
for (auto& [entry, path] : PathMap)
{
CResourceEntry *pEntry = Iter->first;
if (UnnamedOnly && pEntry->IsNamed()) continue;
if (UnnamedOnly && entry->IsNamed())
continue;
TString Path = Iter->second;
TString Dir = Path.GetFileDirectory();
TString Name = Path.GetFileName(false);
if (Dir.IsEmpty()) Dir = pEntry->DirectoryPath();
TString Dir = path.GetFileDirectory();
TString Name = path.GetFileName(false);
if (Dir.IsEmpty())
Dir = entry->DirectoryPath();
pEntry->MoveAndRename(Dir, Name);
entry->MoveAndRename(Dir, Name);
}
// 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.
// Name must not be a path.
return ( CVirtualDirectory::IsValidDirectoryPath(rkPath) &&
FileUtil::IsValidName(rkName, false) &&
!rkName.Contains('/') &&
!rkName.Contains('\\') );
return CVirtualDirectory::IsValidDirectoryPath(rkPath) &&
FileUtil::IsValidName(rkName, false) &&
!rkName.Contains('/') &&
!rkName.Contains('\\');
}
TString CResourceStore::StaticDefaultResourceDirPath(EGame Game)
{
return (Game < EGame::CorruptionProto ? "Uncategorized/" : "uncategorized/");
return Game < EGame::CorruptionProto ? "Uncategorized/" : "uncategorized/";
}