From 51a98cac3cd5a1008c3e5038e097ff6c9042e478 Mon Sep 17 00:00:00 2001 From: Nightkingale <63483138+Nightkingale@users.noreply.github.com> Date: Wed, 10 Apr 2024 18:42:28 -0600 Subject: [PATCH] Tidy up code into separate scripts --- source/globals.h | 12 +++ source/main.cpp | 249 ++------------------------------------------- source/screens.cpp | 56 ++++++++++ source/screens.h | 9 ++ source/switch.cpp | 104 +++++++++++++++++++ source/switch.h | 7 ++ source/unlink.cpp | 107 +++++++++++++++++++ source/unlink.h | 7 ++ 8 files changed, 311 insertions(+), 240 deletions(-) create mode 100644 source/globals.h create mode 100644 source/screens.cpp create mode 100644 source/screens.h create mode 100644 source/switch.cpp create mode 100644 source/switch.h create mode 100644 source/unlink.cpp create mode 100644 source/unlink.h diff --git a/source/globals.h b/source/globals.h new file mode 100644 index 0000000..a785e4c --- /dev/null +++ b/source/globals.h @@ -0,0 +1,12 @@ +// globals.h +#ifndef GLOBALS_H +#define GLOBALS_H + +extern const char* accountFile; +extern const char* inkayConfig; +extern const int BUFFER_SIZE; + +void deinitialize(); +void initialize(); + +#endif // GLOBALS_H \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index 51ac138..3d282c1 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,30 +1,22 @@ -#include -#include -#include -#include -#include - -#include -#include -#include -#include - -#include #include #include #include #include -#include -#include +#include #include #include #include #include -#define BUFFER_SIZE 0x2000 +#include "globals.h" +#include "screens.h" +#include "switch.h" +#include "unlink.h" + const char* accountFile = "storage_mlc:/usr/save/system/act/8000000e/account.dat"; -const char* inkayConfig = "/vol/external01/wiiu/environments/aroma/plugins/config/inkay.json"; +const char* inkayConfig = "/vol/external01/wiiu/environments/aroma/plugins/config/inkay.json";\ +const int BUFFER_SIZE = 0x2000; void deinitialize() { @@ -51,233 +43,10 @@ void initialize() Mocha_MountFS("storage_mlc", NULL, "/vol/storage_mlc01"); } - -void printOnScreen(int line, const char* format, ...) -{ - char buffer[256]; - va_list args; - // Format the string into a buffer. - va_start(args, format); - vsnprintf(buffer, sizeof(buffer), format, args); - va_end(args); - // Print the string to the screen. - OSScreenPutFontEx(SCREEN_TV, 0, line, buffer); - OSScreenPutFontEx(SCREEN_DRC, 0, line, buffer); -} - -void printMainMenu() -{ - OSScreenClearBufferEx(SCREEN_TV, 0x4A198500); - OSScreenClearBufferEx(SCREEN_DRC, 0x4A198500); - - printOnScreen(0, "Wii U Account Swap (v1.0.0)"); - printOnScreen(1, "Created by Nightkingale"); - printOnScreen(2, "-----------------------------------------------------------"); - - printOnScreen(3, "Press A to switch to Nintendo Network ID."); - printOnScreen(4, "Press B to switch to Pretendo Network ID."); - printOnScreen(5, "Press X to unlink your account locally."); - printOnScreen(6, "Press HOME to exit."); - - OSScreenFlipBuffersEx(SCREEN_TV); - OSScreenFlipBuffersEx(SCREEN_DRC); -} - -void printWarningScreen() { - OSScreenClearBufferEx(SCREEN_TV, 0x4A198500); - OSScreenClearBufferEx(SCREEN_DRC, 0x4A198500); - - printOnScreen(0, "Warning: This will unlink your account locally!"); - printOnScreen(1, "This will not unlink your account from the server!"); - printOnScreen(2, "-----------------------------------------------------------"); - printOnScreen(3, "Your user can have a new account reattached!"); - printOnScreen(4, "You will be able to login again on this Wii U as normal."); - printOnScreen(5, "However, you will NOT be able to login to another Wii U!"); - printOnScreen(6, "-----------------------------------------------------------"); - printOnScreen(7, "Press A to confirm the unlink or B to cancel."); - - OSScreenFlipBuffersEx(SCREEN_TV); - OSScreenFlipBuffersEx(SCREEN_DRC); -} - -void unlinkAccount() { - std::map defaultValues = { - {"IsMiiUpdated", "1"}, - {"AccountId", ""}, - {"BirthYear", "0"}, - {"BirthMonth", "0"}, - {"BirthDay", "0"}, - {"Gender", "0"}, - {"IsMailAddressValidated", "0"}, - {"EmailAddress", ""}, - {"Country", "0"}, - {"SimpleAddressId", "0"}, - {"TimeZoneId", ""}, - {"UtcOffset", "0"}, - {"PrincipalId", "0"}, - {"NfsPassword", ""}, - {"EciVirtualAccount", ""}, - {"NeedsToDownloadMiiImage", "0"}, - {"MiiImageUrl", ""}, - {"AccountPasswordHash", "0000000000000000000000000000000000000000000000000000000000000000"}, - {"IsPasswordCacheEnabled", "0"}, - {"AccountPasswordCache", "0000000000000000000000000000000000000000000000000000000000000000"}, - {"NnasType", "0"}, - {"NfsType", "0"}, - {"NfsNo", "1"}, - {"NnasSubDomain", ""}, - {"NnasNfsEnv", "L1"}, - {"IsPersistentIdUploaded", "0"}, - {"IsConsoleAccountInfoUploaded", "0"}, - {"LastAuthenticationResult", ""}, - {"StickyAccountId", ""}, - {"NextAccountId", ""}, - {"StickyPrincipalId", "0"}, - {"IsServerAccountDeleted", "0"}, - {"ServerAccountStatus", "1"}, - {"MiiImageLastModifiedDate", "Sat, 01 Jan 2000 00:00:00 GMT"}, - {"IsCommitted", "1"} - }; - - WHBLogPrintf("Unlinking: Default settings will be applied."); - WHBLogPrintf("%s", accountFile); - WHBLogPrint("----------------------------------------"); - WHBLogConsoleDraw(); - - // Read the entire file into a string. - std::ifstream inFile(accountFile); - std::string fileContents((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); - inFile.close(); - WHBLogPrint("System account.dat file is in memory!"); - WHBLogConsoleDraw(); - - // Process each line in the string. - std::istringstream iss(fileContents); - std::string line; - while (std::getline(iss, line)) { - size_t pos = line.find('='); - if (pos != std::string::npos) { - std::string key = line.substr(0, pos); - if (defaultValues.count(key) > 0) { - line = key + "=" + defaultValues[key]; - } - } - fileContents += line + "\n"; - } - WHBLogPrint("Account file in memory was patched!"); - WHBLogConsoleDraw(); - - // Write the string back to the file. - std::ofstream outFile(accountFile); - outFile << fileContents; - outFile.close(); - WHBLogPrint("System account.dat file was saved!"); - WHBLogConsoleDraw(); - - WHBLogConsoleSetColor(0x00990000); - WHBLogPrint("----------------------------------------"); - WHBLogPrint("The account.dat was unlinked successfully!"); - WHBLogPrint("Your console will restart in 3 seconds..."); - WHBLogConsoleDraw(); - - OSSleepTicks(OSMillisecondsToTicks(3000)); - OSForceFullRelaunch(); - SYSLaunchMenu(); - deinitialize(); -} - -void switchAccount(const char* backupFile, const char* accountType) -{ - WHBLogPrintf("Switching to %s...", accountType); - WHBLogPrintf("Source: %s", backupFile); - WHBLogPrintf("%s", accountFile); - WHBLogPrint("----------------------------------------"); - WHBLogConsoleDraw(); - - WHBLogPrintf("Switching account.dat file to %s.", accountType); - WHBLogConsoleDraw(); - // Open the account.dat file and switch it to the specified account. - FILE *backup = fopen(backupFile, "rb"); - if (backup == NULL) - { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrintf("Error opening %s account backup!", accountType); - WHBLogConsoleDraw(); - } - else - { - WHBLogPrintf("%s account backup opened!", accountType); - WHBLogConsoleDraw(); - char *buffer = (char *)malloc(BUFFER_SIZE); - if (buffer == NULL) - { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrint("Error allocating memory!"); - WHBLogConsoleDraw(); - } - else - { - WHBLogPrint("Memory was allocated successfully!"); - WHBLogConsoleDraw(); - - FILE *account = fopen(accountFile, "wb"); - if (account == NULL) - { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrint("Error opening system account.dat file!"); - WHBLogConsoleDraw(); - } - else - { - WHBLogPrint("System account.dat file opened!"); - WHBLogConsoleDraw(); - size_t bytesRead = 0; - while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0) - { - fwrite(buffer, 1, bytesRead, account); - } - fclose(account); - WHBLogConsoleSetColor(0x00990000); - WHBLogPrint("System account.dat file was saved!"); - // We'll attempt to automatically swap the network using Inkay's configuration. - FILE *inkay = fopen(inkayConfig, "wb"); - if (inkay == NULL) - { - // If we can't open the file, we will move on. - WHBLogPrint("Error opening Inkay config file!"); - WHBLogPrint("Network will not be automatically swapped."); - WHBLogConsoleDraw(); - } - else - { - WHBLogPrint("Inkay config file opened!"); - WHBLogConsoleDraw(); - WHBLogPrintf("Swapping network to %s!", accountType); - const char *inkayContent = "{\"storageitems\":{\"connect_to_network\":%d}}"; - fprintf(inkay, inkayContent, strcmp(accountType, "Pretendo Network ID") == 0 ? 1 : 0); - fclose(inkay); - WHBLogPrint("Inkay config file was saved!"); - WHBLogConsoleDraw(); - } - WHBLogPrint("----------------------------------------"); - WHBLogPrint("The account.dat was restored successfully!"); - WHBLogPrint("Your console will restart in 3 seconds..."); - WHBLogConsoleDraw(); - } - free(buffer); - } - fclose(backup); - } - OSSleepTicks(OSMillisecondsToTicks(3000)); - OSForceFullRelaunch(); - SYSLaunchMenu(); - deinitialize(); -} - int main() { initialize(); - // Set some variables for the Wii U GamePad. + VPADStatus input; VPADReadError error; diff --git a/source/screens.cpp b/source/screens.cpp new file mode 100644 index 0000000..4040130 --- /dev/null +++ b/source/screens.cpp @@ -0,0 +1,56 @@ +#include +#include + +#include +#include +#include + + +void printOnScreen(int line, const char* format, ...) +{ + char buffer[256]; + va_list args; + // Format the string into a buffer. + va_start(args, format); + vsnprintf(buffer, sizeof(buffer), format, args); + va_end(args); + // Print the string to the screen. + OSScreenPutFontEx(SCREEN_TV, 0, line, buffer); + OSScreenPutFontEx(SCREEN_DRC, 0, line, buffer); +} + +void printMainMenu() +{ + OSScreenClearBufferEx(SCREEN_TV, 0x4A198500); + OSScreenClearBufferEx(SCREEN_DRC, 0x4A198500); + + printOnScreen(0, "Wii U Account Swap (v1.0.0)"); + printOnScreen(1, "Created by Nightkingale"); + printOnScreen(2, "-----------------------------------------------------------"); + printOnScreen(3, "Press A to switch to Nintendo Network ID."); + printOnScreen(4, "Press B to switch to Pretendo Network ID."); + printOnScreen(5, "Press X to unlink your account locally."); + printOnScreen(6, "Press HOME to exit."); + printOnScreen(7, "-----------------------------------------------------------"); + + OSScreenFlipBuffersEx(SCREEN_TV); + OSScreenFlipBuffersEx(SCREEN_DRC); +} + +void printWarningScreen() +{ + OSScreenClearBufferEx(SCREEN_TV, 0x4A198500); + OSScreenClearBufferEx(SCREEN_DRC, 0x4A198500); + + printOnScreen(0, "Warning: This will unlink your account locally!"); + printOnScreen(1, "This will not unlink your account from the server!"); + printOnScreen(2, "-----------------------------------------------------------"); + printOnScreen(3, "Your user can have a new account reattached!"); + printOnScreen(4, "You will be able to login again on this Wii U as normal."); + printOnScreen(5, "However, you will NOT be able to login to another Wii U!"); + printOnScreen(6, "-----------------------------------------------------------"); + printOnScreen(7, "Press A to confirm the unlink or B to cancel."); + + OSScreenFlipBuffersEx(SCREEN_TV); + OSScreenFlipBuffersEx(SCREEN_DRC); +} \ No newline at end of file diff --git a/source/screens.h b/source/screens.h new file mode 100644 index 0000000..3b13dd0 --- /dev/null +++ b/source/screens.h @@ -0,0 +1,9 @@ +// screens.h +#ifndef SCREENS_H +#define SCREENS_H + +void printOnScreen(int line, const char* format, ...); +void printMainMenu(); +void printWarningScreen(); + +#endif // SCREENS_H \ No newline at end of file diff --git a/source/switch.cpp b/source/switch.cpp new file mode 100644 index 0000000..351b284 --- /dev/null +++ b/source/switch.cpp @@ -0,0 +1,104 @@ +#include + +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "globals.h" + + +void switchAccount(const char* backupFile, const char* accountType) +{ + WHBLogPrintf("Switching to %s...", accountType); + WHBLogPrintf("Source: %s", backupFile); + WHBLogPrintf("%s", accountFile); + WHBLogPrint("----------------------------------------"); + WHBLogConsoleDraw(); + + WHBLogPrintf("Switching account.dat file to %s.", accountType); + WHBLogConsoleDraw(); + // Open the account.dat file and switch it to the specified account. + FILE *backup = fopen(backupFile, "rb"); + if (backup == NULL) + { + WHBLogConsoleSetColor(0x99000000); + WHBLogPrintf("Error opening %s account backup!", accountType); + WHBLogConsoleDraw(); + } + else + { + WHBLogPrintf("%s account backup opened!", accountType); + WHBLogConsoleDraw(); + char *buffer = (char *)malloc(BUFFER_SIZE); + if (buffer == NULL) + { + WHBLogConsoleSetColor(0x99000000); + WHBLogPrint("Error allocating memory!"); + WHBLogConsoleDraw(); + } + else + { + WHBLogPrint("Memory was allocated successfully!"); + WHBLogConsoleDraw(); + + FILE *account = fopen(accountFile, "wb"); + if (account == NULL) + { + WHBLogConsoleSetColor(0x99000000); + WHBLogPrint("Error opening system account.dat file!"); + WHBLogConsoleDraw(); + } + else + { + WHBLogPrint("System account.dat file opened!"); + WHBLogConsoleDraw(); + size_t bytesRead = 0; + while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0) + { + fwrite(buffer, 1, bytesRead, account); + } + fclose(account); + WHBLogConsoleSetColor(0x00990000); + WHBLogPrint("System account.dat file was saved!"); + // We'll attempt to automatically swap the network using Inkay's configuration. + FILE *inkay = fopen(inkayConfig, "wb"); + if (inkay == NULL) + { + // If we can't open the file, we will move on. + WHBLogPrint("Error opening Inkay config file!"); + WHBLogPrint("Network will not be automatically swapped."); + WHBLogConsoleDraw(); + } + else + { + WHBLogPrint("Inkay config file opened!"); + WHBLogConsoleDraw(); + WHBLogPrintf("Swapping network to %s!", accountType); + const char *inkayContent = "{\"storageitems\":{\"connect_to_network\":%d}}"; + fprintf(inkay, inkayContent, strcmp(accountType, "Pretendo Network ID") == 0 ? 1 : 0); + fclose(inkay); + WHBLogPrint("Inkay config file was saved!"); + WHBLogConsoleDraw(); + } + WHBLogPrint("----------------------------------------"); + WHBLogPrint("The account.dat was restored successfully!"); + WHBLogPrint("Your console will restart in 3 seconds..."); + WHBLogConsoleDraw(); + } + free(buffer); + } + fclose(backup); + } + OSSleepTicks(OSMillisecondsToTicks(3000)); + OSForceFullRelaunch(); + SYSLaunchMenu(); + deinitialize(); +} \ No newline at end of file diff --git a/source/switch.h b/source/switch.h new file mode 100644 index 0000000..09386fe --- /dev/null +++ b/source/switch.h @@ -0,0 +1,7 @@ +// switch.h +#ifndef SWITCH_H +#define SWITCH_H + +void switchAccount(const char* backupFile, const char* accountType); + +#endif // SWITCH_H \ No newline at end of file diff --git a/source/unlink.cpp b/source/unlink.cpp new file mode 100644 index 0000000..e2f9237 --- /dev/null +++ b/source/unlink.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include +#include + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "globals.h" + + +void unlinkAccount() { + std::map defaultValues = { + {"IsMiiUpdated", "1"}, + {"AccountId", ""}, + {"BirthYear", "0"}, + {"BirthMonth", "0"}, + {"BirthDay", "0"}, + {"Gender", "0"}, + {"IsMailAddressValidated", "0"}, + {"EmailAddress", ""}, + {"Country", "0"}, + {"SimpleAddressId", "0"}, + {"TimeZoneId", ""}, + {"UtcOffset", "0"}, + {"PrincipalId", "0"}, + {"NfsPassword", ""}, + {"EciVirtualAccount", ""}, + {"NeedsToDownloadMiiImage", "0"}, + {"MiiImageUrl", ""}, + {"AccountPasswordHash", "0000000000000000000000000000000000000000000000000000000000000000"}, + {"IsPasswordCacheEnabled", "0"}, + {"AccountPasswordCache", "0000000000000000000000000000000000000000000000000000000000000000"}, + {"NnasType", "0"}, + {"NfsType", "0"}, + {"NfsNo", "1"}, + {"NnasSubDomain", ""}, + {"NnasNfsEnv", "L1"}, + {"IsPersistentIdUploaded", "0"}, + {"IsConsoleAccountInfoUploaded", "0"}, + {"LastAuthenticationResult", ""}, + {"StickyAccountId", ""}, + {"NextAccountId", ""}, + {"StickyPrincipalId", "0"}, + {"IsServerAccountDeleted", "0"}, + {"ServerAccountStatus", "1"}, + {"MiiImageLastModifiedDate", "Sat, 01 Jan 2000 00:00:00 GMT"}, + {"IsCommitted", "1"} + }; + + WHBLogPrintf("Unlinking: Default settings will be applied."); + WHBLogPrintf("%s", accountFile); + WHBLogPrint("----------------------------------------"); + WHBLogConsoleDraw(); + + // Read the entire file into a string. + std::ifstream inFile(accountFile); + std::string fileContents((std::istreambuf_iterator(inFile)), std::istreambuf_iterator()); + inFile.close(); + WHBLogPrint("System account.dat file is in memory!"); + WHBLogConsoleDraw(); + + // Process each line in the string. + std::istringstream iss(fileContents); + std::string line; + while (std::getline(iss, line)) { + size_t pos = line.find('='); + if (pos != std::string::npos) { + std::string key = line.substr(0, pos); + if (defaultValues.count(key) > 0) { + line = key + "=" + defaultValues[key]; + } + } + fileContents += line + "\n"; + } + WHBLogPrint("Account file in memory was patched!"); + WHBLogConsoleDraw(); + + // Write the string back to the file. + std::ofstream outFile(accountFile); + outFile << fileContents; + outFile.close(); + WHBLogPrint("System account.dat file was saved!"); + WHBLogConsoleDraw(); + + WHBLogConsoleSetColor(0x00990000); + WHBLogPrint("----------------------------------------"); + WHBLogPrint("The account.dat was unlinked successfully!"); + WHBLogPrint("Your console will restart in 3 seconds..."); + WHBLogConsoleDraw(); + + OSSleepTicks(OSMillisecondsToTicks(3000)); + OSForceFullRelaunch(); + SYSLaunchMenu(); + deinitialize(); +} diff --git a/source/unlink.h b/source/unlink.h new file mode 100644 index 0000000..8341a51 --- /dev/null +++ b/source/unlink.h @@ -0,0 +1,7 @@ +// unlink.h +#ifndef UNLINK_H +#define UNLINK_H + +void unlinkAccount(); + +#endif // UNLINK_H \ No newline at end of file