src/core/library.c (view raw)
1/* Copyright (c) 2013-2016 Jeffrey Pfau
2 *
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6#include <mgba/core/library.h>
7
8#include <mgba-util/vfs.h>
9
10DEFINE_VECTOR(mLibraryListing, struct mLibraryEntry);
11
12void mLibraryInit(struct mLibrary* library) {
13 mLibraryListingInit(&library->listing, 0);
14}
15
16void mLibraryDeinit(struct mLibrary* library) {
17 size_t i;
18 for (i = 0; i < mLibraryListingSize(&library->listing); ++i) {
19 struct mLibraryEntry* entry = mLibraryListingGetPointer(&library->listing, i);
20 free(entry->filename);
21 free(entry->title);
22 }
23 mLibraryListingDeinit(&library->listing);
24}
25
26void mLibraryLoadDirectory(struct mLibrary* library, struct VDir* dir) {
27 struct VDirEntry* dirent = dir->listNext(dir);
28 while (dirent) {
29 struct VFile* vf = dir->openFile(dir, dirent->name(dirent), O_RDONLY);
30 if (!vf) {
31 dirent = dir->listNext(dir);
32 continue;
33 }
34 mLibraryAddEntry(library, dirent->name(dirent), vf);
35 dirent = dir->listNext(dir);
36 }
37}
38
39void mLibraryAddEntry(struct mLibrary* library, const char* filename, struct VFile* vf) {
40 struct mCore* core;
41 if (!vf) {
42 vf = VFileOpen(filename, O_RDONLY);
43 }
44 if (!vf) {
45 return;
46 }
47 core = mCoreFindVF(vf);
48 if (core) {
49 core->init(core);
50 core->loadROM(core, vf);
51
52 struct mLibraryEntry* entry = mLibraryListingAppend(&library->listing);
53 core->getGameTitle(core, entry->internalTitle);
54 core->getGameCode(core, entry->internalCode);
55 entry->title = NULL;
56 entry->filename = strdup(filename);
57 entry->filesize = vf->size(vf);
58 // Note: this destroys the VFile
59 core->deinit(core);
60 } else {
61 vf->close(vf);
62 }
63}