-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainScreen.cpp
More file actions
370 lines (313 loc) · 11 KB
/
Copy pathMainScreen.cpp
File metadata and controls
370 lines (313 loc) · 11 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
#include "TitleSelectionScreen.hpp"
#include "MainScreen.hpp"
#include "ProgressScreen.hpp"
#include "AccountScreen.hpp"
#include "../title.hpp"
#include "../savedata.hpp"
#include "../remote.hpp"
#include "../utils.hpp"
#include "../fileio.hpp"
namespace gui
{
MainScreen::MainScreen(Config& config)
: config(config)
{
}
void MainScreen::resolveAccount()
{
bool useSelector = (bool)config["account"]["useProfileSelector"];
std::string defaultName = config["account"]["defaultAccountName"].value;
// useProfileSelector=0 이고 유효한 defaultAccountName이 있으면 선택창 없이 매칭
if (!useSelector && !defaultName.empty())
{
AccountResolveOptions opts;
opts.defaultAccountName = defaultName;
opts.useProfileSelector = false;
if (getCurrentAccount(&account, opts) == 0)
{
accountResolved = true;
rebuildMenu();
}
// 매칭 실패해도 선택창 안 띄움 (accountResolved=false 상태로 에러 표시)
return;
}
// useProfileSelector=1 이거나, defaultAccountName이 비어있으면 선택창
// full application mode에서는 psel 먼저 시도
const AppletType appletType = appletGetAppletType();
const bool isFullApp =
appletType == AppletType_Application ||
appletType == AppletType_SystemApplication;
if (isFullApp)
{
AccountResolveOptions opts;
opts.defaultAccountName = defaultName;
opts.useProfileSelector = true;
if (getCurrentAccount(&account, opts) == 0)
{
accountResolved = true;
rebuildMenu();
return;
}
}
// applet mode이거나 psel 실패: 내장 계정 선택 화면
App::instance().pushScreen(new AccountScreen([this](const Account& selected)
{
onAccountSelected(selected);
}));
}
void MainScreen::onAccountSelected(const Account& selected)
{
account = selected;
accountResolved = true;
rebuildMenu();
}
void MainScreen::rebuildMenu()
{
menuItems.clear();
selectedIndex = 0;
if (accountResolved)
{
bool remoteEnabled = (bool)config["remote"]["enabled"];
menuItems.push_back({"Push to Server", [this]() { startPush(); }, remoteEnabled});
menuItems.push_back({"Pull from Server", [this]() { startPull(); }, true});
menuItems.push_back({"Select Games", [this]() { App::instance().pushScreen(new TitleSelectionScreen(config, account.uid)); }, true});
}
}
void MainScreen::update(u64 kDown)
{
// 첫 프레임: 계정 해석
if (!initialSelectDone)
{
initialSelectDone = true;
resolveAccount();
return;
}
if (!accountResolved) return;
if (kDown & HidNpadButton_AnyUp)
{
selectedIndex--;
if (selectedIndex < 0) selectedIndex = menuItems.size() - 1;
}
if (kDown & HidNpadButton_AnyDown)
{
selectedIndex++;
if (selectedIndex >= (int)menuItems.size()) selectedIndex = 0;
}
if (kDown & HidNpadButton_A)
{
if (!menuItems.empty() && menuItems[selectedIndex].enabled)
{
menuItems[selectedIndex].action();
}
}
if (kDown & HidNpadButton_Minus)
{
switchAccount();
}
}
void MainScreen::switchAccount()
{
const AppletType appletType = appletGetAppletType();
const bool isFullApplicationMode =
appletType == AppletType_Application ||
appletType == AppletType_SystemApplication;
if (isFullApplicationMode)
{
AccountUid uid;
PselUserSelectionSettings settings = {};
if (R_SUCCEEDED(pselShowUserSelector(&uid, &settings)))
{
AccountProfile profile;
AccountProfileBase profileBase;
if (R_SUCCEEDED(accountGetProfile(&profile, uid)))
{
if (R_SUCCEEDED(accountProfileGet(&profile, NULL, &profileBase)))
{
strcpy(account.nickname, profileBase.nickname);
account.uid = uid;
accountResolved = true;
rebuildMenu();
}
accountProfileClose(&profile);
}
}
}
else
{
App::instance().pushScreen(new AccountScreen([this](const Account& selected)
{
onAccountSelected(selected);
}));
}
}
void MainScreen::render(Renderer& r)
{
int x = 80;
int y = 60;
r.drawText("micro NX Save Sync", x, y, 32, COLOR_ACCENT);
{
const AppletType appletType = appletGetAppletType();
const bool isFullApp =
appletType == AppletType_Application ||
appletType == AppletType_SystemApplication;
if (!isFullApp)
{
int tagW = r.getTextWidth("Applet Mode", 18);
r.drawText("Applet Mode", r.screenWidth() - x - tagW, y + 8, 18, COLOR_ERROR);
}
}
y += 50;
r.drawRect(x, y, r.screenWidth() - x * 2, 2, COLOR_ACCENT);
y += 20;
if (!accountResolved)
{
r.drawText("Waiting for account selection...", x, y, 24, COLOR_DIM);
return;
}
r.drawText(std::string("Account: ") + account.nickname, x, y, 24);
y += 36;
bool remoteEnabled = (bool)config["remote"]["enabled"];
r.drawText(std::string("Remote: ") + (remoteEnabled ? "Enabled" : "Disabled"), x, y, 18, COLOR_DIM);
y += 28;
if (remoteEnabled)
{
r.drawText(std::string("Server: ") + (std::string)config["remote"]["serverUrl"], x, y, 18, COLOR_DIM);
y += 28;
}
y += 30;
for (int i = 0; i < (int)menuItems.size(); i++)
{
int btnW = r.screenWidth() - x * 2;
int btnH = 50;
Color bgColor = (i == selectedIndex) ? COLOR_HIGHLIGHT : COLOR_BUTTON;
Color textColor = menuItems[i].enabled ? COLOR_TEXT : COLOR_DIM;
if (!menuItems[i].enabled && i == selectedIndex)
{
bgColor = {80, 80, 80, 255};
}
r.drawRect(x, y, btnW, btnH, bgColor);
r.drawText(menuItems[i].label, x + 20, y + 12, 24, textColor);
y += btnH + 10;
}
int fy = r.screenHeight() - 50;
r.drawRect(x, fy - 10, r.screenWidth() - x * 2, 2, {80, 80, 80, 255});
r.drawText("A: Select -: Account +: Exit", x, fy, 18, COLOR_DIM);
}
void MainScreen::startPush()
{
const std::string saveDataPath = "sdmc:/uNSS/saves";
const std::string serverUrl = (std::string)config["remote"]["serverUrl"];
const std::string archiveBy = config["title"]["archiveBy"].value;
const std::string excludedIds = config["title"]["excludedTitleIds"].value;
const std::string excludedNames = config["title"]["excludedTitleNames"].value;
const std::string nickname = account.nickname;
const AccountUid uid = account.uid;
auto work = [=](std::function<void(const std::string&)> log) -> int
{
HTTPRemoteStore remoteStore(serverUrl, saveDataPath);
recursiveMkdir(saveDataPath.c_str());
const ProbeTitlesFunc probeFunc = [&](const AccountUid probeUid, std::vector<u64>& titleIDs) -> int
{
int ret = archiveBy == "all"
? probeAllTitles(probeUid, titleIDs)
: probeSaveDataCreatedTitles(probeUid, titleIDs);
if (ret == 0)
{
filterExcludedTitles(titleIDs, excludedIds, excludedNames);
}
return ret;
};
return archiveAllSaveData(
uid,
saveDataPath,
probeFunc,
[&log](int total, int current, u64 titleID) -> bool
{
std::string titleName;
if (getTitleName(titleID, titleName) != 0)
titleName = "Unknown";
log("[" + padding(current, 3) + "/" + padding(total, 3) + "] " + titleName);
return true;
},
[&log, &remoteStore, &nickname](int total, int current, int ret, u64 titleID) -> bool
{
if (ret != SAVEDATA_OK)
log("Failed to archive, ret=" + std::to_string(ret));
else
{
int pushRet = remoteStore.push(nickname, titleID);
if (pushRet != 0)
log("Failed to push, ret=" + std::to_string(pushRet));
}
return true;
}
);
};
App::instance().pushScreen(new ProgressScreen("Push to Server", std::move(work)));
}
void MainScreen::startPull()
{
const std::string saveDataPath = "sdmc:/uNSS/saves";
const std::string serverUrl = (std::string)config["remote"]["serverUrl"];
const std::string restoreBy = config["title"]["restoreBy"].value;
const std::string excludedIds = config["title"]["excludedTitleIds"].value;
const std::string excludedNames = config["title"]["excludedTitleNames"].value;
const std::string nickname = account.nickname;
const AccountUid uid = account.uid;
const bool remoteEnabled = (bool)config["remote"]["enabled"];
auto work = [=](std::function<void(const std::string&)> log) -> int
{
HTTPRemoteStore remoteStore(serverUrl, saveDataPath);
recursiveMkdir(saveDataPath.c_str());
if (!remoteEnabled)
{
log("Remote is disabled, restoring from local...");
return restoreAllSaveData(
uid, saveDataPath,
[&log](int total, int current, u64 titleID) -> bool
{
std::string titleName;
if (getTitleName(titleID, titleName) != 0)
titleName = "Unknown";
log("[" + padding(current, 3) + "/" + padding(total, 3) + "] " + titleName);
return true;
},
[&log](int total, int current, int ret, u64 titleID) -> bool
{
if (ret != SAVEDATA_OK)
log("Failed to restore, ret=" + std::to_string(ret));
return true;
}
);
}
std::vector<u64> titleIDs;
int probeRet = restoreBy == "all"
? probeAllTitles(uid, titleIDs)
: probeSaveDataCreatedTitles(uid, titleIDs);
if (probeRet == 0)
filterExcludedTitles(titleIDs, excludedIds, excludedNames);
if (probeRet != 0)
{
log("Failed to probe titles");
return probeRet;
}
for (size_t i = 0; i < titleIDs.size(); ++i)
{
std::string titleName;
if (getTitleName(titleIDs[i], titleName) != 0)
titleName = "Unknown";
log("[" + padding(i + 1, 3) + "/" + padding(titleIDs.size(), 3) + "] " + titleName);
if (remoteStore.pull(nickname, titleIDs[i]) != 0)
{
log("Failed to pull from server");
}
else
{
restoreSaveData(uid, titleIDs[i], saveDataPath);
}
}
return 0;
};
App::instance().pushScreen(new ProgressScreen("Pull from Server", std::move(work)));
}
} // namespace gui