forked from blastrock/pkgj
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.cpp
More file actions
621 lines (555 loc) · 17 KB
/
Copy pathdb.cpp
File metadata and controls
621 lines (555 loc) · 17 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
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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
#include "db.hpp"
#include "file.hpp"
#include "pkgi.hpp"
#include "sha256.hpp"
#include "utils.hpp"
#include <fmt/format.h>
#include <boost/scope_exit.hpp>
#include <algorithm>
#include <cstring>
#include <stdexcept>
#include <string>
#include <stddef.h>
std::string pkgi_mode_to_string(Mode mode)
{
switch (mode)
{
#define RET(mode, str) \
case Mode##mode: \
return str
RET(Games, "PSV games");
RET(Dlcs, "PSV DLCs");
RET(Demos, "PSV demos");
RET(Themes, "PSV themes");
RET(PsmGames, "PSM games");
RET(PsxGames, "PS1 games");
RET(PspGames, "PSP games");
RET(PspDlcs, "PSP DLCs");
#undef RET
}
return "unknown mode";
}
TitleDatabase::TitleDatabase(const std::string& dbPath) : _dbPath(dbPath)
{
}
static const char* pkgi_mode_to_file_name(Mode mode)
{
switch (mode)
{
case ModeGames:
return "titles_psvgames.tsv";
case ModeDlcs:
return "titles_psvdlcs.tsv";
case ModeDemos:
return "titles_psvdemos.tsv";
case ModeThemes:
return "titles_psvthemes.tsv";
case ModePsmGames:
return "titles_psmgames.tsv";
case ModePspGames:
return "titles_pspgames.tsv";
case ModePspDlcs:
return "titles_pspdlcs.tsv";
case ModePsxGames:
return "titles_psxgames.tsv";
}
throw formatEx<std::runtime_error>(
"unknown mode {}", static_cast<int>(mode));
}
namespace
{
std::vector<const char*> pkgi_split_row(char** pptr, const char* end)
{
auto& ptr = *pptr;
std::vector<const char*> result;
while (ptr != end && *ptr != '\n')
{
const char* field = ptr;
while (ptr != end && *ptr != '\t' && *ptr != '\r')
++ptr;
if (ptr == end)
{
result.push_back(field);
break;
}
*ptr++ = 0;
result.push_back(field);
if (ptr == end)
{
result.push_back(field);
break;
}
}
while (ptr != end && *ptr++ != '\n')
;
return result;
}
enum class Column
{
Region,
Content,
Name,
NameOrg,
AppVersion,
Zrif,
Url,
Digest,
Size,
FwVersion,
LastModification,
};
int pkgi_get_column_number(Mode mode, Column column)
{
#define MAP_COL(name, i) \
case Column::name: \
return i
switch (mode)
{
case ModeGames:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Zrif, 4);
MAP_COL(Content, 5);
MAP_COL(LastModification, 6);
MAP_COL(NameOrg, 7);
MAP_COL(Size, 8);
MAP_COL(Digest, 9);
MAP_COL(FwVersion, 10);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModeDlcs:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Zrif, 4);
MAP_COL(Content, 5);
MAP_COL(LastModification, 6);
MAP_COL(Size, 7);
MAP_COL(Digest, 8);
MAP_COL(NameOrg, -1);
MAP_COL(FwVersion, -1);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModeDemos:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Zrif, 4);
MAP_COL(Content, 5);
MAP_COL(LastModification, 6);
MAP_COL(NameOrg, 7);
MAP_COL(Size, 8);
MAP_COL(Digest, 9);
MAP_COL(FwVersion, 10);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModeThemes:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Zrif, 4);
MAP_COL(Content, 5);
MAP_COL(LastModification, 6);
MAP_COL(Size, 7);
MAP_COL(Digest, 8);
MAP_COL(NameOrg, -1);
MAP_COL(FwVersion, -1);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModePsmGames:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Zrif, 4);
MAP_COL(Content, 5);
MAP_COL(LastModification, 6);
MAP_COL(Size, 7);
MAP_COL(Digest, 8);
MAP_COL(NameOrg, -1);
MAP_COL(FwVersion, -1);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModePsxGames:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Content, 4);
MAP_COL(LastModification, 5);
MAP_COL(NameOrg, 6);
MAP_COL(Size, 7);
MAP_COL(Digest, 8);
MAP_COL(Zrif, -1);
MAP_COL(FwVersion, -1);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModePspGames:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 3);
MAP_COL(Url, 4);
MAP_COL(Content, 5);
MAP_COL(LastModification, 6);
MAP_COL(Size, 9);
MAP_COL(Digest, 10);
MAP_COL(FwVersion, -1);
MAP_COL(NameOrg, -1);
MAP_COL(Zrif, -1);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
case ModePspDlcs:
switch (column)
{
MAP_COL(Region, 1);
MAP_COL(Name, 2);
MAP_COL(Url, 3);
MAP_COL(Content, 4);
MAP_COL(LastModification, 5);
MAP_COL(Size, 8);
MAP_COL(Digest, 9);
MAP_COL(FwVersion, -1);
MAP_COL(NameOrg, -1);
MAP_COL(Zrif, -1);
MAP_COL(AppVersion, -1);
default:
throw std::runtime_error("invalid column");
}
default:
throw std::runtime_error("invalid mode");
}
#undef MAP_COL
}
const char* get_or_empty(
Mode mode, std::vector<const char*> const& v, Column column)
{
const auto pos = pkgi_get_column_number(mode, column);
if (pos < 0)
return "";
return v.at(pos);
}
}
void TitleDatabase::update(Mode mode, Http* http, const std::string& update_url)
{
const auto tmppath = _dbPath + "/dbtmp.tsv";
auto item_file = pkgi_create(tmppath);
BOOST_SCOPE_EXIT_ALL(&)
{
if (item_file)
pkgi_close(item_file);
};
std::vector<uint8_t> db_data(64 * 1024);
db_total = 0;
db_size = 0;
LOGF("Fetching game database from: {}", update_url);
http->start(update_url, 0);
db_total = http->get_length();
for (;;)
{
int read = http->read(db_data.data(), db_data.size());
if (read == 0)
break;
db_size += read;
pkgi_write(item_file, db_data.data(), read);
}
if (db_size == 0)
throw std::runtime_error(
"list is empty... check for newer pkgj version");
if (db_size != db_total)
throw std::runtime_error(
"TSV file is truncated, check your Internet connection and "
"retry");
pkgi_close(item_file);
item_file = nullptr;
const auto filepath =
fmt::format("{}/{}", _dbPath, pkgi_mode_to_file_name(mode));
pkgi_rename(tmppath, filepath);
LOG("Game database download complete");
}
namespace
{
const char* region_to_string(GameRegion region)
{
switch (region)
{
case RegionASA:
return "ASIA";
case RegionEUR:
return "EU";
case RegionJPN:
return "JP";
case RegionUSA:
return "US";
default:
throw std::runtime_error(fmt::format("unknown region {}", (int)region));
}
}
std::set<std::string> filter_to_vector(uint32_t filter)
{
std::set<std::string> ret;
#define HANDLE_REGION(reg) \
if (filter & DbFilterRegion##reg) \
ret.insert(region_to_string(Region##reg))
HANDLE_REGION(ASA);
HANDLE_REGION(EUR);
HANDLE_REGION(JPN);
HANDLE_REGION(USA);
#undef HANDLE_REGION
return ret;
}
bool lower(const DbItem& a, const DbItem& b, DbSort sort, DbSortOrder order)
{
GameRegion reg_a = pkgi_get_region(a.titleid);
GameRegion reg_b = pkgi_get_region(b.titleid);
int64_t cmp;
if (sort == SortByTitle)
cmp = a.titleid.compare(b.titleid);
else if (sort == SortByRegion)
cmp = reg_a - reg_b;
else if (sort == SortByName)
cmp = pkgi_stricmp(a.name.c_str(), b.name.c_str());
else if (sort == SortBySize)
cmp = a.size - b.size;
else if (sort == SortByDate)
cmp = a.date.compare(b.date);
else
throw std::runtime_error(
fmt::format("unknown sort order {}", (int)sort));
if (cmp == 0)
cmp = a.titleid.compare(b.titleid);
if (order == SortDescending)
cmp = -cmp;
return cmp < 0;
}
}
void TitleDatabase::reload(
Mode mode,
uint32_t region_filter,
DbSort sort_by,
DbSortOrder sort_order,
const std::string& search,
const std::set<std::string>& installed_games)
{
const auto filter_by_region =
(region_filter & DbFilterAllRegions) != DbFilterAllRegions;
const auto regions = filter_to_vector(region_filter);
db.clear();
_title_count = 0;
const auto dbpath =
fmt::format("{}/{}", _dbPath, pkgi_mode_to_file_name(mode));
if (!pkgi_file_exists(dbpath))
return;
auto db_data = pkgi_load(dbpath);
auto ptr = reinterpret_cast<char*>(db_data.data());
const auto end = reinterpret_cast<char*>(db_data.data() + db_data.size());
// skip header
while (ptr < end && *ptr != '\n')
ptr++;
if (ptr == end)
return;
ptr++; // \n
unsigned line = 1;
while (ptr < end && *ptr)
{
++line;
try
{
const auto fields = pkgi_split_row(&ptr, end);
const std::string content =
get_or_empty(mode, fields, Column::Content);
const std::string titleid =
content.size() >= 7 + 9 ? content.substr(7, 9) : "";
const auto region = get_or_empty(mode, fields, Column::Region);
const std::string name = get_or_empty(mode, fields, Column::Name);
const auto name_org = get_or_empty(mode, fields, Column::NameOrg);
const auto url = get_or_empty(mode, fields, Column::Url);
const auto zrif = get_or_empty(mode, fields, Column::Zrif);
const auto digest = get_or_empty(mode, fields, Column::Digest);
const std::string size = get_or_empty(mode, fields, Column::Size);
const std::string fw_version =
get_or_empty(mode, fields, Column::FwVersion);
const auto last_modification =
get_or_empty(mode, fields, Column::LastModification);
const std::string app_version =
get_or_empty(mode, fields, Column::AppVersion);
if (*url == '\0' || std::string(url) == "MISSING" ||
std::string(url) == "CART ONLY" ||
std::string(zrif) == "MISSING")
continue;
++_title_count;
if (filter_by_region && !regions.count(region))
continue;
if (!search.empty() &&
!pkgi_stricontains(name.c_str(), search.c_str()) &&
!pkgi_stricontains(titleid.c_str(), search.c_str()))
continue;
bool bdigest = true;
std::array<uint8_t, 32> digest_array{};
if (std::all_of(
digest,
digest + 64,
[](const auto c) { return c != 0; }))
digest_array = pkgi_hexbytes(digest, SHA256_DIGEST_SIZE);
else
bdigest = false;
std::string full_name = name;
if (!app_version.empty())
full_name = fmt::format("{} ({})", name, app_version);
if (!name.empty() && name.back() != ']' && fw_version > "3.60")
full_name = fmt::format("{} [{}]", full_name, fw_version);
if (!(region_filter & DbFilterInstalled) ||
installed_games.find(titleid) != installed_games.end())
db.push_back(DbItem{
PresenceUnknown,
titleid,
content,
0,
full_name,
name_org ? name_org : "",
zrif ? zrif : "",
url,
static_cast<bool>(bdigest),
digest_array,
size.empty() ? 0 : std::stoll(size),
last_modification,
app_version,
fw_version,
/*selected=*/false,
/*user_flag=*/UserFlag::None,
/*user_comment=*/"",
});
}
catch (const std::exception& e)
{
throw formatEx<std::runtime_error>(
"failed to parse line {}: {}", line, e.what());
}
}
std::sort(
db.begin(),
db.end(),
[&](const auto& a, const auto& b)
{ return lower(a, b, sort_by, sort_order); });
LOGF("Database loaded: {}/{} items", db.size(), _title_count);
}
void TitleDatabase::get_update_status(uint32_t* updated, uint32_t* total)
{
*updated = db_size;
*total = db_total;
}
uint32_t TitleDatabase::count()
{
return db.size();
}
uint32_t TitleDatabase::total()
{
return _title_count;
}
DbItem* TitleDatabase::get(uint32_t index)
{
return index < db.size() ? &db[index] : NULL;
}
DbItem* TitleDatabase::get_by_content(const char* content)
{
for (size_t i = 0; i < db.size(); ++i)
if (db[i].content == content)
return &db[i];
return NULL;
}
GameRegion pkgi_get_region(const std::string& titleid)
{
if (titleid.size() < 4)
return RegionUnknown;
uint32_t first = get32le((uint8_t*)titleid.c_str());
#define ID(a, b, c, d) \
(uint32_t)( \
((uint8_t)(d) << 24) | ((uint8_t)(c) << 16) | \
((uint8_t)(b) << 8) | ((uint8_t)(a)))
// https://github.com/Yoti/pkg2zip/blob/master/pkg2zip.c#L241
switch (first)
{
case ID('N', 'P', 'H', 'I'): // PS1
case ID('N', 'P', 'H', 'J'): // PS1
case ID('N', 'P', 'H', 'G'): // PSP
case ID('N', 'P', 'H', 'H'): // PSP
case ID('N', 'P', 'H', 'Z'): // PSP
case ID('U', 'C', 'A', 'S'): // PSP
case ID('U', 'L', 'A', 'S'): // PSP
case ID('U', 'C', 'K', 'S'): // PSP KOR
case ID('U', 'L', 'K', 'S'): // PSP KOR
case ID('P', 'C', 'S', 'D'): // PSV
case ID('P', 'C', 'S', 'H'): // PSV
case ID('N', 'P', 'Q', 'A'): // PSM
return RegionASA;
case ID('N', 'P', 'E', 'E'): // PS1
case ID('N', 'P', 'E', 'F'): // PS1
case ID('N', 'P', 'E', 'G'): // PSP
case ID('N', 'P', 'E', 'H'): // PSP
case ID('N', 'P', 'E', 'X'): // PSP
case ID('N', 'P', 'E', 'Z'): // PSP
case ID('U', 'C', 'E', 'S'): // PSP
case ID('U', 'L', 'E', 'S'): // PSP
case ID('P', 'C', 'S', 'B'): // PSV
case ID('P', 'C', 'S', 'F'): // PSV
case ID('N', 'P', 'O', 'A'): // PSM
return RegionEUR;
case ID('N', 'P', 'J', 'I'): // PS1
case ID('N', 'P', 'J', 'J'): // PS1 (1-790) and PSP-PCE (30000-30059)
case ID('N', 'P', 'J', 'G'): // PSP
case ID('N', 'P', 'J', 'H'): // PSP
case ID('U', 'C', 'J', 'B'): // PSP
case ID('U', 'C', 'J', 'M'): // PSP
case ID('U', 'C', 'J', 'S'): // PSP
case ID('U', 'L', 'J', 'M'): // PSP
case ID('U', 'L', 'J', 'S'): // PSP
case ID('P', 'C', 'S', 'C'): // PSV
case ID('P', 'C', 'S', 'G'): // PSV
case ID('N', 'P', 'P', 'A'): // PSM
return RegionJPN;
case ID('N', 'P', 'U', 'F'): // PS1 (1-686) and PSP-PCE (30000-30016)
case ID('N', 'P', 'U', 'I'): // PS1
case ID('N', 'P', 'U', 'J'): // PS1
case ID('N', 'P', 'U', 'G'): // PSP
case ID('N', 'P', 'U', 'H'): // PSP
case ID('N', 'P', 'U', 'X'): // PSP
case ID('N', 'P', 'U', 'Z'): // PSP
case ID('U', 'C', 'U', 'S'): // PSP
case ID('U', 'L', 'U', 'S'): // PSP
case ID('P', 'C', 'S', 'A'): // PSV
case ID('P', 'C', 'S', 'E'): // PSV
case ID('N', 'P', 'N', 'A'): // PSM
return RegionUSA;
case ID('N', 'P', 'X', 'S'): // PSV
case ID('P', 'C', 'S', 'I'): // PSV
return RegionINT;
default:
return RegionUnknown;
}
#undef ID
}