2016-08-27 08:01:03 +00:00
|
|
|
/* Copyright (c) 2013-2016 Jeffrey Pfau
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba/core/library.h>
|
2016-08-27 08:01:03 +00:00
|
|
|
|
2016-12-31 01:00:22 +00:00
|
|
|
#include <mgba-util/vfs.h>
|
2016-08-27 08:01:03 +00:00
|
|
|
|
|
|
|
DEFINE_VECTOR(mLibraryListing, struct mLibraryEntry);
|
|
|
|
|
|
|
|
void mLibraryInit(struct mLibrary* library) {
|
|
|
|
mLibraryListingInit(&library->listing, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mLibraryDeinit(struct mLibrary* library) {
|
|
|
|
size_t i;
|
|
|
|
for (i = 0; i < mLibraryListingSize(&library->listing); ++i) {
|
|
|
|
struct mLibraryEntry* entry = mLibraryListingGetPointer(&library->listing, i);
|
|
|
|
free(entry->filename);
|
|
|
|
free(entry->title);
|
|
|
|
}
|
|
|
|
mLibraryListingDeinit(&library->listing);
|
|
|
|
}
|
|
|
|
|
|
|
|
void mLibraryLoadDirectory(struct mLibrary* library, struct VDir* dir) {
|
|
|
|
struct VDirEntry* dirent = dir->listNext(dir);
|
|
|
|
while (dirent) {
|
|
|
|
struct VFile* vf = dir->openFile(dir, dirent->name(dirent), O_RDONLY);
|
|
|
|
if (!vf) {
|
|
|
|
dirent = dir->listNext(dir);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
mLibraryAddEntry(library, dirent->name(dirent), vf);
|
|
|
|
dirent = dir->listNext(dir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void mLibraryAddEntry(struct mLibrary* library, const char* filename, struct VFile* vf) {
|
|
|
|
struct mCore* core;
|
|
|
|
if (!vf) {
|
|
|
|
vf = VFileOpen(filename, O_RDONLY);
|
|
|
|
}
|
2016-09-17 19:41:54 +00:00
|
|
|
if (!vf) {
|
|
|
|
return;
|
|
|
|
}
|
2016-08-27 08:01:03 +00:00
|
|
|
core = mCoreFindVF(vf);
|
|
|
|
if (core) {
|
|
|
|
core->init(core);
|
|
|
|
core->loadROM(core, vf);
|
|
|
|
|
|
|
|
struct mLibraryEntry* entry = mLibraryListingAppend(&library->listing);
|
|
|
|
core->getGameTitle(core, entry->internalTitle);
|
|
|
|
core->getGameCode(core, entry->internalCode);
|
|
|
|
entry->title = NULL;
|
|
|
|
entry->filename = strdup(filename);
|
|
|
|
entry->filesize = vf->size(vf);
|
|
|
|
// Note: this destroys the VFile
|
|
|
|
core->deinit(core);
|
|
|
|
} else {
|
|
|
|
vf->close(vf);
|
|
|
|
}
|
|
|
|
}
|