mirror of
https://github.com/AxioDL/PrimeWorldEditor.git
synced 2025-12-21 10:49:23 +00:00
Initial implementation of world/area exports, added support for asset lists to specify a path/name for resources
This commit is contained in:
@@ -1,12 +1,16 @@
|
||||
#include "CGameExporter.h"
|
||||
#include "Core/Resource/CResCache.h"
|
||||
#include "Core/Resource/CWorld.h"
|
||||
#include <FileIO/FileIO.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
#include <Common/CompressionUtil.h>
|
||||
#include <Common/CScopedTimer.h>
|
||||
#include <Common/FileUtil.h>
|
||||
#include <tinyxml2.h>
|
||||
|
||||
#define COPY_DISC_DATA 1
|
||||
#define COPY_DISC_DATA 0
|
||||
#define LOAD_PAKS 1
|
||||
#define EXPORT_WORLDS 1
|
||||
#define EXPORT_COOKED 1
|
||||
|
||||
CGameExporter::CGameExporter(const TString& rkInputDir, const TString& rkOutputDir)
|
||||
@@ -15,25 +19,37 @@ CGameExporter::CGameExporter(const TString& rkInputDir, const TString& rkOutputD
|
||||
mExportDir = FileUtil::MakeAbsolute(rkOutputDir);
|
||||
|
||||
mpProject = new CGameProject(mExportDir);
|
||||
mDiscDir = mpProject->DiscDir();
|
||||
mResDir = mpProject->ResourcesDir();
|
||||
mWorldsDir = mpProject->WorldsDir();
|
||||
mCookedDir = mpProject->CookedDir();
|
||||
mCookedResDir = mpProject->CookedResourcesDir();
|
||||
mCookedWorldsDir = mpProject->CookedWorldsDir();
|
||||
mDiscDir = mpProject->DiscDir(true);
|
||||
mResDir = mpProject->ResourcesDir(true);
|
||||
mWorldsDir = mpProject->WorldsDir(true);
|
||||
mCookedDir = mpProject->CookedDir(false);
|
||||
mCookedResDir = mpProject->CookedResourcesDir(true);
|
||||
mCookedWorldsDir = mpProject->CookedWorldsDir(true);
|
||||
}
|
||||
|
||||
bool CGameExporter::Export()
|
||||
{
|
||||
SCOPED_TIMER(ExportGame);
|
||||
gResCache.SetGameExporter(this);
|
||||
FileUtil::CreateDirectory(mExportDir);
|
||||
FileUtil::ClearDirectory(mExportDir);
|
||||
|
||||
CopyDiscData();
|
||||
LoadAssetList();
|
||||
LoadPaks();
|
||||
ExportWorlds();
|
||||
ExportCookedResources();
|
||||
|
||||
gResCache.SetGameExporter(nullptr);
|
||||
return true;
|
||||
}
|
||||
|
||||
void CGameExporter::LoadResource(const CUniqueID& rkID, std::vector<u8>& rBuffer)
|
||||
{
|
||||
SResourceInstance *pInst = FindResourceInstance(rkID);
|
||||
if (pInst) LoadResource(*pInst, rBuffer);
|
||||
}
|
||||
|
||||
// ************ PROTECTED ************
|
||||
void CGameExporter::CopyDiscData()
|
||||
{
|
||||
@@ -93,6 +109,57 @@ void CGameExporter::CopyDiscData()
|
||||
ASSERT(Game() != eUnknownVersion);
|
||||
}
|
||||
|
||||
void CGameExporter::LoadAssetList()
|
||||
{
|
||||
SCOPED_TIMER(LoadAssetList);
|
||||
|
||||
// Determine the asset list to use
|
||||
TString ListFile = "../resources/list/AssetList";
|
||||
|
||||
switch (Game())
|
||||
{
|
||||
case ePrimeDemo: ListFile += "MP1Demo"; break;
|
||||
case ePrime: ListFile += "MP1"; break;
|
||||
case eEchoesDemo: ListFile += "MP2Demo"; break;
|
||||
case eEchoes: ListFile += "MP2"; break;
|
||||
case eCorruptionProto: ListFile += "MP3Proto"; break;
|
||||
case eCorruption: ListFile += "MP3"; break;
|
||||
case eReturns: ListFile += "DKCR"; break;
|
||||
default: ASSERT(false);
|
||||
}
|
||||
|
||||
ListFile += ".xml";
|
||||
|
||||
// Load list
|
||||
tinyxml2::XMLDocument List;
|
||||
List.LoadFile(*ListFile);
|
||||
|
||||
if (List.Error())
|
||||
{
|
||||
Log::Error("Couldn't open asset list: " + ListFile);
|
||||
return;
|
||||
}
|
||||
|
||||
tinyxml2::XMLElement *pRoot = List.FirstChildElement("AssetList");
|
||||
tinyxml2::XMLElement *pAsset = pRoot->FirstChildElement("Asset");
|
||||
|
||||
while (pAsset)
|
||||
{
|
||||
u64 ResourceID = TString(pAsset->Attribute("ID")).ToInt64(16);
|
||||
|
||||
tinyxml2::XMLElement *pDir = pAsset->FirstChildElement("Dir");
|
||||
TString Dir = pDir ? pDir->GetText() : "";
|
||||
|
||||
tinyxml2::XMLElement *pName = pAsset->FirstChildElement("Name");
|
||||
TString Name = pName ? pName->GetText() : "";
|
||||
|
||||
if (!Dir.EndsWith("/") && !Dir.EndsWith("\\")) Dir.Append("\\");
|
||||
SetResourcePath(ResourceID, mResDir + Dir.ToUTF16(), Name.ToUTF16());
|
||||
|
||||
pAsset = pAsset->NextSiblingElement("Asset");
|
||||
}
|
||||
}
|
||||
|
||||
// ************ RESOURCE LOADING ************
|
||||
void CGameExporter::LoadPaks()
|
||||
{
|
||||
@@ -117,7 +184,7 @@ void CGameExporter::LoadPaks()
|
||||
continue;
|
||||
}
|
||||
|
||||
CPackage *pPackage = new CPackage(CharPak.GetFileName(false));
|
||||
CPackage *pPackage = new CPackage(CharPak.GetFileName(false), FileUtil::MakeRelative(PakPath.GetFileDirectory(), mExportDir));
|
||||
|
||||
// MP1-MP3Proto
|
||||
if (Game() < eCorruption)
|
||||
@@ -134,11 +201,11 @@ void CGameExporter::LoadPaks()
|
||||
|
||||
for (u32 iName = 0; iName < NumNamedResources; iName++)
|
||||
{
|
||||
Pak.Seek(0x4, SEEK_CUR); // Skip resource type
|
||||
CFourCC ResType = Pak.ReadLong();
|
||||
CUniqueID ResID(Pak, IDLength);
|
||||
u32 NameLen = Pak.ReadLong();
|
||||
TString Name = Pak.ReadString(NameLen);
|
||||
pPackage->AddNamedResource(Name, ResID);
|
||||
pPackage->AddNamedResource(Name, ResID, ResType);
|
||||
}
|
||||
|
||||
u32 NumResources = Pak.ReadLong();
|
||||
@@ -153,7 +220,7 @@ void CGameExporter::LoadPaks()
|
||||
|
||||
u64 IntegralID = ResID.ToLongLong();
|
||||
if (mResourceMap.find(IntegralID) == mResourceMap.end())
|
||||
mResourceMap[IntegralID] = SResourceInstance { PakPath, ResID, ResType, ResOffset, ResSize, Compressed };
|
||||
mResourceMap[IntegralID] = SResourceInstance { PakPath, ResID, ResType, ResOffset, ResSize, Compressed, false };
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -194,9 +261,9 @@ void CGameExporter::LoadPaks()
|
||||
for (u32 iName = 0; iName < NumNamedResources; iName++)
|
||||
{
|
||||
TString Name = Pak.ReadString();
|
||||
Pak.Seek(0x4, SEEK_CUR); // Skip type
|
||||
CFourCC ResType = Pak.ReadLong();
|
||||
CUniqueID ResID(Pak, IDLength);
|
||||
pPackage->AddNamedResource(Name, ResID);
|
||||
pPackage->AddNamedResource(Name, ResID, ResType);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -216,7 +283,7 @@ void CGameExporter::LoadPaks()
|
||||
|
||||
u64 IntegralID = ResID.ToLongLong();
|
||||
if (mResourceMap.find(IntegralID) == mResourceMap.end())
|
||||
mResourceMap[IntegralID] = SResourceInstance { PakPath, ResID, Type, Offset, Size, Compressed };
|
||||
mResourceMap[IntegralID] = SResourceInstance { PakPath, ResID, Type, Offset, Size, Compressed, false };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -231,7 +298,7 @@ void CGameExporter::LoadPaks()
|
||||
#endif
|
||||
}
|
||||
|
||||
void CGameExporter::LoadPakResource(const SResourceInstance& rkResource, std::vector<u8>& rBuffer)
|
||||
void CGameExporter::LoadResource(const SResourceInstance& rkResource, std::vector<u8>& rBuffer)
|
||||
{
|
||||
CFileInStream Pak(rkResource.PakFile.ToUTF8().ToStdString(), IOUtil::eBigEndian);
|
||||
|
||||
@@ -327,30 +394,106 @@ void CGameExporter::LoadPakResource(const SResourceInstance& rkResource, std::ve
|
||||
}
|
||||
}
|
||||
|
||||
void CGameExporter::ExportWorlds()
|
||||
{
|
||||
#if EXPORT_WORLDS
|
||||
SCOPED_TIMER(ExportWorlds);
|
||||
//CResourceDatabase *pResDB = mpProject->ResourceDatabase();
|
||||
|
||||
for (u32 iPak = 0; iPak < mpProject->NumWorldPaks(); iPak++)
|
||||
{
|
||||
CPackage *pPak = mpProject->WorldPakByIndex(iPak);
|
||||
|
||||
// Get output path. DKCR paks are stored in a Worlds folder so we should get the path relative to that so we don't have Worlds\Worlds\.
|
||||
// Other games have all paks in the game root dir so we're fine just taking the original root dir-relative directory.
|
||||
TWideString PakPath = pPak->PakPath();
|
||||
TWideString WorldsDir = PakPath.GetParentDirectoryPath(L"Worlds", false);
|
||||
|
||||
if (!WorldsDir.IsEmpty())
|
||||
PakPath = FileUtil::MakeRelative(PakPath, WorldsDir);
|
||||
|
||||
for (u32 iRes = 0; iRes < pPak->NumNamedResources(); iRes++)
|
||||
{
|
||||
const SNamedResource& rkRes = pPak->NamedResourceByIndex(iRes);
|
||||
|
||||
if (rkRes.Type == "MLVL" && !rkRes.Name.EndsWith("NODEPEND"))
|
||||
{
|
||||
TResPtr<CWorld> pWorld = (CWorld*) gResCache.GetResource(rkRes.ID, rkRes.Type);
|
||||
|
||||
if (!pWorld)
|
||||
{
|
||||
Log::Error("Couldn't load world " + rkRes.Name + " from package " + pPak->PakName() + "; unable to export");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Export world
|
||||
TWideString Name = rkRes.Name.ToUTF16();
|
||||
TWideString WorldDir = mWorldsDir + PakPath + FileUtil::SanitizeName(Name, true) + L"\\";
|
||||
FileUtil::CreateDirectory(mCookedDir + WorldDir);
|
||||
|
||||
SResourceInstance *pInst = FindResourceInstance(rkRes.ID);
|
||||
ASSERT(pInst != nullptr);
|
||||
|
||||
SetResourcePath(rkRes.ID, WorldDir, Name);
|
||||
ExportResource(*pInst);
|
||||
|
||||
// Export areas
|
||||
for (u32 iArea = 0; iArea < pWorld->NumAreas(); iArea++)
|
||||
{
|
||||
// Determine area names
|
||||
TWideString InternalAreaName = pWorld->AreaInternalName(iArea).ToUTF16();
|
||||
if (InternalAreaName.IsEmpty()) InternalAreaName = TWideString::FromInt32(iArea, 2, 10);
|
||||
|
||||
TWideString GameAreaName;
|
||||
CStringTable *pTable = pWorld->AreaName(iArea);
|
||||
if (pTable) GameAreaName = pTable->String("ENGL", 0);
|
||||
if (GameAreaName.IsEmpty()) GameAreaName = InternalAreaName;
|
||||
|
||||
// Load area
|
||||
CUniqueID AreaID = pWorld->AreaResourceID(iArea);
|
||||
CGameArea *pArea = (CGameArea*) gResCache.GetResource(AreaID, "MREA");
|
||||
|
||||
if (!pArea)
|
||||
{
|
||||
Log::Error("Unable to export area " + GameAreaName.ToUTF8() + " from world " + rkRes.Name + "; couldn't load area");
|
||||
continue;
|
||||
}
|
||||
|
||||
// Export area
|
||||
TWideString AreaDir = WorldDir + TWideString::FromInt32(iArea, 2, 10) + L"_" + FileUtil::SanitizeName(GameAreaName, true) + L"\\";
|
||||
FileUtil::CreateDirectory(mCookedDir + AreaDir);
|
||||
|
||||
SResourceInstance *pInst = FindResourceInstance(AreaID);
|
||||
ASSERT(pInst != nullptr);
|
||||
|
||||
SetResourcePath(AreaID, AreaDir, InternalAreaName);
|
||||
ExportResource(*pInst);
|
||||
}
|
||||
|
||||
gResCache.Clean();
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
Log::Error("Unexpected named resource type in world pak: " + rkRes.Type.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void CGameExporter::ExportCookedResources()
|
||||
{
|
||||
#if EXPORT_COOKED
|
||||
CResourceDatabase *pResDB = mpProject->ResourceDatabase();
|
||||
{
|
||||
SCOPED_TIMER(ExportCookedResources);
|
||||
FileUtil::CreateDirectory(mCookedResDir);
|
||||
FileUtil::CreateDirectory(mCookedDir + mResDir);
|
||||
|
||||
for (auto It = mResourceMap.begin(); It != mResourceMap.end(); It++)
|
||||
{
|
||||
const SResourceInstance& rkRes = It->second;
|
||||
std::vector<u8> ResourceData;
|
||||
LoadPakResource(rkRes, ResourceData);
|
||||
|
||||
TString OutName = rkRes.ResourceID.ToString() + "." + rkRes.ResourceType.ToString();
|
||||
TString OutDir = mCookedResDir.ToUTF8() + "\\";
|
||||
TString OutPath = OutDir + OutName;
|
||||
CFileOutStream Out(OutPath.ToStdString(), IOUtil::eBigEndian);
|
||||
|
||||
if (Out.IsValid())
|
||||
Out.WriteBytes(ResourceData.data(), ResourceData.size());
|
||||
|
||||
// Add to resource DB
|
||||
pResDB->RegisterResource(rkRes.ResourceID, FileUtil::MakeRelative(OutDir, mCookedDir), OutName, CResource::ResTypeForExtension(rkRes.ResourceType));
|
||||
SResourceInstance& rRes = It->second;
|
||||
ExportResource(rRes);
|
||||
}
|
||||
}
|
||||
{
|
||||
@@ -359,3 +502,37 @@ void CGameExporter::ExportCookedResources()
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void CGameExporter::ExportResource(SResourceInstance& rRes)
|
||||
{
|
||||
if (!rRes.Exported)
|
||||
{
|
||||
std::vector<u8> ResourceData;
|
||||
LoadResource(rRes, ResourceData);
|
||||
|
||||
// Determine output path
|
||||
SResourcePath *pPath = FindResourcePath(rRes.ResourceID);
|
||||
TString OutName, OutDir;
|
||||
|
||||
if (pPath)
|
||||
{
|
||||
OutName = pPath->Name.ToUTF8();
|
||||
OutDir = pPath->Dir.ToUTF8();
|
||||
}
|
||||
|
||||
if (OutName.IsEmpty()) OutName = rRes.ResourceID.ToString();
|
||||
if (OutDir.IsEmpty()) OutDir = mResDir;
|
||||
|
||||
// Write to file
|
||||
FileUtil::CreateDirectory(mCookedDir + OutDir.ToUTF16());
|
||||
TString OutPath = mCookedDir.ToUTF8() + OutDir + OutName + "." + rRes.ResourceType.ToString();
|
||||
CFileOutStream Out(OutPath.ToStdString(), IOUtil::eBigEndian);
|
||||
|
||||
if (Out.IsValid())
|
||||
Out.WriteBytes(ResourceData.data(), ResourceData.size());
|
||||
|
||||
// Add to resource DB
|
||||
mpProject->ResourceDatabase()->RegisterResource(rRes.ResourceID, OutDir, OutName, CResource::ResTypeForExtension(rRes.ResourceType));
|
||||
rRes.Exported = true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,18 +35,55 @@ class CGameExporter
|
||||
u32 PakOffset;
|
||||
u32 PakSize;
|
||||
bool Compressed;
|
||||
bool Exported;
|
||||
};
|
||||
std::map<u64, SResourceInstance> mResourceMap;
|
||||
|
||||
struct SResourcePath
|
||||
{
|
||||
TWideString Dir;
|
||||
TWideString Name;
|
||||
};
|
||||
std::map<u64, SResourcePath> mResourcePaths;
|
||||
|
||||
public:
|
||||
CGameExporter(const TString& rkInputDir, const TString& rkOutputDir);
|
||||
bool Export();
|
||||
void LoadResource(const CUniqueID& rkID, std::vector<u8>& rBuffer);
|
||||
|
||||
protected:
|
||||
void CopyDiscData();
|
||||
void LoadAssetList();
|
||||
void LoadPaks();
|
||||
void LoadPakResource(const SResourceInstance& rkResource, std::vector<u8>& rBuffer);
|
||||
void LoadResource(const SResourceInstance& rkResource, std::vector<u8>& rBuffer);
|
||||
void ExportWorlds();
|
||||
void ExportCookedResources();
|
||||
void ExportResource(SResourceInstance& rRes);
|
||||
|
||||
// Convenience Functions
|
||||
inline SResourceInstance* FindResourceInstance(const CUniqueID& rkID)
|
||||
{
|
||||
u64 IntegralID = rkID.ToLongLong();
|
||||
auto Found = mResourceMap.find(IntegralID);
|
||||
return (Found == mResourceMap.end() ? nullptr : &Found->second);
|
||||
}
|
||||
|
||||
inline SResourcePath* FindResourcePath(const CUniqueID& rkID)
|
||||
{
|
||||
u64 IntegralID = rkID.ToLongLong();
|
||||
auto Found = mResourcePaths.find(IntegralID);
|
||||
return (Found == mResourcePaths.end() ? nullptr : &Found->second);
|
||||
}
|
||||
|
||||
inline void SetResourcePath(const CUniqueID& rkID, const TWideString& rkDir, const TWideString& rkName)
|
||||
{
|
||||
SetResourcePath(rkID.ToLongLong(), rkDir, rkName);
|
||||
}
|
||||
|
||||
inline void SetResourcePath(u64 ID, const TWideString& rkDir, const TWideString& rkName)
|
||||
{
|
||||
mResourcePaths[ID] = SResourcePath { rkDir, rkName };
|
||||
}
|
||||
|
||||
inline EGame Game() const { return mpProject->Game(); }
|
||||
inline void SetGame(EGame Game) { mpProject->SetGame(Game); }
|
||||
|
||||
@@ -28,18 +28,21 @@ public:
|
||||
void AddPackage(CPackage *pPackage, bool WorldPak);
|
||||
|
||||
// Directory Handling
|
||||
inline TWideString ProjectRoot() const { return mProjectRoot; }
|
||||
inline TWideString DiscDir() const { return mProjectRoot + L"Disc\\"; }
|
||||
inline TWideString ResourcesDir() const { return mProjectRoot + L"Resources\\"; }
|
||||
inline TWideString WorldsDir() const { return mProjectRoot + L"Worlds\\"; }
|
||||
inline TWideString CookedDir() const { return mProjectRoot + L"Cooked\\"; }
|
||||
inline TWideString CookedResourcesDir() const { return CookedDir() + L"Resources\\"; }
|
||||
inline TWideString CookedWorldsDir() const { return CookedDir() + L"Worlds\\"; }
|
||||
inline TWideString ProjectRoot() const { return mProjectRoot; }
|
||||
inline TWideString DiscDir(bool Relative) const { return Relative ? L"Disc\\" : mProjectRoot + L"Disc\\"; }
|
||||
inline TWideString ResourcesDir(bool Relative) const { return Relative ? L"Resources\\" : mProjectRoot + L"Resources\\"; }
|
||||
inline TWideString WorldsDir(bool Relative) const { return Relative ? L"Worlds\\" : mProjectRoot + L"Worlds\\"; }
|
||||
inline TWideString CookedDir(bool Relative) const { return Relative ? L"Cooked\\" : mProjectRoot + L"Cooked\\"; }
|
||||
inline TWideString CookedResourcesDir(bool Relative) const { return CookedDir(Relative) + L"Resources\\"; }
|
||||
inline TWideString CookedWorldsDir(bool Relative) const { return CookedDir(Relative) + L"Worlds\\"; }
|
||||
|
||||
// Accessors
|
||||
inline void SetGame(EGame Game) { mGame = Game; }
|
||||
inline void SetProjectName(const TString& rkName) { mProjectName = rkName; }
|
||||
|
||||
inline u32 NumWorldPaks() const { return mWorldPaks.size(); }
|
||||
inline CPackage* WorldPakByIndex(u32 Index) const { return mWorldPaks[Index]; }
|
||||
|
||||
inline EGame Game() const { return mGame; }
|
||||
inline CResourceDatabase* ResourceDatabase() const { return mpResourceDatabase; }
|
||||
};
|
||||
|
||||
@@ -9,25 +9,32 @@ struct SNamedResource
|
||||
{
|
||||
TString Name;
|
||||
CUniqueID ID;
|
||||
CFourCC Type;
|
||||
};
|
||||
|
||||
class CPackage
|
||||
{
|
||||
TString mPakName;
|
||||
TWideString mPakPath;
|
||||
std::vector<SNamedResource> mNamedResources;
|
||||
std::vector<CUniqueID> mPakResources;
|
||||
|
||||
public:
|
||||
CPackage() {}
|
||||
CPackage(const TString& rkName) : mPakName(rkName) {}
|
||||
|
||||
CPackage(const TString& rkName, const TWideString& rkPath)
|
||||
: mPakName(rkName)
|
||||
, mPakPath(rkPath)
|
||||
{}
|
||||
|
||||
inline TString PakName() const { return mPakName; }
|
||||
inline TWideString PakPath() const { return mPakPath; }
|
||||
inline u32 NumNamedResources() const { return mNamedResources.size(); }
|
||||
inline const SNamedResource& NamedResourceByIndex(u32 Index) const { return mNamedResources[Index]; }
|
||||
|
||||
inline void SetPakName(TString NewName) { mPakName = NewName; }
|
||||
inline void AddNamedResource(TString Name, const CUniqueID& rkID) { mNamedResources.push_back( SNamedResource { Name, rkID } ); }
|
||||
inline void AddPakResource(const CUniqueID& rkID) { mPakResources.push_back(rkID); }
|
||||
inline void SetPakName(TString NewName) { mPakName = NewName; }
|
||||
inline void AddNamedResource(TString Name, const CUniqueID& rkID, const CFourCC& rkType) { mNamedResources.push_back( SNamedResource { Name, rkID, rkType } ); }
|
||||
inline void AddPakResource(const CUniqueID& rkID) { mPakResources.push_back(rkID); }
|
||||
};
|
||||
|
||||
#endif // CPACKAGE
|
||||
|
||||
@@ -28,7 +28,7 @@ TString CResourceEntry::RawAssetPath() const
|
||||
TString CResourceEntry::CookedAssetPath() const
|
||||
{
|
||||
TWideString Ext = GetCookedExtension(mResourceType, mpDatabase->GameProject()->Game()).ToUTF16();
|
||||
return mpDatabase->GameProject()->CookedDir() + mFileDir + mFileName + L"." + Ext;
|
||||
return mpDatabase->GameProject()->CookedDir(false) + mFileDir + mFileName + L"." + Ext;
|
||||
}
|
||||
|
||||
bool CResourceEntry::NeedsRecook() const
|
||||
|
||||
@@ -14,12 +14,14 @@
|
||||
#include "Core/Resource/Factory/CWorldLoader.h"
|
||||
|
||||
#include <FileIO/FileIO.h>
|
||||
#include <Common/AssertMacro.h>
|
||||
#include <Common/FileUtil.h>
|
||||
#include <Common/Log.h>
|
||||
#include <Common/TString.h>
|
||||
#include <iostream>
|
||||
|
||||
CResCache::CResCache()
|
||||
: mpGameExporter(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
@@ -71,8 +73,33 @@ TString CResCache::GetSourcePath()
|
||||
CResource* CResCache::GetResource(CUniqueID ResID, CFourCC Type)
|
||||
{
|
||||
if (!ResID.IsValid()) return nullptr;
|
||||
TString Source = mResDir + ResID.ToString() + "." + Type.ToString();
|
||||
return GetResource(Source);
|
||||
TString StringName = ResID.ToString() + "." + Type.ToString();
|
||||
|
||||
// With Game Exporter - get data buffer from exporter
|
||||
if (mpGameExporter)
|
||||
{
|
||||
// Check if we already have resource loaded
|
||||
auto Got = mResourceCache.find(ResID.ToLongLong());
|
||||
if (Got != mResourceCache.end())
|
||||
return Got->second;
|
||||
|
||||
// Otherwise load resource
|
||||
std::vector<u8> DataBuffer;
|
||||
mpGameExporter->LoadResource(ResID, DataBuffer);
|
||||
if (DataBuffer.empty()) return nullptr;
|
||||
|
||||
CMemoryInStream MemStream(DataBuffer.data(), DataBuffer.size(), IOUtil::eBigEndian);
|
||||
CResource *pRes = InternalLoadResource(MemStream, ResID, Type);
|
||||
pRes->mResSource = StringName;
|
||||
return pRes;
|
||||
}
|
||||
|
||||
// Without Game Exporter - load from file
|
||||
else
|
||||
{
|
||||
TString Source = mResDir + StringName;
|
||||
return GetResource(Source);
|
||||
}
|
||||
}
|
||||
|
||||
CResource* CResCache::GetResource(const TString& rkResPath)
|
||||
@@ -98,32 +125,11 @@ CResource* CResCache::GetResource(const TString& rkResPath)
|
||||
mResDir = rkResPath.GetFileDirectory();
|
||||
|
||||
// Load resource
|
||||
CResource *pRes = nullptr;
|
||||
CFourCC Type = rkResPath.GetFileExtension().ToUpper();
|
||||
bool SupportedFormat = true;
|
||||
|
||||
if (Type == "CMDL") pRes = CModelLoader::LoadCMDL(File);
|
||||
else if (Type == "TXTR") pRes = CTextureDecoder::LoadTXTR(File);
|
||||
else if (Type == "ANCS") pRes = CAnimSetLoader::LoadANCS(File);
|
||||
else if (Type == "CHAR") pRes = CAnimSetLoader::LoadCHAR(File);
|
||||
else if (Type == "MREA") pRes = CAreaLoader::LoadMREA(File);
|
||||
else if (Type == "MLVL") pRes = CWorldLoader::LoadMLVL(File);
|
||||
else if (Type == "STRG") pRes = CStringLoader::LoadSTRG(File);
|
||||
else if (Type == "FONT") pRes = CFontLoader::LoadFONT(File);
|
||||
else if (Type == "SCAN") pRes = CScanLoader::LoadSCAN(File);
|
||||
else if (Type == "DCLN") pRes = CCollisionLoader::LoadDCLN(File);
|
||||
else if (Type == "EGMC") pRes = CPoiToWorldLoader::LoadEGMC(File);
|
||||
else if (Type == "CINF") pRes = CSkeletonLoader::LoadCINF(File);
|
||||
else if (Type == "ANIM") pRes = CAnimationLoader::LoadANIM(File);
|
||||
else if (Type == "CSKR") pRes = CSkinLoader::LoadCSKR(File);
|
||||
else SupportedFormat = false;
|
||||
|
||||
if (!pRes) pRes = new CResource(); // Default for unsupported formats
|
||||
CResource *pRes = InternalLoadResource(File, ResID, Type);
|
||||
pRes->mResSource = rkResPath;
|
||||
|
||||
// Add to cache and cleanup
|
||||
pRes->mID = *rkResPath;
|
||||
pRes->mResSource = rkResPath;
|
||||
mResourceCache[ResID.ToLongLong()] = pRes;
|
||||
mResDir = OldResDir;
|
||||
return pRes;
|
||||
}
|
||||
@@ -168,4 +174,36 @@ void CResCache::DeleteResource(CUniqueID ResID)
|
||||
}
|
||||
}
|
||||
|
||||
// ************ PROTECTED ************
|
||||
CResource* CResCache::InternalLoadResource(IInputStream& rInput, const CUniqueID& rkID, CFourCC Type)
|
||||
{
|
||||
// todo - need some sort of auto-registration of loaders to avoid this if-else mess
|
||||
ASSERT(mResourceCache.find(rkID.ToLongLong()) == mResourceCache.end()); // this test should be done before calling this func!
|
||||
CResource *pRes = nullptr;
|
||||
|
||||
// Load resource
|
||||
if (Type == "CMDL") pRes = CModelLoader::LoadCMDL(rInput);
|
||||
else if (Type == "TXTR") pRes = CTextureDecoder::LoadTXTR(rInput);
|
||||
else if (Type == "ANCS") pRes = CAnimSetLoader::LoadANCS(rInput);
|
||||
else if (Type == "CHAR") pRes = CAnimSetLoader::LoadCHAR(rInput);
|
||||
else if (Type == "MREA") pRes = CAreaLoader::LoadMREA(rInput);
|
||||
else if (Type == "MLVL") pRes = CWorldLoader::LoadMLVL(rInput);
|
||||
else if (Type == "STRG") pRes = CStringLoader::LoadSTRG(rInput);
|
||||
else if (Type == "FONT") pRes = CFontLoader::LoadFONT(rInput);
|
||||
else if (Type == "SCAN") pRes = CScanLoader::LoadSCAN(rInput);
|
||||
else if (Type == "DCLN") pRes = CCollisionLoader::LoadDCLN(rInput);
|
||||
else if (Type == "EGMC") pRes = CPoiToWorldLoader::LoadEGMC(rInput);
|
||||
else if (Type == "CINF") pRes = CSkeletonLoader::LoadCINF(rInput);
|
||||
else if (Type == "ANIM") pRes = CAnimationLoader::LoadANIM(rInput);
|
||||
else if (Type == "CSKR") pRes = CSkinLoader::LoadCSKR(rInput);
|
||||
if (!pRes) pRes = new CResource(); // Default for unsupported formats
|
||||
|
||||
ASSERT(pRes->mRefCount == 0);
|
||||
|
||||
// Cache and return
|
||||
pRes->mID = rkID;
|
||||
mResourceCache[rkID.ToLongLong()] = pRes;
|
||||
return pRes;
|
||||
}
|
||||
|
||||
CResCache gResCache;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#define CRESCACHE_H
|
||||
|
||||
#include "CResource.h"
|
||||
#include "Core/GameProject/CGameExporter.h"
|
||||
#include <Common/types.h>
|
||||
#include <Common/TString.h>
|
||||
#include <unordered_map>
|
||||
@@ -10,6 +11,7 @@ class CResCache
|
||||
{
|
||||
std::unordered_map<u64, CResource*> mResourceCache;
|
||||
TString mResDir;
|
||||
CGameExporter *mpGameExporter;
|
||||
|
||||
public:
|
||||
CResCache();
|
||||
@@ -22,6 +24,11 @@ public:
|
||||
CFourCC FindResourceType(CUniqueID ResID, const TStringList& rkPossibleTypes);
|
||||
void CacheResource(CResource *pRes);
|
||||
void DeleteResource(CUniqueID ResID);
|
||||
|
||||
inline void SetGameExporter(CGameExporter *pExporter) { mpGameExporter = pExporter; }
|
||||
|
||||
protected:
|
||||
CResource* InternalLoadResource(IInputStream& rInput, const CUniqueID& rkID, CFourCC Type);
|
||||
};
|
||||
|
||||
extern CResCache gResCache;
|
||||
|
||||
@@ -120,7 +120,7 @@ REGISTER_RESOURCE_TYPE(MREA, eArea, ePrimeDemo, eReturns)
|
||||
REGISTER_RESOURCE_TYPE(NTWK, eTweak, eEchoesDemo, eReturns)
|
||||
REGISTER_RESOURCE_TYPE(PAK , ePackage, ePrimeDemo, eReturns)
|
||||
REGISTER_RESOURCE_TYPE(PART, eParticle, ePrimeDemo, eReturns)
|
||||
REGISTER_RESOURCE_TYPE(PATH, eNavMesh, ePrimeDemo, eCorruption)
|
||||
REGISTER_RESOURCE_TYPE(PATH, ePathfinding, ePrimeDemo, eCorruption)
|
||||
REGISTER_RESOURCE_TYPE(PTLA, ePortalArea, eEchoesDemo, eCorruption)
|
||||
REGISTER_RESOURCE_TYPE(RULE, eRuleSet, eEchoesDemo, eReturns)
|
||||
REGISTER_RESOURCE_TYPE(SAND, eSourceAnimData, eCorruptionProto, eCorruption)
|
||||
|
||||
@@ -14,7 +14,6 @@ enum EResType
|
||||
eAudioMacro,
|
||||
eAudioGroupSet,
|
||||
eAudioSample,
|
||||
eStreamedAudio,
|
||||
eAudioLookupTable,
|
||||
eBinaryData,
|
||||
eBurstFireData,
|
||||
@@ -25,14 +24,12 @@ enum EResType
|
||||
eGuiFrame,
|
||||
eGuiKeyFrame,
|
||||
eHintSystem,
|
||||
eInvalidResType,
|
||||
eMapArea,
|
||||
eMapWorld,
|
||||
eMapUniverse,
|
||||
eMidi,
|
||||
eModel,
|
||||
eMusicTrack,
|
||||
eNavMesh,
|
||||
ePackage,
|
||||
eParticle,
|
||||
eParticleCollisionResponse,
|
||||
@@ -43,6 +40,7 @@ enum EResType
|
||||
eParticleSwoosh,
|
||||
eParticleTransform,
|
||||
eParticleWeapon,
|
||||
ePathfinding,
|
||||
ePortalArea,
|
||||
eResource,
|
||||
eRuleSet,
|
||||
@@ -56,6 +54,7 @@ enum EResType
|
||||
eStateMachine,
|
||||
eStateMachine2, // For distinguishing AFSM/FSM2
|
||||
eStaticGeometryMap,
|
||||
eStreamedAudio,
|
||||
eStringList,
|
||||
eStringTable,
|
||||
eTexture,
|
||||
@@ -63,7 +62,9 @@ enum EResType
|
||||
eUnknown_CAAD,
|
||||
eUserEvaluatorData,
|
||||
eVideo,
|
||||
eWorld
|
||||
eWorld,
|
||||
|
||||
eInvalidResType = -1
|
||||
};
|
||||
|
||||
// defined in CResource.cpp
|
||||
|
||||
@@ -201,6 +201,10 @@ void CTextureDecoder::ReadDDS(IInputStream& rDDS)
|
||||
// ************ DECODE ************
|
||||
void CTextureDecoder::PartialDecodeGXTexture(IInputStream& TXTR)
|
||||
{
|
||||
// TODO: This function doesn't handle very small mipmaps correctly.
|
||||
// The format applies padding when the size of a mipmap is less than the block size for that format.
|
||||
// The decode needs to be adjusted to account for the padding and skip over it (since we don't have padding in OpenGL).
|
||||
|
||||
// Get image data size, create output buffer
|
||||
u32 ImageStart = TXTR.Tell();
|
||||
TXTR.Seek(0x0, SEEK_END);
|
||||
@@ -233,11 +237,22 @@ void CTextureDecoder::PartialDecodeGXTexture(IInputStream& TXTR)
|
||||
MipH /= 4;
|
||||
}
|
||||
|
||||
// This value set to true if we hit the end of the file earlier than expected.
|
||||
// This is necessary due to a mistake Retro made in their cooker for I8 textures where very small mipmaps are cut off early, resulting in an out-of-bounds memory access.
|
||||
// This affects one texture that I know of - Echoes 3bb2c034.TXTR
|
||||
bool BreakEarly = false;
|
||||
|
||||
for (u32 iMip = 0; iMip < mNumMipMaps; iMip++)
|
||||
{
|
||||
if (MipW < BWidth) MipW = BWidth;
|
||||
if (MipH < BHeight) MipH = BHeight;
|
||||
|
||||
for (u32 iBlockY = 0; iBlockY < MipH; iBlockY += BHeight)
|
||||
for (u32 iBlockX = 0; iBlockX < MipW; iBlockX += BWidth) {
|
||||
for (u32 iImgY = iBlockY; iImgY < iBlockY + BHeight; iImgY++) {
|
||||
{
|
||||
for (u32 iBlockX = 0; iBlockX < MipW; iBlockX += BWidth)
|
||||
{
|
||||
for (u32 iImgY = iBlockY; iImgY < iBlockY + BHeight; iImgY++)
|
||||
{
|
||||
for (u32 iImgX = iBlockX; iImgX < iBlockX + BWidth; iImgX++)
|
||||
{
|
||||
u32 DstPos = ((iImgY * MipW) + iImgX) * PixelStride;
|
||||
@@ -256,10 +271,17 @@ void CTextureDecoder::PartialDecodeGXTexture(IInputStream& TXTR)
|
||||
|
||||
// I4 and C4 have 4bpp images, so I'm forced to read two pixels at a time.
|
||||
if ((mTexelFormat == eGX_I4) || (mTexelFormat == eGX_C4)) iImgX++;
|
||||
|
||||
// Check if we're at the end of the file.
|
||||
if (TXTR.EoF()) BreakEarly = true;
|
||||
}
|
||||
if (BreakEarly) break;
|
||||
}
|
||||
if (mTexelFormat == eGX_RGBA8) TXTR.Seek(0x20, SEEK_CUR);
|
||||
if (BreakEarly) break;
|
||||
}
|
||||
if (BreakEarly) break;
|
||||
}
|
||||
|
||||
u32 MipSize = (u32) (MipW * MipH * gskPixelsToBytes[mTexelFormat]);
|
||||
if (mTexelFormat == eGX_CMPR) MipSize *= 16; // Since we're pretending the image is 1/4 its actual size, we have to multiply the size by 16 to get the correct offset
|
||||
@@ -267,8 +289,8 @@ void CTextureDecoder::PartialDecodeGXTexture(IInputStream& TXTR)
|
||||
MipOffset += MipSize;
|
||||
MipW /= 2;
|
||||
MipH /= 2;
|
||||
if (MipW < BWidth) MipW = BWidth;
|
||||
if (MipH < BHeight) MipH = BHeight;
|
||||
|
||||
if (BreakEarly) break;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ class CScriptObject
|
||||
friend class CAreaLoader;
|
||||
|
||||
CScriptTemplate *mpTemplate;
|
||||
TResPtr<CGameArea> mpArea;
|
||||
CGameArea *mpArea;
|
||||
CScriptLayer *mpLayer;
|
||||
u32 mVersion;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user