2
0
mirror of https://github.com/AxioDL/metaforce.git synced 2025-12-08 17:04:55 +00:00

Finish CPakFile and CResLoader

This commit is contained in:
Jack Andersen
2017-10-25 19:37:46 -10:00
parent e274cd12b9
commit 7c3fb4174f
20 changed files with 561 additions and 124 deletions

View File

@@ -4,6 +4,11 @@
namespace urde
{
CResLoader::CResLoader()
{
x48_curPak = x18_pakLoadedList.end();
}
const std::vector<CAssetId>* CResLoader::GetTagListForFile(const std::string& name) const
{
std::string namePak = name + ".pak";
@@ -30,79 +35,78 @@ void CResLoader::AddPakFile(const std::string& name, bool samusPak, bool worldPa
AsyncIdlePakLoading();
}
CInputStream* CResLoader::LoadNewResourcePartSync(const SObjectTag& tag, int offset, int length, void* extBuf)
std::unique_ptr<CInputStream> CResLoader::LoadNewResourcePartSync(const SObjectTag& tag, int offset, int length, void* extBuf)
{
void* buf = extBuf;
CPakFile* file = FindResourceForLoad(tag);
if (!buf)
buf = new u8[length];
file->SyncSeekRead(buf, length, ESeekOrigin::Begin, x50_cachedResInfo->x4_offset + offset);
return new CMemoryInStream((atUint8*)buf, length, !extBuf);
file->SyncSeekRead(buf, length, ESeekOrigin::Begin, x50_cachedResInfo->GetOffset() + offset);
return std::unique_ptr<CInputStream>(new athena::io::MemoryReader((atUint8*)buf, length, !extBuf));
}
void CResLoader::LoadMemResourceSync(const SObjectTag& tag, void** bufOut, int* sizeOut)
void CResLoader::LoadMemResourceSync(const SObjectTag& tag, std::unique_ptr<u8[]>& bufOut, int* sizeOut)
{
CPakFile* file = FindResourceForLoad(tag);
void* buf = new u8[x50_cachedResInfo->x8_size];
file->SyncSeekRead(buf, x50_cachedResInfo->x8_size, ESeekOrigin::Begin,
x50_cachedResInfo->x4_offset);
*bufOut = buf;
*sizeOut = x50_cachedResInfo->x8_size;
bufOut = std::unique_ptr<u8[]>(new u8[x50_cachedResInfo->GetSize()]);
file->SyncSeekRead(bufOut.get(), x50_cachedResInfo->GetSize(), ESeekOrigin::Begin,
x50_cachedResInfo->GetOffset());
*sizeOut = x50_cachedResInfo->GetSize();
}
CInputStream* CResLoader::LoadResourceFromMemorySync(const SObjectTag& tag, const void* buf)
std::unique_ptr<CInputStream> CResLoader::LoadResourceFromMemorySync(const SObjectTag& tag, const void* buf)
{
FindResourceForLoad(tag);
CInputStream* newStrm = new CMemoryInStream((atUint8*)buf, x50_cachedResInfo->x8_size);
if (x50_cachedResInfo->xb_compressed)
CInputStream* newStrm = new athena::io::MemoryReader((atUint8*)buf, x50_cachedResInfo->GetSize());
if (x50_cachedResInfo->IsCompressed())
{
newStrm->readUint32Big();
newStrm = new CZipInputStream(std::unique_ptr<CInputStream>(newStrm));
}
return newStrm;
return std::unique_ptr<CInputStream>(newStrm);
}
CInputStream* CResLoader::LoadNewResourceSync(const SObjectTag& tag, void* extBuf)
std::unique_ptr<CInputStream> CResLoader::LoadNewResourceSync(const SObjectTag& tag, void* extBuf)
{
void* buf = extBuf;
CPakFile* file = FindResourceForLoad(tag);
size_t resSz = ROUND_UP_32(x50_cachedResInfo->x8_size);
size_t resSz = ROUND_UP_32(x50_cachedResInfo->GetSize());
if (!buf)
buf = new u8[resSz];
file->SyncSeekRead(buf, resSz, ESeekOrigin::Begin, x50_cachedResInfo->x4_offset);
CInputStream* newStrm = new CMemoryInStream((atUint8*)buf, resSz, !extBuf);
if (x50_cachedResInfo->xb_compressed)
file->SyncSeekRead(buf, resSz, ESeekOrigin::Begin, x50_cachedResInfo->GetOffset());
CInputStream* newStrm = new athena::io::MemoryReader((atUint8*)buf, resSz, !extBuf);
if (x50_cachedResInfo->IsCompressed())
{
newStrm->readUint32Big();
newStrm = new CZipInputStream(std::unique_ptr<CInputStream>(newStrm));
}
return newStrm;
return std::unique_ptr<CInputStream>(newStrm);
}
std::shared_ptr<IDvdRequest> CResLoader::LoadResourcePartAsync(const SObjectTag& tag, int offset, int length, void* buf)
{
return FindResourceForLoad(tag.id)->AsyncSeekRead(buf, length,
ESeekOrigin::Begin, x50_cachedResInfo->x4_offset + offset);
ESeekOrigin::Begin, x50_cachedResInfo->GetOffset() + offset);
}
std::shared_ptr<IDvdRequest> CResLoader::LoadResourceAsync(const SObjectTag& tag, void* buf)
{
return FindResourceForLoad(tag.id)->AsyncSeekRead(buf, ROUND_UP_32(x50_cachedResInfo->x8_size),
ESeekOrigin::Begin, x50_cachedResInfo->x4_offset);
return FindResourceForLoad(tag.id)->AsyncSeekRead(buf, ROUND_UP_32(x50_cachedResInfo->GetSize()),
ESeekOrigin::Begin, x50_cachedResInfo->GetOffset());
}
bool CResLoader::GetResourceCompression(const SObjectTag& tag)
{
if (FindResource(tag.id))
return x50_cachedResInfo->xb_compressed;
return x50_cachedResInfo->IsCompressed();
return false;
}
u32 CResLoader::ResourceSize(const SObjectTag& tag)
{
if (FindResource(tag.id))
return x50_cachedResInfo->x8_size;
return false;
return x50_cachedResInfo->GetSize();
return 0;
}
bool CResLoader::ResourceExists(const SObjectTag& tag)
@@ -113,7 +117,7 @@ bool CResLoader::ResourceExists(const SObjectTag& tag)
FourCC CResLoader::GetResourceTypeById(CAssetId id) const
{
if (FindResource(id))
return x50_cachedResInfo->x0_type;
return x50_cachedResInfo->GetType();
return FourCC();
}
@@ -151,17 +155,41 @@ void CResLoader::AsyncIdlePakLoading()
bool CResLoader::FindResource(CAssetId id) const
{
for (const std::unique_ptr<CPakFile>& file : x18_pakLoadedList)
if (const_cast<CResLoader*>(this)->CacheFromPak(*file, id))
if (x4c_cachedResId == id)
return true;
if (x48_curPak != x18_pakLoadedList.end())
if (CacheFromPak(**x48_curPak, id))
return true;
for (auto it = x18_pakLoadedList.begin() ; it != x18_pakLoadedList.end() ; ++it)
{
if (it == x48_curPak)
continue;
if (CacheFromPak(**it, id))
return true;
}
return false;
}
CPakFile* CResLoader::FindResourceForLoad(CAssetId id)
{
for (std::unique_ptr<CPakFile>& file : x18_pakLoadedList)
if (CacheFromPakForLoad(*file, id))
return file.get();
if (x48_curPak != x18_pakLoadedList.end())
if (CacheFromPakForLoad(**x48_curPak, id))
return &**x48_curPak;
for (auto it = x18_pakLoadedList.begin() ; it != x18_pakLoadedList.end() ; ++it)
{
if (it == x48_curPak)
continue;
if (CacheFromPakForLoad(**it, id))
{
x48_curPak = it;
return &**it;
}
}
return nullptr;
}
@@ -172,7 +200,16 @@ CPakFile* CResLoader::FindResourceForLoad(const SObjectTag& tag)
bool CResLoader::CacheFromPakForLoad(CPakFile& file, CAssetId id)
{
const CPakFile::SResInfo* info = file.GetResInfoForLoad(id);
const CPakFile::SResInfo* info;
if (x54_forwardSeek)
{
info = file.GetResInfoForLoadPreferForward(id);
x54_forwardSeek = false;
}
else
{
info = file.GetResInfoForLoadDirectionless(id);
}
if (info)
{
x4c_cachedResId = id;
@@ -182,7 +219,7 @@ bool CResLoader::CacheFromPakForLoad(CPakFile& file, CAssetId id)
return false;
}
bool CResLoader::CacheFromPak(const CPakFile& file, CAssetId id)
bool CResLoader::CacheFromPak(const CPakFile& file, CAssetId id) const
{
const CPakFile::SResInfo* info = file.GetResInfo(id);
if (info)