-
-
Notifications
You must be signed in to change notification settings - Fork 747
/
CoreManager.cpp
178 lines (153 loc) · 4.54 KB
/
CoreManager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/* Copyright (c) 2013-2017 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/. */
#include "CoreManager.h"
#include "CoreController.h"
#include "LogController.h"
#include "VFileDevice.h"
#include <QDir>
#ifdef M_CORE_GBA
#include <mgba/gba/core.h>
#endif
#include <mgba/core/core.h>
#include <mgba-util/string.h>
#include <mgba-util/vfs.h>
using namespace QGBA;
void CoreManager::setConfig(const mCoreConfig* config) {
m_config = config;
}
void CoreManager::setMultiplayerController(MultiplayerController* multiplayer) {
m_multiplayer = multiplayer;
}
CoreController* CoreManager::loadGame(const QString& path) {
QFileInfo info(path);
if (!info.isReadable()) {
// Open specific file in archive
QString fname = info.fileName();
QString base = info.path();
if (base.endsWith("/") || base.endsWith(QDir::separator())) {
base.chop(1);
}
VDir* dir = VDirOpenArchive(base.toUtf8().constData());
if (dir) {
VFile* vf = dir->openFile(dir, fname.toUtf8().constData(), O_RDONLY);
if (vf) {
struct VFile* vfclone = VFileDevice::openMemory(vf->size(vf));
VFileDevice::copyFile(vf, vfclone);
vf = vfclone;
}
dir->close(dir);
return loadGame(vf, fname, base);
} else {
LOG(QT, ERROR) << tr("Failed to open game file: %1").arg(path);
}
return nullptr;
}
VFile* vf = nullptr;
VDir* archive = VDirOpenArchive(path.toUtf8().constData());
if (archive) {
// Open first file in archive
VFile* vfOriginal = VDirFindFirst(archive, [](VFile* vf) {
return mCoreIsCompatible(vf) != mPLATFORM_NONE;
});
if (vfOriginal) {
ssize_t size = vfOriginal->size(vfOriginal);
if (size > 0) {
struct VFile* vfclone = VFileDevice::openMemory(vfOriginal->size(vfOriginal));
VFileDevice::copyFile(vfOriginal, vfclone);
vf = vfclone;
}
vfOriginal->close(vfOriginal);
}
archive->close(archive);
}
QDir dir(info.dir());
if (!vf) {
// Open bare file
vf = VFileOpen(info.canonicalFilePath().toUtf8().constData(), O_RDONLY);
}
return loadGame(vf, info.fileName(), dir.canonicalPath());
}
CoreController* CoreManager::loadGame(VFile* vf, const QString& path, const QString& base) {
if (!vf) {
return nullptr;
}
mCore* core = mCoreFindVF(vf);
if (!core) {
vf->close(vf);
LOG(QT, ERROR) << tr("Could not load game. Are you sure it's in the correct format?");
return nullptr;
}
core->init(core);
mCoreInitConfig(core, nullptr);
if (m_config) {
mCoreLoadForeignConfig(core, m_config);
}
if (m_preload) {
mCorePreloadVF(core, vf);
} else {
core->loadROM(core, vf);
}
QByteArray bytes(path.toUtf8());
separatePath(bytes.constData(), nullptr, core->dirs.baseName, nullptr);
QFileInfo info(base);
if (info.isDir()) {
info = QFileInfo(base + "/" + path);
}
bytes = info.dir().canonicalPath().toUtf8();
mDirectorySetAttachBase(&core->dirs, VDirOpen(bytes.constData()));
if (!mCoreAutoloadSave(core)) {
LOG(QT, ERROR) << tr("Failed to open save file; in-game saves cannot be updated. Please ensure the save directory is writable without additional privileges (e.g. UAC on Windows).");
}
mCoreAutoloadCheats(core);
CoreController* cc = new CoreController(core);
if (m_multiplayer) {
cc->setMultiplayerController(m_multiplayer);
}
cc->setPath(path, info.dir().canonicalPath());
emit coreLoaded(cc);
return cc;
}
CoreController* CoreManager::loadBIOS(int platform, const QString& path) {
QFileInfo info(path);
VFile* vf = VFileOpen(info.canonicalFilePath().toUtf8().constData(), O_RDONLY);
if (!vf) {
return nullptr;
}
mCore* core = nullptr;
switch (platform) {
#ifdef M_CORE_GBA
case mPLATFORM_GBA:
core = GBACoreCreate();
break;
#endif
default:
vf->close(vf);
return nullptr;
}
if (!core) {
vf->close(vf);
return nullptr;
}
core->init(core);
mCoreInitConfig(core, nullptr);
if (m_config) {
mCoreLoadForeignConfig(core, m_config);
}
core->loadBIOS(core, vf, 0);
mCoreConfigSetOverrideIntValue(&core->config, "useBios", 1);
mCoreConfigSetOverrideIntValue(&core->config, "skipBios", 0);
QByteArray bytes(info.baseName().toUtf8());
strlcpy(core->dirs.baseName, bytes.constData(), sizeof(core->dirs.baseName));
bytes = info.dir().canonicalPath().toUtf8();
mDirectorySetAttachBase(&core->dirs, VDirOpen(bytes.constData()));
CoreController* cc = new CoreController(core);
if (m_multiplayer) {
cc->setMultiplayerController(m_multiplayer);
}
cc->setPath(path, info.dir().canonicalPath());
emit coreLoaded(cc);
return cc;
}