Dropped support for transient resources; I am not using this functionality at all whatsoever and likely won't for a while, so why do I have it?

This commit is contained in:
Aruki
2017-05-07 20:29:33 -06:00
parent 283444cea4
commit f72f82d519
29 changed files with 156 additions and 330 deletions

View File

@@ -32,7 +32,7 @@ void ApplyGeneratedName(CResourceEntry *pEntry, const TString& rkDir, const TStr
if (pEntry->Game() >= eCorruptionProto)
SanitizedDir = SanitizedDir.ToLower();
CVirtualDirectory *pNewDir = pEntry->ResourceStore()->GetVirtualDirectory(SanitizedDir, false, true);
CVirtualDirectory *pNewDir = pEntry->ResourceStore()->GetVirtualDirectory(SanitizedDir, true);
if (pEntry->Directory() == pNewDir && pEntry->Name() == SanitizedName) return;
TString Name = SanitizedName;

View File

@@ -468,35 +468,32 @@ void CGameExporter::ExportResourceEditorData()
// should really be doing this by dependency order instead of by ID order.
for (CResourceIterator It(mpStore); It; ++It)
{
if (!It->IsTransient())
// Worlds need some info we can only get from the pak at export time; namely, which areas can
// have duplicates, as well as the world's internal name.
if (It->ResourceType() == eWorld)
{
// Worlds need some info we can only get from the pak at export time; namely, which areas can
// have duplicates, as well as the world's internal name.
if (It->ResourceType() == eWorld)
CWorld *pWorld = (CWorld*) It->Load();
// Set area duplicate flags
for (u32 iArea = 0; iArea < pWorld->NumAreas(); iArea++)
{
CWorld *pWorld = (CWorld*) It->Load();
CAssetID AreaID = pWorld->AreaResourceID(iArea);
auto Find = mAreaDuplicateMap.find(AreaID);
// Set area duplicate flags
for (u32 iArea = 0; iArea < pWorld->NumAreas(); iArea++)
{
CAssetID AreaID = pWorld->AreaResourceID(iArea);
auto Find = mAreaDuplicateMap.find(AreaID);
if (Find != mAreaDuplicateMap.end())
pWorld->SetAreaAllowsPakDuplicates(iArea, Find->second);
}
// Set world name
TString WorldName = MakeWorldName(pWorld->ID());
pWorld->SetName(WorldName);
if (Find != mAreaDuplicateMap.end())
pWorld->SetAreaAllowsPakDuplicates(iArea, Find->second);
}
// Save raw resource + generate dependencies
if (It->TypeInfo()->CanBeSerialized())
It->Save(true);
else
It->UpdateDependencies();
// Set world name
TString WorldName = MakeWorldName(pWorld->ID());
pWorld->SetName(WorldName);
}
// Save raw resource + generate dependencies
if (It->TypeInfo()->CanBeSerialized())
It->Save(true);
else
It->UpdateDependencies();
}
}
{

View File

@@ -12,7 +12,7 @@
CResourceEntry::CResourceEntry(CResourceStore *pStore, const CAssetID& rkID,
const TString& rkDir, const TString& rkFilename,
EResType Type, bool Transient /*= false*/)
EResType Type)
: mpResource(nullptr)
, mpStore(pStore)
, mpDependencies(nullptr)
@@ -25,9 +25,7 @@ CResourceEntry::CResourceEntry(CResourceStore *pStore, const CAssetID& rkID,
mpTypeInfo = CResTypeInfo::FindTypeInfo(Type);
ASSERT(mpTypeInfo);
if (Transient) mFlags |= eREF_Transient;
mpDirectory = mpStore->GetVirtualDirectory(rkDir, Transient, true);
mpDirectory = mpStore->GetVirtualDirectory(rkDir, true);
if (mpDirectory) mpDirectory->AddChild("", this);
}
@@ -39,8 +37,6 @@ CResourceEntry::~CResourceEntry()
void CResourceEntry::SerializeCacheData(IArchive& rArc)
{
ASSERT(!IsTransient());
u32 Flags = mFlags & eREF_SavedFlags;
rArc << SERIAL_AUTO(Flags);
if (rArc.IsReader()) mFlags = Flags & eREF_SavedFlags;
@@ -99,7 +95,7 @@ TString CResourceEntry::RawAssetPath(bool Relative) const
TString Ext = RawExtension();
TString Path = mpDirectory ? mpDirectory->FullPath() : "";
TString Name = mName + "." + Ext;
return ((IsTransient() || Relative) ? Path + Name : mpStore->RawDir(false) + Path + Name);
return Relative ? Path + Name : mpStore->RawDir(false) + Path + Name;
}
TString CResourceEntry::RawExtension() const
@@ -112,7 +108,7 @@ TString CResourceEntry::CookedAssetPath(bool Relative) const
TString Ext = CookedExtension().ToString();
TString Path = mpDirectory ? mpDirectory->FullPath() : "";
TString Name = mName + "." + Ext;
return ((IsTransient() || Relative) ? Path + Name : mpStore->CookedDir(false) + Path + Name);
return Relative ? Path + Name : mpStore->CookedDir(false) + Path + Name;
}
CFourCC CResourceEntry::CookedExtension() const
@@ -267,11 +263,12 @@ bool CResourceEntry::Cook()
CResource* CResourceEntry::Load()
{
// If the asset is already loaded then just return it immediately
if (mpResource) return mpResource;
// Always try to load raw version as the raw version contains extra editor-only data.
// If there is no raw version (which will be the case for resource types that don't
// support serialization yet) then load the cooked version as a backup.
if (mpResource) return mpResource;
if (HasRawVersion())
{
mpResource = CResourceFactory::SpawnResource(this);
@@ -353,14 +350,11 @@ bool CResourceEntry::Unload()
bool CResourceEntry::CanMoveTo(const TString& rkDir, const TString& rkName)
{
// Transient resources can't be moved
if (IsTransient()) return false;
// Validate that the path/name are valid
if (!mpStore->IsValidResourcePath(rkDir, rkName)) return false;
// We need to validate the path isn't taken already - either the directory doesn't exist, or doesn't have a resource by this name
CVirtualDirectory *pDir = mpStore->GetVirtualDirectory(rkDir, false, false);
CVirtualDirectory *pDir = mpStore->GetVirtualDirectory(rkDir, false);
if (pDir && pDir->FindChildResource(rkName, ResourceType())) return false;
// All checks are true
@@ -378,7 +372,7 @@ bool CResourceEntry::Move(const TString& rkDir, const TString& rkName)
TString OldRawPath = RawAssetPath();
// Set new directory and name
CVirtualDirectory *pNewDir = mpStore->GetVirtualDirectory(rkDir, IsTransient(), true);
CVirtualDirectory *pNewDir = mpStore->GetVirtualDirectory(rkDir, true);
if (pNewDir == mpDirectory && rkName == mName) return false;
// Check if we can legally move to this spot
@@ -459,35 +453,6 @@ bool CResourceEntry::Move(const TString& rkDir, const TString& rkName)
}
}
void CResourceEntry::AddToProject(const TString& rkDir, const TString& rkName)
{
if (mFlags.HasFlag(eREF_Transient))
{
mFlags.ClearFlag(eREF_Transient);
Move(rkDir, rkName);
}
else
{
Log::Error("AddToProject called on non-transient resource entry: " + CookedAssetPath(true));
}
}
void CResourceEntry::RemoveFromProject()
{
if (!mFlags.HasFlag(eREF_Transient))
{
TString Dir = CookedAssetPath().GetFileDirectory();
mFlags.SetFlag(eREF_Transient);
Move(Dir, mName);
}
else
{
Log::Error("RemoveFromProject called on transient resource entry: " + CookedAssetPath(true));
}
}
CGameProject* CResourceEntry::Project() const
{
return mpStore ? mpStore->Project() : nullptr;

View File

@@ -17,7 +17,7 @@ class CDependencyTree;
enum EResEntryFlag
{
eREF_NeedsRecook = 0x00000001, // Resource has been updated but not recooked
eREF_Transient = 0x00000002, // Resource is transient (not part of a game project resource DB)
// UNUSED = 0x00000002,
eREF_Hidden = 0x00000004, // Resource is hidden, doesn't show up in resource browser
eREF_HasBeenModified = 0x00000008, // Resource has been modified and resaved by the user
eREF_IsUserResource = 0x00000010, // Resource was created by the user (i.e. isn't a Retro Studios asset)
@@ -43,7 +43,7 @@ class CResourceEntry
public:
CResourceEntry(CResourceStore *pStore, const CAssetID& rkID,
const TString& rkDir, const TString& rkFilename,
EResType Type, bool Transient = false);
EResType Type);
~CResourceEntry();
void SerializeCacheData(IArchive& rArc);
@@ -65,8 +65,6 @@ public:
bool Unload();
bool CanMoveTo(const TString& rkDir, const TString& rkName);
bool Move(const TString& rkDir, const TString& rkName);
void AddToProject(const TString& rkDir, const TString& rkName);
void RemoveFromProject();
CGameProject* Project() const;
EGame Game() const;
@@ -87,7 +85,6 @@ public:
inline TString Name() const { return mName; }
inline const TString& UppercaseName() const { return mCachedUppercaseName; }
inline EResType ResourceType() const { return mpTypeInfo->Type(); }
inline bool IsTransient() const { return mFlags.HasFlag(eREF_Transient); }
inline bool IsHidden() const { return mFlags.HasFlag(eREF_Hidden); }
protected:

View File

@@ -56,9 +56,6 @@ CResourceStore::~CResourceStore()
for (auto It = mResourceEntries.begin(); It != mResourceEntries.end(); It++)
delete It->second;
for (auto It = mTransientRoots.begin(); It != mTransientRoots.end(); It++)
delete *It;
}
void CResourceStore::SerializeResourceDatabase(IArchive& rArc)
@@ -83,10 +80,7 @@ void CResourceStore::SerializeResourceDatabase(IArchive& rArc)
Resources.reserve(mResourceEntries.size());
for (CResourceIterator It(this); It; ++It)
{
if (!It->IsTransient())
Resources.push_back( SDatabaseResource { It->ID(), It->TypeInfo(), It->Directory()->FullPath(), It->Name() } );
}
Resources.push_back( SDatabaseResource { It->ID(), It->TypeInfo(), It->Directory()->FullPath(), It->Name() } );
}
// Serialize
@@ -175,7 +169,7 @@ bool CResourceStore::LoadCacheFile()
CResourceEntry *pEntry = FindEntry(ID);
if (pEntry && !pEntry->IsTransient())
if (pEntry)
{
CBasicBinaryReader Reader(&CacheFile, Version);
@@ -215,26 +209,23 @@ bool CResourceStore::SaveCacheFile()
// Structure: Entry Asset ID -> Entry Cache Size -> Serialized Entry Cache Data
for (CResourceIterator It(this); It; ++It)
{
if (!It->IsTransient())
ResCount++;
It->ID().Write(CacheFile);
u32 SizeOffset = CacheFile.Tell();
CacheFile.WriteLong(0);
CBasicBinaryWriter Writer(&CacheFile, Version.FileVersion(), Version.Game());
if (Writer.ParamBegin("EntryCache"))
{
ResCount++;
It->ID().Write(CacheFile);
u32 SizeOffset = CacheFile.Tell();
CacheFile.WriteLong(0);
CBasicBinaryWriter Writer(&CacheFile, Version.FileVersion(), Version.Game());
if (Writer.ParamBegin("EntryCache"))
{
It->SerializeCacheData(Writer);
Writer.ParamEnd();
}
u32 EntryCacheEnd = CacheFile.Tell();
CacheFile.Seek(SizeOffset, SEEK_SET);
CacheFile.WriteLong(EntryCacheEnd - SizeOffset - 4);
CacheFile.Seek(EntryCacheEnd, SEEK_SET);
It->SerializeCacheData(Writer);
Writer.ParamEnd();
}
u32 EntryCacheEnd = CacheFile.Tell();
CacheFile.Seek(SizeOffset, SEEK_SET);
CacheFile.WriteLong(EntryCacheEnd - SizeOffset - 4);
CacheFile.Seek(EntryCacheEnd, SEEK_SET);
}
CacheFile.Seek(ResCountOffset, SEEK_SET);
@@ -294,16 +285,8 @@ void CResourceStore::CloseProject()
while (It != mResourceEntries.end())
{
CResourceEntry *pEntry = It->second;
if (!pEntry->IsTransient())
{
delete pEntry;
It = mResourceEntries.erase(It);
}
else
It++;
delete It->second;
It = mResourceEntries.erase(It);
}
delete mpDatabaseRoot;
@@ -312,34 +295,14 @@ void CResourceStore::CloseProject()
mGame = eUnknownGame;
}
CVirtualDirectory* CResourceStore::GetVirtualDirectory(const TString& rkPath, bool Transient, bool AllowCreate)
CVirtualDirectory* CResourceStore::GetVirtualDirectory(const TString& rkPath, bool AllowCreate)
{
if (rkPath.IsEmpty()) return mpDatabaseRoot;
else if (Transient)
{
for (u32 iTrans = 0; iTrans < mTransientRoots.size(); iTrans++)
{
if (mTransientRoots[iTrans]->Name() == rkPath)
return mTransientRoots[iTrans];
}
if (AllowCreate)
{
CVirtualDirectory *pDir = new CVirtualDirectory(rkPath, this);
mTransientRoots.push_back(pDir);
return pDir;
}
else return nullptr;
}
if (rkPath.IsEmpty())
return mpDatabaseRoot;
else if (mpDatabaseRoot)
{
return mpDatabaseRoot->FindChildDirectory(rkPath, AllowCreate);
}
else return nullptr;
else
return nullptr;
}
void CResourceStore::ConditionalDeleteDirectory(CVirtualDirectory *pDir)
@@ -382,16 +345,7 @@ CResourceEntry* CResourceStore::RegisterResource(const CAssetID& rkID, EResType
CResourceEntry *pEntry = FindEntry(rkID);
if (pEntry)
{
if (pEntry->IsTransient())
{
ASSERT(pEntry->ResourceType() == Type);
pEntry->AddToProject(rkDir, rkName);
}
else
Log::Error("Attempted to register resource that's already tracked in the database: " + rkID.ToString() + " / " + rkDir + " / " + rkName);
}
Log::Error("Attempted to register resource that's already tracked in the database: " + rkID.ToString() + " / " + rkDir + " / " + rkName);
else
{
@@ -409,133 +363,74 @@ CResourceEntry* CResourceStore::RegisterResource(const CAssetID& rkID, EResType
return pEntry;
}
CResourceEntry* CResourceStore::RegisterTransientResource(EResType Type, const TString& rkDir /*= ""*/, const TString& rkFileName /*= ""*/)
{
CResourceEntry *pEntry = new CResourceEntry(this, CAssetID::RandomID(), rkDir, rkFileName, Type, true);
mResourceEntries[pEntry->ID()] = pEntry;
return pEntry;
}
CResourceEntry* CResourceStore::RegisterTransientResource(EResType Type, const CAssetID& rkID, const TString& rkDir /*=L ""*/, const TString& rkFileName /*= ""*/)
{
CResourceEntry *pEntry = FindEntry(rkID);
if (!pEntry)
{
pEntry = new CResourceEntry(this, rkID, rkDir, rkFileName, Type, true);
mResourceEntries[rkID] = pEntry;
}
return pEntry;
}
CResource* CResourceStore::LoadResource(const CAssetID& rkID, const CFourCC& rkType)
CResource* CResourceStore::LoadResource(const CAssetID& rkID)
{
if (!rkID.IsValid()) return nullptr;
// Check if resource is already loaded
auto Find = mLoadedResources.find(rkID);
if (Find != mLoadedResources.end())
return Find->second->Resource();
// Check for resource in store
CResourceEntry *pEntry = FindEntry(rkID);
if (pEntry) return pEntry->Load();
// Check in transient load directory - this only works for cooked
EResType Type = CResTypeInfo::TypeForCookedExtension(mGame, rkType)->Type();
if (Type != eInvalidResType)
{
// Note the entry may not be able to find the resource on its own (due to not knowing what game
// it is) so we will attempt to open the file stream ourselves and pass it to the entry instead.
TString Name = rkID.ToString();
CResourceEntry *pEntry = RegisterTransientResource(Type, mTransientLoadDir, Name);
TString Path = mTransientLoadDir + Name + "." + rkType.ToString();
CFileInStream File(Path, IOUtil::eBigEndian);
CResource *pRes = pEntry->LoadCooked(File);
if (!pRes) DeleteResourceEntry(pEntry);
return pRes;
}
if (pEntry)
return pEntry->Load();
else
{
Log::Error("Can't load requested resource with ID \"" + rkID.ToString() + "\"; can't locate resource. Note: Loading raw assets from an arbitrary directory is unsupported.");;
// Resource doesn't seem to exist
Log::Error("Can't find requested resource with ID \"" + rkID.ToString() + "\".");;
return nullptr;
}
}
CResource* CResourceStore::LoadResource(const CAssetID& rkID, EResType Type)
{
CResource *pRes = LoadResource(rkID);
if (pRes)
{
if (pRes->Type() == Type)
{
return pRes;
}
else
{
CResTypeInfo *pExpectedType = CResTypeInfo::FindTypeInfo(Type);
CResTypeInfo *pGotType = pRes->TypeInfo();
ASSERT(pExpectedType && pGotType);
Log::Error("Resource with ID \"" + rkID.ToString() + "\" requested with the wrong type; expected " + pExpectedType->TypeName() + " asset, got " + pGotType->TypeName() + " asset");
return nullptr;
}
}
else
return nullptr;
}
CResource* CResourceStore::LoadResource(const TString& rkPath)
{
// If this is a relative path then load via the resource DB
if (!FileUtil::IsAbsolute(rkPath))
CResourceEntry *pEntry = FindEntry(rkPath);
if (pEntry)
{
CResourceEntry *pEntry = FindEntry(rkPath);
// Verify extension matches the entry + load resource
TString Ext = rkPath.GetFileExtension();
if (pEntry)
if (!Ext.IsEmpty())
{
// Verify extension matches the entry + load resource
TString Ext = rkPath.GetFileExtension();
if (!Ext.IsEmpty())
if (Ext.Length() == 4)
{
if (Ext.Length() == 4)
{
ASSERT(Ext.CaseInsensitiveCompare(pEntry->CookedExtension().ToString()));
}
else
{
ASSERT(Ext.CaseInsensitiveCompare(pEntry->RawExtension()));
}
ASSERT(Ext.CaseInsensitiveCompare(pEntry->CookedExtension().ToString()));
}
else
{
ASSERT(Ext.CaseInsensitiveCompare(pEntry->RawExtension()));
}
return pEntry->Load();
}
else return nullptr;
return pEntry->Load();
}
// Otherwise create transient entry; construct ID from string, check if resource is loaded already
TString Dir = FileUtil::MakeAbsolute(rkPath.GetFileDirectory());
TString Name = rkPath.GetFileName(false);
CAssetID ID = (Name.IsHexString() ? Name.ToInt64() : rkPath.Hash64());
auto Find = mLoadedResources.find(ID);
if (Find != mLoadedResources.end())
return Find->second->Resource();
// Determine type
TString Extension = rkPath.GetFileExtension().ToUpper();
EResType Type = CResTypeInfo::TypeForCookedExtension(mGame, Extension)->Type();
if (Type == eInvalidResType)
{
Log::Error("Unable to load resource " + rkPath + "; unrecognized extension: " + Extension);
return nullptr;
}
// Open file
CFileInStream File(rkPath, IOUtil::eBigEndian);
if (!File.IsValid())
{
Log::Error("Unable to load resource; couldn't open file: " + rkPath);
return nullptr;
}
// Load resource
TString OldTransientDir = mTransientLoadDir;
mTransientLoadDir = Dir;
CResourceEntry *pEntry = RegisterTransientResource(Type, ID, Dir, Name);
CResource *pRes = pEntry->LoadCooked(File);
if (!pRes) DeleteResourceEntry(pEntry);
mTransientLoadDir = OldTransientDir;
return pRes;
else return nullptr;
}
void CResourceStore::TrackLoadedResource(CResourceEntry *pEntry)
@@ -563,27 +458,11 @@ void CResourceStore::DestroyUnreferencedResources()
{
It = mLoadedResources.erase(It);
NumDeleted++;
// Transient resources should have their entries cleared out when the resource is unloaded
if (pEntry->IsTransient())
DeleteResourceEntry(pEntry);
}
else It++;
}
} while (NumDeleted > 0);
// Destroy empty virtual directories
for (auto DirIt = mTransientRoots.begin(); DirIt != mTransientRoots.end(); DirIt++)
{
CVirtualDirectory *pRoot = *DirIt;
if (pRoot->IsEmpty())
{
delete pRoot;
DirIt = mTransientRoots.erase(DirIt);
}
}
}
bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry)
@@ -611,13 +490,6 @@ bool CResourceStore::DeleteResourceEntry(CResourceEntry *pEntry)
return true;
}
void CResourceStore::SetTransientLoadDir(const TString& rkDir)
{
mTransientLoadDir = rkDir;
mTransientLoadDir.EnsureEndsWith('/');
Log::Write("Set resource directory: " + rkDir);
}
void CResourceStore::ImportNamesFromPakContentsTxt(const TString& rkTxtPath, bool UnnamedOnly)
{
// Read file contents -first- then move assets -after-; this

View File

@@ -22,7 +22,6 @@ class CResourceStore
CGameProject *mpProj;
EGame mGame;
CVirtualDirectory *mpDatabaseRoot;
std::vector<CVirtualDirectory*> mTransientRoots;
std::map<CAssetID, CResourceEntry*> mResourceEntries;
std::map<CAssetID, CResourceEntry*> mLoadedResources;
bool mDatabaseDirty;
@@ -33,7 +32,6 @@ class CResourceStore
TString mDatabaseName;
TString mRawDir;
TString mCookedDir;
TString mTransientLoadDir;
enum EDatabaseVersion
{
@@ -56,22 +54,21 @@ public:
void ConditionalSaveStore();
void SetProject(CGameProject *pProj);
void CloseProject();
CVirtualDirectory* GetVirtualDirectory(const TString& rkPath, bool Transient, bool AllowCreate);
CVirtualDirectory* GetVirtualDirectory(const TString& rkPath, bool AllowCreate);
void ConditionalDeleteDirectory(CVirtualDirectory *pDir);
bool IsResourceRegistered(const CAssetID& rkID) const;
CResourceEntry* RegisterResource(const CAssetID& rkID, EResType Type, const TString& rkDir, const TString& rkName);
CResourceEntry* FindEntry(const CAssetID& rkID) const;
CResourceEntry* FindEntry(const TString& rkPath) const;
CResourceEntry* RegisterTransientResource(EResType Type, const TString& rkDir = "", const TString& rkFileName = "");
CResourceEntry* RegisterTransientResource(EResType Type, const CAssetID& rkID, const TString& rkDir = "", const TString& rkFileName = "");
CResource* LoadResource(const CAssetID& rkID, const CFourCC& rkType);
template<typename ResType> ResType* LoadResource(const CAssetID& rkID) { return static_cast<ResType*>(LoadResource(rkID, ResType::StaticType())); }
CResource* LoadResource(const CAssetID& rkID);
CResource* LoadResource(const CAssetID& rkID, EResType Type);
CResource* LoadResource(const TString& rkPath);
void TrackLoadedResource(CResourceEntry *pEntry);
void DestroyUnreferencedResources();
bool DeleteResourceEntry(CResourceEntry *pEntry);
void SetTransientLoadDir(const TString& rkDir);
void ImportNamesFromPakContentsTxt(const TString& rkTxtPath, bool UnnamedOnly);

View File

@@ -98,7 +98,7 @@ void CCharacterUsageMap::DebugPrintContents()
{
CAssetID ID = Iter->first;
std::vector<bool>& rUsedList = Iter->second;
CAnimSet *pSet = (CAnimSet*) mpStore->LoadResource(ID, "ANCS");
CAnimSet *pSet = mpStore->LoadResource<CAnimSet>(ID);
for (u32 iChar = 0; iChar < pSet->NumCharacters(); iChar++)
{