From decfb9dc182ad6ad88cfcd7f03580ea2cad9a620 Mon Sep 17 00:00:00 2001 From: Koi Date: Mon, 8 Aug 2022 20:30:26 -0500 Subject: [PATCH] Minor refactor, make WiFi default if no config file is present, fix pointer reads (thanks, Anubis!). --- sys-botbase/source/commands.c | 64 ++++++++++++------ sys-botbase/source/commands.h | 11 +++- sys-botbase/source/main.c | 120 ++++------------------------------ 3 files changed, 65 insertions(+), 130 deletions(-) diff --git a/sys-botbase/source/commands.c b/sys-botbase/source/commands.c index 223f6b7..257509a 100644 --- a/sys-botbase/source/commands.c +++ b/sys-botbase/source/commands.c @@ -16,6 +16,7 @@ HiddbgHdlsDeviceInfo controllerDevice = {0}; HiddbgHdlsState controllerState = {0}; time_t curTime = 0; time_t origTime = 0; +USBResponse response; //Keyboard: HiddbgKeyboardAutoPilotState dummyKeyboardState = {0}; @@ -196,25 +197,36 @@ void writeMem(u64 offset, u64 size, u8* val) printf("svcWriteDebugProcessMemory: %d\n", rc); } -void peek(u8* out, u64 offset, u64 size) +void peek(u64 offset, u64 size) { + u8 *out = malloc(sizeof(u8) * size); attach(); readMem(out, offset, size); detach(); - if (!usb) - { - u64 i; - for (i = 0; i < size; i++) - { - printf("%02X", out[i]); - } - printf("\n"); - } + if (usb) + { + response.size = size; + response.data = &out[0]; + sendUsbResponse(response); + } + else + { + u64 i; + for (i = 0; i < size; i++) + printf("%02X", out[i]); + printf("\n"); + } + free(out); } -void peekMulti(u8* out, u64* offset, u64* size, u64 count, u64 totalSize) +void peekMulti(u64* offset, u64* size, u64 count) { + u64 totalSize = 0; + for (int i = 0; i < count; i++) + totalSize += size[i]; + + u8 *out = malloc(sizeof(u8) * totalSize); u64 ofs = 0; attach(); for (int i = 0; i < count; i++) @@ -224,15 +236,20 @@ void peekMulti(u8* out, u64* offset, u64* size, u64 count, u64 totalSize) } detach(); - if (!usb) - { - u64 i; - for (i = 0; i < totalSize; i++) - { - printf("%02X", out[i]); - } - printf("\n"); - } + if (usb) + { + response.size = totalSize; + response.data = &out[0]; + sendUsbResponse(response); + } + else + { + u64 i; + for (i = 0; i < totalSize; i++) + printf("%02X", out[i]); + printf("\n"); + } + free(out); } void readMem(u8* out, u64 offset, u64 size) @@ -501,3 +518,10 @@ void resetTime() if (R_FAILED(rt)) fatalThrow(rt); } + +void sendUsbResponse(USBResponse response) +{ + usbCommsWrite((void*)&response, 4); + if (response.size > 0) + usbCommsWrite(response.data, response.size); +} diff --git a/sys-botbase/source/commands.h b/sys-botbase/source/commands.h index 092d204..7789d21 100644 --- a/sys-botbase/source/commands.h +++ b/sys-botbase/source/commands.h @@ -36,6 +36,12 @@ typedef struct { u8 state; } KeyData; +typedef struct +{ + u64 size; + void* data; +}USBResponse; + #define JOYSTICK_LEFT 0 #define JOYSTICK_RIGHT 1 @@ -51,8 +57,8 @@ bool getIsProgramOpen(u64 id); void poke(u64 offset, u64 size, u8* val); void writeMem(u64 offset, u64 size, u8* val); -void peek(u8* out, u64 offset, u64 size); -void peekMulti(u8* out, u64* offset, u64* size, u64 count, u64 totalSize); +void peek(u64 offset, u64 size); +void peekMulti(u64* offset, u64* size, u64 count); void readMem(u8* out, u64 offset, u64 size); void click(HidNpadButton btn); void press(HidNpadButton btn); @@ -65,3 +71,4 @@ void key(HiddbgKeyboardAutoPilotState* states, u64 sequentialCount); void clickSequence(char* seq, u8* token); void dateSkip(); void resetTime(); +void sendUsbResponse(USBResponse response); diff --git a/sys-botbase/source/main.c b/sys-botbase/source/main.c index ae94fa2..3ffccaa 100644 --- a/sys-botbase/source/main.c +++ b/sys-botbase/source/main.c @@ -19,12 +19,6 @@ #define HEAP_SIZE 0x00C00000 #define THREAD_SIZE 0x1A000 -typedef struct -{ - u64 size; - void* data; -}USBResponse; - typedef enum { Active = 0, Exit = 1, @@ -53,7 +47,7 @@ u8 clickThreadState = 0; // 1 = break thread KeyData currentKeyEvent = {0}; TouchData currentTouchEvent = {0}; char* currentClick = NULL; -bool usb = true; +bool usb = false; // for cancelling the touch/click thread u8 touchToken = 0; @@ -63,13 +57,6 @@ u8 clickToken = 0; u32 __nx_applet_type = AppletType_None; TimeServiceType __nx_time_service_type = TimeServiceType_System; -void sendUsbResponse(USBResponse response) -{ - usbCommsWrite((void*)&response, 4); - if (response.size > 0) - usbCommsWrite(response.data, response.size); -} - // we override libnx internals to do a minimal init void __libnx_initheap(void) { @@ -208,16 +195,7 @@ int argmain(int argc, char **argv) MetaData meta = getMetaData(); u64 offset = parseStringToInt(argv[1]); u64 size = parseStringToInt(argv[2]); - - u8* out = malloc(sizeof(u8) * size); - peek(out, meta.heap_base + offset, size); - if (usb) - { - response.size = size; - response.data = &out[0]; - sendUsbResponse(response); - } - free(out); + peek(meta.heap_base + offset, size); } if (!strcmp(argv[0], "peekMulti")) @@ -236,20 +214,7 @@ int argmain(int argc, char **argv) offsets[i] = meta.heap_base + parseStringToInt(argv[(i*2)+1]); sizes[i] = parseStringToInt(argv[(i*2)+2]); } - - u64 totalSize = 0; - for (int i = 0; i < itemCount; i++) - totalSize += sizes[i]; - - u8* out = malloc(sizeof(u8) * totalSize); - peekMulti(out, offsets, sizes, itemCount, totalSize); - if (usb) - { - response.size = totalSize; - response.data = &out[0]; - sendUsbResponse(response); - } - free(out); + peekMulti(offsets, sizes, itemCount); } if (!strcmp(argv[0], "peekAbsolute")) @@ -259,15 +224,7 @@ int argmain(int argc, char **argv) u64 offset = parseStringToInt(argv[1]); u64 size = parseStringToInt(argv[2]); - u8 data[size]; - - peek(data, offset, size); - if (usb) - { - response.size = size; - response.data = &data[0]; - sendUsbResponse(response); - } + peek(offset, size); } if (!strcmp(argv[0], "peekAbsoluteMulti")) @@ -284,20 +241,7 @@ int argmain(int argc, char **argv) offsets[i] = parseStringToInt(argv[(i*2)+1]); sizes[i] = parseStringToInt(argv[(i*2)+2]); } - - u64 totalSize = 0; - for (int i = 0; i < itemCount; i++) - totalSize += sizes[i]; - - u8* out = malloc(sizeof(u8) * totalSize); - peekMulti(out, offsets, sizes, itemCount, totalSize); - if (usb) - { - response.size = totalSize; - response.data = &out[0]; - sendUsbResponse(response); - } - free(out); + peekMulti(offsets, sizes, itemCount); } if (!strcmp(argv[0], "peekMain")) @@ -308,15 +252,7 @@ int argmain(int argc, char **argv) MetaData meta = getMetaData(); u64 offset = parseStringToInt(argv[1]); u64 size = parseStringToInt(argv[2]); - u8 data[size]; - - peek(data, meta.main_nso_base + offset, size); - if (usb) - { - response.size = size; - response.data = &data[0]; - sendUsbResponse(response); - } + peek(meta.main_nso_base + offset, size); } if (!strcmp(argv[0], "peekMainMulti")) @@ -335,20 +271,7 @@ int argmain(int argc, char **argv) offsets[i] = meta.main_nso_base + parseStringToInt(argv[(i*2)+1]); sizes[i] = parseStringToInt(argv[(i*2)+2]); } - - u64 totalSize = 0; - for (int i = 0; i < itemCount; i++) - totalSize += sizes[i]; - - u8* out = malloc(sizeof(u8) * totalSize); - peekMulti(out, offsets, sizes, itemCount, totalSize); - if (usb) - { - response.size = totalSize; - response.data = &out[0]; - sendUsbResponse(response); - } - free(out); + peekMulti(offsets, sizes, itemCount); } //poke
@@ -723,14 +646,7 @@ int argmain(int argc, char **argv) u64 solved = followMainPointer(jumps, count); solved += finalJump; - u8 data[size]; - peek(data, solved, size); - if (usb) - { - response.size = size; - response.data = &data[0]; - sendUsbResponse(response); - } + peek(solved, size); } // pointerPeekMulti split by asterisks (*) @@ -779,19 +695,7 @@ int argmain(int argc, char **argv) lastIndex = currIndex; } - u64 totalSize = 0; - for (int i = 0; i < itemCount; i++) - totalSize += sizes[i]; - - u8* out = malloc(sizeof(u8) * totalSize); - peekMulti(out, offsets, sizes, itemCount, totalSize); - if (usb) - { - response.size = totalSize; - response.data = &out[0]; - sendUsbResponse(response); - } - free(out); + peekMulti(offsets, sizes, itemCount); } // pointerPoke @@ -1404,8 +1308,8 @@ bool isUSB() { fscanf(config, "%[^\n]", str); fclose(config); - if (strcmp(strlwr(str), "wifi") == 0) - return false; + if (strcmp(strlwr(str), "usb") == 0) + return true; } - return true; + return false; }