-
Notifications
You must be signed in to change notification settings - Fork 502
/
DownloadManager.h
504 lines (430 loc) · 12.9 KB
/
DownloadManager.h
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
#pragma once
#include "util/helpers/Semaphore.h"
#include "Cemu/ncrypto/ncrypto.h"
#include "Cafe/TitleList/TitleId.h"
#include "util/helpers/ConcurrentQueue.h"
#include "config/NetworkSettings.h"
// forward declarations
namespace NAPI
{
struct IDBEIconDataV0;
struct AuthInfo;
}
namespace NCrypto
{
class TMDParser;
}
struct DlMgrTitleReport
{
enum class STATUS
{
INSTALLABLE, // not a package
INSTALLABLE_UNFINISHED, // same as INSTALLABLE, but a previous unfinished package was detected
INSTALLABLE_UPDATE, // same as INSTALLABLE, but an older version is already installed (used for DLC updates after purchasing more content)
// below are packages
QUEUED,
PAUSED,
INITIALIZING, // not active yet, downloading TMD
CHECKING, // checking for previously downloaded files
DOWNLOADING, // downloading content files
VERIFYING, // verifying downloaded files
INSTALLING,
INSTALLED,
HAS_ERROR
};
DlMgrTitleReport(STATUS status, uint64 titleId, uint16 version, std::string name, uint32 progress, uint32 progressMax, bool isPaused) : status(status), titleId(titleId), version(version), name(name), progress(progress), progressMax(progressMax), isPaused(isPaused) {}
uint64 titleId;
uint16 version;
std::string name; // utf8
STATUS status;
uint32 progress;
uint32 progressMax;
bool isPaused;
std::string errorMsg;
};
enum class DLMGR_STATUS_CODE
{
UNINITIALIZED,
CONNECTING,
FAILED,
CONNECTED
};
class DownloadManager
{
public:
/* singleton */
static DownloadManager* GetInstance(bool createIfNotExist = true)
{
static DownloadManager* s_instance = nullptr;
if (s_instance)
return s_instance;
if (createIfNotExist)
s_instance = new DownloadManager();
return s_instance;
}
// login
void connect(
std::string_view nnidAccountName,
const std::array<uint8, 32>& passwordHash,
CafeConsoleRegion region,
std::string_view country,
uint32 deviceId,
std::string_view serial,
std::string_view deviceCertBase64);
bool IsConnected() const;
private:
/* connect / login */
enum class CONNECT_STATE
{
UNINITIALIZED = 0, // connect() not called
REQUESTED = 1, // connect() requested, but not being processed yet
PROCESSING = 2, // processing login request
COMPLETE = 3, // login complete / succeeded
FAILED = 4, // failed to login
};
struct
{
std::string cachefileName;
std::string nnidAccountName;
std::array<uint8, 32> passwordHash;
std::string deviceCertBase64;
CafeConsoleRegion region;
std::string country;
uint32 deviceId; // deviceId without platform (0x5<<32 for WiiU)
std::string serial;
}m_authInfo{};
struct
{
// auth info we have to request from the server
std::string serviceAccountId; // internal account id (integer) provided by server when registering account (GetRegistrationInfo)
std::string deviceToken;
}m_iasToken{};
std::atomic<CONNECT_STATE> m_connectState{ CONNECT_STATE::UNINITIALIZED };
void _handle_connect();
bool _connect_refreshIASAccountIdAndDeviceToken();
bool _connect_queryAccountStatusAndServiceURLs();
NetworkService GetDownloadMgrNetworkService();
NAPI::AuthInfo GetAuthInfo(bool withIasToken);
/* idbe cache */
public:
void prepareIDBE(uint64 titleId);
std::string getNameFromCachedIDBE(uint64 titleId);
private:
NAPI::IDBEIconDataV0* getIDBE(uint64 titleId);
/* ticket cache */
public:
struct ETicketInfo
{
enum class SOURCE : uint8
{
ECS_TICKET = 0, // personalized ticket of owned title
PUBLIC_TICKET = 1, // public ticket file (available as /CETK from NUS server or via SOAP GetSystemCommonETicket. The former is from Wii era while the latter is how Wii U requests public tickets?)
// note: These ids are baked into the serialized cache format. Do not modify
};
ETicketInfo(SOURCE source, uint64 ticketId, uint32 ticketVersion, std::vector<uint8>& eTicket);
ETicketInfo(SOURCE source, uint64 ticketId, uint32 ticketVersion, std::vector<uint8>& eTicket, std::vector<std::vector<uint8>>& eTicketCerts);
void GetTitleKey(NCrypto::AesKey& key);
SOURCE source;
// tiv
uint64 ticketId;
uint32 ticketVersion;
// titleInfo
uint64 titleId{0}; // parsed from eTicket
std::vector<uint8> eTicket;
std::vector<std::vector<uint8>> eTicketCerts;
};
std::vector<ETicketInfo> m_ticketCache;
struct UnfinishedDownload
{
UnfinishedDownload(uint64 titleId, uint16 titleVersion) : titleId(titleId), titleVersion(titleVersion) {};
uint64 titleId;
uint16 titleVersion;
};
std::vector<UnfinishedDownload> m_unfinishedDownloads;
void loadTicketCache();
void storeTicketCache();
bool syncAccountTickets();
bool syncSystemTitleTickets();
bool syncUpdateTickets();
bool syncTicketCache();
void searchForIncompleteDownloads();
void reportAvailableTitles();
private:
ETicketInfo* findTicketByTicketId(uint64 ticketId)
{
for (auto& itr : m_ticketCache)
{
if (itr.ticketId == ticketId)
return &itr;
}
return nullptr;
}
void deleteTicketByTicketId(uint64 ticketId)
{
auto itr = m_ticketCache.begin();
while (itr != m_ticketCache.end())
{
if (itr->ticketId == ticketId)
{
m_ticketCache.erase(itr);
return;
}
itr++;
}
cemu_assert_debug(false); // ticketId not found
}
ETicketInfo* findTicketByTitleIdAndVersion(uint64 titleId, uint16 version)
{
for (auto& itr : m_ticketCache)
{
if (itr.titleId == titleId && itr.ticketVersion == version)
return &itr;
}
return nullptr;
}
ETicketInfo* findFirstTicketByTitleId(uint64 titleId)
{
for (auto& itr : m_ticketCache)
{
if (itr.titleId == titleId)
return &itr;
}
return nullptr;
}
bool hasPartialDownload(uint64 titleId, uint16 titleVersion)
{
for (auto& itr : m_unfinishedDownloads)
{
if (itr.titleId == titleId && itr.titleVersion == titleVersion)
return true;
}
return false;
}
void deleteUnfinishedDownloadRecord(uint64 titleId)
{
auto itr = m_unfinishedDownloads.begin();
while (itr != m_unfinishedDownloads.end())
{
if (itr->titleId == titleId)
{
itr = m_unfinishedDownloads.erase(itr);
continue;
}
itr++;
}
}
/* packages / downloading */
struct Package
{
Package(uint64 titleId, uint16 version, std::span<uint8> ticketData) : titleId(titleId), version(version), eTicketData(ticketData.data(), ticketData.data() + ticketData.size())
{
NCrypto::ETicketParser eTicketParser;
cemu_assert(eTicketParser.parse(ticketData.data(), ticketData.size()));
eTicketParser.GetTitleKey(ticketKey);
};
~Package()
{
delete state.tmd;
}
enum class STATE
{
INITIAL,
CHECKING,
DOWNLOADING,
VERIFYING,
INSTALLING,
INSTALLED // done
};
uint64 titleId;
uint16 version;
std::vector<uint8> eTicketData;
NCrypto::AesKey ticketKey;
// internal
struct ContentFile
{
ContentFile(uint16 index, uint32 contentId, uint64 size, NCrypto::TMDParser::TMDContentFlags contentFlags, uint8 hash[32]) : index(index), contentId(contentId), size(size), contentFlags(contentFlags)
{
std::memcpy(contentHash, hash, 32);
CalcPaddedSize();
};
const uint16 index;
const uint32 contentId;
const uint64 size;
uint64 paddedSize; // includes padding forced by encryption/hashing (e.g. Nintendo Land update has one non-hashed content size of 0x8001, padded to 0x8010)
const NCrypto::TMDParser::TMDContentFlags contentFlags;
uint8 contentHash[32];
enum class STATE
{
CHECK,
DOWNLOAD,
VERIFY,
INSTALL,
ENUM_COUNT, // for enum_array
};
STATE currentState{STATE::CHECK};
bool isBeingProcessed{false}; // worked thread assigned and processing current state
void finishProcessing(STATE newState)
{
currentState = newState;
isBeingProcessed = false;
};
// progress tracking
uint64 amountDownloaded{};
private:
void CalcPaddedSize()
{
if (HAS_FLAG(contentFlags, NCrypto::TMDParser::TMDContentFlags::FLAG_HASHED_CONTENT))
{
// pad to 0x10000 bytes
paddedSize = (size + 0xFFFFull) & ~0xFFFFull;
}
else
{
// pad to 16 bytes
paddedSize = (size + 0xFull) & ~0xFull;
}
}
};
struct
{
bool isActive{}; // actively downloading
bool isPaused{};
STATE currentState{ STATE::INITIAL };
// tmd
bool isDownloadingTMD{};
std::vector<uint8> tmdData;
NCrypto::TMDParser* tmd{};
// app/h3 tracking
std::unordered_map<uint16, ContentFile> contentFiles;
// progress of current operation
uint32 progress{}; // for downloading: in 1/10th of a percent (0-1000), for installing: number of files
uint32 progressMax{}; // maximum (downloading: unused, installing: total number of files)
// installing
bool isInstalling{};
// error state
bool hasError{};
std::string errorMsg;
}state;
};
std::recursive_mutex m_mutex;
std::vector<Package*> m_packageList;
public:
void initiateDownload(uint64 titleId, uint16 version);
void pauseDownload(uint64 titleId, uint16 version);
private:
Package* getPackage(uint64 titleId, uint16 version);
fs::path getPackageDownloadPath(Package* package);
fs::path getPackageInstallPath(Package* package);
void updatePackage(Package* package);
void checkPackagesState();
void setPackageError(Package* package, std::string errorMsg);
void reportPackageStatus(Package* package);
void reportPackageProgress(Package* package, uint32 newProgress);
void asyncPackageDownloadTMD(Package* package);
void calcPackageDownloadProgress(Package* package);
void asyncPackageDownloadContentFile(Package* package, uint16 index);
void asyncPackageVerifyFile(Package* package, uint16 index, bool isCheckState);
void asyncPackageInstall(Package* package);
bool asyncPackageInstallRecursiveExtractFiles(Package* package, class FSTVolume* fstVolume, const std::string& sourcePath, const fs::path& destinationPath);
/* callback interface */
public:
void setUserData(void* ptr)
{
m_userData = ptr;
}
void* getUserData() const
{
return m_userData;
}
// register/unregister callbacks
// setting valid callbacks will also trigger transfer of the entire title/package state and the current status message
void registerCallbacks(
void(*cbUpdateConnectStatus)(std::string statusText, DLMGR_STATUS_CODE statusCode),
void(*cbAddDownloadableTitle)(const DlMgrTitleReport& titleInfo),
void(*cbRemoveDownloadableTitle)(uint64 titleId, uint16 version)
)
{
std::unique_lock<std::recursive_mutex> _l(m_mutex);
m_cbUpdateConnectStatus = cbUpdateConnectStatus;
m_cbAddDownloadableTitle = cbAddDownloadableTitle;
m_cbRemoveDownloadableTitle = cbRemoveDownloadableTitle;
// resend data
if (m_cbUpdateConnectStatus || m_cbAddDownloadableTitle)
{
std::unique_lock<std::recursive_mutex> _l(m_mutex);
setStatusMessage(m_statusMessage, m_statusCode);
reportAvailableTitles();
for (auto& p : m_packageList)
reportPackageStatus(p);
}
}
void setStatusMessage(std::string_view msg, DLMGR_STATUS_CODE statusCode)
{
m_statusMessage = msg;
m_statusCode = statusCode;
if (m_cbUpdateConnectStatus)
m_cbUpdateConnectStatus(m_statusMessage, statusCode);
}
std::string m_statusMessage{};
DLMGR_STATUS_CODE m_statusCode{ DLMGR_STATUS_CODE::UNINITIALIZED };
bool hasActiveDownloads()
{
std::unique_lock<std::recursive_mutex> _l(m_mutex);
for (auto& p : m_packageList)
{
if (p->state.isActive && !p->state.hasError &&
(p->state.currentState != Package::STATE::INSTALLED))
{
return true;
}
}
return false;
}
void reset()
{
std::unique_lock<std::recursive_mutex> _l(m_mutex);
m_packageList.clear();
m_statusCode = DLMGR_STATUS_CODE::UNINITIALIZED;
m_statusMessage.clear();
}
private:
void(*m_cbUpdateConnectStatus)(std::string statusText, DLMGR_STATUS_CODE statusCode) { nullptr };
void(*m_cbAddDownloadableTitle)(const DlMgrTitleReport& titleInfo);
void(*m_cbRemoveDownloadableTitle)(uint64 titleId, uint16 version);
void* m_userData{};
/* title version list */
bool m_hasTitleVersionList{};
std::unordered_map<uint64, uint16> m_titleVersionList;
void downloadTitleVersionList();
bool getTitleLatestVersion(TitleId titleId, uint16& version);
/* helper for building list of owned titles (is installed or has cached ticket) */
struct TitleInstallState
{
TitleInstallState(DownloadManager* dlMgr, uint64 titleId);
uint64 titleId;
bool isInstalled;
uint16 installedTitleVersion;
uint16 installedTicketVersion{};
bool operator<(const TitleInstallState& comp) const
{
return this->titleId < comp.titleId;
}
};
struct TitleDownloadAvailableState : TitleInstallState
{
TitleDownloadAvailableState(DownloadManager* dlMgr, uint64 titleId);
bool isUpdateAvailable; // update available for this specific titleId
uint16 availableTitleVersion;
};
std::set<TitleInstallState> getOwnedTitleList();
std::set<TitleDownloadAvailableState> getFullDownloadList();
/* thread */
void threadFunc();
void runManager();
void notifyManager();
void queueManagerJob(const std::function<void()>& callback);
std::atomic_bool m_threadLaunched{ false };
CounterSemaphore m_queuedEvents;
ConcurrentQueue<std::function<void()>> m_jobQueue;
std::mutex m_updateList; // not needed?
};