-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathconfig.cpp
More file actions
285 lines (253 loc) · 7.81 KB
/
Copy pathconfig.cpp
File metadata and controls
285 lines (253 loc) · 7.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
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
#include "config.hpp"
#include <fstream>
#include <iomanip>
#ifdef NXDK
// Note: pulling this in can create a conflict with fflush as defined in
// <fstream> leading to a compilation failure w/ clang 12.0.0 on macOS. It is
// important that <fstream> is included before any lwip headers.
// clang-format off
#include <lwip/inet.h>
// clang-format on
#define SEPARATOR "\\"
#define HOME "A:" SEPARATOR
#else
#define SEPARATOR "/"
#define HOME "." SEPARATOR
#endif
#ifdef NXDK
static unsigned int parseIPV4(const std::string& val) {
in_addr addr{ 0 };
if (inet_aton(val.c_str(), &addr)) {
return addr.s_addr;
}
return 0;
}
static std::string ipV4String(unsigned int addr) {
in_addr buf{ 0 };
buf.s_addr = addr;
std::string ret(inet_ntoa(buf));
return ret;
}
netConfig::netConfig() : enable(true), useDHCP(true) {
ip4_addr_t addr;
IP4_ADDR(&addr, 10, 0, 1, 1);
staticGateway = addr.addr;
IP4_ADDR(&addr, 10, 0, 1, 7);
staticIP = addr.addr;
IP4_ADDR(&addr, 255, 255, 255, 0);
staticNetmask = addr.addr;
}
void to_json(nlohmann::json& j, netConfig const& o) {
j = nlohmann::json{ { "enable", o.getEnabled() },
{ "use_dhcp", o.getUseDHCP() },
{ "static_ip", ipV4String(o.getStaticIP()) },
{ "static_gateway", ipV4String(o.getStaticGateway()) },
{ "static_netmask", ipV4String(o.getStaticNetmask()) } };
}
void from_json(nlohmann::json const& j, netConfig& o) {
if (j.contains("enable") && j["enable"].is_boolean()) {
o.setEnabled(j["enable"]);
}
if (j.contains("use_dhcp") && j["use_dhcp"].is_boolean()) {
o.setUseDHCP(j["use_dhcp"]);
}
if (j.contains("static_ip")) {
o.setStaticIP(parseIPV4(j["static_ip"]));
}
if (j.contains("static_gateway")) {
o.setStaticGateway(parseIPV4(j["static_gateway"]));
}
if (j.contains("static_netmask")) {
o.setStaticNetmask(parseIPV4(j["static_netmask"]));
}
}
void to_json(nlohmann::json& j, sntpConfig const& o) {
j = nlohmann::json{ { "enable", o.getEnabled() },
{ "server", o.getServer() },
{ "port", o.getPort() } };
}
void from_json(nlohmann::json const& j, sntpConfig& o) {
if (j.contains("enable") && j["enable"].is_boolean()) {
o.setEnabled(j["enable"]);
}
if (j.contains("server")) {
o.setServer(j["server"]);
}
if (j.contains("port")) {
o.setPort(j["port"]);
}
}
#endif
ftpConfig::ftpConfig() {
enable = true;
username = "xbox";
password = "xbox";
port = 21;
}
void to_json(nlohmann::json& j, ftpConfig const& f) {
j = nlohmann::json{ { "enable", f.getEnabled() },
{ "username", f.getUser() },
{ "password", f.getPassword() },
{ "port", f.getPort() } };
}
void from_json(nlohmann::json const& j, ftpConfig& f) {
if (j.contains("enable")) {
if (j["enable"].is_boolean()) {
f.setEnabled(j["enable"]);
}
}
if (j.contains("username")) {
if (j["username"].is_string()) {
f.setUser(j["username"]);
}
}
if (j.contains("password")) {
if (j["password"].is_string()) {
f.setPassword(j["password"]);
}
}
if (j.contains("port")) {
if (j["port"].is_number()) {
f.setPort(j["port"]);
}
}
}
void ftpConfig::setEnabled(bool val) {
if (enable != val) {
enable = val;
}
}
void ftpConfig::setUser(std::string const& user) {
if (username.compare(user)) {
username = user;
}
}
void ftpConfig::setPassword(std::string const& pwd) {
if (password.compare(pwd)) {
password = pwd;
}
}
void ftpConfig::setPort(int p) {
if (port != p) {
port = p;
}
}
mountConfig::mountConfig() {
enableF = true;
enableG = true;
}
void to_json(nlohmann::json& j, mountConfig const& o) {
j = nlohmann::json{ { "enable_f", o.getFEnabled() }, { "enable_g", o.getGEnabled() } };
}
void from_json(nlohmann::json const& j, mountConfig& o) {
if (j.contains("enable_f") && j["enable_f"].is_boolean()) {
o.setFEnabled(j["enable_f"]);
}
if (j.contains("enable_g") && j["enable_g"].is_boolean()) {
o.setGEnabled(j["enable_g"]);
}
}
void to_json(nlohmann::json& j, loggingConfig const& o) {
// FIXME: Use a float for colors when https://github.com/XboxDev/nxdk/issues/508 is fixed.
j = nlohmann::json{ { "overlay_log_level", o.getOverlayLogLevel() },
{ "enable_overlay", o.getOverlayEnabled() },
{ "overlay_duration_frames", o.getOverlayDurationFrames() },
{ "overlay_bg_alpha_int",
static_cast<int>(o.getOverlayBackgroundAlpha() * 0xFF) } };
}
void from_json(nlohmann::json const& j, loggingConfig& o) {
if (j.contains("overlay_log_level")) {
o.setOverlayLogLevel(j["overlay_log_level"]);
}
if (j.contains("enable_overlay") && j["enable_overlay"].is_boolean()) {
o.setOverlayEnabled(j["enable_overlay"]);
}
if (j.contains("overlay_duration_frames")
&& j["overlay_duration_frames"].is_number_integer()) {
o.setOverlayDurationFrames(j["overlay_duration_frames"]);
}
// FIXME: Use a float for colors when https://github.com/XboxDev/nxdk/issues/508 is fixed.
if (j.contains("overlay_bg_alpha_int") && j["overlay_bg_alpha_int"].is_number_integer()) {
float val = j["overlay_bg_alpha_int"];
o.setOverlayBackgroundAlpha(val / 255.0f);
}
}
void to_json(nlohmann::json& j, homescreenConfig const& o) {
j = nlohmann::json{ { "show_fps", o.getShowFPS() }, { "show_ip", o.getShowIP() } };
}
void from_json(nlohmann::json const& j, homescreenConfig& o) {
if (j.contains("show_fps") && j["show_fps"].is_boolean()) {
o.setShowFPS(j["show_fps"]);
}
if (j.contains("show_ip") && j["show_ip"].is_boolean()) {
o.setShowIP(j["show_ip"]);
}
}
void to_json(nlohmann::json& j, Settings const& o) {
j = nlohmann::json{ { "active_theme_directory", o.activeThemeDirectory },
{ "ftp", nlohmann::json(o.ftp) },
{ "mount", nlohmann::json(o.mount) },
#ifdef NXDK
{ "network", nlohmann::json(o.net) },
#endif
{ "logging", nlohmann::json(o.logging) },
{ "homescreen", nlohmann::json(o.homescreen) } };
}
void from_json(nlohmann::json const& j, Settings& o) {
if (j.contains("active_theme_directory")) {
o.activeThemeDirectory = j["active_theme_directory"];
}
if (j.contains("ftp")) {
o.ftp = j["ftp"].get<ftpConfig>();
}
if (j.contains("mount")) {
o.mount = j["mount"].get<mountConfig>();
}
#ifdef NXDK
if (j.contains("network")) {
o.net = j["network"].get<netConfig>();
}
if (j.contains("sntp")) {
o.sntp = j["sntp"].get<sntpConfig>();
}
#endif
if (j.contains("logging")) {
o.logging = j["logging"].get<loggingConfig>();
}
if (j.contains("homescreen")) {
o.homescreen = j["homescreen"].get<homescreenConfig>();
}
}
Config::Config() {
std::ifstream configFile(HOME "config.json");
nlohmann::json json;
// FIXME: Once nxdk supports C++ Exceptions, this needs to be put in a try-catch block!
configFile >> json;
if (json.contains("settings")) {
settings = json["settings"].get<Settings>();
} else {
changed = true;
}
if (!json.contains("menu")) {
json["menu"] = R"([{"label": "Games", "type": "scan", "path": "F:\\Games\\"},
{"label": "Applications", "type": "scan", "path": "F:\\Apps"},
{"label": "Launch DVD", "type": "launch", "path": "D:\\default.xbe"},
{"label": "Settings", "type": "settings"},
{"label": "Reboot", "type": "reboot"}])"_json;
changed = true;
}
menu = json["menu"];
configFile.close();
}
void Config::setChanged() {
changed = true;
}
void Config::storeToDisk() {
if (changed) {
std::ofstream configFile(HOME "config.json");
nlohmann::json json;
to_json(json, *this);
configFile << std::setw(4) << json << std::endl;
configFile.close();
}
}