GBA Context: Use GBADirectorySet in GBAContext

This commit is contained in:
Jeffrey Pfau 2015-12-30 22:01:04 -08:00
parent 91cf3be128
commit 3a7350c8d2
7 changed files with 35 additions and 72 deletions

View File

@ -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;

View File

@ -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;

View File

@ -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;
}

View File

@ -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

View File

@ -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) {

View File

@ -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);

View File

@ -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);