mirror of
https://github.com/elasota/Aerofoil.git
synced 2025-12-14 03:59:36 +00:00
File system refactor, bug fixes
This commit is contained in:
@@ -100,6 +100,7 @@ namespace
|
||||
|
||||
namespace PortabilityLayer
|
||||
{
|
||||
class CompositeFile;
|
||||
struct MMHandleBlock;
|
||||
struct IResourceArchive;
|
||||
|
||||
@@ -114,7 +115,7 @@ namespace PortabilityLayer
|
||||
THandle<void> GetAppResource(const ResTypeID &resTypeID, int16_t resID) const override;
|
||||
IResourceArchive *GetAppResourceArchive() const override;
|
||||
|
||||
IResourceArchive *LoadResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) const override;
|
||||
IResourceArchive *LoadResFile(CompositeFile *file) const override;
|
||||
PLError_t CreateBlankResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) override;
|
||||
|
||||
void DissociateHandle(MMHandleBlock *hdl) const override;
|
||||
@@ -125,21 +126,23 @@ namespace PortabilityLayer
|
||||
private:
|
||||
void UnloadAndDestroyResourceFile(IResourceArchive *rf);
|
||||
|
||||
IResourceArchive *LoadResDirectory(VirtualDirectory_t virtualDir, const PLPasStr &filename) const;
|
||||
|
||||
IResourceArchive *m_appResArchive;
|
||||
CompositeFile *m_appResFile;
|
||||
|
||||
static ResourceManagerImpl ms_instance;
|
||||
};
|
||||
|
||||
ResourceManagerImpl::ResourceManagerImpl()
|
||||
: m_appResArchive(nullptr)
|
||||
, m_appResFile(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
void ResourceManagerImpl::Init()
|
||||
{
|
||||
m_appResArchive = LoadResFile(VirtualDirectories::kApplicationData, PSTR("ApplicationResources"));
|
||||
m_appResFile = PortabilityLayer::FileManager::GetInstance()->OpenCompositeFile(VirtualDirectories::kApplicationData, PSTR("ApplicationResources"));
|
||||
if (m_appResFile)
|
||||
m_appResArchive = LoadResFile(m_appResFile);
|
||||
}
|
||||
|
||||
void ResourceManagerImpl::Shutdown()
|
||||
@@ -147,6 +150,9 @@ namespace PortabilityLayer
|
||||
if (m_appResArchive)
|
||||
m_appResArchive->Destroy();
|
||||
|
||||
if (m_appResFile)
|
||||
m_appResFile->Close();
|
||||
|
||||
m_appResArchive = nullptr;
|
||||
}
|
||||
|
||||
@@ -168,47 +174,25 @@ namespace PortabilityLayer
|
||||
rf->Destroy();
|
||||
}
|
||||
|
||||
IResourceArchive *ResourceManagerImpl::LoadResDirectory(VirtualDirectory_t virtualDir, const PLPasStr &filename) const
|
||||
{
|
||||
ResourceArchiveDirectory *archive = ResourceArchiveDirectory::Create(virtualDir, filename);
|
||||
if (!archive)
|
||||
return nullptr;
|
||||
|
||||
if (!archive->Init())
|
||||
{
|
||||
archive->Destroy();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return archive;
|
||||
}
|
||||
|
||||
ResourceManagerImpl *ResourceManagerImpl::GetInstance()
|
||||
{
|
||||
return &ms_instance;
|
||||
}
|
||||
|
||||
IResourceArchive *ResourceManagerImpl::LoadResFile(VirtualDirectory_t virtualDir, const PLPasStr &filename) const
|
||||
IResourceArchive *ResourceManagerImpl::LoadResFile(CompositeFile *file) const
|
||||
{
|
||||
if (PLDrivers::GetFileSystem()->IsVirtualDirectoryLooseResources(virtualDir))
|
||||
return LoadResDirectory(virtualDir, filename);
|
||||
|
||||
GpIOStream *fStream = nullptr;
|
||||
if (FileManager::GetInstance()->RawOpenFileResources(virtualDir, filename, EFilePermission_Read, true, GpFileCreationDispositions::kOpenExisting, fStream) != PLErrors::kNone)
|
||||
ZipFileProxy *proxy = nullptr;
|
||||
bool proxyIsShared = false;
|
||||
if (file->OpenResources(fStream, proxy, proxyIsShared) != PLErrors::kNone)
|
||||
return nullptr;
|
||||
|
||||
ZipFileProxy *proxy = ZipFileProxy::Create(fStream);
|
||||
if (!proxy)
|
||||
{
|
||||
fStream->Close();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
IResourceArchive *archive = ResourceArchiveZipFile::Create(proxy, fStream);
|
||||
IResourceArchive *archive = ResourceArchiveZipFile::Create(proxy, proxyIsShared, fStream);
|
||||
if (!archive)
|
||||
{
|
||||
proxy->Destroy();
|
||||
fStream->Close();
|
||||
if (fStream)
|
||||
fStream->Close();
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
@@ -296,7 +280,7 @@ namespace PortabilityLayer
|
||||
|
||||
// ===========================================================================================
|
||||
|
||||
ResourceArchiveZipFile *ResourceArchiveZipFile::Create(ZipFileProxy *zipFileProxy, GpIOStream *stream)
|
||||
ResourceArchiveZipFile *ResourceArchiveZipFile::Create(ZipFileProxy *zipFileProxy, bool proxyIsShared, GpIOStream *stream)
|
||||
{
|
||||
PortabilityLayer::MemoryManager *mm = PortabilityLayer::MemoryManager::GetInstance();
|
||||
|
||||
@@ -320,7 +304,7 @@ namespace PortabilityLayer
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
return new (storage) ResourceArchiveZipFile(zipFileProxy, stream, refs);
|
||||
return new (storage) ResourceArchiveZipFile(zipFileProxy, proxyIsShared, stream, refs);
|
||||
}
|
||||
|
||||
void ResourceArchiveZipFile::Destroy()
|
||||
@@ -509,8 +493,9 @@ namespace PortabilityLayer
|
||||
return THandle<void>(handle);
|
||||
}
|
||||
|
||||
ResourceArchiveZipFile::ResourceArchiveZipFile(ZipFileProxy *zipFileProxy, GpIOStream *stream, ResourceArchiveRef *resourceHandles)
|
||||
ResourceArchiveZipFile::ResourceArchiveZipFile(ZipFileProxy *zipFileProxy, bool proxyIsShared, GpIOStream *stream, ResourceArchiveRef *resourceHandles)
|
||||
: m_zipFileProxy(zipFileProxy)
|
||||
, m_proxyIsShared(proxyIsShared)
|
||||
, m_stream(stream)
|
||||
, m_resourceHandles(resourceHandles)
|
||||
{
|
||||
@@ -532,336 +517,10 @@ namespace PortabilityLayer
|
||||
|
||||
mm->Release(m_resourceHandles);
|
||||
|
||||
m_zipFileProxy->Destroy();
|
||||
m_stream->Close();
|
||||
}
|
||||
if (!m_proxyIsShared)
|
||||
m_zipFileProxy->Destroy();
|
||||
|
||||
// ========================================================================================
|
||||
|
||||
ResourceArchiveDirectory *ResourceArchiveDirectory::Create(VirtualDirectory_t directory, const PLPasStr &subdirectory)
|
||||
{
|
||||
void *storage = PortabilityLayer::MemoryManager::GetInstance()->Alloc(sizeof(ResourceArchiveDirectory));
|
||||
if (!storage)
|
||||
return nullptr;
|
||||
|
||||
return new (storage) ResourceArchiveDirectory(directory, subdirectory);
|
||||
}
|
||||
|
||||
void ResourceArchiveDirectory::Destroy()
|
||||
{
|
||||
this->~ResourceArchiveDirectory();
|
||||
PortabilityLayer::MemoryManager::GetInstance()->Release(this);
|
||||
}
|
||||
|
||||
THandle<void> ResourceArchiveDirectory::LoadResource(const ResTypeID &resTypeID, int id)
|
||||
{
|
||||
return GetResource(resTypeID, id, true);
|
||||
}
|
||||
|
||||
bool ResourceArchiveDirectory::HasAnyResourcesOfType(const ResTypeID &resTypeID) const
|
||||
{
|
||||
int16_t scratch;
|
||||
return FindFirstResourceOfType(resTypeID, scratch);
|
||||
}
|
||||
|
||||
bool ResourceArchiveDirectory::FindFirstResourceOfType(const ResTypeID &resTypeID, int16_t &outID) const
|
||||
{
|
||||
int32_t resID32 = resTypeID.ExportAsInt32();
|
||||
|
||||
const ResTypeEntry *firstTypeEntry = *m_resTypes;
|
||||
const ResTypeEntry *lastTypeEntry = firstTypeEntry + m_numResourceTypes;
|
||||
|
||||
const ResTypeEntry *entry = BinarySearch(firstTypeEntry, lastTypeEntry, resID32, ResourceArchiveDirectory::ResTypeSearchPredicate);
|
||||
|
||||
if (entry == lastTypeEntry)
|
||||
return false;
|
||||
|
||||
outID = (*m_resIDs)[entry->m_firstRes];
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourceArchiveDirectory::Init()
|
||||
{
|
||||
IGpFileSystem *fs = PLDrivers::GetFileSystem();
|
||||
|
||||
const char *typePaths[1] = { this->m_subdirectory };
|
||||
|
||||
IGpDirectoryCursor *typeDirCursor = fs->ScanDirectoryNested(m_directory, typePaths, 1);
|
||||
if (!typeDirCursor)
|
||||
return false;
|
||||
|
||||
PortabilityLayer::MemoryManager *mm = PortabilityLayer::MemoryManager::GetInstance();
|
||||
|
||||
m_resTypes = THandle<ResTypeEntry>(mm->AllocHandle(0));
|
||||
if (!m_resTypes)
|
||||
return false;
|
||||
|
||||
m_resIDs = THandle<int16_t>(mm->AllocHandle(0));
|
||||
if (!m_resIDs)
|
||||
return false;
|
||||
|
||||
size_t resTypeCapacity = 0;
|
||||
size_t resIDCapacity = 0;
|
||||
|
||||
const char *typeScanFilename = nullptr;
|
||||
while (typeDirCursor->GetNext(typeScanFilename))
|
||||
{
|
||||
GpArcResourceTypeTag resourceTypeTag;
|
||||
if (!resourceTypeTag.Load(typeScanFilename))
|
||||
continue;
|
||||
|
||||
ResTypeID resTypeID;
|
||||
if (!resourceTypeTag.Decode(resTypeID))
|
||||
continue;
|
||||
|
||||
int32_t dirResType = resTypeID.ExportAsInt32();
|
||||
|
||||
ResTypeEntry rte;
|
||||
rte.m_resTypeID = dirResType;
|
||||
rte.m_firstRes = m_numResources;
|
||||
rte.m_lastRes = m_numResources;
|
||||
|
||||
const char *idScanFilenames[2] = { this->m_subdirectory, typeScanFilename };
|
||||
IGpDirectoryCursor *typeIDCursor = fs->ScanDirectoryNested(m_directory, idScanFilenames, 2);
|
||||
if (!typeIDCursor)
|
||||
continue;
|
||||
|
||||
const char *idScanFilename = nullptr;
|
||||
while (typeIDCursor->GetNext(idScanFilename))
|
||||
{
|
||||
int resID = 0;
|
||||
bool isNegative = false;
|
||||
|
||||
for (size_t chi = 0; idScanFilename[chi] != '.' && idScanFilename[chi] != '\0'; chi++)
|
||||
{
|
||||
char ch = idScanFilename[chi];
|
||||
if (ch == '-')
|
||||
isNegative = true;
|
||||
else if (ch >= '0' && ch <= '9')
|
||||
{
|
||||
resID *= 10;
|
||||
int digit = ch - '0';
|
||||
if (isNegative)
|
||||
resID -= digit;
|
||||
else
|
||||
resID += digit;
|
||||
}
|
||||
else
|
||||
break;
|
||||
}
|
||||
|
||||
if (m_numResources == resIDCapacity)
|
||||
{
|
||||
const size_t oldCapacity = resIDCapacity;
|
||||
|
||||
resIDCapacity *= 2;
|
||||
if (resIDCapacity == 0)
|
||||
resIDCapacity = 1;
|
||||
|
||||
if (!mm->ResizeHandle(m_resIDs.MMBlock(), sizeof(int16_t) * resIDCapacity))
|
||||
{
|
||||
typeIDCursor->Destroy();
|
||||
typeDirCursor->Destroy();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
(*m_resIDs)[m_numResources] = resID;
|
||||
m_numResources++;
|
||||
}
|
||||
|
||||
typeIDCursor->Destroy();
|
||||
rte.m_lastRes = m_numResources;
|
||||
|
||||
if (m_numResourceTypes == resTypeCapacity)
|
||||
{
|
||||
const size_t oldCapacity = resTypeCapacity;
|
||||
|
||||
resTypeCapacity *= 2;
|
||||
if (resTypeCapacity == 0)
|
||||
resTypeCapacity = 1;
|
||||
|
||||
if (!mm->ResizeHandle(m_resTypes.MMBlock(), sizeof(ResTypeEntry) * resTypeCapacity))
|
||||
{
|
||||
typeDirCursor->Destroy();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
(*m_resTypes)[m_numResourceTypes] = rte;
|
||||
m_numResourceTypes++;
|
||||
}
|
||||
|
||||
mm->ResizeHandle(m_resTypes.MMBlock(), sizeof(ResTypeEntry) * m_numResourceTypes);
|
||||
mm->ResizeHandle(m_resIDs.MMBlock(), sizeof(int16_t) * m_numResources);
|
||||
|
||||
ResTypeEntry *resTypes = *m_resTypes;
|
||||
int16_t *resIDs = *m_resIDs;
|
||||
|
||||
std::sort(resTypes, resTypes + m_numResourceTypes, ResourceArchiveDirectory::ResTypeEntrySortPredicate);
|
||||
|
||||
for (size_t i = 0; i < m_numResourceTypes; i++)
|
||||
{
|
||||
int16_t *resIDStart = resIDs + resTypes[i].m_firstRes;
|
||||
int16_t *resIDEnd = resIDs + resTypes[i].m_lastRes;
|
||||
|
||||
std::sort(resIDStart, resIDEnd);
|
||||
}
|
||||
|
||||
m_resourceHandles = static_cast<ResourceArchiveRef*>(mm->Alloc(sizeof(ResourceArchiveRef) * m_numResources));
|
||||
if (!m_resourceHandles)
|
||||
return false;
|
||||
|
||||
for (size_t i = 0; i < m_numResources; i++)
|
||||
new (m_resourceHandles + i) ResourceArchiveRef();
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ResourceArchiveDirectory::IndexResource(const ResTypeID &resTypeID, int id, size_t &outIndex) const
|
||||
{
|
||||
int32_t resID32 = resTypeID.ExportAsInt32();
|
||||
|
||||
const ResTypeEntry *firstTypeEntry = *m_resTypes;
|
||||
const ResTypeEntry *lastTypeEntry = firstTypeEntry + m_numResourceTypes;
|
||||
|
||||
const ResTypeEntry *entry = BinarySearch(firstTypeEntry, lastTypeEntry, resID32, ResourceArchiveDirectory::ResTypeSearchPredicate);
|
||||
|
||||
if (entry == lastTypeEntry)
|
||||
return false;
|
||||
|
||||
const int16_t *resIDs = *m_resIDs;
|
||||
const int16_t *firstRes = resIDs + entry->m_firstRes;
|
||||
const int16_t *lastRes = resIDs + entry->m_lastRes;
|
||||
|
||||
const int16_t *idLoc = BinarySearch(firstRes, lastRes, static_cast<int16_t>(id), ResourceArchiveDirectory::ResIDSearchPredicate);
|
||||
if (idLoc == lastRes)
|
||||
return false;
|
||||
|
||||
outIndex = static_cast<size_t>(idLoc - resIDs);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
THandle<void> ResourceArchiveDirectory::GetResource(const ResTypeID &resTypeID, int id, bool load)
|
||||
{
|
||||
|
||||
int validationRule = 0;
|
||||
size_t index = 0;
|
||||
if (!IndexResource(resTypeID, id, index))
|
||||
return THandle<void>();
|
||||
|
||||
ResourceArchiveRef *ref = m_resourceHandles + index;
|
||||
|
||||
MMHandleBlock *handle = nullptr;
|
||||
if (ref->m_handle != nullptr)
|
||||
handle = ref->m_handle;
|
||||
else
|
||||
{
|
||||
handle = MemoryManager::GetInstance()->AllocHandle(0);
|
||||
if (!handle)
|
||||
return THandle<void>();
|
||||
|
||||
handle->m_rmSelfRef = ref;
|
||||
ref->m_handle = handle;
|
||||
ref->m_resID = static_cast<int16_t>(id);
|
||||
ref->m_size = 0;
|
||||
}
|
||||
|
||||
if (handle->m_contents == nullptr && load)
|
||||
{
|
||||
int validationRule = 0;
|
||||
const char *extension = GetFileExtensionForResType(resTypeID, validationRule);
|
||||
|
||||
GpArcResourceTypeTag resTypeTag = GpArcResourceTypeTag::Encode(resTypeID);
|
||||
char fileName[32];
|
||||
|
||||
snprintf(fileName, sizeof(fileName) - 1, "%i%s", id, extension);
|
||||
|
||||
const char *paths[3] = { m_subdirectory, resTypeTag.m_id, fileName };
|
||||
|
||||
GpIOStream *ioStream = PLDrivers::GetFileSystem()->OpenFileNested(m_directory, paths, 3, false, GpFileCreationDispositions::kOpenExisting);
|
||||
if (!ioStream)
|
||||
return THandle<void>();
|
||||
|
||||
size_t size = ioStream->Size();
|
||||
|
||||
void *contents = MemoryManager::GetInstance()->Alloc(size);
|
||||
handle->m_contents = contents;
|
||||
handle->m_size = size;
|
||||
ref->m_size = size;
|
||||
|
||||
bool readOK = (ioStream->Read(contents, size));
|
||||
ioStream->Close();
|
||||
|
||||
if (!readOK || (validationRule != ResourceValidationRules::kNone && !ValidateResource(contents, ref->m_size, static_cast<ResourceValidationRule_t>(validationRule))))
|
||||
{
|
||||
MemoryManager::GetInstance()->Release(contents);
|
||||
handle->m_contents = nullptr;
|
||||
handle->m_size = 0;
|
||||
ref->m_size = 0;
|
||||
|
||||
return THandle<void>();
|
||||
}
|
||||
}
|
||||
|
||||
return THandle<void>(handle);
|
||||
}
|
||||
|
||||
int ResourceArchiveDirectory::ResTypeSearchPredicate(int32_t resTypeID, const ResTypeEntry &entry)
|
||||
{
|
||||
if (resTypeID < entry.m_resTypeID)
|
||||
return -1;
|
||||
if (resTypeID > entry.m_resTypeID)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ResourceArchiveDirectory::ResIDSearchPredicate(int16_t resTypeID, int16_t entry)
|
||||
{
|
||||
if (resTypeID < entry)
|
||||
return -1;
|
||||
if (resTypeID > entry)
|
||||
return 1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
bool ResourceArchiveDirectory::ResTypeEntrySortPredicate(const ResTypeEntry &a, const ResTypeEntry &b)
|
||||
{
|
||||
return a.m_resTypeID < b.m_resTypeID;
|
||||
}
|
||||
|
||||
ResourceArchiveDirectory::ResourceArchiveDirectory(VirtualDirectory_t directory, const PLPasStr &subdirectory)
|
||||
: m_directory(directory)
|
||||
, m_numResourceTypes(0)
|
||||
, m_resourceHandles(nullptr)
|
||||
, m_numResources(0)
|
||||
{
|
||||
memcpy(m_subdirectory, subdirectory.UChars(), subdirectory.Length());
|
||||
m_subdirectory[subdirectory.Length()] = '\0';
|
||||
}
|
||||
|
||||
ResourceArchiveDirectory::~ResourceArchiveDirectory()
|
||||
{
|
||||
MemoryManager *mm = MemoryManager::GetInstance();
|
||||
|
||||
const size_t numHandles = m_numResources;
|
||||
|
||||
if (m_resourceHandles)
|
||||
{
|
||||
for (size_t i = 0; i < numHandles; i++)
|
||||
{
|
||||
ResourceArchiveRef &ref = m_resourceHandles[numHandles - 1 - i];
|
||||
if (ref.m_handle)
|
||||
mm->ReleaseHandle(ref.m_handle);
|
||||
|
||||
ref.~ResourceArchiveRef();
|
||||
}
|
||||
}
|
||||
|
||||
mm->Release(m_resourceHandles);
|
||||
|
||||
m_resIDs.Dispose();
|
||||
m_resTypes.Dispose();
|
||||
if (m_stream)
|
||||
m_stream->Close();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user