all repos — mgba @ 4c38f769565e8ddd7d3a8eef1a41975206c129a0

mGBA Game Boy Advance Emulator

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 "library.h"
 7
 8#include "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	core = mCoreFindVF(vf);
45	if (core) {
46		core->init(core);
47		core->loadROM(core, vf);
48
49		struct mLibraryEntry* entry = mLibraryListingAppend(&library->listing);
50		core->getGameTitle(core, entry->internalTitle);
51		core->getGameCode(core, entry->internalCode);
52		entry->title = NULL;
53		entry->filename = strdup(filename);
54		entry->filesize = vf->size(vf);
55		// Note: this destroys the VFile
56		core->deinit(core);
57	} else {
58		vf->close(vf);
59	}
60}