diff --git a/include/backup.hpp b/include/backup.hpp index 9c96adf..e1cbc3f 100644 --- a/include/backup.hpp +++ b/include/backup.hpp @@ -2,7 +2,7 @@ #define BACKUP_HPP -void backup_account(); +bool backup_account(); #endif \ No newline at end of file diff --git a/include/screen.hpp b/include/screen.hpp index 6edc93e..778426c 100644 --- a/include/screen.hpp +++ b/include/screen.hpp @@ -8,6 +8,7 @@ void draw_menu_screen(int selected_menu_item); void draw_unlink_menu(); void draw_backup_menu(); void draw_overwrite_menu(const char* backup_path); - +void draw_error_menu(const char* error_message); +void draw_success_menu(const char* type, bool inkay = false); #endif \ No newline at end of file diff --git a/include/switch.hpp b/include/switch.hpp index 5b925d9..6ac56e3 100644 --- a/include/switch.hpp +++ b/include/switch.hpp @@ -2,7 +2,7 @@ #define SWITCH_HPP -void switch_account(const char* backup_file, const char* account_type); +bool switch_account(const char* backup_file, const char* account_type); #endif \ No newline at end of file diff --git a/include/unlink.hpp b/include/unlink.hpp index 93d156e..c6f1201 100644 --- a/include/unlink.hpp +++ b/include/unlink.hpp @@ -2,7 +2,7 @@ #define UNLINK_HPP -void unlink_account(); +bool unlink_account(); #endif \ No newline at end of file diff --git a/source/backup.cpp b/source/backup.cpp index 1b6cc52..beeed51 100644 --- a/source/backup.cpp +++ b/source/backup.cpp @@ -22,7 +22,6 @@ bool backup_confirm = false; void handle_cleanup(FILE* account, FILE* backup, char* buffer, bool is_error = false) { - OSSleepTicks(OSMillisecondsToTicks(5000)); OSEnableHomeButtonMenu(1); // Free the buffer. @@ -44,7 +43,6 @@ void handle_cleanup(FILE* account, FILE* backup, char* buffer, bool is_error = f } if (is_error) { - WHBLogPrint("---------------------------------------------------------"); draw_menu_screen(2); } } @@ -58,18 +56,11 @@ void write_backup(FILE* account, const std::string& backup_path, char* buffer) { // Open the backup file for writing. FILE *backup = fopen(backup_path.c_str(), "wb"); if (backup == NULL) { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrintf("Error opening backup file."); - WHBLogPrintf("%s", backup_path.c_str()); - WHBLogConsoleDraw(); + draw_error_menu("Error opening backup account.dat file!"); handle_cleanup(account, backup, buffer, true); return; } - // Print the backup path to the screen. - WHBLogPrintf("%s", backup_path.c_str()); - WHBLogConsoleDraw(); - // Open the backup file and write the account data to it. rewind(account); // Move the file pointer to the beginning. @@ -79,91 +70,55 @@ void write_backup(FILE* account, const std::string& backup_path, char* buffer) { // Close the backup file. fclose(backup); - WHBLogPrintf("Backup account.dat written.", backup_path); - WHBLogConsoleDraw(); - // Wait 5 seconds, then go back to the menu. - WHBLogConsoleSetColor(0x00990000); - WHBLogPrint("---------------------------------------------------------"); - WHBLogPrint("The account.dat was backed up successfully!"); - WHBLogPrint("The main menu will appear in 5 seconds..."); - WHBLogConsoleDraw(); - WHBLogPrint("---------------------------------------------------------"); - fclose(backup); + draw_success_menu("backup"); } -void backup_account() { - // Inform the user that the backup process has started. - WHBLogConsoleSetColor(0x00009900); - WHBLogPrintf("Backup: A Network ID backup will be created."); - WHBLogPrint("---------------------------------------------------------"); - WHBLogConsoleDraw(); - +bool backup_account() { // Check if the account.dat file exists. std::string backup_path; FILE *account = fopen(ACCOUNT_FILE.c_str(), "rb"); if (account == NULL) { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrintf("Error opening system account.dat file!"); - WHBLogConsoleDraw(); + draw_error_menu("Error opening system account.dat file!"); handle_cleanup(account, NULL, NULL, true); - return; + return false; } else { - WHBLogPrintf("System account.dat file opened."); - WHBLogConsoleDraw(); - std::string content; char *buffer = (char *)malloc(BUFFER_SIZE); if (buffer == NULL) { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrint("Error allocating memory!"); - WHBLogConsoleDraw(); + draw_error_menu("Error allocating memory!"); handle_cleanup(account, NULL, buffer, true); - return; + return false; } else { - WHBLogPrint("Memory was allocated successfully."); - WHBLogConsoleDraw(); // Read the entire file into a string. size_t bytesRead = 0; while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0) { content.append(buffer, bytesRead); } - WHBLogPrint("account.dat file read in memory."); - WHBLogConsoleDraw(); - bool network_account_found = false; if (content.find("account.nintendo.net") != std::string::npos) { // Nintendo Network ID is linked to the account. backup_path = NNID_BACKUP; - WHBLogPrint("Nintendo Network ID detected."); - WHBLogConsoleDraw(); network_account_found = true; } else if (content.find("pretendo-cdn.b-cdn.net") != std::string::npos) { // Pretendo Network ID is linked to the account. backup_path = PNID_BACKUP; - WHBLogPrint("Pretendo Network ID detected."); - WHBLogConsoleDraw(); network_account_found = true; } else { // The check failed, domain not accounted for? - WHBLogConsoleSetColor(0x99000000); - WHBLogPrint("Network ID detection failed!"); - WHBLogPrint("Is this user a local-only account?"); - WHBLogConsoleDraw(); + draw_error_menu("No network account found!"); handle_cleanup(account, NULL, buffer, true); + return false; } if (network_account_found) { // Check if the backup file exists. - WHBLogPrintf("Opening backup account.dat for writing.", backup_path.c_str()); - WHBLogConsoleDraw(); - std::ifstream ifile(backup_path); if (ifile) { backup_confirm = false; @@ -191,5 +146,6 @@ void backup_account() { } handle_cleanup(account, NULL, buffer, !backup_confirm); + return true; } } \ No newline at end of file diff --git a/source/main.cpp b/source/main.cpp index c3a3b23..abc590c 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -1,6 +1,7 @@ #include #include +#include #include #include #include @@ -158,21 +159,25 @@ int main() { if (button == VPAD_BUTTON_UP) { selected_option--; - if (selected_option < 0) { + if (selected_option < 0) selected_option = NUM_OPTIONS - 1; - } } else if (button == VPAD_BUTTON_DOWN) { selected_option++; - if (selected_option >= NUM_OPTIONS) { + if (selected_option >= NUM_OPTIONS) selected_option = 0; - } } else if (button == VPAD_BUTTON_A) { switch (selected_option) { case 0: - switch_account(NNID_BACKUP.c_str(), "Nintendo Network ID"); + if (switch_account(NNID_BACKUP.c_str(), "Nintendo Network ID")) { + OSForceFullRelaunch(); + SYSLaunchMenu(); + } break; case 1: - switch_account(PNID_BACKUP.c_str(), "Pretendo Network ID"); + if (switch_account(PNID_BACKUP.c_str(), "Pretendo Network ID")) { + OSForceFullRelaunch(); + SYSLaunchMenu(); + } break; case 2: while (WHBProcIsRunning()) { @@ -182,9 +187,8 @@ int main() { if (button == VPAD_BUTTON_A) { backup_account(); break; - } else if (button == VPAD_BUTTON_B) { + } else if (button == VPAD_BUTTON_B) break; - } } break; case 3: @@ -193,11 +197,13 @@ int main() { button = read_input(); if (button == VPAD_BUTTON_A) { - unlink_account(); + if (unlink_account()) { + OSForceFullRelaunch(); + SYSLaunchMenu(); + } break; - } else if (button == VPAD_BUTTON_B) { + } else if (button == VPAD_BUTTON_B) break; - } } break; } diff --git a/source/screen.cpp b/source/screen.cpp index 1381b30..1c98bbc 100644 --- a/source/screen.cpp +++ b/source/screen.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include #include @@ -19,12 +20,14 @@ void draw_background(int r, int g, int b, int a) { SDL_RenderClear(renderer); } + void draw_rectangle(int x, int y, int w, int h, int r, int g, int b, int a) { SDL_Rect rect = {x, y, w, h}; SDL_SetRenderDrawColor(renderer, r, g, b, a); SDL_RenderFillRect(renderer, &rect); } + void draw_text(const char* text, int x, int y, int size, SDL_Color color = {255, 255, 255, 255}) { void* font_data = nullptr; uint32_t font_size = 0; @@ -56,6 +59,7 @@ void draw_text(const char* text, int x, int y, int size, SDL_Color color = {255, TTF_CloseFont(font); } + int get_text_width(const char* text, int size) { void* font_data = nullptr; uint32_t font_size = 0; @@ -74,6 +78,7 @@ int get_text_width(const char* text, int size) { return width; } + void draw_screen_bars() { draw_rectangle(0, 0, 1920, 90, 125, 0, 125, 255); draw_text("Wii U Account Swap", 64, 10, 50); @@ -86,12 +91,14 @@ void draw_screen_bars() { draw_text(ACCOUNT_FILE.c_str(), 64, 1005, 40); } + void draw_confirm_button(const char* text) { draw_rectangle(64, 760, 896, 100, 100, 100, 255, 255); draw_rectangle(69, 765, 886, 90, 0, 0, 0, 255); draw_text(text, 93, 780, 50); } + void draw_menu_screen(int selected_menu_item) { draw_background(16, 16, 16, 255); draw_screen_bars(); @@ -116,6 +123,7 @@ void draw_menu_screen(int selected_menu_item) { SDL_RenderPresent(renderer); } + void draw_unlink_menu() { draw_background(16, 16, 16, 255); draw_screen_bars(); @@ -134,6 +142,7 @@ void draw_unlink_menu() { SDL_RenderPresent(renderer); } + void draw_backup_menu() { draw_background(16, 16, 16, 255); draw_screen_bars(); @@ -152,6 +161,7 @@ void draw_backup_menu() { SDL_RenderPresent(renderer); } + void draw_overwrite_menu(const char* backup_path) { draw_background(16, 16, 16, 255); draw_screen_bars(); @@ -160,9 +170,52 @@ void draw_overwrite_menu(const char* backup_path) { draw_text("This will overwrite the existing backup file:", 64, 270, 50); draw_text(backup_path, 64, 330, 50); - draw_text("Are you sure you want to overwrite this file?", 64, 390, 50); + + draw_text("Are you sure you want to overwrite this file?", 64, 440, 50); draw_confirm_button("Confirm Overwrite"); SDL_RenderPresent(renderer); +} + + +void draw_error_menu(const char* error_message) { + draw_background(0, 0, 30, 255); + draw_screen_bars(); + + draw_text("Error: An error has occurred!", 64, 160, 50, {176, 176, 176, 255}); + + draw_text(error_message, 64, 270, 50); + + draw_text("You will return to the main menu.", 64, 390, 50); + + SDL_RenderPresent(renderer); + OSSleepTicks(OSMillisecondsToTicks(5000)); +} + +void draw_success_menu(const char* type, bool inkay_configured = false) { + draw_background(0, 30, 0, 255); + draw_screen_bars(); + + draw_text("Success: The operation was successful!", 64, 160, 50, {176, 176, 176, 255}); + + if (strcmp(type, "backup") == 0) { + draw_text("Your account.dat file has been backed up.", 64, 270, 50); + draw_text("You will return to the main menu.", 64, 330, 50); + + } else if (strcmp(type, "unlink") == 0) { + draw_text("Your Network ID has been unlinked from this user.", 64, 270, 50); + draw_text("The Wii U will now reboot.", 64, 330, 50); + + } else if (strcmp(type, "switch") == 0) { + draw_text("Your account has been switched successfully.", 64, 270, 50); + draw_text("The Wii U will now reboot.", 64, 330, 50); + + if (inkay_configured) { + draw_text("Inkay was also configured automatically!", 64, 440, 50); + } + } + + SDL_RenderPresent(renderer); + OSSleepTicks(OSMillisecondsToTicks(5000)); } \ No newline at end of file diff --git a/source/switch.cpp b/source/switch.cpp index 7786fd7..92d9c20 100644 --- a/source/switch.cpp +++ b/source/switch.cpp @@ -34,8 +34,6 @@ void handle_cleanup(FILE* backup, const char* account_type, char* buffer, bool i // If there was an error, return to the menu. if (is_error) { // Print the main menu. - WHBLogPrint("---------------------------------------------------------"); - if (strcmp(account_type, "Nintendo Network ID")) draw_menu_screen(0); else if (strcmp(account_type, "Pretendo Network ID")) @@ -44,100 +42,55 @@ void handle_cleanup(FILE* backup, const char* account_type, char* buffer, bool i } -void switch_account(const char* backup_file, const char* account_type) { +bool switch_account(const char* backup_file, const char* account_type) { // Disable the HOME Button temporarily. OSEnableHomeButtonMenu(0); - // Print the account type and the switch message. - WHBLogConsoleSetColor(0x00009900); - WHBLogPrintf("Switch: You will be swapped to a %s.", account_type); - WHBLogPrint("---------------------------------------------------------"); - WHBLogConsoleDraw(); - - WHBLogPrintf("Switching account.dat to %s.", account_type); - WHBLogConsoleDraw(); - // Open the account.dat file and switch it to the specified account. FILE *backup = fopen(backup_file, "rb"); if (backup == NULL) { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrintf("Error opening %s account backup!", account_type); - WHBLogPrint("Have you made a backup of this account yet?"); - WHBLogConsoleDraw(); + draw_error_menu("Error opening backup account.dat file!"); handle_cleanup(backup, account_type, NULL, true); - return; + return false; } else { // Open the account.dat file for writing. - WHBLogPrintf("%s account backup opened.", account_type); - WHBLogConsoleDraw(); - char *buffer = (char *)malloc(BUFFER_SIZE); if (buffer == NULL) { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrint("Error allocating memory!"); - WHBLogConsoleDraw(); + draw_error_menu("Error allocating memory!"); handle_cleanup(backup, account_type, buffer, true); - return; + return false; } else { - WHBLogPrint("Memory was allocated successfully."); - WHBLogConsoleDraw(); - FILE *account = fopen(ACCOUNT_FILE.c_str(), "wb"); if (account == NULL) { - WHBLogConsoleSetColor(0x99000000); - WHBLogPrint("Error opening system account.dat file!"); - WHBLogConsoleDraw(); + draw_error_menu("Error opening system account.dat file!"); handle_cleanup(backup, account_type, buffer, true); - return; + return false; } 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); - WHBLogPrint("System account.dat file restored."); - // We'll attempt to automatically swap the network using Inkay's configuration. + bool inkay_configured = false; FILE *inkay = fopen(INKAY_CONFIG.c_str(), "wb"); - if (inkay == NULL) { - // If we can't open the file, we will move on. - WHBLogPrint("The Inkay config file wasn't found!"); - WHBLogPrint("Network will not be automatically swapped."); - WHBLogConsoleDraw(); - - } else { + if (inkay != NULL) { // Write the network configuration to the file. - WHBLogPrint("Inkay config file opened."); - WHBLogConsoleDraw(); - WHBLogPrintf("Swapping network to %s.", account_type); - - const char *inkayContent = "{\"storageitems\":{\"connect_to_network\":%d}}"; - fprintf(inkay, inkayContent, strcmp(account_type, "Pretendo Network ID") == 0 ? 1 : 0); + const char *inkay_content = "{\"storageitems\":{\"connect_to_network\":%d}}"; + fprintf(inkay, inkay_content, strcmp(account_type, "Pretendo Network ID") == 0 ? 1 : 0); fclose(inkay); inkay = NULL; - - WHBLogPrint("Inkay config file edited."); - WHBLogConsoleDraw(); + inkay_configured = true; } - // Inform the user that the switch was successful. - WHBLogConsoleSetColor(0x00990000); - WHBLogPrint("---------------------------------------------------------"); - WHBLogPrint("The account.dat was restored successfully!"); - WHBLogPrint("Your console will restart in 5 seconds..."); - WHBLogConsoleDraw(); + draw_success_menu("switch", inkay_configured); } } // Clean-up and exit. handle_cleanup(backup, account_type, buffer, false); - OSForceFullRelaunch(); - SYSLaunchMenu(); - deinitialize(); + return true; } } \ No newline at end of file diff --git a/source/unlink.cpp b/source/unlink.cpp index b241016..8bc3341 100644 --- a/source/unlink.cpp +++ b/source/unlink.cpp @@ -17,9 +17,10 @@ #include #include "main.hpp" +#include "screen.hpp" -void unlink_account() { +bool unlink_account() { // The default values to apply to the account.dat file. std::map default_values = { {"IsMiiUpdated", "1"}, @@ -58,19 +59,11 @@ void unlink_account() { {"MiiImageLastModifiedDate", "Sat, 01 Jan 2000 00:00:00 GMT"}, {"IsCommitted", "1"} }; - - // Inform the user that the unlink process has started. - WHBLogConsoleSetColor(0x00009900); - WHBLogPrintf("Unlinking: Default settings will be applied."); - WHBLogPrint("---------------------------------------------------------"); - WHBLogConsoleDraw(); // Read the entire file into a string. std::ifstream account_input(ACCOUNT_FILE); std::string file_contents((std::istreambuf_iterator(account_input)), std::istreambuf_iterator()); account_input.close(); - WHBLogPrint("System account.dat file read in memory."); - WHBLogConsoleDraw(); // Process each line in the string. std::istringstream file_contentstream(file_contents); @@ -85,28 +78,13 @@ void unlink_account() { file_contents += line + "\n"; } - WHBLogPrint("Account file in memory patched."); - WHBLogConsoleDraw(); - // Write the string back to the file. std::ofstream account_output(ACCOUNT_FILE); account_output << file_contents; account_output.close(); - WHBLogPrint("System account.dat file written."); - WHBLogConsoleDraw(); - // Inform the user that the unlink was successful. - WHBLogConsoleSetColor(0x00990000); - WHBLogPrint("---------------------------------------------------------"); - WHBLogPrint("The account.dat was unlinked successfully!"); - WHBLogPrint("Your console will restart in 5 seconds..."); - WHBLogConsoleDraw(); - WHBLogPrint("---------------------------------------------------------"); - - OSSleepTicks(OSMillisecondsToTicks(5000)); + draw_success_menu("unlink"); OSEnableHomeButtonMenu(1); - OSForceFullRelaunch(); - SYSLaunchMenu(); - deinitialize(); + return true; }