all repos — mgba @ 34db16b483c8d2714e90b45b8f4620d21d972193

mGBA Game Boy Advance Emulator

src/core/core.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 "core.h"
  7
  8#include "core/log.h"
  9#include "core/serialize.h"
 10#include "util/vfs.h"
 11
 12#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
 13#include "util/png-io.h"
 14
 15#ifdef M_CORE_GB
 16#include "gb/core.h"
 17#include "gb/gb.h"
 18#endif
 19#ifdef M_CORE_GBA
 20#include "gba/core.h"
 21#include "gba/gba.h"
 22#endif
 23
 24static struct mCoreFilter {
 25	bool (*filter)(struct VFile*);
 26	struct mCore* (*open)(void);
 27} _filters[] = {
 28#ifdef M_CORE_GBA
 29	{ GBAIsROM, GBACoreCreate },
 30#endif
 31#ifdef M_CORE_GB
 32	{ GBIsROM, GBCoreCreate },
 33#endif
 34	{ 0, 0 }
 35};
 36
 37struct mCore* mCoreFind(const char* path) {
 38	struct VDir* archive = VDirOpenArchive(path);
 39	struct mCore* (*open)(void) = NULL;
 40	if (archive) {
 41		struct VDirEntry* dirent = archive->listNext(archive);
 42		while (dirent) {
 43			struct VFile* vf = archive->openFile(archive, dirent->name(dirent), O_RDONLY);
 44			if (!vf) {
 45				dirent = archive->listNext(archive);
 46				continue;
 47			}
 48			struct mCoreFilter* filter;
 49			for (filter = &_filters[0]; filter->filter; ++filter) {
 50				if (filter->filter(vf)) {
 51					break;
 52				}
 53			}
 54			vf->close(vf);
 55			if (filter->open) {
 56				open = filter->open;
 57				break;
 58			}
 59			dirent = archive->listNext(archive);
 60		}
 61		archive->close(archive);
 62	} else {
 63		struct VFile* vf = VFileOpen(path, O_RDONLY);
 64		if (!vf) {
 65			return NULL;
 66		}
 67		struct mCoreFilter* filter;
 68		for (filter = &_filters[0]; filter->filter; ++filter) {
 69			if (filter->filter(vf)) {
 70				break;
 71			}
 72		}
 73		vf->close(vf);
 74		if (filter->open) {
 75			open = filter->open;
 76		}
 77	}
 78	if (open) {
 79		return open();
 80	}
 81	return NULL;
 82}
 83
 84bool mCoreLoadFile(struct mCore* core, const char* path) {
 85	struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
 86	if (!rom) {
 87		return false;
 88	}
 89
 90	bool ret = core->loadROM(core, rom);
 91	if (!ret) {
 92		rom->close(rom);
 93	}
 94	return ret;
 95}
 96
 97bool mCoreAutoloadSave(struct mCore* core) {
 98	return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.save, ".sav", O_CREAT | O_RDWR));
 99}
100
101bool mCoreAutoloadPatch(struct mCore* core) {
102	return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ups", O_RDONLY)) ||
103	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".ips", O_RDONLY)) ||
104	       core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, core->dirs.patch, ".bps", O_RDONLY));
105}
106
107bool mCoreSaveState(struct mCore* core, int slot, int flags) {
108	struct VFile* vf = mCoreGetState(core, slot, true);
109	if (!vf) {
110		return false;
111	}
112	bool success = mCoreSaveStateNamed(core, vf, flags);
113	vf->close(vf);
114	if (success) {
115		mLOG(STATUS, INFO, "State %i saved", slot);
116	} else {
117		mLOG(STATUS, INFO, "State %i failed to save", slot);
118	}
119
120	return success;
121}
122
123bool mCoreLoadState(struct mCore* core, int slot, int flags) {
124	struct VFile* vf = mCoreGetState(core, slot, false);
125	if (!vf) {
126		return false;
127	}
128	bool success = mCoreLoadStateNamed(core, vf, flags);
129	vf->close(vf);
130	if (success) {
131		mLOG(STATUS, INFO, "State %i loaded", slot);
132	} else {
133		mLOG(STATUS, INFO, "State %i failed to loaded", slot);
134	}
135
136	return success;
137}
138
139struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
140	char name[PATH_MAX];
141	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
142	return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
143}
144
145void mCoreDeleteState(struct mCore* core, int slot) {
146	char name[PATH_MAX];
147	snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
148	core->dirs.state->deleteFile(core->dirs.state, name);
149}
150
151void mCoreTakeScreenshot(struct mCore* core) {
152#ifdef USE_PNG
153	size_t stride;
154	color_t* pixels = 0;
155	unsigned width, height;
156	core->desiredVideoDimensions(core, &width, &height);
157	struct VFile* vf = VDirFindNextAvailable(core->dirs.screenshot, core->dirs.baseName, "-", ".png", O_CREAT | O_TRUNC | O_WRONLY);
158	bool success = false;
159	if (vf) {
160		core->getVideoBuffer(core, &pixels, &stride);
161		png_structp png = PNGWriteOpen(vf);
162		png_infop info = PNGWriteHeader(png, width, height);
163		success = PNGWritePixels(png, width, height, stride, pixels);
164		PNGWriteClose(png, info);
165		vf->close(vf);
166	}
167	if (success) {
168		mLOG(STATUS, INFO, "Screenshot saved");
169		return;
170	}
171#else
172	UNUSED(core);
173#endif
174	mLOG(STATUS, WARN, "Failed to take screenshot");
175}
176#endif
177
178void mCoreInitConfig(struct mCore* core, const char* port) {
179	mCoreConfigInit(&core->config, port);
180}
181
182void mCoreLoadConfig(struct mCore* core) {
183#ifndef MINIMAL_CORE
184	mCoreConfigLoad(&core->config);
185#endif
186	mCoreLoadForeignConfig(core, &core->config);
187}
188
189void mCoreLoadForeignConfig(struct mCore* core, const struct mCoreConfig* config) {
190	mCoreConfigMap(config, &core->opts);
191#ifndef MINIMAL_CORE
192	mDirectorySetMapOptions(&core->dirs, &core->opts);
193#endif
194	if (core->opts.audioBuffers) {
195		core->setAudioBufferSize(core, core->opts.audioBuffers);
196	}
197	core->loadConfig(core, config);
198}