/
main.c
1459 lines (1410 loc) · 47.9 KB
/
main.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
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <3ds.h>
#include "text.h"
#include "svdt.h"
#include "filesystem.h"
#include "secure_values.h"
//Uncomment this line to get additional on screen information
//#define DEBUG_INFO
#define MAX_LS_LINES (HEIGHT-5)
#define CURSOR_WIDTH 3
#define LIST_WIDTH (TOP_WIDTH/2-CURSOR_WIDTH)
#define RES_OUT_OF_SPACE_CARD 0xc86044cd // probably
#define RES_OUT_OF_SPACE_ESHOP 0xd8604664 // at least I think so
#define HELD_THRESHOLD 10
// I know, global variables, but I swear they're useful here
int canHasConsole = 0;
int alphabetSort = 1; // 1 for sort by name, 0 for sort by filesystem load order (probably date)
int calledFromCopyDir = 0; // incremented for every call of copyDir, decremented at end of call
int dirOverwriteAll = 1; // 0 to keep prompting, 1 to always overwrite, -1 to always skip
// not to worry ... this is set to 0 past emergency mode
lsTitle* firstTitle = NULL;
u8 mediatype = 2;
//int productCodeKnown = 0;
char productCode[9] = {0};
const char HOME[2] = "/";
const char SDMC_CURSOR[4] = ">>>";
const char SAVE_CURSOR[4] = ">>>";
enum state
{
SELECT_SDMC,
SELECT_SAVE,
CONFIRM_DELETE,
CONFIRM_OVERWRITE,
SVDT_IS_KILL,
SET_TARGET_TITLE,
CONFIRM_SAVE_ROOT,
CONFIRM_SECURE_VALUE,
EXIT_REQUESTED
};
enum state machine_state;
enum state previous_state;
PrintConsole titleBar, sdmcList, saveList, sdmcCursor, saveCursor;
PrintConsole statusBar, notifyBar;
void printAlert();
void printTarget();
void debugOut(char* garbled)
{
consoleSelect(&statusBar);
printf("\n");//consoleClear();
textcolour(NEONGREEN);
printf("%s\n",garbled);
}
void debugOutPrompt(char* garbled)
{
consoleSelect(&statusBar);
printf("\n");//consoleClear();
textcolour(YELLOW);
printf("%s\n",garbled);
}
void debugOutSuccess(char* garbled)
{
consoleSelect(&statusBar);
printf("\n");//consoleClear();
textcolour(CYAN);
printf("%s\n",garbled);
}
void debugOutCancel(char* garbled)
{
consoleSelect(&statusBar);
printf("\n");//consoleClear();
textcolour(RED);
printf("%s\n",garbled);
}
int detectOverwrite(char* path, lsDir* destDir)
{
if (!destDir || !path) return -1;
if (canHasConsole)
{
debugOut("Checking for overwrite risk.");
#ifdef DEBUG_INFO
if (canHasConsole){
printf("checking against %s\n",path);
printf(" destDir: %s",destDir->thisDir);
}
#endif
}
lsLine* curLine = destDir->firstLine;
#ifdef DEBUG_INFO
if (canHasConsole){
debugOut("starting check");
}
#endif
while (curLine)
{
if (!(curLine->isDirectory))
#ifdef DEBUG_INFO
if(canHasConsole)printf("looking at %s\n",curLine->thisLine);
#endif
if (!strcmp(curLine->thisLine,path))
{
if (canHasConsole)
printf("Found overwrite for: %s\n",curLine->thisLine);
return 1;
}
curLine = curLine->nextLine;
}
if (canHasConsole)
printf("No overwrite risk.\n");
return 0;
}
void copyFile(lsDir* dir, char* path, u64 size, lsDir* destDir)
{
if (!dir || !destDir || !path) return;
Handle* curFsHandle = NULL;
Handle* destFsHandle = NULL;
FS_archive* curArchive = NULL;
FS_archive* destArchive = NULL;
switch(machine_state)
{
case SELECT_SAVE:
curFsHandle = &saveGameFsHandle;
curArchive = &saveGameArchive;
destFsHandle = &sdmcFsHandle;
destArchive = &sdmcArchive;
break;
case SELECT_SDMC:
curFsHandle = &sdmcFsHandle;
curArchive = &sdmcArchive;
destFsHandle = &saveGameFsHandle;
destArchive = &saveGameArchive;
break;
default:
break;
}
if (detectOverwrite(path,destDir)) {
int goGoGadgetOverwrite = 0;
if (dirOverwriteAll == -1) return;
if (!dirOverwriteAll)
{
debugOutPrompt("Possible overwrite detected.\n\t[SELECT] Overwrite\n\t[B] Skip");
if(calledFromCopyDir && canHasConsole)
{
printf("\t[L+R] apply to all files\n");
textcolour(BROWN);
wordwrap("(Hold down both shoulder buttons while choosing to apply for all other files in this directory.)",BOTTOM_WIDTH);
}
previous_state = machine_state;
machine_state = CONFIRM_OVERWRITE;
printAlert();
while (aptMainLoop())
{
hidScanInput();
if(hidKeysDown() & KEY_START)
{
machine_state = EXIT_REQUESTED;
return;
}
if(hidKeysDown() & KEY_B)
{
debugOutCancel("Overwrite cancelled.");
if(hidKeysHeld() & (KEY_L | KEY_R))
dirOverwriteAll = -1;
break;
}
if(hidKeysDown() & KEY_SELECT)
{
debugOutSuccess("Overwrite confirmed!");
goGoGadgetOverwrite = 1;
if(hidKeysHeld() & (KEY_L | KEY_R))
dirOverwriteAll = 1;
break;
}
gspWaitForVBlank();
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
}
machine_state = previous_state;
previous_state = CONFIRM_OVERWRITE;
} else { goGoGadgetOverwrite = 1; }
if(canHasConsole)
printTarget();
if (!goGoGadgetOverwrite)
{
hidScanInput();
return;
}
char* deletePath = (char*)malloc(strlen(dir->thisDir)+strlen(path)+1);
strcpy(deletePath,dir->thisDir);
strcat(deletePath,path);
if(canHasConsole)
{
debugOut("Overwriting file.");
printf("[deletePath %s]",deletePath);
}
if (curArchive==&sdmcArchive)
deleteFile(deletePath,&saveGameArchive,&saveGameFsHandle);
if (curArchive==&saveGameArchive)
deleteFile(deletePath,&sdmcArchive,&sdmcFsHandle);
free(deletePath);
}
char origpath[MAX_PATH_LENGTH] = {0};
char destpath[MAX_PATH_LENGTH] = {0};
strncpy(origpath,dir->thisDir,MAX_PATH_LENGTH);
strcat(origpath,path);
strncpy(destpath,destDir->thisDir,MAX_PATH_LENGTH);
strcat(destpath,path);
u8* data = (u8*) malloc(size);
if (canHasConsole)
{
debugOut("Reading original file ...");
printf("[original path %s]\n",origpath);
}
Result res = loadFile(origpath,data,curArchive,curFsHandle,size);
if(res)
{
if (canHasConsole)
{
debugOutCancel("! Error reading file.");
printf("[result code %08x]",(unsigned int)res);
}
return;
}
if (canHasConsole)
{
debugOut("Writing new file ...");
printf("[destination path %s]\n",destpath);
}
res = writeFile(destpath,data,(u32)size,destArchive,destFsHandle);
free(data);
if(res)
{
if (canHasConsole)
{
debugOutCancel("! Error writing file.");
printf("[result code %08x]\n",(unsigned int)res);
if(res==RES_OUT_OF_SPACE_CARD || res==RES_OUT_OF_SPACE_ESHOP)
printf("(You may be running out of save space!)\n");
}
#ifdef DEBUG_INFO
while(1){
hidScanInput();
if (hidKeysDown() & KEY_Y) break;
}
#endif
machine_state = SVDT_IS_KILL;
return;
}
if((isSecureFile(destpath))&&(whichSecureGame!=SECURE_UNKNOWN))
{
if (canHasConsole)
debugOut("Rewriting secure value from value loaded at startup.");
res = writeSecureValue();
}
if(res)
{
if (canHasConsole){
debugOutCancel("! Error rewriting secure value.");
printf("[result code %08x]\n",(unsigned int)res);
}
}
if (canHasConsole)
debugOutSuccess("Finished!");
}
void copyDir(lsDir* dir, char* path, lsDir* destDir, char* destName)
{
if (!dir || !destDir) return;
Handle* curFsHandle = NULL;
Handle* destFsHandle = NULL;
FS_archive* curArchive = NULL;
FS_archive* destArchive = NULL;
char origpath[MAX_PATH_LENGTH] = {0};
char destpath[MAX_PATH_LENGTH] = {0};
strncpy(origpath,dir->thisDir,MAX_PATH_LENGTH);
if (path)
strcat(origpath,path);
if (canHasConsole)
debugOut("constructing paths:");
strncpy(destpath,destDir->thisDir,MAX_PATH_LENGTH);
if (canHasConsole)
printf("origpath %s\n",origpath);
switch(machine_state)
{
case SELECT_SAVE:
curFsHandle = &saveGameFsHandle;
curArchive = &saveGameArchive;
destFsHandle = &sdmcFsHandle;
destArchive = &sdmcArchive;
break;
case SELECT_SDMC:
curFsHandle = &sdmcFsHandle;
curArchive = &sdmcArchive;
destFsHandle = &saveGameFsHandle;
destArchive = &saveGameArchive;
break;
default:
break;
}
#ifdef DEBUG_INFO
if (canHasConsole){
debugOut("got handles");
debugOut("manipulating destDir->thisDir");
printf("currently %s\n",destDir->thisDir);
}
#endif
if (destName)
{
strcat(destpath,destName);
if (canHasConsole)
printf("destpath %s\n",destpath);
FSUSER_CreateDirectory(destFsHandle,*destArchive,FS_makePath(PATH_CHAR,destpath));
gotoSubDirectory(destDir,destName);
} else {
if (path)
{
strcat(destpath,path);
if (canHasConsole)
printf("destpath %s\n",destpath);
FSUSER_CreateDirectory(destFsHandle,*destArchive,FS_makePath(PATH_CHAR,destpath));
gotoSubDirectory(destDir,path);
}
}
scanDir(destDir,destArchive,destFsHandle);
if(destArchive==&saveGameArchive)
{
#ifdef DEBUG_INFO
if (canHasConsole)
printf("\ncalling ControlArchive\n");
#endif
FSUSER_ControlArchive(saveGameFsHandle, saveGameArchive);
// this is absolutely necessary
// otherwise any changes we make don't stick!
}
if (path)
gotoSubDirectory(dir,path);
#ifdef DEBUG_INFO
if (canHasConsole){
printf("currently %s\n",destDir->thisDir);
printf("destpath %s",destpath);
}
#endif
scanDir(dir,curArchive,curFsHandle);
lsLine* curLine = dir->firstLine;
calledFromCopyDir++;
while (curLine && (machine_state!=SVDT_IS_KILL))
{
if (curLine->isDirectory)
{
copyDir(dir,curLine->thisLine,destDir,NULL);
}
else
{
copyFile(dir,curLine->thisLine,curLine->fileSize,destDir);
if(machine_state == EXIT_REQUESTED) return;
}
curLine = curLine->nextLine;
}
calledFromCopyDir--;
if (!calledFromCopyDir)
dirOverwriteAll = 0;
if (path)
gotoParentDirectory(dir);
gotoParentDirectory(destDir);
scanDir(destDir,destArchive,destFsHandle);
scanDir(dir,curArchive,curFsHandle);
}
void printDir(lsDir* dir)
{
switch(machine_state)
{
case SELECT_SAVE:
consoleSelect(&saveList);
break;
case SELECT_SDMC:
consoleSelect(&sdmcList);
break;
default:
break;
}
consoleClear();
int lines_available = MAX_LS_LINES;
int i;
char lineOut[LIST_WIDTH] = {0};
textcolour(PURPLE);
if(strlen(dir->thisDir)<LIST_WIDTH)
{
strncpy(lineOut,dir->thisDir,LIST_WIDTH-1);
} else {
strncpy(lineOut,lsDirBasename(dir),LIST_WIDTH-1);
if(strlen(lsDirBasename(dir))>=LIST_WIDTH)
{
char x;
for(i=0;i<5;i++)
{
switch(i)
{
case 0:
x = ']';
break;
case 4:
x = '[';
break;
default:
x = '.';
break;
}
lineOut[LIST_WIDTH-2-i] = x;
}
}
}
if (canHasConsole)
printf("%s\n",lineOut);//lsDirBasename(dir));
lines_available--;
if (!dir->lsOffset)
{
if (strcmp("/",(const char*)dir->thisDir)){
if (canHasConsole)
printf("../\n");
}else{
if (canHasConsole)
printf("[is root]\n");
}
lines_available--;
}
lsLine* currentLine = dir->firstLine;
for (i=1;i<dir->lsOffset;i++)
{
if(currentLine)
currentLine = currentLine->nextLine;
}
while (currentLine && lines_available)
{
if(currentLine->isDirectory)
textcolour(MAGENTA);
else
textcolour(WHITE);
strncpy(lineOut,currentLine->thisLine,LIST_WIDTH-1);
if(strlen(currentLine->thisLine)>=LIST_WIDTH)
{
char x;
for(i=0;i<5;i++)
{
switch(i)
{
case 0:
x = ']';
break;
case 4:
x = '[';
break;
default:
x = '.';
break;
}
lineOut[LIST_WIDTH-2-i] = x;
}
}
if (canHasConsole)
printf("%s\n",lineOut);
lines_available--;
#ifdef DEBUG_INFO
if (canHasConsole)
//printf("[REDACTED]\n");
#endif
currentLine = currentLine->nextLine;
}
}
void redrawCursor(int* cursor_y, lsDir* dir)
{
if (dir->lsOffset && (*cursor_y <= 0))
{
if (*cursor_y < 0){
dir->lsOffset += *cursor_y;
if (dir->lsOffset < 0) dir->lsOffset = 0;
}else {
dir->lsOffset--;
}
*cursor_y = 1;
#ifdef DEBUG_INFO
if (canHasConsole){
debugOut("reached top of listing");
printf("dir->lsOffset is %d",dir->lsOffset);
}
#endif
printDir(dir);
}
if (*cursor_y<0)
*cursor_y = 0;
int cursor_y_bound = HEIGHT-6;
if (dir->dirEntryCount+1<cursor_y_bound)
cursor_y_bound = dir->dirEntryCount+1;
if (*cursor_y>cursor_y_bound)
{
if (dir->dirEntryCount+2>MAX_LS_LINES+dir->lsOffset)
{
dir->lsOffset+=*cursor_y-cursor_y_bound;
if (MAX_LS_LINES+dir->lsOffset>dir->dirEntryCount+2)
dir->lsOffset=dir->dirEntryCount+2-MAX_LS_LINES;
#ifdef DEBUG_INFO
if (canHasConsole){
debugOut("scrolling down listing");
printf("dir->lsOffset is %d",dir->lsOffset);
}
#endif
printDir(dir);
}
*cursor_y = cursor_y_bound;
}
switch(machine_state)
{
case SELECT_SAVE:
consoleSelect(&saveCursor);
consoleClear();
gotoxy(0,*cursor_y);
if (canHasConsole)
printf(SAVE_CURSOR);
break;
case SELECT_SDMC:
consoleSelect(&sdmcCursor);
consoleClear();
gotoxy(0,*cursor_y);
if (canHasConsole)
printf(SDMC_CURSOR);
break;
default:
break;
}
}
void printInstructions()
{
consoleSelect(&statusBar);
consoleClear();
textcolour(WHITE);
wordwrap("svdt is tdvs, reversed and without vowels. Use it to transfer files between your SD card and your save data. (Directories marked in purple).\n",BOTTOM_WIDTH);
printf("\n> [L/R] or [left/right] to change\n between save/SD data.\n");
printf("> [up/down] select file or folder.\n");
printf("> [X] delete file or folder.\n");
printf("> [A] navigate inside a folder.\n");
printf("> [B] return to the parent folder.\n");
printf("> [Y] copy file/folder from/to\n save/SD.\n");
printf("> [Y on dir root] copy current dir.\n");
printf("> [SELECT] reprint these instructions\n and toggle sorting method\n");
printf("> [START] exit.\n");
}
void printAlert()
{
consoleSelect(¬ifyBar);
consoleClear();
textcolour(SALMON);
printf("Action requires confirmation. See lower screen.");
}
char titleTitle[0x40];
int titleTitle_set = -1;
void printTarget()
{
consoleSelect(¬ifyBar);
consoleClear();
gotoxy(0,0);
if(titleTitle_set > -1)
{
textcolour(TEAL);
printf("Title: ");
textcolour(WHITE);
printf("%.42s",titleTitle);
}
}
int checkInjectDirectory(char* path, lsDir* dir)
{
lsLine* curLine = dir->firstLine;
while (curLine)
{
if (curLine->isDirectory)
if (!strcmp(curLine->thisLine,path))
return 1;
curLine = curLine->nextLine;
}
return 0;
}
int main()
{
amInit();
filesystemInit();
//if (!doesFileNotExist("/3ds/svdt/no_alpha_sort",&sdmcFsHandle,sdmcArchive))
if (file_exist("no_alpha_sort"))
alphabetSort = 0;
FSUSER_CreateDirectory(&sdmcFsHandle,sdmcArchive,FS_makePath(PATH_CHAR,"/svdt"));
u64 tid;
u64 tid2 = 0;
char productCodeBuffer[16] = {0};
int titleTitles_available;
char destPath[MAX_PATH_LENGTH];
char tempStr[16] = {0};
time_t temps = time(NULL);
strftime(tempStr,16,"%Y%m%d_%H%M%S",gmtime(&temps));
strncpy(titleTitle,tempStr,MAX_PATH_LENGTH);
FSUSER_GetMediaType(&saveGameFsHandle,&mediatype);
if (mediatype==2)
{
// we fetch target app title automatically for gamecards
getTitleTitle(0x0,2,titleTitle);
titleTitle_set = 1;
} else {
aptOpenSession();
APT_GetProgramID(NULL, &tid2);
aptCloseSession();
/*
//Fetch title from /svdt/tid.bin
FILE * pFile;
long lSize;
size_t result;
pFile = fopen ( "/svdt/tid.bin" , "rb" );
if (pFile!=NULL) {
fseek (pFile , 0 , SEEK_END);
lSize = ftell (pFile);
rewind (pFile);
result = fread (&tid2,1,sizeof(tid2),pFile);
if (result == lSize) {
fclose (pFile);
//Make sure we can't load this again without going trough HBL
remove("/svdt/tid.bin");
}
}
fclose (pFile);
*/
getTitleList(mediatype,&titleTitles_available);
if (titleTitle_set < 0){
if (tid2 != 0){
lsTitle* currentTitle = firstTitle;
int i = 0;
while(currentTitle != NULL){
if (currentTitle->thisTitle == tid2){
titleTitle_set = i;
getTitleTitle(tid2, mediatype, titleTitle);
tid = tid2;
break;
}
currentTitle = currentTitle->nextTitle;
i++;
}
}
}
AM_GetTitleProductCode(mediatype,tid2,productCodeBuffer);
strncpy(productCode,productCodeBuffer,9);
secureGameFromProductCode(productCode);
if (whichSecureGame == SECURE_UNKNOWN) {
secureGameFromFilesystem();
}
getSecureValue();
}
lsDir cwd_sdmc, cwd_save;
strncpy(cwd_sdmc.thisDir,HOME,MAX_PATH_LENGTH);
strncpy(cwd_save.thisDir,HOME,MAX_PATH_LENGTH);
scanDir(&cwd_sdmc,&sdmcArchive,&sdmcFsHandle);
scanDir(&cwd_save,&saveGameArchive,&saveGameFsHandle);
hidScanInput();
//if ((hidKeysHeld() & KEY_L) != doesFileNotExist("/3ds/svdt/disable_auto_backups",&sdmcFsHandle,sdmcArchive))
if ((hidKeysHeld() & KEY_L) || !file_exist("disable_auto_backups"))
{
// so! if disable_auto_backups is present but L is held down, we execute a backup
// if disable_auto_backups is present but L is left open, no backup is made
// if disable_auto_backups is absent, we always execute a backup
machine_state = SELECT_SAVE;
memset(destPath,0,MAX_PATH_LENGTH);
gotoSubDirectory(&cwd_sdmc,"svdt");
if (titleTitle_set > -1)
{
strcat(destPath,"/svdt/");
strcat(destPath,titleTitle);
FSUSER_CreateDirectory(&sdmcFsHandle,sdmcArchive,FS_makePath(PATH_CHAR,destPath));
gotoSubDirectory(&cwd_sdmc,titleTitle);
}
copyDir(&cwd_save,NULL,&cwd_sdmc,tempStr);
gotoParentDirectory(&cwd_sdmc);
if (titleTitle_set > -1)
{
gotoParentDirectory(&cwd_sdmc);
}
scanDir(&cwd_sdmc,&sdmcArchive,&sdmcFsHandle);
if(!(hidKeysHeld() & KEY_R))
canHasConsole = 2;
}
if((hidKeysHeld() & KEY_R) && !canHasConsole)
{
// super secret emergency save write mode
// again, for games like ACNL that won't play nice
machine_state = SELECT_SDMC;
if (!checkInjectDirectory("svdt_inject", &cwd_sdmc))
{
canHasConsole = -1;
} else {
gotoSubDirectory(&cwd_sdmc,"svdt_inject");
freeDir(&cwd_sdmc);
scanDir(&cwd_sdmc,&sdmcArchive,&sdmcFsHandle);
copyDir(&cwd_sdmc,NULL,&cwd_save,NULL);
scanDir(&cwd_save,&saveGameArchive,&saveGameFsHandle);
gotoParentDirectory(&cwd_sdmc);
scanDir(&cwd_sdmc,&sdmcArchive,&sdmcFsHandle);
canHasConsole = 3;
}
}
if (mediatype!=2)
machine_state = SET_TARGET_TITLE;
gfxInitDefault();
gfxSet3D(false);
dirOverwriteAll = 0;
consoleInit(GFX_TOP, &titleBar);
consoleInit(GFX_TOP, &sdmcList);
consoleInit(GFX_TOP, &saveList);
consoleInit(GFX_TOP, &sdmcCursor);
consoleInit(GFX_TOP, &saveCursor);
consoleInit(GFX_BOTTOM, &statusBar);
consoleInit(GFX_TOP, ¬ifyBar);
consoleSetWindow(&titleBar,0,0,TOP_WIDTH,3);
consoleSetWindow(&saveCursor,0,3,3,MAX_LS_LINES+1);
consoleSetWindow(&saveList,CURSOR_WIDTH,3,LIST_WIDTH,MAX_LS_LINES+1);
consoleSetWindow(&sdmcCursor,TOP_WIDTH/2,3,CURSOR_WIDTH,MAX_LS_LINES+1);
consoleSetWindow(&sdmcList,TOP_WIDTH/2+CURSOR_WIDTH,3,LIST_WIDTH,MAX_LS_LINES+1);
consoleSetWindow(&statusBar,0,0,BOTTOM_WIDTH,HEIGHT);//8);
consoleSetWindow(¬ifyBar,0,HEIGHT-1,TOP_WIDTH,1);
if (machine_state != SET_TARGET_TITLE)
{
printInstructions();
printTarget();
}
switch (canHasConsole)
{
case -1:
debugOut("Emergency inject invoked w/o svdt_inject directory.");
canHasConsole = 4;
break;
case 2:
debugOut("Standard dump to SD was invoked.");
break;
case 3:
debugOut("Emergency savegame inject was invoked.");
break;
default:
debugOut("Successful startup, I guess. Huh.");
canHasConsole = 1;
break;
}
if (mediatype!=2)
{
printf("Secure game inferred at startup:\n ");
printSecureGame();
if(secureValueSet)
{
debugOutSuccess("Secure value set.");
int i;
for(i=0;i<8;i++)
printf("%02x ",secureValue[i]);
putchar('\n');
}
}
consoleSelect(&titleBar);
textcolour(TEAL);
printf("svdt 0.10.42d, meladroit/willidleaway/suloku\n");
printf("a hacked-together save data explorer/manager\n");
gotoxy(CURSOR_WIDTH,2);
textcolour(GREY);
printf("save data:");
gotoxy(TOP_WIDTH/2,2);
printf("SD data:");
machine_state = SELECT_SAVE;
printDir(&cwd_save);
previous_state = machine_state;
machine_state = SELECT_SDMC;
printDir(&cwd_sdmc);
consoleSelect(&sdmcCursor);
consoleClear();
gotoxy(0,0);
printf(SDMC_CURSOR);
int cursor_y = 0;
char deletePath[MAX_PATH_LENGTH] = {0};
lsDir* ccwd = &cwd_sdmc;
lsDir* notccwd = &cwd_save;
PrintConsole* curList = &sdmcList;
Handle* curFsHandle = &sdmcFsHandle;
FS_archive* curArchive = &sdmcArchive;
u8 sdmcCurrent, sdmcPrevious;
sdmcCurrent = 1;
if (mediatype!=2)
machine_state = SET_TARGET_TITLE;
if (machine_state == SET_TARGET_TITLE)
{
//getTitleList(mediatype,&titleTitles_available);
consoleSelect(&statusBar);
gotoxy(0,10);
int i;
for (i=0;i<BOTTOM_WIDTH;i++) { printf(" "); }
if (titleTitle_set < 0){
if (tid2 != 0){
lsTitle* currentTitle = firstTitle;
int i = 0;
while(currentTitle != NULL){
if (currentTitle->thisTitle == tid2){
titleTitle_set = i;
getTitleTitle(tid2, mediatype, titleTitle);
tid = tid2;
break;
}
currentTitle = currentTitle->nextTitle;
i++;
}
}
}
if (titleTitle_set < 0)
nthTitleInList(titleTitle_set,mediatype,titleTitle,&tid);
AM_GetTitleProductCode(mediatype,tid,productCodeBuffer);
strncpy(productCode,productCodeBuffer,9);
gotoxy(1,10);
printf("<");
gotoxy(BOTTOM_WIDTH-2,10);
printf(">");
gotoxy((BOTTOM_WIDTH-strlen(titleTitle))/2,10);
textcolour(NEONGREEN);
printf(titleTitle);
gotoxy(0,29);
textcolour(WHITE);
printf(productCode);
printf(" - %016llX", tid);
gotoxy(0,12);
textcolour(SALMON);
wordwrap("Target app is not on gamecard. Fetching target app titles automatically is not implemented for NAND/SD apps. Use left/right on D-pad with the [A] button to select the correct target app name. Press [B] to skip.",BOTTOM_WIDTH);
textcolour(WHITE);
}
int heldU = 0;
int heldD = 0;
bool firstloop = false;
while (aptMainLoop())
{
hidScanInput();
if(hidKeysDown() & KEY_START)break;
if(machine_state == EXIT_REQUESTED)break;
if(machine_state == SET_TARGET_TITLE)
{
int titleTitle_update = 0;
if(hidKeysDown() & KEY_START)break;
if(hidKeysDown() & KEY_LEFT)
{
titleTitle_set--;
titleTitle_set+= titleTitles_available;
titleTitle_update = 1;
}
if(hidKeysDown() & KEY_RIGHT)
{
titleTitle_set++;
titleTitle_update = 1;
}
if (titleTitle_update)
{
titleTitle_set = titleTitle_set % titleTitles_available;
gotoxy(0,10);
int i;
for (i=0;i<BOTTOM_WIDTH;i++) { printf(" "); }
nthTitleInList(titleTitle_set,mediatype,titleTitle,&tid);
gotoxy(1,10);
printf("<");
gotoxy(BOTTOM_WIDTH-2,10);
printf(">");
gotoxy((BOTTOM_WIDTH-strlen(titleTitle))/2,10);
textcolour(NEONGREEN);
printf(titleTitle);
titleTitle_update = 0;
textcolour(WHITE);
AM_GetTitleProductCode(mediatype,tid,productCodeBuffer);
strncpy(productCode,productCodeBuffer,9);
gotoxy(0,29);
printf(productCode);
printf(" - %016llX", tid);
}
if(hidKeysDown() & KEY_A || (titleTitle_set > -1 && !firstloop))
{
printInstructions();
printTarget();
switch (canHasConsole)
{
case 4:
debugOut("Emergency inject invoked w/o svdt_inject directory.");
break;
case 2:
debugOut("Standard dump to SD was invoked.");
break;
case 3:
debugOut("Emergency savegame inject was invoked.");
break;
default:
debugOut("Successful startup, I guess. Huh.");
canHasConsole = 1;
break;
}
previous_state = machine_state;
machine_state = SELECT_SDMC;
if (canHasConsole == 2 && titleTitle_set < 0)
{
debugOut("Trying to rename dump directory");
char tempPath[MAX_PATH_LENGTH] = {0};
memset(destPath,0,MAX_PATH_LENGTH);
strcat(destPath,"/svdt/");
strcat(destPath,titleTitle);
FSUSER_CreateDirectory(&sdmcFsHandle,sdmcArchive,FS_makePath(PATH_CHAR,destPath));
strcat(destPath,"/");
strcat(destPath,tempStr);
strcat(tempPath,"/svdt/");
strcat(tempPath,tempStr);
Result res = FSUSER_RenameDirectory(&sdmcFsHandle,sdmcArchive,FS_makePath(PATH_CHAR,tempPath),sdmcArchive,FS_makePath(PATH_CHAR,destPath));
if (res)
{
textcolour(RED);
printf("Failed with result code %08x",(unsigned int)res);
} else { debugOutSuccess("Success!"); }
}
titleTitle_set = 1;
scanDir(&cwd_sdmc,&sdmcArchive,&sdmcFsHandle);
clearTitleList();
// if we're here, then mediatype!=2, so ...
secureGameFromProductCode(productCode);
debugOut("ASR inferred from product code:");
printSecureGame();
getSecureValue();
if(secureValueSet)
{
debugOutSuccess("Secure value set:");
int i;
for(i=0;i<8;i++)
printf("%02x ",secureValue[i]);
putchar('\n');
textcolour(YELLOW);
wordwrap("Enable automatic secure value rewriting on restore?\n",BOTTOM_WIDTH);
printf("\t[A] enable secure value rewriting\n\t[B] disable secure value rewriting\n");
previous_state = machine_state;
machine_state = CONFIRM_SECURE_VALUE;
}
continue;
}
if(hidKeysDown() & KEY_B)
{
titleTitle_set = 0;
printInstructions();
wordwrap("Target app title unknown. Copies of / will be timestamped.",BOTTOM_WIDTH);
whichSecureGame = SECURE_UNKNOWN;
previous_state = machine_state;
machine_state = SELECT_SDMC;
clearTitleList();
}
gspWaitForVBlank();
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
firstloop = true;