1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-12 21:46:39 +00:00
nxdumptool/source/main.c

158 lines
4 KiB
C
Raw Normal View History

2018-05-15 13:57:40 +01:00
#include <stdio.h>
2018-05-16 17:06:57 +01:00
#include <malloc.h>
2018-05-15 13:57:40 +01:00
#include <switch.h>
2018-05-16 17:06:57 +01:00
#include <dirent.h>
#include <memory.h>
2018-05-15 17:00:19 +01:00
#include "menu.h"
#include "dumper.h"
#include "ccolor.h"
2018-05-15 13:57:40 +01:00
2018-05-15 17:00:19 +01:00
FsDeviceOperator fsOperatorInstance;
2018-05-15 13:57:40 +01:00
2018-05-16 16:34:05 +01:00
bool shouldExit = false;
2018-05-15 17:00:19 +01:00
bool shouldWaitForAnyButton = false;
2018-05-15 13:57:40 +01:00
2018-05-16 16:34:05 +01:00
void menuExit() {
shouldExit = true;
}
2018-05-15 17:00:19 +01:00
void menuWaitForAnyButton() {
printf(C_DIM "Press any button to return to menu\n");
shouldWaitForAnyButton = true;
}
2018-05-15 13:57:40 +01:00
2018-05-15 17:00:19 +01:00
void startOperation(const char* title) {
consoleClear();
printf(C_DIM "%s\n\n" C_RESET, title);
}
2018-05-15 13:57:40 +01:00
2018-05-16 17:06:57 +01:00
void dumpPartitionZero(MenuItem* item) {
2018-05-15 17:00:19 +01:00
startOperation("Raw Dump Partition 0 (SysUpdate)");
workaroundPartitionZeroAccess(&fsOperatorInstance);
2018-05-15 17:00:19 +01:00
dumpPartitionRaw(&fsOperatorInstance, 0);
menuWaitForAnyButton();
2018-05-15 13:57:40 +01:00
}
2018-05-16 17:06:57 +01:00
void printFilesInDirMenuItem(MenuItem* item);
2018-05-15 17:00:19 +01:00
MenuItem mainMenu[] = {
{ .text = "Raw Dump Partition 0 (SysUpdate)", .callback = dumpPartitionZero },
2018-05-16 17:06:57 +01:00
{ .text = "Print files on SD Card", .callback = printFilesInDirMenuItem, .userdata = "/" },
2018-05-15 17:00:19 +01:00
{ .text = NULL }
};
2018-05-16 17:06:57 +01:00
MenuItem* currentFileListBuf;
void freeCurrentFileListBuf() {
MenuItem* ptr = currentFileListBuf;
while (ptr != NULL) {
free(ptr->text);
if (ptr->userdata != NULL)
free(ptr->userdata);
ptr++;
}
free(currentFileListBuf);
}
void exitFileList() {
freeCurrentFileListBuf();
menuSetCurrent(mainMenu, menuExit);
}
char* getParentDir(const char* path) {
char* ptr = strrchr(path, '/');
if (ptr == NULL || ptr == path) // not found or first character
return NULL;
char* retval = (char*) malloc(ptr - path + 1);
memcpy(retval, path, ptr - path);
retval[ptr - path] = '\0';
return retval;
}
char* pathJoin(const char* p1, const char* p2) {
size_t p1s = strlen(p1);
if (p1s == 0)
return strdup(p2);
size_t p2s = strlen(p2);
char* retval = (char*) malloc(p1s + 1 + p2s + 1);
memcpy(retval, p1, p1s);
retval[p1s] = '/';
memcpy(&retval[p1s + 1], p2, p2s + 1); // copy with null terminator
return retval;
}
void printFilesInDir(const char* path) {
int maxMenuItemCount = 48;
MenuItem* buf = (MenuItem*) malloc(sizeof(MenuItem) * (maxMenuItemCount + 1));
currentFileListBuf = buf;
DIR* dir = opendir(path);
struct dirent* ent;
int bufi = 0;
char* parentDir = getParentDir(path);
if (parentDir != NULL) {
buf[bufi].userdata = parentDir;
buf[bufi].text = strdup("..");
buf[bufi].callback = printFilesInDirMenuItem;
bufi++;
}
while ((ent = readdir(dir)) != NULL) {
if (ent->d_type == DT_DIR) {
buf[bufi].text = pathJoin(ent->d_name, "");
buf[bufi].userdata = pathJoin(path, ent->d_name);
buf[bufi].callback = printFilesInDirMenuItem;
} else {
buf[bufi].text = strdup(ent->d_name);
buf[bufi].userdata = buf[bufi].callback = NULL;
}
if (++bufi >= maxMenuItemCount)
break;
}
buf[bufi].text = NULL;
closedir(dir);
menuSetCurrent(buf, exitFileList);
}
void printFilesInDirMenuItem(MenuItem* item) {
printFilesInDir(item->userdata);
}
int main(int argc, char **argv) {
2018-05-15 13:57:40 +01:00
gfxInitDefault();
consoleInit(NULL);
2018-05-15 17:00:19 +01:00
if (R_FAILED(fsOpenDeviceOperator(&fsOperatorInstance))) {
2018-05-15 13:57:40 +01:00
printf("Failed to open device operator\n");
return -1;
}
2018-05-16 16:34:05 +01:00
menuSetCurrent(mainMenu, menuExit);
2018-05-15 13:57:40 +01:00
while(appletMainLoop())
{
2018-05-15 17:00:19 +01:00
bool btnWait = shouldWaitForAnyButton;
2018-05-15 13:57:40 +01:00
hidScanInput();
2018-05-15 17:00:19 +01:00
if (!btnWait)
menuUpdate(&fsOperatorInstance);
2018-05-15 13:57:40 +01:00
u64 kDown = hidKeysDown(CONTROLLER_P1_AUTO);
if (kDown & KEY_PLUS) break;
2018-05-15 17:00:19 +01:00
if (btnWait && kDown) {
shouldWaitForAnyButton = false;
menuPrint();
}
2018-05-16 16:34:05 +01:00
if (shouldExit)
break;
2018-05-15 13:57:40 +01:00
gfxFlushBuffers();
gfxSwapBuffers();
gfxWaitForVsync();
}
2018-05-15 17:00:19 +01:00
fsDeviceOperatorClose(&fsOperatorInstance);
2018-05-15 13:57:40 +01:00
gfxExit();
return 0;
}