-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
207 lines (167 loc) · 4.81 KB
/
main.cpp
File metadata and controls
207 lines (167 loc) · 4.81 KB
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
//
// main.cpp
// rnes
//
//
#include <boost/filesystem.hpp>
#include <boost/iostreams/device/file.hpp>
#include <boost/iostreams/device/mapped_file.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/program_options.hpp>
#include <crypt.h>
#include <iostream>
#include <memory>
#include <pwd.h>
#include <stdlib.h>
#include <string>
#include <sys/types.h>
#include "nes.h"
#include "save.pb.h"
using namespace Rnes;
// TODO:
// - mmcs: nrom, mmc5
// - redo sprite/bg ordering. It's currently broken.
// - sprite0 flag
// - clipping flags
// - apu dmc channel
// - color emphasis
std::string help = {"--rom [filename]\n"
"-r [filename]\n"};
void displayHelpAndQuit() {
std::cerr << help;
exit(1);
}
namespace Rnes {
std::string md5OfFile(std::string file) {
std::string salt = "$1$$";
struct crypt_data data = {0};
std::string contents;
using namespace std;
namespace io = boost::iostreams;
io::stream<io::mapped_file_source> stream(file);
stream >> contents;
auto ret = std::string(crypt_r(contents.c_str(), salt.c_str(), &data));
for_each(begin(ret), end(ret), [](char &c) {
if (c == '/' or c == '.') {
c = 'x';
}
});
return ret.substr(salt.size());
}
std::string getUserHomeDir() {
struct passwd pwd = {0};
struct passwd *pwdRet;
char buf[1024];
getpwuid_r(getuid(), &pwd, buf, sizeof(buf), &pwdRet);
using namespace std;
string homeDir(pwdRet->pw_dir);
return homeDir;
}
std::string getGameSaveDir(const std::string &romFile) {
const std::string homeDir = getUserHomeDir();
const std::string md5 = md5OfFile(romFile);
return homeDir + "/.rnes/" + md5 + "/save";
}
void setupDirectories(std::string romMd5) {
namespace fs = boost::filesystem;
fs::path homeDir(getUserHomeDir());
fs::path rnesDir(homeDir);
rnesDir /= ".rnes";
if (not fs::exists(rnesDir)) {
fs::create_directories(rnesDir);
}
fs::path gamePath(rnesDir);
gamePath /= fs::path(romMd5);
if (not fs::exists(gamePath)) {
fs::create_directories(gamePath);
}
}
void verifyRomExists(std::string romFile) {
namespace fs = boost::filesystem;
using namespace std;
fs::path path(romFile);
if (not fs::exists(path) or not fs::is_regular_file(path)) {
cerr << "specified rom file does not exist: " << romFile << endl;
exit(-1);
}
cerr << "rom md5: " << md5OfFile(romFile) << endl;
}
void loadNesState(Nes *nes, std::string saveFile) {
namespace io = boost::iostreams;
namespace fs = boost::filesystem;
using namespace std;
try {
io::stream<io::mapped_file_source> stream(saveFile);
unique_ptr<SaveState> gameState{new SaveState{}};
// Read out state from save file.
gameState->ParseFromIstream(&stream);
// Restore nes state.
nes->restore(*gameState.get());
cerr << "State restored" << endl;
} catch (const std::ios_base::failure &exception) {
cerr << "Exception: " << exception.what() << endl;
}
}
void saveNesState(Nes *nes, std::string saveFile) {
namespace io = boost::iostreams;
namespace fs = boost::filesystem;
using namespace std;
// Useless comment.
try {
io::stream<io::file_sink> stream(saveFile);
unique_ptr<SaveState> gameState{new SaveState{}};
// Save nes state.
nes->save(*gameState.get());
// Serialize save to file.
gameState->SerializeToOstream(&stream);
cerr << "state saved." << endl;
} catch (const std::ios_base::failure &exception) {
cerr << "Exception: " << exception.what() << endl;
}
}
} // namespace Rnes
int main(int argc, char *argv[]) {
using namespace std;
namespace fs = boost::filesystem;
string romFile;
bool romFileSpecified = false;
// Verify that the version of the library that we linked against is
// compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
// TODO: convert argument parsing to boost program options
for (int i = 0; i < argc; i++) {
if (argv[i] == string("--rom") || argv[i] == string("-r")) {
romFile = argv[i + 1];
romFileSpecified = true;
i += 2;
}
}
if (!romFileSpecified) {
displayHelpAndQuit();
}
try {
// verify the rom file exists.
verifyRomExists(romFile);
auto nes = unique_ptr<Nes>{new Nes{}};
int res = nes->loadRom(romFile);
if (res) {
cerr << "Failed to load rom: " << romFile << endl;
exit(1);
}
// Setup the rnes directories.
setupDirectories(md5OfFile(romFile));
// Start the emulator loop.
nes->run();
} catch (const fs::filesystem_error &exception) {
cerr << "Exception: " << exception.what() << endl;
return -1;
} catch (const std::exception &exception) {
cerr << "Exception: " << exception.what() << endl;
return -1;
} catch (...) {
cerr << "Unknown Exception!" << endl;
return -1;
}
google::protobuf::ShutdownProtobufLibrary();
return 0;
}