From 3a7350c8d2d7332713630b7b4cf47f6f11a74b8e Mon Sep 17 00:00:00 2001 From: Jeffrey Pfau Date: Wed, 30 Dec 2015 22:01:04 -0800 Subject: [PATCH] GBA Context: Use GBADirectorySet in GBAContext --- src/gba/context/context.c | 45 +++++++++-------------------------- src/gba/context/context.h | 3 ++- src/gba/context/directories.c | 19 +++++++++++++++ src/gba/context/directories.h | 2 ++ src/gba/supervisor/thread.c | 7 +----- src/util/vfs.c | 28 ---------------------- src/util/vfs.h | 3 --- 7 files changed, 35 insertions(+), 72 deletions(-) diff --git a/src/gba/context/context.c b/src/gba/context/context.c index 0a5d552f1..4911c05eb 100644 --- a/src/gba/context/context.c +++ b/src/gba/context/context.c @@ -17,11 +17,11 @@ bool GBAContextInit(struct GBAContext* context, const char* port) { context->gba = anonymousMemoryMap(sizeof(struct GBA)); context->cpu = anonymousMemoryMap(sizeof(struct ARMCore)); context->rom = 0; - context->romDir = 0; context->bios = 0; context->fname = 0; context->save = 0; context->renderer = 0; + GBADirectorySetInit(&context->dirs); memset(context->components, 0, sizeof(context->components)); if (!context->gba || !context->cpu) { @@ -71,50 +71,31 @@ void GBAContextDeinit(struct GBAContext* context) { mappedMemoryFree(context->gba, 0); mappedMemoryFree(context->cpu, 0); GBAConfigDeinit(&context->config); + GBADirectorySetDeinit(&context->dirs); } bool GBAContextLoadROM(struct GBAContext* context, const char* path, bool autoloadSave) { - context->rom = VFileOpen(path, O_RDONLY); - struct VDir* dir = 0; - if (!GBAIsROM(context->rom)) { - context->rom->close(context->rom); - context->rom = 0; - 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); - } - if (!context->rom) { - dir->close(dir); - } else { - context->romDir = dir; - } - } else { - } - + context->rom = GBADirectorySetOpenPath(&context->dirs, path, GBAIsROM); if (!context->rom) { return false; } context->fname = path; if (autoloadSave) { - context->save = VDirOptionalOpenFile(0, path, 0, ".sav", O_RDWR | O_CREAT); + char dirname[PATH_MAX]; + char basename[PATH_MAX]; + separatePath(context->fname, dirname, basename, 0); + // TODO: Remove autoloadSave + GBADirectorySetAttachBase(&context->dirs, VDirOpen(dirname)); + strncat(basename, ".sav", PATH_MAX - strlen(basename) - 1); + context->save = context->dirs.save->openFile(context->dirs.save, basename, O_RDWR | O_CREAT); } return true; } void GBAContextUnloadROM(struct GBAContext* context) { GBAUnloadROM(context->gba); + GBADirectorySetDetachBase(&context->dirs); if (context->bios) { context->bios->close(context->bios); context->bios = 0; @@ -123,10 +104,6 @@ void GBAContextUnloadROM(struct GBAContext* context) { context->rom->close(context->rom); context->rom = 0; } - if (context->romDir) { - context->romDir->close(context->romDir); - context->romDir = 0; - } if (context->save) { context->save->close(context->save); context->save = 0; diff --git a/src/gba/context/context.h b/src/gba/context/context.h index d08d2f6f2..d71385074 100644 --- a/src/gba/context/context.h +++ b/src/gba/context/context.h @@ -9,6 +9,7 @@ #include "util/common.h" #include "gba/context/config.h" +#include "gba/context/directories.h" #include "gba/context/sync.h" #include "gba/input.h" @@ -17,10 +18,10 @@ struct GBAContext { struct ARMCore* cpu; struct GBAVideoRenderer* renderer; struct VFile* rom; - struct VDir* romDir; const char* fname; struct VFile* save; struct VFile* bios; + struct GBADirectorySet dirs; struct ARMComponent* components[GBA_COMPONENT_MAX]; struct GBAConfig config; struct GBAOptions opts; diff --git a/src/gba/context/directories.c b/src/gba/context/directories.c index 224b96d32..f9481fd9d 100644 --- a/src/gba/context/directories.c +++ b/src/gba/context/directories.c @@ -80,3 +80,22 @@ void GBADirectorySetDetachBase(struct GBADirectorySet* dirs) { dirs->base = 0; } } + +struct VFile* GBADirectorySetOpenPath(struct GBADirectorySet* dirs, const char* path, bool (*filter)(struct VFile*)) { + dirs->archive = VDirOpenArchive(path); + struct VFile* file; + if (dirs->archive) { + file = VDirFindFirst(dirs->archive, filter); + if (!file) { + dirs->archive->close(dirs->archive); + dirs->archive = 0; + } + } else { + file = VFileOpen(path, O_RDONLY); + if (!filter(file)) { + file->close(file); + file = 0; + } + } + return file; +} diff --git a/src/gba/context/directories.h b/src/gba/context/directories.h index 3a2044157..bf906c4ab 100644 --- a/src/gba/context/directories.h +++ b/src/gba/context/directories.h @@ -25,4 +25,6 @@ void GBADirectorySetDeinit(struct GBADirectorySet* dirs); void GBADirectorySetAttachBase(struct GBADirectorySet* dirs, struct VDir* base); void GBADirectorySetDetachBase(struct GBADirectorySet* dirs); +struct VFile* GBADirectorySetOpenPath(struct GBADirectorySet* dirs, const char* path, bool (*filter)(struct VFile*)); + #endif diff --git a/src/gba/supervisor/thread.c b/src/gba/supervisor/thread.c index 88696b693..bbc75ce80 100644 --- a/src/gba/supervisor/thread.c +++ b/src/gba/supervisor/thread.c @@ -685,12 +685,7 @@ void GBAThreadPauseFromThread(struct GBAThread* threadContext) { } void GBAThreadLoadROM(struct GBAThread* threadContext, const char* fname) { - threadContext->dirs.archive = VDirOpenArchive(fname); - if (threadContext->dirs.archive) { - threadContext->rom = VDirFindFirst(threadContext->dirs.archive, GBAIsROM); - } else { - threadContext->rom = VFileOpen(fname, O_RDONLY); - } + threadContext->rom = GBADirectorySetOpenPath(&threadContext->dirs, fname, GBAIsROM); } void GBAThreadReplaceROM(struct GBAThread* threadContext, const char* fname) { diff --git a/src/util/vfs.c b/src/util/vfs.c index 80d201384..39a85d2ef 100644 --- a/src/util/vfs.c +++ b/src/util/vfs.c @@ -203,34 +203,6 @@ void separatePath(const char* path, char* dirname, char* basename, char* extensi } } -struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, int mode) { - char path[PATH_MAX]; - path[PATH_MAX - 1] = '\0'; - struct VFile* vf; - if (!dir) { - if (!realPath) { - return 0; - } - char* dotPoint = strrchr(realPath, '.'); - if (dotPoint - realPath + 1 >= PATH_MAX - 1) { - return 0; - } - if (dotPoint > strrchr(realPath, '/')) { - int len = dotPoint - realPath; - strncpy(path, realPath, len); - path[len] = 0; - strncat(path + len, suffix, PATH_MAX - len - 1); - } else { - snprintf(path, PATH_MAX - 1, "%s%s", realPath, suffix); - } - vf = VFileOpen(path, mode); - } else { - snprintf(path, PATH_MAX - 1, "%s%s", prefix, suffix); - vf = dir->openFile(dir, path, mode); - } - return vf; -} - struct VFile* VDirFindFirst(struct VDir* dir, bool (*filter)(struct VFile*)) { dir->rewind(dir); struct VDirEntry* dirent = dir->listNext(dir); diff --git a/src/util/vfs.h b/src/util/vfs.h index 81dddc9c1..f7b89ad66 100644 --- a/src/util/vfs.h +++ b/src/util/vfs.h @@ -84,9 +84,6 @@ struct VDir* VDeviceList(void); void separatePath(const char* path, char* dirname, char* basename, char* extension); -struct VFile* VDirOptionalOpenFile(struct VDir* dir, const char* realPath, const char* prefix, const char* suffix, - int mode); - struct VFile* VDirFindFirst(struct VDir* dir, bool (*filter)(struct VFile*)); struct VFile* VDirFindNextAvailable(struct VDir*, const char* basename, const char* infix, const char* suffix, int mode);