GBA Context: Use GBADirectorySet in GBAContext
Jeffrey Pfau jeffrey@endrift.com
Wed, 30 Dec 2015 22:01:04 -0800
7 files changed,
35 insertions(+),
72 deletions(-)
M
src/gba/context/context.c
→
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 @@ GBADestroy(context->gba);
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;@@ -122,10 +103,6 @@ }
if (context->rom) { 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);
M
src/gba/context/context.h
→
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 GBA* gba;
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;
M
src/gba/context/directories.c
→
src/gba/context/directories.c
@@ -80,3 +80,22 @@ dirs->base->close(dirs->base);
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; +}
M
src/gba/context/directories.h
→
src/gba/context/directories.h
@@ -25,4 +25,6 @@
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
M
src/gba/supervisor/thread.c
→
src/gba/supervisor/thread.c
@@ -685,12 +685,7 @@ GBASyncSetVideoSync(&threadContext->sync, frameOn);
} 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) {
M
src/util/vfs.c
→
src/util/vfs.c
@@ -203,34 +203,6 @@ }
} } -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);
M
src/util/vfs.h
→
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);