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 "util/vfs.h"
10
11#if !defined(MINIMAL_CORE) || MINIMAL_CORE < 2
12bool mCoreLoadFile(struct mCore* core, const char* path) {
13 struct VFile* rom = mDirectorySetOpenPath(&core->dirs, path, core->isROM);
14 if (!rom) {
15 return false;
16 }
17
18 bool ret = core->loadROM(core, rom);
19 if (!ret) {
20 rom->close(rom);
21 }
22 return ret;
23}
24
25bool mCoreAutoloadSave(struct mCore* core) {
26 return core->loadSave(core, mDirectorySetOpenSuffix(&core->dirs, ".sav", O_CREAT | O_RDWR));
27}
28
29bool mCoreAutoloadPatch(struct mCore* core) {
30 return core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, ".ups", O_RDONLY)) ||
31 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, ".ips", O_RDONLY)) ||
32 core->loadPatch(core, mDirectorySetOpenSuffix(&core->dirs, ".bps", O_RDONLY));
33}
34
35bool mCoreSaveState(struct mCore* core, int slot, int flags) {
36 struct VFile* vf = mCoreGetState(core, slot, true);
37 if (!vf) {
38 return false;
39 }
40 bool success = core->saveState(core, vf, flags);
41 vf->close(vf);
42 if (success) {
43 mLOG(STATUS, INFO, "State %i saved", slot);
44 } else {
45 mLOG(STATUS, INFO, "State %i failed to save", slot);
46 }
47
48 return success;
49}
50
51bool mCoreLoadState(struct mCore* core, int slot, int flags) {
52 struct VFile* vf = mCoreGetState(core, slot, false);
53 if (!vf) {
54 return false;
55 }
56 bool success = core->loadState(core, vf, flags);
57 vf->close(vf);
58 if (success) {
59 mLOG(STATUS, INFO, "State %i loaded", slot);
60 } else {
61 mLOG(STATUS, INFO, "State %i failed to loaded", slot);
62 }
63
64 return success;
65}
66
67struct VFile* mCoreGetState(struct mCore* core, int slot, bool write) {
68 char name[PATH_MAX];
69 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
70 return core->dirs.state->openFile(core->dirs.state, name, write ? (O_CREAT | O_TRUNC | O_RDWR) : O_RDONLY);
71}
72
73void mCoreDeleteState(struct mCore* core, int slot) {
74 char name[PATH_MAX];
75 snprintf(name, sizeof(name), "%s.ss%i", core->dirs.baseName, slot);
76 core->dirs.state->deleteFile(core->dirs.state, name);
77}
78#endif
79
80void mCoreInitConfig(struct mCore* core, const char* port) {
81 mCoreConfigInit(&core->config, port);
82}
83
84void mCoreLoadConfig(struct mCore* core) {
85#ifndef MINIMAL_CORE
86 mCoreConfigLoad(&core->config);
87#endif
88 mCoreConfigMap(&core->config, &core->opts);
89#ifndef MINIMAL_CORE
90 mDirectorySetMapOptions(&core->dirs, &core->opts);
91#endif
92 core->loadConfig(core);
93}