forked from ps2homebrew/Open-PS2-Loader
-
Notifications
You must be signed in to change notification settings - Fork 4
/
supportbase.c
865 lines (716 loc) · 27.2 KB
/
supportbase.c
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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
#include "include/opl.h"
#include "include/lang.h"
#include "include/util.h"
#include "include/iosupport.h"
#include "include/system.h"
#include "include/supportbase.h"
#include "include/ioman.h"
#include "modules/iopcore/common/cdvd_config.h"
#include "include/cheatman.h"
#include "include/pggsm.h"
#include "include/cheatman.h"
#include "include/ps2cnf.h"
#define NEWLIB_PORT_AWARE
#include <fileXio_rpc.h> // fileXioMount("iso:", ***), fileXioUmount("iso:")
#include <io_common.h> // FIO_MT_RDONLY
#include <ps2sdkapi.h> // lseek64
#include "../modules/isofs/zso.h"
/// internal linked list used to populate the list from directory listing
struct game_list_t
{
base_game_info_t gameinfo;
struct game_list_t *next;
};
struct game_cache_list
{
unsigned int count;
base_game_info_t *games;
};
int sbIsSameSize(const char *prefix, int prevSize)
{
int size = -1;
char path[256];
snprintf(path, sizeof(path), "%sul.cfg", prefix);
int fd = openFile(path, O_RDONLY);
if (fd >= 0) {
size = getFileSize(fd);
close(fd);
}
return size == prevSize;
}
int sbCreateSemaphore(void)
{
ee_sema_t sema;
sema.option = sema.attr = 0;
sema.init_count = 1;
sema.max_count = 1;
return CreateSema(&sema);
}
// 0 = Not ISO disc image, GAME_FORMAT_OLD_ISO = legacy ISO disc image (filename follows old naming requirement), GAME_FORMAT_ISO = plain ISO image.
int isValidIsoName(char *name, int *pNameLen)
{
// Old ISO image naming format: SCUS_XXX.XX.ABCDEFGHIJKLMNOP.iso
// Minimum is 17 char, GameID (11) + "." (1) + filename (1 min.) + ".iso" (4)
int size = strlen(name);
if (strcasecmp(&name[size - 4], ".iso") == 0 || strcasecmp(&name[size - 4], ".zso") == 0) {
if ((size >= 17) && (name[4] == '_') && (name[8] == '.') && (name[11] == '.')) {
*pNameLen = size - 16;
return GAME_FORMAT_OLD_ISO;
} else if (size >= 5) {
*pNameLen = size - 4;
return GAME_FORMAT_ISO;
}
}
return 0;
}
static int GetStartupExecName(const char *path, char *filename, int maxlength)
{
char ps2disc_boot[CNF_PATH_LEN_MAX] = "";
const char *key;
int ret;
if ((ret = ps2cnfGetBootFile(path, ps2disc_boot)) == 0) {
int length = 0;
const char *start;
/* Skip the device name part of the path ("cdrom0:\"). */
key = ps2disc_boot;
for (; *key != ':'; key++) {
if (*key == '\0') {
LOG("GetStartupExecName: missing ':' (%s).\n", ps2disc_boot);
return -1;
}
}
++key;
while (*key == '\\') {
key++;
}
start = key;
while ((*key != ';') && (*key != '\0')) {
length++;
key++;
}
if (length > maxlength) {
length = maxlength;
}
if (length == 0) {
LOG("GetStartupExecName: serial len 0 ':' (%s).\n", ps2disc_boot);
return -1;
}
strncpy(filename, start, length);
filename[length] = '\0';
LOG("GetStartupExecName: serial len %d %s \n", length, filename);
return 0;
} else {
LOG("GetStartupExecName: Could not get BOOT2 parameter.\n");
return ret;
}
}
static void freeISOGameListCache(struct game_cache_list *cache);
static int loadISOGameListCache(const char *path, struct game_cache_list *cache)
{
char filename[256];
FILE *file;
base_game_info_t *games;
int result, size, count;
freeISOGameListCache(cache);
sprintf(filename, "%s/games.bin", path);
file = fopen(filename, "rb");
if (file != NULL) {
fseek(file, 0, SEEK_END);
size = ftell(file);
rewind(file);
count = size / sizeof(base_game_info_t);
if (count > 0) {
games = memalign(64, count * sizeof(base_game_info_t));
if (games != NULL) {
if (fread(games, sizeof(base_game_info_t), count, file) == count) {
LOG("loadISOGameListCache: %d games loaded.\n", count);
cache->count = count;
cache->games = games;
result = 0;
} else {
LOG("loadISOGameListCache: I/O error.\n");
free(games);
result = EIO;
}
} else {
LOG("loadISOGameListCache: failed to allocate memory.\n");
result = ENOMEM;
}
} else {
result = -1; // Empty file (should not happen)
}
fclose(file);
} else {
result = ENOENT;
}
return result;
}
static void freeISOGameListCache(struct game_cache_list *cache)
{
if (cache->games != NULL) {
free(cache->games);
cache->games = NULL;
cache->count = 0;
}
}
static int updateISOGameList(const char *path, const struct game_cache_list *cache, const struct game_list_t *head, int count)
{
char filename[256];
FILE *file;
const struct game_list_t *game;
int result, i, j, modified;
base_game_info_t *list;
modified = 0;
if (cache != NULL) {
if ((head != NULL) && (count > 0)) {
game = head;
for (i = 0; i < count; i++) {
for (j = 0; j < cache->count; j++) {
if (strncmp(cache->games[i].name, game->gameinfo.name, ISO_GAME_NAME_MAX + 1) == 0 && strncmp(cache->games[i].extension, game->gameinfo.extension, ISO_GAME_EXTENSION_MAX + 1) == 0)
break;
}
if (j == cache->count) {
LOG("updateISOGameList: game added.\n");
modified = 1;
break;
}
game = game->next;
}
if ((!modified) && (count != cache->count)) {
LOG("updateISOGameList: game removed.\n");
modified = 1;
}
} else {
modified = 0;
}
} else {
modified = ((head != NULL) && (count > 0)) ? 1 : 0;
}
if (!modified)
return 0;
LOG("updateISOGameList: caching new game list.\n");
result = 0;
sprintf(filename, "%s/games.bin", path);
if ((head != NULL) && (count > 0)) {
list = (base_game_info_t *)memalign(64, sizeof(base_game_info_t) * count);
if (list != NULL) {
// Convert the linked list into a flat array, for writing performance.
game = head;
for (i = 0; (i < count) && (game != NULL); i++, game = game->next) {
// copy one game, advance
memcpy(&list[i], &game->gameinfo, sizeof(base_game_info_t));
}
file = fopen(filename, "wb");
if (file != NULL) {
result = fwrite(list, sizeof(base_game_info_t), count, file) == count ? 0 : EIO;
fclose(file);
if (result != 0)
remove(filename);
} else
result = EIO;
free(list);
} else
result = ENOMEM;
} else {
// Last game deleted.
remove(filename);
}
return result;
}
// Queries for the game entry, based on filename. Only the new filename format is supported (filename.ext).
static int queryISOGameListCache(const struct game_cache_list *cache, base_game_info_t *ginfo, const char *filename)
{
char isoname[ISO_GAME_FNAME_MAX + 1];
int i;
for (i = 0; i < cache->count; i++) {
snprintf(isoname, sizeof(isoname), "%s%s", cache->games[i].name, cache->games[i].extension);
if (strcmp(filename, isoname) == 0) {
memcpy(ginfo, &cache->games[i], sizeof(base_game_info_t));
return 0;
}
}
return ENOENT;
}
static int scanForISO(char *path, char type, struct game_list_t **glist)
{
int NameLen, count = 0, format, MountFD, cacheLoaded;
struct game_cache_list cache;
base_game_info_t cachedGInfo;
char fullpath[256], startup[GAME_STARTUP_MAX];
struct dirent *dirent;
DIR *dir;
struct stat statbuf;
cache.games = NULL;
cache.count = 0;
cacheLoaded = loadISOGameListCache(path, &cache) == 0;
if ((dir = opendir(path)) != NULL) {
while ((dirent = readdir(dir)) != NULL) {
if ((format = isValidIsoName(dirent->d_name, &NameLen)) > 0) {
base_game_info_t *game;
if (NameLen > ISO_GAME_NAME_MAX)
continue; // Skip files that cannot be supported properly.
snprintf(fullpath, sizeof(fullpath), "%s/%s", path, dirent->d_name);
if (format == GAME_FORMAT_OLD_ISO) {
struct game_list_t *next = (struct game_list_t *)malloc(sizeof(struct game_list_t));
if (next != NULL) {
next->next = *glist;
*glist = next;
game = &(*glist)->gameinfo;
memset(game, 0, sizeof(base_game_info_t));
strncpy(game->name, &dirent->d_name[GAME_STARTUP_MAX], NameLen);
game->name[NameLen] = '\0';
strncpy(game->startup, dirent->d_name, GAME_STARTUP_MAX - 1);
game->startup[GAME_STARTUP_MAX - 1] = '\0';
strncpy(game->extension, &dirent->d_name[GAME_STARTUP_MAX + NameLen], sizeof(game->extension));
game->extension[sizeof(game->extension) - 1] = '\0';
} else {
// Out of memory.
break;
}
} else {
if (queryISOGameListCache(&cache, &cachedGInfo, dirent->d_name) != 0) {
if ((MountFD = fileXioMount("iso:", fullpath, FIO_MT_RDONLY)) >= 0) {
if (GetStartupExecName("iso:/SYSTEM.CNF;1", startup, GAME_STARTUP_MAX - 1) == 0) {
struct game_list_t *next = (struct game_list_t *)malloc(sizeof(struct game_list_t));
if (next != NULL) {
next->next = *glist;
*glist = next;
game = &(*glist)->gameinfo;
memset(game, 0, sizeof(base_game_info_t));
strcpy(game->startup, startup);
strncpy(game->name, dirent->d_name, NameLen);
game->name[NameLen] = '\0';
strncpy(game->extension, &dirent->d_name[NameLen], sizeof(game->extension));
game->extension[sizeof(game->extension) - 1] = '\0';
} else {
// Out of memory.
fileXioUmount("iso:");
break;
}
} else {
// Unable to parse SYSTEM.CNF.
fileXioUmount("iso:");
continue;
}
fileXioUmount("iso:");
} else {
// Unable to mount game.
continue;
}
} else {
// Entry was found in cache.
struct game_list_t *next = (struct game_list_t *)malloc(sizeof(struct game_list_t));
if (next != NULL) {
next->next = *glist;
*glist = next;
game = &(*glist)->gameinfo;
memcpy(game, &cachedGInfo, sizeof(base_game_info_t));
} else {
// Out of memory.
break;
}
}
}
game->parts = 1;
game->media = type;
game->format = format;
if (stat(fullpath, &statbuf) == 0) {
game->sizeMB = statbuf.st_size >> 20;
} else {
game->sizeMB = 0;
}
count++;
}
}
closedir(dir);
} else {
count = 0;
}
if (cacheLoaded) {
updateISOGameList(path, &cache, *glist, count);
freeISOGameListCache(&cache);
} else {
updateISOGameList(path, NULL, *glist, count);
}
return count;
}
int sbReadList(base_game_info_t **list, const char *prefix, int *fsize, int *gamecount)
{
int fd, size, id = 0, result;
int count;
char path[256];
free(*list);
*list = NULL;
*fsize = -1;
*gamecount = 0;
// temporary storage for the game names
struct game_list_t *dlist_head = NULL;
// count iso games in "cd" directory
snprintf(path, sizeof(path), "%sCD", prefix);
count = scanForISO(path, SCECdPS2CD, &dlist_head);
// count iso games in "dvd" directory
snprintf(path, sizeof(path), "%sDVD", prefix);
if ((result = scanForISO(path, SCECdPS2DVD, &dlist_head)) >= 0) {
count = count < 0 ? result : count + result;
}
// count and process games in ul.cfg
snprintf(path, sizeof(path), "%sul.cfg", prefix);
fd = openFile(path, O_RDONLY);
if (fd >= 0) {
USBExtreme_game_entry_t GameEntry;
if (count < 0)
count = 0;
size = getFileSize(fd);
*fsize = size;
count += size / sizeof(USBExtreme_game_entry_t);
if (count > 0) {
if ((*list = (base_game_info_t *)malloc(sizeof(base_game_info_t) * count)) != NULL) {
memset(*list, 0, sizeof(base_game_info_t) * count);
while (size > 0) {
base_game_info_t *g = &(*list)[id++];
// populate game entry in list even if entry corrupted
read(fd, &GameEntry, sizeof(USBExtreme_game_entry_t));
size -= sizeof(USBExtreme_game_entry_t);
// to ensure no leaks happen, we copy manually and pad the strings
memcpy(g->name, GameEntry.name, UL_GAME_NAME_MAX);
g->name[UL_GAME_NAME_MAX] = '\0';
memcpy(g->startup, GameEntry.startup, GAME_STARTUP_MAX);
g->startup[GAME_STARTUP_MAX] = '\0';
g->extension[0] = '\0';
g->parts = GameEntry.parts;
g->media = GameEntry.media;
g->format = GAME_FORMAT_USBLD;
g->sizeMB = 0;
/* TODO: size calculation is very slow
implmented some caching, or do not touch at all */
// calculate total size for individual game
/*int ulfd = 1;
u8 part;
unsigned int name_checksum = USBA_crc32(g->name);
for (part = 0; part < g->parts && ulfd >= 0; part++) {
snprintf(path, sizeof(path), "%sul.%08X.%s.%02x", prefix, name_checksum, g->startup, part);
ulfd = openFile(path, O_RDONLY);
if (ulfd >= 0) {
g->sizeMB += (getFileSize(ulfd) >> 20);
close(ulfd);
}
}*/
}
}
}
close(fd);
} else if (count > 0) {
*list = (base_game_info_t *)malloc(sizeof(base_game_info_t) * count);
}
if (*list != NULL) {
// copy the dlist into the list
while ((id < count) && dlist_head) {
// copy one game, advance
struct game_list_t *cur = dlist_head;
dlist_head = dlist_head->next;
memcpy(&(*list)[id++], &cur->gameinfo, sizeof(base_game_info_t));
free(cur);
}
} else
count = 0;
if (count > 0)
*gamecount = count;
return count;
}
extern int probed_fd;
extern u32 probed_lba;
extern u8 IOBuffer[2048];
static int ProbeZISO(int fd)
{
struct
{
ZISO_header header;
u32 first_block;
} ziso_data;
lseek(fd, 0, SEEK_SET);
if (read(fd, &ziso_data, sizeof(ziso_data)) == sizeof(ziso_data) && ziso_data.header.magic == ZSO_MAGIC) {
// initialize ZSO
ziso_init(&ziso_data.header, ziso_data.first_block);
// set ISO file descriptor for ZSO reader
probed_fd = fd;
probed_lba = 0;
return 1;
} else {
return 0;
}
}
u32 sbGetISO9660MaxLBA(const char *path)
{
u32 maxLBA;
int file;
if ((file = open(path, O_RDONLY, 0666)) >= 0) {
if (ProbeZISO(file)) {
if (ziso_read_sector(IOBuffer, 16, 1) == 1) {
maxLBA = *(u32 *)(IOBuffer + 80);
} else {
maxLBA = 0;
}
} else {
lseek(file, 16 * 2048 + 80, SEEK_SET);
if (read(file, &maxLBA, sizeof(maxLBA)) != sizeof(maxLBA))
maxLBA = 0;
}
close(file);
} else {
maxLBA = 0;
}
return maxLBA;
}
int sbProbeISO9660(const char *path, base_game_info_t *game, u32 layer1_offset)
{
int result = -1, fd;
char buffer[6];
result = -1;
if (game->media == SCECdPS2DVD) { // Only DVDs can have multiple layers.
if ((fd = open(path, O_RDONLY, 0666)) >= 0) {
if (ProbeZISO(fd)) {
if (ziso_read_sector(IOBuffer, layer1_offset, 1) == 1 &&
((IOBuffer[0x00] == 1) && (!strncmp((char *)(&IOBuffer[0x01]), "CD001", 5)))) {
result = 0;
}
} else {
if (lseek64(fd, (u64)layer1_offset * 2048, SEEK_SET) == (u64)layer1_offset * 2048) {
if ((read(fd, buffer, sizeof(buffer)) == sizeof(buffer)) &&
((buffer[0x00] == 1) && (!strncmp(&buffer[0x01], "CD001", 5)))) {
result = 0;
}
}
}
close(fd);
} else
result = fd;
}
return result;
}
static const struct cdvdman_settings_common cdvdman_settings_common_sample = CDVDMAN_SETTINGS_DEFAULT_COMMON;
int sbPrepare(base_game_info_t *game, config_set_t *configSet, int size_cdvdman, void **cdvdman_irx, int *patchindex)
{
int i;
struct cdvdman_settings_common *settings;
int compatmask = 0;
configGetInt(configSet, CONFIG_ITEM_COMPAT, &compatmask);
char gameid[5];
configGetDiscIDBinary(configSet, gameid);
for (i = 0, settings = NULL; i < size_cdvdman; i += 4) {
if (!memcmp((void *)((u8 *)cdvdman_irx + i), &cdvdman_settings_common_sample, sizeof(cdvdman_settings_common_sample))) {
settings = (struct cdvdman_settings_common *)((u8 *)cdvdman_irx + i);
break;
}
}
if (settings == NULL) {
LOG("sbPrepare: unable to locate patch zone.\n");
return -1;
}
if (game != NULL) {
settings->NumParts = game->parts;
settings->media = game->media;
}
settings->flags = 0;
if (compatmask & COMPAT_MODE_1) {
settings->flags |= IOPCORE_COMPAT_ACCU_READS;
}
if (compatmask & COMPAT_MODE_2) {
settings->flags |= IOPCORE_COMPAT_ALT_READ;
}
if (compatmask & COMPAT_MODE_4) {
settings->flags |= IOPCORE_COMPAT_0_SKIP_VIDEOS;
}
if (compatmask & COMPAT_MODE_5) {
settings->flags |= IOPCORE_COMPAT_EMU_DVDDL;
}
if (compatmask & COMPAT_MODE_6) {
settings->flags |= IOPCORE_ENABLE_POFF;
}
settings->fakemodule_flags = 0;
settings->fakemodule_flags |= FAKE_MODULE_FLAG_CDVDFSV;
settings->fakemodule_flags |= FAKE_MODULE_FLAG_CDVDSTM;
InitGSMConfig(configSet);
InitCheatsConfig(configSet);
config_set_t *configGame = configGetByType(CONFIG_GAME);
#ifdef PADEMU
gPadEmuSource = 0;
gEnablePadEmu = 0;
gPadEmuSettings = 0;
gPadMacroSource = 0;
gPadMacroSettings = 0;
if (configGetInt(configSet, CONFIG_ITEM_PADEMUSOURCE, &gPadEmuSource)) {
configGetInt(configSet, CONFIG_ITEM_ENABLEPADEMU, &gEnablePadEmu);
configGetInt(configSet, CONFIG_ITEM_PADEMUSETTINGS, &gPadEmuSettings);
} else {
configGetInt(configGame, CONFIG_ITEM_ENABLEPADEMU, &gEnablePadEmu);
configGetInt(configGame, CONFIG_ITEM_PADEMUSETTINGS, &gPadEmuSettings);
}
if (configGetInt(configSet, CONFIG_ITEM_PADMACROSOURCE, &gPadMacroSource)) {
configGetInt(configSet, CONFIG_ITEM_PADMACROSETTINGS, &gPadMacroSettings);
} else {
configGetInt(configGame, CONFIG_ITEM_PADMACROSETTINGS, &gPadMacroSettings);
}
if (gEnablePadEmu) {
settings->fakemodule_flags |= FAKE_MODULE_FLAG_USBD;
}
#endif
// sanitise the settings
gOSDLanguageSource = 0;
gOSDLanguageEnable = 0;
gOSDLanguageValue = 0;
gOSDTVAspectRatio = 0;
gOSDVideOutput = 0;
if (configGetInt(configSet, CONFIG_ITEM_OSD_SETTINGS_SOURCE, &gOSDLanguageSource)) {
configGetInt(configSet, CONFIG_ITEM_OSD_SETTINGS_ENABLE, &gOSDLanguageEnable);
configGetInt(configSet, CONFIG_ITEM_OSD_SETTINGS_LANGID, &gOSDLanguageValue);
configGetInt(configSet, CONFIG_ITEM_OSD_SETTINGS_TV_ASP, &gOSDTVAspectRatio);
configGetInt(configSet, CONFIG_ITEM_OSD_SETTINGS_VMODE, &gOSDVideOutput);
} else {
configGetInt(configGame, CONFIG_ITEM_OSD_SETTINGS_ENABLE, &gOSDLanguageEnable);
configGetInt(configGame, CONFIG_ITEM_OSD_SETTINGS_LANGID, &gOSDLanguageValue);
configGetInt(configGame, CONFIG_ITEM_OSD_SETTINGS_TV_ASP, &gOSDTVAspectRatio);
configGetInt(configGame, CONFIG_ITEM_OSD_SETTINGS_VMODE, &gOSDVideOutput);
}
*patchindex = i;
// game id
memcpy(settings->DiscID, gameid, sizeof(settings->DiscID));
return compatmask;
}
void sbUnprepare(void *pCommon)
{
memcpy(pCommon, &cdvdman_settings_common_sample, sizeof(struct cdvdman_settings_common));
}
void sbRebuildULCfg(base_game_info_t **list, const char *prefix, int gamecount, int excludeID)
{
char path[256];
USBExtreme_game_entry_t GameEntry;
snprintf(path, sizeof(path), "%sul.cfg", prefix);
FILE *file = fopen(path, "wb");
if (file != NULL) {
int i;
base_game_info_t *game;
memset(&GameEntry, 0, sizeof(GameEntry));
GameEntry.Byte08 = 0x08; // just to be compatible with original ul.cfg
memcpy(GameEntry.magic, "ul.", 3);
for (i = 0; i < gamecount; i++) {
game = &(*list)[i];
if (game->format == GAME_FORMAT_USBLD && (i != excludeID)) {
memcpy(GameEntry.startup, game->startup, GAME_STARTUP_MAX);
memcpy(GameEntry.name, game->name, UL_GAME_NAME_MAX);
// don't fill last symbol with zero, cause trailing symbol can be useful character
GameEntry.parts = game->parts;
GameEntry.media = game->media;
fwrite(&GameEntry, sizeof(GameEntry), 1, file);
}
}
fclose(file);
}
}
static void sbCreatePath_name(const base_game_info_t *game, char *path, const char *prefix, const char *sep, int part, const char *game_name)
{
switch (game->format) {
case GAME_FORMAT_USBLD:
snprintf(path, 256, "%sul.%08X.%s.%02x", prefix, USBA_crc32(game_name), game->startup, part);
break;
case GAME_FORMAT_ISO:
snprintf(path, 256, "%s%s%s%s%s", prefix, (game->media == SCECdPS2CD) ? "CD" : "DVD", sep, game_name, game->extension);
break;
case GAME_FORMAT_OLD_ISO:
snprintf(path, 256, "%s%s%s%s.%s%s", prefix, (game->media == SCECdPS2CD) ? "CD" : "DVD", sep, game->startup, game_name, game->extension);
break;
}
}
void sbCreatePath(const base_game_info_t *game, char *path, const char *prefix, const char *sep, int part)
{
sbCreatePath_name(game, path, prefix, sep, part, game->name);
}
void sbDelete(base_game_info_t **list, const char *prefix, const char *sep, int gamecount, int id)
{
int part;
char path[256];
base_game_info_t *game = &(*list)[id];
for (part = 0; part < game->parts; part++) {
sbCreatePath(game, path, prefix, sep, part);
unlink(path);
}
if (game->format == GAME_FORMAT_USBLD) {
sbRebuildULCfg(list, prefix, gamecount, id);
}
}
void sbRename(base_game_info_t **list, const char *prefix, const char *sep, int gamecount, int id, char *newname)
{
int part;
char oldpath[256], newpath[256];
base_game_info_t *game = &(*list)[id];
for (part = 0; part < game->parts; part++) {
sbCreatePath_name(game, oldpath, prefix, sep, part, game->name);
sbCreatePath_name(game, newpath, prefix, sep, part, newname);
rename(oldpath, newpath);
}
if (game->format == GAME_FORMAT_USBLD) {
memset(game->name, 0, UL_GAME_NAME_MAX + 1);
memcpy(game->name, newname, UL_GAME_NAME_MAX);
sbRebuildULCfg(list, prefix, gamecount, -1);
}
}
config_set_t *sbPopulateConfig(base_game_info_t *game, const char *prefix, const char *sep)
{
char path[256];
snprintf(path, sizeof(path), "%sCFG%s%s.cfg", prefix, sep, game->startup);
config_set_t *config = configAlloc(0, NULL, path);
configRead(config); // Does not matter if the config file could be loaded or not.
configSetStr(config, CONFIG_ITEM_NAME, game->name);
configSetInt(config, CONFIG_ITEM_SIZE, game->sizeMB);
if (game->format != GAME_FORMAT_USBLD) {
if (!strcmp(game->extension, ".iso"))
configSetStr(config, CONFIG_ITEM_FORMAT, "ISO");
else if (!strcmp(game->extension, ".zso"))
configSetStr(config, CONFIG_ITEM_FORMAT, "ZSO");
} else if (game->format == GAME_FORMAT_USBLD)
configSetStr(config, CONFIG_ITEM_FORMAT, "UL");
configSetStr(config, CONFIG_ITEM_MEDIA, game->media == SCECdPS2CD ? "CD" : "DVD");
configSetStr(config, CONFIG_ITEM_STARTUP, game->startup);
return config;
}
static void sbCreateFoldersFromList(const char *path, const char **folders)
{
int i;
char fullpath[256];
for (i = 0; folders[i] != NULL; i++) {
sprintf(fullpath, "%s%s", path, folders[i]);
mkdir(fullpath, 0777);
}
}
void sbCreateFolders(const char *path, int createDiscImgFolders)
{
const char *basicFolders[] = {"CFG", "THM", "LNG", "ART", "VMC", "CHT", "APPS", NULL};
const char *discImgFolders[] = {"CD", "DVD", NULL};
sbCreateFoldersFromList(path, basicFolders);
if (createDiscImgFolders)
sbCreateFoldersFromList(path, discImgFolders);
}
int sbLoadCheats(const char *path, const char *file)
{
char cheatfile[64];
const u32 *cheatList;
int result;
if (GetCheatsEnabled()) {
snprintf(cheatfile, sizeof(cheatfile), "%sCHT/%s.cht", path, file);
LOG("Loading Cheat File %s\n", cheatfile);
if ((result = load_cheats(cheatfile)) < 0) {
LOG("Error: failed to load cheats\n");
} else {
cheatList = GetCheatsList();
if (!((cheatList[0] == 0) && (cheatList[1] == 0))) {
LOG("Cheats found\n");
result = 0;
} else {
LOG("No cheats found\n");
result = -ENOENT;
}
}
} else {
result = 0;
}
return result;
}