1
0
Fork 0
mirror of https://github.com/DarkMatterCore/nxdumptool.git synced 2024-11-12 21:46:39 +00:00
nxdumptool/source/gamecard_tab.cpp
Pablo Curiel 32c097c055 [ci skip] Add GameCardImageDumpTask and FileWriter classes
GameCardImageDumpTask is a derived class of DataTransferTask, and it's designed to dump a gamecard image using the options selected by the user (which must be passed from a GameCardImageDumpOptionsFrame object). It uses std::optional<std::string> as its return type -- the idea behind this is to return error strings that may later be displayed by an ErrorFrame during the dump process (views not yet implemented).

FileWriter is a class that encapsulates write operations to different storage mediums (SD card, USB host and UMS devices), based on the provided input path. It is used by GameCardImageDumpTask to painlessly write data to the right output storage without explicitly having to implement multiple code paths for all storage types as part of the actual dump code. Furthermore, FileWriter also supports writing split files to FAT-formatted UMS devices if an output file is >= 4 GiB -- part file handling is completely abstracted away from any callers.

Other changes include:

* AsyncTask: rename all class methods to use PascalCase naming.
* AsyncTask: rename get() -> GetResult().

* DataTransferTask: reflect the changes made to AsyncTask.
* DataTransferTask: pause the RepeatingTask right after LoopCallback() returns true instead of pausing it in the cancel/post-execute callbacks.
* DataTransferTask: add private FormatTimeString() method.
* DataTransferTask: remove superfluous override for DoInBackground() -- classes derived from DataTransferTask must provide it on their own, anyway.
* DataTransferTask: add public GetDurationString() method.

* defines: update FAT32_FILESIZE_LIMIT macro to use UINT32_MAX.
* defines: add CONCATENATION_FILE_PART_SIZE macro (used by the new FileWriter class).

* DownloadTask: reflect the changes made to AsyncTask.

* DumpOptionsFrame: file extension is no longer stored as a class member, nor required by the class constructor.
* DumpOptionsFrame: change the return type for GetOutputFilePath() to bool. The method now saves its output to a variable passed by reference.

* GameCardImageDumpOptionsFrame: reflect the changes made to DumpOptionsFrame.

* i18n: update localization strings where applicable.

* nxdt_utils: fix a potential buffer overflow in utilsGetFileSystemStatsByPath().

* OptionsTab: reflect the changes made to AsyncTask.

* usb: add const qualifier to the input buffer required by usbSendFileData().
* usb: add const qualifier to the input buffer required by usbSendNspHeader().
2024-04-25 01:49:04 +02:00

309 lines
15 KiB
C++

/*
* gamecard_tab.cpp
*
* Copyright (c) 2020-2024, DarkMatterCore <pabloacurielz@gmail.com>.
*
* This file is part of nxdumptool (https://github.com/DarkMatterCore/nxdumptool).
*
* nxdumptool is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* nxdumptool is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <nxdt_utils.h>
#include <gamecard_tab.hpp>
#include <titles_tab.hpp>
#include <gamecard_image_dump_options_frame.hpp>
namespace i18n = brls::i18n; /* For getStr(). */
using namespace i18n::literals; /* For _i18n. */
namespace nxdt::views
{
GameCardTab::GameCardTab(RootView *root_view) : LayeredErrorFrame("gamecard_tab/error_frame/not_inserted"_i18n), root_view(root_view)
{
/* Set custom spacing. */
this->list->setSpacing(this->list->getSpacing() / 2);
this->list->setMarginBottom(20);
/* Subscribe to the gamecard status event. */
this->gc_status_task_sub = this->root_view->RegisterGameCardTaskListener([this](GameCardStatus gc_status) {
/* Process gamecard status. */
this->ProcessGameCardStatus(gc_status);
});
/* Process gamecard status. */
this->ProcessGameCardStatus(GameCardStatus_NotInserted);
}
GameCardTab::~GameCardTab()
{
/* Unregister task listener. */
this->root_view->UnregisterGameCardTaskListener(this->gc_status_task_sub);
}
void GameCardTab::ProcessGameCardStatus(GameCardStatus gc_status)
{
/* Switch to the error layer if gamecard info hasn't been loaded. */
if (gc_status < GameCardStatus_InsertedAndInfoLoaded) this->SwitchLayerView(true);
switch(gc_status)
{
case GameCardStatus_NotInserted:
this->error_frame->SetMessage("gamecard_tab/error_frame/not_inserted"_i18n);
break;
case GameCardStatus_Processing:
this->error_frame->SetMessage("gamecard_tab/error_frame/processing"_i18n);
break;
case GameCardStatus_NoGameCardPatchEnabled:
this->error_frame->SetMessage("gamecard_tab/error_frame/nogc_enabled"_i18n);
break;
case GameCardStatus_LotusAsicFirmwareUpdateRequired:
this->error_frame->SetMessage("gamecard_tab/error_frame/lafw_update_required"_i18n);
break;
case GameCardStatus_InsertedAndInfoNotLoaded:
this->error_frame->SetMessage(i18n::getStr("gamecard_tab/error_frame/info_not_loaded"_i18n, GITHUB_NEW_ISSUE_URL));
break;
case GameCardStatus_InsertedAndInfoLoaded:
/* Update list and switch to it. */
this->PopulateList();
break;
default:
break;
}
/* Update internal gamecard status. */
this->gc_status = gc_status;
}
std::string GameCardTab::GetFormattedSizeString(GameCardSizeFunc func)
{
u64 size = 0;
char strbuf[0x40] = {0};
func(&size);
utilsGenerateFormattedSizeString(static_cast<double>(size), strbuf, sizeof(strbuf));
return std::string(strbuf);
}
std::string GameCardTab::GetCardIdSetString(FsGameCardIdSet *card_id_set)
{
char card_id_set_str[0x20] = {0};
utilsGenerateHexString(card_id_set_str, sizeof(card_id_set_str), &(card_id_set->id1), sizeof(card_id_set->id1), true);
card_id_set_str[8] = ' ';
utilsGenerateHexString(card_id_set_str + 9, sizeof(card_id_set_str) - 9, &(card_id_set->id2), sizeof(card_id_set->id2), true);
card_id_set_str[17] = ' ';
utilsGenerateHexString(card_id_set_str + 18, sizeof(card_id_set_str) - 18, &(card_id_set->id3), sizeof(card_id_set->id3), true);
return std::string(card_id_set_str);
}
void GameCardTab::PopulateList(void)
{
TitleApplicationMetadata **app_metadata = nullptr;
u32 app_metadata_count = 0;
GameCardHeader card_header = {0};
GameCardInfo card_info = {0};
FsGameCardIdSet card_id_set = {0};
char *raw_filename = nullptr;
bool update_focused_view = this->IsListItemFocused();
int focus_stack_index = this->GetFocusStackViewIndex();
/* Clear list. */
this->list->clear();
this->list->invalidate(true);
/* Information about how to handle HOS launch errors. */
/* TODO: remove this if we ever find a way to fix this issue. */
FocusableLabel *launch_error_info = new FocusableLabel(true, false, brls::LabelStyle::DESCRIPTION, "gamecard_tab/list/launch_error_info"_i18n, true);
launch_error_info->setHorizontalAlign(NVG_ALIGN_CENTER);
this->list->addView(launch_error_info);
/* Retrieve gamecard application metadata. */
app_metadata = titleGetGameCardApplicationMetadataEntries(&app_metadata_count);
if (app_metadata)
{
/* Display the applications that are part of the inserted gamecard. */
this->list->addView(new brls::Header("gamecard_tab/list/user_titles/header"_i18n));
/* Information about how to handle user titles. */
brls::Label *user_titles_info = new brls::Label(brls::LabelStyle::DESCRIPTION, i18n::getStr("gamecard_tab/list/user_titles/info"_i18n, \
"root_view/tabs/user_titles"_i18n), true);
user_titles_info->setHorizontalAlign(NVG_ALIGN_CENTER);
this->list->addView(user_titles_info);
/* Populate list. */
for(u32 i = 0; i < app_metadata_count; i++)
{
TitlesTabItem *title = new TitlesTabItem(app_metadata[i], false, false);
title->unregisterAction(brls::Key::A);
this->list->addView(title);
}
/* Free application metadata array. */
free(app_metadata);
}
/* Populate gamecard properties table. */
this->list->addView(new brls::Header("gamecard_tab/list/properties_table/header"_i18n));
FocusableTable *properties_table = new FocusableTable(true, false);
brls::TableRow *capacity = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/capacity"_i18n);
brls::TableRow *total_size = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/total_size"_i18n);
brls::TableRow *trimmed_size = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/trimmed_size"_i18n);
brls::TableRow *update_version = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/update_version"_i18n);
brls::TableRow *lafw_version = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/lafw_version"_i18n);
brls::TableRow *sdk_version = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/sdk_version"_i18n);
brls::TableRow *compatibility_type = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/compatibility_type"_i18n);
brls::TableRow *package_id = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/package_id"_i18n);
brls::TableRow *card_id_set_row = properties_table->addRow(brls::TableRowType::BODY, "gamecard_tab/list/properties_table/card_id_set"_i18n);
capacity->setValue(this->GetFormattedSizeString(&gamecardGetRomCapacity));
total_size->setValue(this->GetFormattedSizeString(&gamecardGetTotalSize));
trimmed_size->setValue(this->GetFormattedSizeString(&gamecardGetTrimmedSize));
gamecardGetHeader(&card_header);
gamecardGetPlaintextCardInfoArea(&card_info);
gamecardGetCardIdSet(&card_id_set);
const SystemVersion upp_version = card_info.upp_version.system_version;
/* TODO: move somewhere else? */
if (upp_version.major == 0 && upp_version.minor == 0)
{
std::string upp_version_display = "";
switch(upp_version.micro)
{
case 0: /* v450 / 0.0.0-450 */
upp_version_display = "1.0.0";
break;
case 1: /* v65796 / 0.0.1-260 */
upp_version_display = "2.0.0";
break;
case 2: /* v131162 / 0.0.2-90 */
upp_version_display = "2.1.0";
break;
case 3: /* v196628 / 0.0.3-20 */
upp_version_display = "2.2.0";
break;
case 4: /* v262164 / 0.0.4-20 */
upp_version_display = "2.3.0";
break;
default:
break;
}
if (upp_version_display != "")
{
update_version->setValue(fmt::format("{} ({}.{}.{}-{}) (v{})", upp_version_display, upp_version.major, upp_version.minor, upp_version.micro, \
upp_version.relstep, upp_version.value));
} else {
update_version->setValue(fmt::format("{}.{}.{}-{} (v{})", upp_version.major, upp_version.minor, upp_version.micro, \
upp_version.relstep, upp_version.value));
}
} else {
update_version->setValue(fmt::format("{}.{}.{}-{}.{} (v{})", upp_version.major, upp_version.minor, upp_version.micro, \
upp_version.major_relstep, upp_version.minor_relstep, upp_version.value));
}
u64 fw_version = card_info.fw_version;
lafw_version->setValue(fmt::format("{} ({})", fw_version, fw_version >= GameCardFwVersion_Count ? "generic/unknown"_i18n : gamecardGetRequiredHosVersionString(fw_version)));
const SdkAddOnVersion fw_mode = card_info.fw_mode.sdk_addon_version;
sdk_version->setValue(fmt::format("{}.{}.{}-{} (v{})", fw_mode.major, fw_mode.minor, fw_mode.micro, fw_mode.relstep, fw_mode.value));
u8 compat_type = card_info.compatibility_type;
compatibility_type->setValue(fmt::format("{} ({})", \
compat_type >= GameCardCompatibilityType_Count ? "generic/unknown"_i18n : gamecardGetCompatibilityTypeString(compat_type), \
compat_type));
char package_id_str[0x11] = {0};
utilsGenerateHexString(package_id_str, sizeof(package_id_str), card_header.package_id, sizeof(card_header.package_id), true);
package_id->setValue(std::string(package_id_str));
card_id_set_row->setValue(this->GetCardIdSetString(&card_id_set));
this->list->addView(properties_table);
/* ListItem elements. */
this->list->addView(new brls::Header("gamecard_tab/list/dump_options"_i18n));
raw_filename = titleGenerateGameCardFileName(TitleNamingConvention_Full, TitleFileNameIllegalCharReplaceType_None);
this->raw_filename_full = std::string(raw_filename);
if (raw_filename) free(raw_filename);
raw_filename = titleGenerateGameCardFileName(TitleNamingConvention_IdAndVersionOnly, TitleFileNameIllegalCharReplaceType_None);
this->raw_filename_id_only = std::string(raw_filename);
if (raw_filename) free(raw_filename);
raw_filename = nullptr;
brls::ListItem *dump_card_image = new brls::ListItem("gamecard_tab/list/dump_card_image/label"_i18n, "gamecard_tab/list/dump_card_image/description"_i18n);
dump_card_image->getClickEvent()->subscribe([this](brls::View *view) {
/* Display gamecard image dump options. */
std::string& raw_filename = (configGetInteger("naming_convention") == TitleNamingConvention_Full ? raw_filename_full : raw_filename_id_only);
brls::Application::pushView(new GameCardImageDumpOptionsFrame(this->root_view, raw_filename), brls::ViewAnimation::SLIDE_LEFT);
});
this->list->addView(dump_card_image);
this->list->addView(new brls::ListItemGroupSpacing(true));
brls::Label *advanced_disclaimer = new brls::Label(brls::LabelStyle::DESCRIPTION, "gamecard_tab/list/advanced_disclaimer"_i18n, true);
advanced_disclaimer->setHorizontalAlign(NVG_ALIGN_CENTER);
this->list->addView(advanced_disclaimer);
brls::ListItem *dump_initial_data = new brls::ListItem("gamecard_tab/list/dump_initial_data/label"_i18n, "gamecard_tab/list/dump_initial_data/description"_i18n);
this->list->addView(dump_initial_data);
brls::ListItem *dump_certificate = new brls::ListItem("gamecard_tab/list/dump_certificate/label"_i18n, i18n::getStr("gamecard_tab/list/dump_certificate/description", GAMECARD_CERT_OFFSET / GAMECARD_PAGE_SIZE));
this->list->addView(dump_certificate);
brls::ListItem *dump_card_id_set = new brls::ListItem("gamecard_tab/list/dump_card_id_set/label"_i18n, "gamecard_tab/list/dump_card_id_set/description"_i18n);
this->list->addView(dump_card_id_set);
brls::ListItem *dump_card_uid = new brls::ListItem("gamecard_tab/list/dump_card_uid/label"_i18n, "gamecard_tab/list/dump_card_uid/description"_i18n);
this->list->addView(dump_card_uid);
brls::ListItem *dump_header = new brls::ListItem("gamecard_tab/list/dump_header/label"_i18n, i18n::getStr("gamecard_tab/list/dump_header/description", 0));
this->list->addView(dump_header);
brls::ListItem *dump_plaintext_cardinfo = new brls::ListItem("gamecard_tab/list/dump_plaintext_cardinfo/label"_i18n, "gamecard_tab/list/dump_plaintext_cardinfo/description"_i18n);
this->list->addView(dump_plaintext_cardinfo);
brls::ListItem *dump_specific_data = new brls::ListItem("gamecard_tab/list/dump_specific_data/label"_i18n, "gamecard_tab/list/dump_specific_data/description"_i18n);
this->list->addView(dump_specific_data);
brls::ListItem *dump_hfs_partitions = new brls::ListItem("gamecard_tab/list/dump_hfs_partitions/label"_i18n, "gamecard_tab/list/dump_hfs_partitions/description"_i18n);
this->list->addView(dump_hfs_partitions);
brls::ListItem *browse_hfs_partitions = new brls::ListItem("gamecard_tab/list/browse_hfs_partitions/label"_i18n, "gamecard_tab/list/browse_hfs_partitions/description"_i18n);
this->list->addView(browse_hfs_partitions);
brls::ListItem *dump_lafw = new brls::ListItem("gamecard_tab/list/dump_lafw/label"_i18n, "gamecard_tab/list/dump_lafw/description"_i18n);
this->list->addView(dump_lafw);
/* Update focus stack, if needed. */
if (focus_stack_index > -1) this->UpdateFocusStackViewAtIndex(focus_stack_index, this->GetListFirstFocusableChild());
/* Switch to the list view. */
this->list->invalidate(true);
this->SwitchLayerView(false, update_focused_view, focus_stack_index < 0);
}
}