diff --git a/source/backup.cpp b/source/backup.cpp index c6ea7e9..e76c5de 100644 --- a/source/backup.cpp +++ b/source/backup.cpp @@ -59,6 +59,7 @@ 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) { draw_error_menu("Error opening backup account.dat file!"); handle_cleanup(account, backup, buffer, true); @@ -90,71 +91,67 @@ backup_account() draw_error_menu("Error opening system account.dat file!"); handle_cleanup(account, NULL, NULL, true); return false; + } - } else { - std::string content; - char *buffer = (char *)malloc(BUFFER_SIZE); - if (buffer == NULL) { - draw_error_menu("Error allocating memory!"); - handle_cleanup(account, NULL, buffer, true); - return false; - - } else { - // Read the entire file into a string. - size_t bytesRead = 0; - while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0) { - content.append(buffer, bytesRead); - } + std::string content; + char *buffer = (char *)malloc(BUFFER_SIZE); + if (buffer == NULL) { + draw_error_menu("Error allocating memory!"); + handle_cleanup(account, NULL, buffer, true); + return false; + } - 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; - 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; - network_account_found = true; - - } else { - // The check failed, domain not accounted for? - draw_error_menu("No network account found!"); - handle_cleanup(account, NULL, buffer, true); - return false; - } + // Read the entire file into a string. + size_t bytesRead = 0; + while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, account)) > 0) { + content.append(buffer, bytesRead); + } + + 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; + 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; + network_account_found = true; + } - if (network_account_found) { - // Check if the backup file exists. - std::ifstream ifile(backup_path); - if (ifile) { - backup_confirm = false; - - while (WHBProcIsRunning()) { - draw_overwrite_menu(backup_path.c_str()); - int button = read_input(); - - if (button == VPAD_BUTTON_A) { - backup_confirm = true; - break; - } else if (button == VPAD_BUTTON_B) { - break; - } - } - - } else { - backup_confirm = true; - } + if (!network_account_found) { + // The check failed, domain not accounted for? + draw_error_menu("No network account found!"); + handle_cleanup(account, NULL, buffer, true); + return false; + } + + // Check if the backup file exists. + std::ifstream ifile(backup_path); + if (ifile) { + backup_confirm = false; + + while (WHBProcIsRunning()) { + draw_overwrite_menu(backup_path.c_str()); + int button = read_input(); + + if (button == VPAD_BUTTON_A) { + backup_confirm = true; + break; + } else if (button == VPAD_BUTTON_B) { + break; } } - // Write the backup file. - if (backup_confirm) { - if (write_backup(account, backup_path, buffer)) - handle_cleanup(account, NULL, buffer, false); - return true; - } + } else { + backup_confirm = true; + } - handle_cleanup(account, NULL, buffer, !backup_confirm); + // Write the backup file. + if (backup_confirm) { + if (write_backup(account, backup_path, buffer)) + handle_cleanup(account, NULL, buffer, false); return true; } + + handle_cleanup(account, NULL, buffer, !backup_confirm); + return true; } \ No newline at end of file diff --git a/source/switch.cpp b/source/switch.cpp index c4a5687..6be65ea 100644 --- a/source/switch.cpp +++ b/source/switch.cpp @@ -56,44 +56,42 @@ switch_account(const char* backup_file, const char* account_type) handle_cleanup(backup, account_type, NULL, true); return false; } - else { - // Open the account.dat file for writing. - char *buffer = (char *)malloc(BUFFER_SIZE); - if (buffer == NULL) { - draw_error_menu("Error allocating memory!"); - handle_cleanup(backup, account_type, buffer, true); - return false; - } else { - FILE *account = fopen(ACCOUNT_FILE.c_str(), "wb"); - if (account == NULL) { - draw_error_menu("Error opening system account.dat file!"); - handle_cleanup(backup, account_type, buffer, true); - return false; + // Open the account.dat file for writing. + char *buffer = (char *)malloc(BUFFER_SIZE); + if (buffer == NULL) { + draw_error_menu("Error allocating memory!"); + handle_cleanup(backup, account_type, buffer, true); + return false; + } - } else { - size_t bytesRead = 0; - while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0) - fwrite(buffer, 1, bytesRead, account); - fclose(account); + FILE *account = fopen(ACCOUNT_FILE.c_str(), "wb"); + if (account == NULL) { + draw_error_menu("Error opening system account.dat file!"); + handle_cleanup(backup, account_type, buffer, true); + return false; + } - // 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) { - // Write the network configuration to the file. - 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; - inkay_configured = true; - } - draw_success_menu("switch", inkay_configured); - } - } - // Clean-up and exit. - handle_cleanup(backup, account_type, buffer, false); + size_t bytesRead = 0; + while ((bytesRead = fread(buffer, 1, BUFFER_SIZE, backup)) > 0) + fwrite(buffer, 1, bytesRead, account); + fclose(account); - return true; + // 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) { + // Write the network configuration to the file. + 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; + inkay_configured = true; } + draw_success_menu("switch", inkay_configured); + + // Clean-up and exit. + handle_cleanup(backup, account_type, buffer, false); + + return true; } \ No newline at end of file