Skip to content

Commit

Permalink
Add DataTransferTaskFrame and GameCardImageDumpTaskFrame classes
Browse files Browse the repository at this point in the history
DataTransferTaskFrame is a template class that's derived from brls::AppletFrame, which automatically starts a background task using an internal object belonging to a class derived from DataTransferTask. A DataTransferProgressDisplay view is used to show progress updates. If the background task hits an error, the class takes care of switching to an ErrorFrame view with the corresponding error message.

GameCardImageDumpTaskFrame is a derived class of DataTransferTaskFrame that uses a GameCardImageDumpTask object as its background task. In layman's terms, this provides a way to fully dump gamecard images using the new UI.

DataTransferTaskFrame depends on the newly added is_base_template helper from goneskiing to check if the class for the provided task is derived from DataTransferTask.

Other changes include:

* DataTransferProgressDisplay: rename setProgress() method to SetProgress().

* DataTransferTask: move post-task-execution code into its own new private method, PostExecutionCallback(), and update both OnCancelled() and OnPostExecute() callbacks to invoke it.
* DataTransferTask: update OnProgressUpdate() to allow sending a last progress update to all event subscribers even if the background task was cancelled.
* DataTransferTask: update OnProgressUpdate() to allow sending a first progress update if no data has been transferred but the total transfer size is already known.
* DataTransferTask: update OnProgressUpdate() to avoid calculating the ETA if the speed isn't greater than 0.

* DumpOptionsFrame: remove UpdateOutputStorages() method.
* DumpOptionsFrame: update class to use the cached output storage value from our RootView.
* DumpOptionsFrame: add GenerateOutputStoragesVector() method, which is used to avoid setting dummy options while initializing the output storages SelectListItem.
* DumpOptionsFrame: update UMS task callback to add the rest of the leftover logic from UpdateOutputStorages().
* DumpOptionsFrame: update RegisterButtonListener() to use a wrapper callback around the user-provided callback to check if the USB host was selected as the output storage but no USB host connection is available.

* ErrorFrame: use const references for all input string arguments.

* FileWriter: fix a localization stirng name typo.
* FileWriter: fix an exception that was previously being thrown by a fmt::format() call because of a wrong format specifier.

* FocusableItem: add a static assert to check if the provided ViewType is derived from brls::View.

* gamecard: redefine global gamecard status variable as an atomic unsigned 8-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call gamecardGetStatus().

* GameCardImageDumpOptionsFrame: define GAMECARD_TOGGLE_ITEM macro, which is used to initialize all ToggleListItem elements from the view.
* GameCardImageDumpOptionsFrame: update button callback to push a GameCardImageDumpTaskFrame view onto the borealis stack.

* GameCardImageDumpTask: move class into its own header and module files.
* GameCardImageDumpTask: update class to also take in the checksum lookup method (not yet implemented).
* GameCardImageDumpTask: update class to send its first progress update as soon as the gamecard image size is known.
* GameCardImageDumpTask: update class to avoid returning a string if the task was cancelled -- DataTransferTaskFrame offers logic to display the appropiate cancel message on its own.

* GameCardTab: update PopulateList() method to display the new version information available in TitleGameCardApplicationMetadataEntry elements as part of the generated TitlesTabItem objects.

* i18n: add new localization strings.

* OptionsTab: update background task callback logic to handle task cancellation, reflecting the changes made to DataTransferTask.
* OptionsTab: reflect changes made to DataTransferProgressDisplay.

* RootView: cache the currently selected output storage value at all times, which is propagated throughout different parts of the UI. Getter and setter helpers have been added to operate with this value.
* RootView: add GetUsbHostSpeed() helper, which can be used by child views to retrieve the USB host speed on demand.
* RootView: update UMS task callback to automatically reset the cached output storage value back to the SD card if a UMS device was previously selected.

* title: define TitleGameCardApplicationMetadataEntry struct, which also holds version-specific information retrieved from the gamecard titles.
* title: refactor titleGetGameCardApplicationMetadataEntries() to return a dynamically allocated array of TitleGameCardApplicationMetadataEntry elements.

* usb: redefine global endpoint max packet size variable as an atomic unsigned 16-bit integer, which fixes a "status-hopping" issue previously experienced by repeating tasks running under other threads that periodically call usbIsReady().

* UsbHostTask: add GetUsbHostSpeed() method.
  • Loading branch information
DarkMatterCore committed Apr 29, 2024
1 parent 32c097c commit 3e10421
Show file tree
Hide file tree
Showing 32 changed files with 639 additions and 268 deletions.
2 changes: 1 addition & 1 deletion include/async_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace nxdt::tasks
if (!this->m_future.valid()) return;

/* Wait until a result is provided by the task thread. */
/* Avoid rethrowing any exceptions here - program execution could end if another exception has already been rethrown. */
/* Avoid rethrowing any exceptions here -- program execution could end if another exception has already been rethrown. */
m_future.wait();
}

Expand Down
2 changes: 1 addition & 1 deletion include/core/nxdt_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ void logFlushLogFile(void);
void logCloseLogFile(void);

/// Returns a pointer to a dynamically allocated buffer that holds the last error message string, or NULL if there's none.
/// The allocated buffer must be freed by the calling function using free().
/// The allocated buffer must be freed by the caller using free().
char *logGetLastMessage(void);

/// (Un)locks the log mutex. Can be used to block other threads and prevent them from writing data to the logfile.
Expand Down
2 changes: 1 addition & 1 deletion include/core/nxdt_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ bool utilsDeleteDirectoryRecursively(const char *path);
/// A path separator is automatically placed between the provided prefix and the filename if the prefix doesn't end with one.
/// A dot *isn't* automatically placed between the filename and the provided extension -- if required, it must be provided as part of the extension string.
/// Furthermore, if the full length for the generated path is >= FS_MAX_PATH, NULL will be returned.
/// The allocated buffer must be freed by the calling function using free().
/// The allocated buffer must be freed by the caller using free().
char *utilsGeneratePath(const char *prefix, const char *filename, const char *extension);

/// Prints an error message using the standard console output and waits for the user to press a button.
Expand Down
16 changes: 12 additions & 4 deletions include/core/title.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ typedef struct {
u8 *icon; ///< JPEG icon data.
} TitleApplicationMetadata;

/// Used to display gamecard-specific title information.
typedef struct {
TitleApplicationMetadata *app_metadata; ///< User application metadata.
Version version; ///< Reflects the title version stored in the inserted gamecard.
char display_version[32]; ///< Reflects the title display version stored in its NACP.
} TitleGameCardApplicationMetadataEntry;

/// Generated using ncm calls.
/// User applications: the previous/next pointers reference other user applications with the same ID.
/// Patches: the previous/next pointers reference other patches with the same ID.
Expand Down Expand Up @@ -102,12 +109,13 @@ NcmContentStorage *titleGetNcmStorageByStorageId(u8 storage_id);
/// Returns a pointer to a dynamically allocated array of pointers to TitleApplicationMetadata entries, as well as their count. Returns NULL if an error occurs.
/// If 'is_system' is true, TitleApplicationMetadata entries from available system titles (NcmStorageId_BuiltInSystem) will be returned.
/// Otherwise, TitleApplicationMetadata entries from user applications with available content data (NcmStorageId_BuiltInUser, NcmStorageId_SdCard, NcmStorageId_GameCard) will be returned.
/// The allocated buffer must be freed by the calling function using free().
/// The allocated buffer must be freed by the caller using free().
TitleApplicationMetadata **titleGetApplicationMetadataEntries(bool is_system, u32 *out_count);

/// Returns a pointer to a dynamically allocated array of pointers to TitleApplicationMetadata entries with matching gamecard user titles, as well as their count. Returns NULL if an error occurs.
/// The allocated buffer must be freed by the calling function using free().
TitleApplicationMetadata **titleGetGameCardApplicationMetadataEntries(u32 *out_count);
/// Returns a pointer to a dynamically allocated array of TitleGameCardApplicationMetadataEntry elements generated from gamecard user titles, as well as their count.
/// Returns NULL if an error occurs.
/// The allocated buffer must be freed by the caller using free().
TitleGameCardApplicationMetadataEntry *titleGetGameCardApplicationMetadataEntries(u32 *out_count);

/// Returns a pointer to a dynamically allocated TitleInfo element with a matching storage ID and title ID. Returns NULL if an error occurs.
/// If NcmStorageId_Any is used, the first entry with a matching title ID is returned.
Expand Down
2 changes: 1 addition & 1 deletion include/core/ums.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void umsExit(void);
/// Returns true if USB Mass Storage device info has been updated.
bool umsIsDeviceInfoUpdated(void);

/// Returns a pointer to a dynamically allocated array of UsbHsFsDevice elements. The allocated buffer must be freed by the calling function.
/// Returns a pointer to a dynamically allocated array of UsbHsFsDevice elements. The allocated buffer must be freed by the caller.
/// Returns NULL if an error occurs.
UsbHsFsDevice *umsGetDevices(u32 *out_count);

Expand Down
2 changes: 1 addition & 1 deletion include/data_transfer_progress_display.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace nxdt::views
DataTransferProgressDisplay();
~DataTransferProgressDisplay();

void setProgress(const nxdt::tasks::DataTransferProgress& progress);
void SetProgress(const nxdt::tasks::DataTransferProgress& progress);

void willAppear(bool resetState = false) override;
void willDisappear(bool resetState = false) override;
Expand Down
63 changes: 35 additions & 28 deletions include/data_transfer_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,32 @@ namespace nxdt::tasks

SteadyTimePoint start_time{}, prev_time{}, end_time{};
size_t prev_xfer_size = 0;
bool first_publish_progress = true;

ALWAYS_INLINE std::string FormatTimeString(double seconds)
{
return fmt::format("{:02.0F}H{:02.0F}M{:02.0F}S", std::fmod(seconds, 86400.0) / 3600.0, std::fmod(seconds, 3600.0) / 60.0, std::fmod(seconds, 60.0));
}

void PostExecutionCallback(void)
{
/* Set end time. */
this->end_time = CurrentSteadyTimePoint();

/* Fire task handler immediately to make it store the last result from AsyncTask::LoopCallback(). */
/* We do this here because all subscribers to our progress event will most likely call IsFinished() to check if the task is complete. */
/* That being the case, if the `finished` flag returned by the task handler isn't updated before the progress event subscribers receive the last progress update, */
/* they won't be able to determine if the task has already finished, leading to unsuspected consequences. */
this->task_handler->fireNow();

/* Update progress one last time. */
/* This will effectively invoke the callbacks from all of our progress event subscribers. */
this->OnProgressUpdate(this->GetProgress());

/* Unset long running process state. */
utilsSetLongRunningProcessState(false);
}

protected:
/* Set class as non-copyable and non-moveable. */
NON_COPYABLE(DataTransferTask);
Expand All @@ -103,32 +123,17 @@ namespace nxdt::tasks
{
NX_IGNORE_ARG(result);

/* Set end time. */
this->end_time = CurrentSteadyTimePoint();

/* Unset long running process state. */
utilsSetLongRunningProcessState(false);
/* Run post execution callback. */
this->PostExecutionCallback();
}

/* Runs on the calling thread. */
void OnPostExecute(const Result& result) override final
{
NX_IGNORE_ARG(result);

/* Set end time. */
this->end_time = CurrentSteadyTimePoint();

/* Fire task handler immediately to make it store the last result from AsyncTask::LoopCallback(). */
/* We do this here because all subscriptors to our progress event will most likely call IsFinished() to check if the task is complete. */
/* That being the case, if the `finished` flag returned by the task handler isn't updated before the progress event subscriptors receive the last progress update, */
/* they won't be able to determine if the task has already finished, leading to unsuspected consequences. */
this->task_handler->fireNow();

/* Update progress one last time. */
this->OnProgressUpdate(this->GetProgress());

/* Unset long running process state. */
utilsSetLongRunningProcessState(false);
/* Run post execution callback. */
this->PostExecutionCallback();
}

/* Runs on the calling thread. */
Expand All @@ -147,17 +152,18 @@ namespace nxdt::tasks
/* Runs on the calling thread. */
void OnProgressUpdate(const DataTransferProgress& progress) override final
{
AsyncTaskStatus status = this->GetStatus();

/* Return immediately if there has been no progress at all, or if it the task has been cancelled. */
bool proceed = (progress.xfer_size > prev_xfer_size || (progress.xfer_size == prev_xfer_size && (!progress.total_size || progress.xfer_size >= progress.total_size)));
if (!proceed || this->IsCancelled()) return;
/* Return immediately if there has been no progress at all. */
bool proceed = (progress.xfer_size > prev_xfer_size || (progress.xfer_size == prev_xfer_size && (!progress.total_size || progress.xfer_size >= progress.total_size ||
this->first_publish_progress)));
if (!proceed) return;

/* Calculate time difference between the last progress update and the current one. */
/* Return immediately if it's less than 1 second, but only if this isn't the last chunk; or if we don't know the total size and the task is still running . */
/* Return immediately if the task hasn't been cancelled and less than 1 second has passed since the last progress update -- but only if */
/* this isn't the last chunk *or* if we don't know the total size and the task is still running . */
AsyncTaskStatus status = this->GetStatus();
SteadyTimePoint cur_time = std::chrono::steady_clock::now();
double diff_time = std::chrono::duration<double>(cur_time - this->prev_time).count();
if (diff_time < 1.0 && ((progress.total_size && progress.xfer_size < progress.total_size) || status == AsyncTaskStatus::RUNNING)) return;
if (!this->IsCancelled() && diff_time < 1.0 && ((progress.total_size && progress.xfer_size < progress.total_size) || status == AsyncTaskStatus::RUNNING)) return;

/* Calculate transferred data size difference between the last progress update and the current one. */
double diff_xfer_size = static_cast<double>(progress.xfer_size - prev_xfer_size);
Expand All @@ -169,14 +175,14 @@ namespace nxdt::tasks
DataTransferProgress new_progress = progress;
new_progress.speed = speed;

if (progress.total_size)
if (progress.total_size && speed > 0.0)
{
/* Calculate remaining data size and ETA if we know the total size. */
double remaining = static_cast<double>(progress.total_size - progress.xfer_size);
double eta = (remaining / speed);
new_progress.eta = this->FormatTimeString(eta);
} else {
/* No total size means no ETA calculation, sadly. */
/* No total size nor speed means no ETA calculation, sadly. */
new_progress.eta = "";
}

Expand All @@ -190,6 +196,7 @@ namespace nxdt::tasks
/* Update class variables. */
this->prev_time = cur_time;
this->prev_xfer_size = progress.xfer_size;
if (this->first_publish_progress) this->first_publish_progress = false;

/* Send updated progress to all listeners. */
this->progress_event.fire(new_progress);
Expand Down
177 changes: 177 additions & 0 deletions include/data_transfer_task_frame.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* data_transfer_task_frame.hpp
*
* Copyright (c) 2020-2024, DarkMatterCore <[email protected]>.
*
* 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/>.
*/

#pragma once

#ifndef __DATA_TRANSFER_TASK_FRAME_HPP__
#define __DATA_TRANSFER_TASK_FRAME_HPP__

#include "is_base_of_template.hpp"
#include "error_frame.hpp"
#include "data_transfer_progress_display.hpp"

namespace nxdt::views
{
template<typename Task>
class DataTransferTaskFrame: public brls::AppletFrame
{
static_assert(nxdt::utils::is_base_of_template_v<nxdt::tasks::DataTransferTask, Task>, "Task must inherit from DataTransferTask");

protected:
Task task;

private:
DataTransferProgressDisplay *task_progress = nullptr;
ErrorFrame *error_frame = nullptr;
bool progress_displayed = false;
std::string notification{};

void DisplayProgress(void)
{
this->setContentView(this->task_progress);
this->progress_displayed = true;
}

void DisplayError(const std::string& msg)
{
this->error_frame->SetMessage(msg);
this->setContentView(this->error_frame);
this->progress_displayed = false;
}

template<typename... Params>
void Initialize(const std::string& title, brls::Image *icon, const Params&... params)
{
/* Set UI properties. */
this->setTitle(title);
this->setIcon(icon);

/* Update B button label. */
this->updateActionHint(brls::Key::B, brls::i18n::getStr("generic/cancel"));

/* Initialize progress display. */
this->task_progress = new DataTransferProgressDisplay();

/* Initialize error frame. */
this->error_frame = new ErrorFrame();

/* Subscribe to the background task. */
this->task.RegisterListener([this](const nxdt::tasks::DataTransferProgress& progress) {
/* Store notification message and return immediately if the background task was cancelled. */
if (this->task.IsCancelled())
{
this->notification = brls::i18n::getStr("generic/process_cancelled");
return;
}

/* Update progress. */
this->task_progress->SetProgress(progress);

/* Check if the background task has finished. */
if (this->task.IsFinished())
{
/* Get background task result and error reason. */
std::string error_msg{};
bool ret = this->GetTaskResult(error_msg);

if (ret)
{
/* Store notification message. */
this->notification = brls::i18n::getStr("generic/process_complete");

/* Pop view. */
this->onCancel();
} else {
/* Update B button label. */
this->updateActionHint(brls::Key::B, brls::i18n::getStr("brls/hints/back"));

/* Display error frame. */
this->DisplayError(error_msg);
}
}
});

/* Start background task. */
this->task.Execute(params...);

/* Set content view. */
this->DisplayProgress();
}

protected:
/* Set class as non-copyable and non-moveable. */
NON_COPYABLE(DataTransferTaskFrame);
NON_MOVEABLE(DataTransferTaskFrame);

bool onCancel(void) override final
{
/* Cancel background task. This will have no effect if the background task already finished or if it was already cancelled. */
this->task.Cancel();

/* Pop view. This will invoke this class' destructor. */
brls::Application::popView(brls::ViewAnimation::SLIDE_RIGHT);

return true;
}

/* Must be implemented by derived classes to determine if the background task succeeded or not by calling GetResult() on their own. */
/* If the task failed, false shall be returned and `error_msg` shall be updated to reflect the error reason. */
virtual bool GetTaskResult(std::string& error_msg) = 0;

public:
template<typename... Params>
DataTransferTaskFrame(const std::string& title, const Params&... params) : brls::AppletFrame(true, true)
{
/* Generate icon using the default image. */
brls::Image *icon = new brls::Image();
icon->setImage(BOREALIS_ASSET("icon/" APP_TITLE ".jpg"));
icon->setScaleType(brls::ImageScaleType::SCALE);

/* Initialize the rest of the elements. */
this->Initialize(title, icon, params...);
}

template<typename... Params>
DataTransferTaskFrame(const std::string& title, brls::Image *icon, const Params&... params) : brls::AppletFrame(true, true)
{
/* Initialize the rest of the elements. */
this->Initialize(title, icon, params...);
}

~DataTransferTaskFrame()
{
/* Delete the view that's not currently being displayed. */
/* The other one will be taken care of by brls::AppletFrame's destructor. */
if (this->progress_displayed)
{
delete this->error_frame;
} else {
delete this->task_progress;
}

/* Show relevant notification, if needed. */
/* This is done here to avoid a slowdown issue while attempting to pop the current view and display a notification at the same time. */
if (!this->notification.empty()) brls::Application::notify(this->notification);
}
};
}

#endif /* __DATA_TRANSFER_TASK_FRAME_HPP__ */
2 changes: 1 addition & 1 deletion include/download_task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ namespace nxdt::tasks
};

/* Asynchronous task used to store downloaded data into a dynamically allocated buffer using a URL. */
/* The buffer returned by std::pair::first() must be manually freed by the calling function using free(). */
/* The buffer returned by std::pair::first() must be manually freed by the caller using free(). */
class DownloadDataTask: public DownloadTask<DownloadDataResult, std::string, bool>
{
protected:
Expand Down
Loading

0 comments on commit 3e10421

Please sign in to comment.