From 5b22a628fa50820e8c20732210614adb0143edc3 Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Tue, 22 Sep 2015 21:18:46 -0700 Subject: [PATCH] VFS: Add VDirOpenArchive --- src/gba/context/context.c | 20 +++++++++++++++++++- src/gba/supervisor/thread.c | 9 +-------- src/util/vfs.c | 15 +++++++++++++++ src/util/vfs.h | 1 + 4 files changed, 36 insertions(+), 9 deletions(-) diff --git a/src/gba/context/context.c b/src/gba/context/context.c index 18f70094c..4818df6c7 100644 --- a/src/gba/context/context.c +++ b/src/gba/context/context.c @@ -73,7 +73,25 @@ void GBAContextDeinit(struct GBAContext* context) { } bool GBAContextLoadROM(struct GBAContext* context, const char* path, bool autoloadSave) { - context->rom = VFileOpen(path, O_RDONLY); + struct VDir* dir = VDirOpenArchive(path); + if (dir) { + struct VDirEntry* de; + while ((de = dir->listNext(dir))) { + struct VFile* vf = dir->openFile(dir, de->name(de), O_RDONLY); + if (!vf) { + continue; + } + if (GBAIsROM(vf)) { + context->rom = vf; + break; + } + vf->close(vf); + } + dir->close(dir); + } else { + context->rom = VFileOpen(path, O_RDONLY); + } + if (!context->rom) { return false; } diff --git a/src/gba/supervisor/thread.c b/src/gba/supervisor/thread.c index d5c35e190..b8e8730bd 100644 --- a/src/gba/supervisor/thread.c +++ b/src/gba/supervisor/thread.c @@ -683,16 +683,9 @@ void GBAThreadPauseFromThread(struct GBAThread* threadContext) { void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname) { threadContext->rom = VFileOpen(fname, O_RDONLY); threadContext->gameDir = 0; -#if USE_LIBZIP if (!threadContext->gameDir) { - threadContext->gameDir = VDirOpenZip(fname, 0); + threadContext->gameDir = VDirOpenArchive(fname); } -#endif -#if USE_LZMA - if (!threadContext->gameDir) { - threadContext->gameDir = VDirOpen7z(fname, 0); - } -#endif } static void _loadGameDir(struct GBAThread* threadContext) { diff --git a/src/util/vfs.c b/src/util/vfs.c index a4097f6c7..b50603b25 100644 --- a/src/util/vfs.c +++ b/src/util/vfs.c @@ -96,6 +96,21 @@ struct VFile* VFileOpen(const char* path, int flags) { #endif } +struct VDir* VDirOpenArchive(const char* path) { + struct VDir* dir = 0; +#if USE_LIBZIP + if (!dir) { + dir = VDirOpenZip(path, 0); + } +#endif +#if USE_LZMA + if (!dir) { + dir = VDirOpen7z(path, 0); + } +#endif + return dir; +} + ssize_t VFileReadline(struct VFile* vf, char* buffer, size_t size) { size_t bytesRead = 0; while (bytesRead < size - 1) { diff --git a/src/util/vfs.h b/src/util/vfs.h index 8f9fbb534..4763aefa5 100644 --- a/src/util/vfs.h +++ b/src/util/vfs.h @@ -69,6 +69,7 @@ struct VFile* VFileFromMemory(void* mem, size_t size); struct VFile* VFileFromFILE(FILE* file); struct VDir* VDirOpen(const char* path); +struct VDir* VDirOpenArchive(const char* path); #ifdef USE_LIBZIP struct VDir* VDirOpenZip(const char* path, int flags);