-
Notifications
You must be signed in to change notification settings - Fork 141
/
Copy pathemulator.cpp
309 lines (273 loc) · 10.2 KB
/
emulator.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include "../desktop-ui.hpp"
#include "emulators.cpp"
vector<shared_pointer<Emulator>> emulators;
shared_pointer<Emulator> emulator;
auto Emulator::enumeratePorts(string name) -> vector<InputPort>& {
for(auto& emulator : emulators) {
if(emulator->name == name && emulator->ports) return emulator->ports;
}
static vector<InputPort> ports;
if(!ports) {
for(auto id : range(5)) {
InputPort port{string{"Controller Port ", 1 + id}};
port.append(virtualPorts[id].pad);
port.append(virtualPorts[id].mouse);
ports.append(port);
}
}
return ports;
}
auto Emulator::location() -> string {
return {Path::userData(), "ares/Saves/", name, "/"};
}
auto Emulator::locate(const string& location, const string& suffix, const string& path, maybe<string> system) -> string {
if(!system) system = root->name();
//game path
if(!path) return {Location::notsuffix(location), suffix};
//path override
string pathname = {path, *system, "/"};
directory::create(pathname);
return {pathname, Location::prefix(location), suffix};
}
//handles region selection when games support multiple regions
auto Emulator::region() -> string {
if(game && game->pak) {
if(auto regions = game->pak->attribute("region").split(",").strip()) {
if(settings.boot.prefer == "NTSC-U" && regions.find("NTSC-U")) return "NTSC-U";
if(settings.boot.prefer == "NTSC-J" && regions.find("NTSC-J")) return "NTSC-J";
if(settings.boot.prefer == "NTSC-U" && regions.find("NTSC" )) return "NTSC";
if(settings.boot.prefer == "NTSC-J" && regions.find("NTSC" )) return "NTSC";
if(settings.boot.prefer == "PAL" && regions.find("PAL" )) return "PAL";
if(regions.first()) return regions.first();
}
}
if(settings.boot.prefer == "NTSC-J") return "NTSC-J";
if(settings.boot.prefer == "NTSC-U") return "NTSC-U";
if(settings.boot.prefer == "PAL" ) return "PAL";
return {};
}
auto Emulator::handleLoadResult(LoadResult result) -> void {
string errorText;
switch (result.result) {
case successful:
return;
case noFileSelected:
return;
case invalidROM:
errorText = { "There was an error trying to parse the selected ROM. \n",
"Your ROM may be corrupt or contain a bad dump. " };
break;
case couldNotParseManifest:
errorText = { "An error occurred while parsing the database file. You \n",
"may need to reinstall ares. " };
break;
case databaseNotFound:
errorText = { "The database file for the system was not found. \n",
"Make sure that you have installed or packaged ares correctly. \n",
"Missing database file: " };
break;
case noFirmware:
errorText = { "Error: firmware is missing or invalid.\n",
result.firmwareSystemName, " - ", result.firmwareType, " (", result.firmwareRegion, ") is required to play this game.\n",
"Would you like to configure firmware settings now? " };
break;
case romNotFound:
errorText = "The selected ROM file was not found or could not be opened. ";
break;
case romNotFoundInDatabase:
errorText = { "The required manifest for this ROM was not found in the database. \n",
"This title may not be currently supported by ares. " };
break;
case otherError:
errorText = "An internal error occurred when initializing the emulator core. ";
break;
}
if(result.info) {
errorText = { errorText, result.info };
}
switch (result.result) {
case noFirmware:
if(MessageDialog().setText({
errorText
}).question() == "Yes") {
settingsWindow.show("Firmware");
firmwareSettings.select(emulator->name, result.firmwareType, result.firmwareRegion);
}
break;
default:
error(errorText);
}
}
auto Emulator::load(const string& location) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(inode::exists(location)) locationQueue.append(location);
LoadResult result = load();
handleLoadResult(result);
if(result != successful) {
return false;
}
setBoolean("Color Emulation", settings.video.colorEmulation);
setBoolean("Deep Black Boost", settings.video.deepBlackBoost);
setBoolean("Interframe Blending", settings.video.interframeBlending);
setOverscan(settings.video.overscan);
setColorBleed(settings.video.colorBleed);
latch = {};
root->power();
return true;
}
auto Emulator::load(shared_pointer<mia::Pak> pak, string& path) -> string {
lock_guard<recursive_mutex> lock(program.programMutex);
string location;
if(locationQueue) {
location = locationQueue.takeFirst(); //pull from the game queue if an entry is available
} else if(program.startGameLoad) {
location = program.startGameLoad.takeFirst(); //pull from the command line if an entry is available
} else if(!program.noFilePrompt) {
BrowserDialog dialog;
dialog.setTitle({"Load ", pak->name(), " Game"});
dialog.setPath(path ? path : Path::desktop());
dialog.setAlignment(presentation);
string filters{"*.zip:"};
for(auto& extension : pak->extensions()) {
filters.append("*.", extension, ":");
}
//support both uppercase and lowercase extensions
filters.append(string{filters}.upcase());
filters.trimRight(":", 1L);
filters.prepend(pak->name(), "|");
dialog.setFilters({filters, "All|*"});
location = program.openFile(dialog);
}
if(location) {
path = Location::dir(location);
return location;
}
return {};
}
auto Emulator::loadFirmware(const Firmware& firmware) -> shared_pointer<vfs::file> {
lock_guard<recursive_mutex> lock(program.programMutex);
if(firmware.location.iendsWith(".zip")) {
Decode::ZIP archive;
if(archive.open(firmware.location) && archive.file) {
auto image = archive.extract(archive.file.first());
return vfs::memory::open(image);
}
} else if(auto image = file::read(firmware.location)) {
return vfs::memory::open(image);
}
return {};
}
auto Emulator::unload() -> void {
lock_guard<recursive_mutex> lock(program.programMutex);
save();
root->unload();
game = {};
system = {};
root.reset();
locationQueue.reset();
}
auto Emulator::load(mia::Pak& node, string name) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto fp = node.pak->read(name)) {
if(auto memory = file::read({node.location, name})) {
fp->read(memory);
return true;
}
}
return false;
}
auto Emulator::save(mia::Pak& node, string name) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto memory = node.pak->write(name)) {
return file::write({node.location, name}, {memory->data(), memory->size()});
}
return false;
}
auto Emulator::refresh() -> void {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto screen = root->scan<ares::Node::Video::Screen>("Screen")) {
screen->refresh();
}
}
auto Emulator::setBoolean(const string& name, bool value) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto node = root->scan<ares::Node::Setting::Boolean>(name)) {
node->setValue(value); //setValue() will not call modify() if value has not changed;
node->modify(value); //but that may prevent the initial setValue() from working
return true;
}
return false;
}
auto Emulator::setOverscan(bool value) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto screen = root->scan<ares::Node::Video::Screen>("Screen")) {
screen->setOverscan(value);
return true;
}
return false;
}
auto Emulator::setColorBleed(bool value) -> bool {
lock_guard<recursive_mutex> lock(program.programMutex);
if(auto screen = root->scan<ares::Node::Video::Screen>("Screen")) {
screen->setColorBleed(screen->height() < 720 ? value : false); //only apply to sub-HD content
return true;
}
return false;
}
auto Emulator::error(const string& text) -> void {
MessageDialog().setTitle("Error").setText(text).setAlignment(presentation).error();
}
auto Emulator::input(ares::Node::Input::Input input) -> void {
lock_guard<recursive_mutex> lock(program.inputMutex); //necessary?
//looking up inputs is very time-consuming; skip call if input was called too recently
//note: allow rumble to be polled at full speed to prevent missed motor events
if(!input->cast<ares::Node::Input::Rumble>()) {
auto thisPoll = chrono::millisecond();
if(thisPoll - input->lastPoll < 5) return;
input->lastPoll = thisPoll;
}
auto device = ares::Node::parent(input);
if(!device) return;
auto port = ares::Node::parent(device);
if(!port) return;
for(auto& inputPort : ports) {
if(inputPort.name != port->name()) continue;
for(auto& inputDevice : inputPort.devices) {
if(inputDevice.name != device->name()) continue;
for(auto& inputNode : inputDevice.inputs) {
if(inputNode.name != input->name()) continue;
if(auto button = input->cast<ares::Node::Input::Button>()) {
auto pressed = inputNode.mapping->pressed();
return button->setValue(pressed);
}
if(auto axis = input->cast<ares::Node::Input::Axis>()) {
auto value = inputNode.mapping->value();
return axis->setValue(value);
}
if(auto rumble = input->cast<ares::Node::Input::Rumble>()) {
if(auto target = dynamic_cast<InputRumble*>(inputNode.mapping)) {
return target->rumble(rumble->strongValue(), rumble->weakValue());
}
}
}
for(auto& inputPair : inputDevice.pairs) {
if(inputPair.name != input->name()) continue;
if(auto axis = input->cast<ares::Node::Input::Axis>()) {
auto value = inputPair.mappingHi->value() - inputPair.mappingLo->value();
return axis->setValue(value);
}
}
}
}
}
auto Emulator::inputKeyboard(string name) -> bool {
lock_guard<recursive_mutex> lock(program.inputMutex);
for (auto& device : inputManager.devices) {
if (!device->isKeyboard()) continue;
auto keyboard = (shared_pointer<HID::Keyboard>)device;
auto key = keyboard->buttons().find(name);
if (!key) return false;
return keyboard->buttons().input(*key).value();
}
return false;
}