Skip to content

Commit 0a23b20

Browse files
Backup badges if no badges found
1 parent a8cc3eb commit 0a23b20

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

source/badges.c

Lines changed: 78 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ u64 getShortcut(char *filename)
115115
if (sscanf(p1, "%08x", &lowpath) != 1) return shortcut;
116116

117117
shortcut = 0x0004001000000000 + lowpath;
118-
DEBUG("Shortcut %08llx found for %s\n", shortcut, filename);
118+
DEBUG("Shortcut %16llx found for %s\n", shortcut, filename);
119119
return shortcut;
120120
}
121121

@@ -480,12 +480,89 @@ Result extract_badges(void)
480480
return res;
481481
}
482482

483+
Result backup_badges_fast(void)
484+
{
485+
char *badgeMng = NULL;
486+
487+
DEBUG("writing badge data: making files...\n");
488+
char mng_path[128] = "/3ds/" APP_TITLE "/BadgeMngFile.dat";
489+
char data_path[128] = "/3ds/" APP_TITLE "/BadgeData.dat";
490+
DEBUG("mng_path: %s, data_path: %s\n", mng_path, data_path);
491+
492+
Handle dataHandle = 0;
493+
Handle sdHandle = 0;
494+
495+
DEBUG("loading existing badge mng file...\n");
496+
u32 mngRead = file_to_buf(fsMakePath(PATH_ASCII, "/BadgeMngFile.dat"), ArchiveBadgeExt, &badgeMng);
497+
DEBUG("loading existing badge data file\n");
498+
Result res = FSUSER_OpenFile(&dataHandle, ArchiveBadgeExt, fsMakePath(PATH_ASCII, "/BadgeData.dat"), FS_OPEN_READ, 0);
499+
if (mngRead != BADGE_MNG_SIZE || R_FAILED(res))
500+
{
501+
char err_string[128] = {0};
502+
sprintf(err_string, language.badges.extdata_locked, res);
503+
throw_error(err_string, ERROR_LEVEL_WARNING);
504+
if (badgeMng) free(badgeMng);
505+
if (dataHandle) FSFILE_Close(dataHandle);
506+
FSFILE_Close(sdHandle);
507+
return -1;
508+
}
509+
remake_file(fsMakePath(PATH_ASCII, mng_path), ArchiveSD, BADGE_MNG_SIZE);
510+
511+
FSUSER_CreateFile(ArchiveSD, fsMakePath(PATH_ASCII, data_path), 0, BADGE_DATA_SIZE);
512+
FSUSER_OpenFile(&sdHandle, ArchiveSD, fsMakePath(PATH_ASCII, data_path), FS_OPEN_WRITE, 0);
513+
514+
DEBUG("writing badge data: writing BadgeMngFile...\n");
515+
res = buf_to_file(mngRead, fsMakePath(PATH_ASCII, mng_path), ArchiveSD, badgeMng);
516+
if (R_FAILED(res))
517+
{
518+
DEBUG("Failed to write badgemngfile: 0x%08lx\n", res);
519+
free(badgeMng);
520+
FSFILE_Close(dataHandle);
521+
FSFILE_Close(sdHandle);
522+
return -1;
523+
}
524+
DEBUG("writing badge data: writing badgedata...\n");
525+
char *buf = malloc(0x10000);
526+
u64 size = BADGE_DATA_SIZE;
527+
u64 cur = 0;
528+
while (size > 0)
529+
{
530+
u32 read = 0;
531+
res = FSFILE_Read(dataHandle, &read, cur, buf, min(0x10000, size));
532+
res = FSFILE_Write(sdHandle, NULL, cur, buf, read, FS_WRITE_FLUSH);
533+
size -= read;
534+
cur += read;
535+
}
536+
537+
free(badgeMng);
538+
free(buf);
539+
FSFILE_Close(dataHandle);
540+
FSFILE_Close(sdHandle);
541+
return 0;
542+
}
543+
483544
Result install_badges(void)
484545
{
485546
Handle handle = 0;
486547
Handle folder = 0;
487548
Result res = 0;
488549
draw_loading_bar(0, 1, INSTALL_BADGES);
550+
{
551+
char testpath[128] = "/3ds/" APP_TITLE "/BadgeData.dat";
552+
if (R_FAILED(res = FSUSER_OpenFile(&handle, ArchiveSD, fsMakePath(PATH_ASCII, testpath), FS_OPEN_READ, 0)))
553+
{
554+
if (R_SUMMARY(res) == RS_NOTFOUND)
555+
{
556+
res = backup_badges_fast();
557+
if (R_FAILED(res)) return res;
558+
} else
559+
{
560+
DEBUG("????: 0x%08lx\n", res);
561+
}
562+
}
563+
}
564+
565+
if (handle) FSFILE_Close(handle);
489566

490567
DEBUG("Initializing ACT\n");
491568
res = actInit();

source/fs.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,8 @@ void remake_file(FS_Path path, FS_Archive archive, u32 size)
504504
FSFILE_Close(handle);
505505
FSUSER_DeleteFile(archive, path);
506506
}
507-
FSUSER_CreateFile(archive, path, 0, size);
507+
Result res = FSUSER_CreateFile(archive, path, 0, size);
508+
DEBUG("Remake file res: 0x%08lx\n", res);
508509
char * buf = calloc(size, 1);
509510
if (buf == NULL)
510511
{

0 commit comments

Comments
 (0)