mirror of
https://github.com/J-D-K/JKSV.git
synced 2026-09-08 18:45:14 -05:00
Commit before the great template war.
This commit is contained in:
@@ -13,9 +13,11 @@
|
||||
#include "fslib.hpp"
|
||||
#include "input.hpp"
|
||||
#include "logger.hpp"
|
||||
#include "remote/remote.hpp"
|
||||
#include "strings.hpp"
|
||||
#include "stringutil.hpp"
|
||||
#include "sys/sys.hpp"
|
||||
#include "tasks/useroptions.hpp"
|
||||
#include "ui/PopMessageManager.hpp"
|
||||
|
||||
namespace
|
||||
@@ -34,14 +36,6 @@ namespace
|
||||
|
||||
} // namespace
|
||||
|
||||
// Declarations here. Defintions after class.
|
||||
// Backs up all save data for the target user.
|
||||
static void backup_all_for_user(sys::ProgressTask *task, std::shared_ptr<UserOptionState::DataStruct> dataStruct);
|
||||
// // Creates all save data for the current user.
|
||||
static void create_all_save_data_for_user(sys::Task *task, std::shared_ptr<UserOptionState::DataStruct> dataStruct);
|
||||
// // Deletes all save data from the system for the target user.
|
||||
static void delete_all_save_data_for_user(sys::Task *task, std::shared_ptr<UserOptionState::DataStruct> dataStruct);
|
||||
|
||||
UserOptionState::UserOptionState(data::User *user, TitleSelectCommon *titleSelect)
|
||||
: m_user{user}
|
||||
, m_titleSelect{titleSelect}
|
||||
@@ -112,7 +106,7 @@ void UserOptionState::render()
|
||||
sm_menuPanel->render(NULL, BaseState::has_focus());
|
||||
}
|
||||
|
||||
void UserOptionState::data_and_view_refresh_required() { m_refreshRequired = true; }
|
||||
void UserOptionState::refresh_required() { m_refreshRequired = true; }
|
||||
|
||||
void UserOptionState::create_menu_panel()
|
||||
{
|
||||
@@ -139,30 +133,28 @@ void UserOptionState::initialize_data_struct()
|
||||
|
||||
void UserOptionState::backup_all()
|
||||
{
|
||||
remote::Storage *remote = remote::get_remote_storage();
|
||||
bool autoUpload = config::get_by_key(config::keys::AUTO_UPLOAD);
|
||||
const char *confirmFormat = strings::get_by_name(strings::names::USEROPTION_CONFS, 0);
|
||||
const char *nickname = m_user->get_nickname();
|
||||
|
||||
const std::string queryString = stringutil::get_formatted_string(confirmFormat, nickname);
|
||||
|
||||
auto confirm = std::make_shared<ProgressConfirm>(queryString, true, backup_all_for_user, m_dataStruct);
|
||||
StateManager::push_state(confirm);
|
||||
const std::string query = stringutil::get_formatted_string(confirmFormat, nickname);
|
||||
if (remote && autoUpload)
|
||||
{
|
||||
ProgressConfirm::create_and_push(query, true, tasks::useroptions::backup_all_for_user_remote, m_dataStruct, false);
|
||||
}
|
||||
else { ProgressConfirm::create_and_push(query, true, tasks::useroptions::backup_all_for_user_local, m_dataStruct, false); }
|
||||
}
|
||||
|
||||
void UserOptionState::create_save_create()
|
||||
{
|
||||
auto saveCreate = std::make_shared<SaveCreateState>(m_user, m_titleSelect);
|
||||
StateManager::push_state(saveCreate);
|
||||
}
|
||||
void UserOptionState::create_save_create() { SaveCreateState::create_and_push(m_user, m_titleSelect); }
|
||||
|
||||
void UserOptionState::create_all_save_data()
|
||||
{
|
||||
const char *confirmFormat = strings::get_by_name(strings::names::USEROPTION_CONFS, 1);
|
||||
const char *nickname = m_user->get_nickname();
|
||||
|
||||
const std::string queryString = stringutil::get_formatted_string(confirmFormat, nickname);
|
||||
|
||||
auto confirm = std::make_shared<TaskConfirm>(queryString, true, create_all_save_data_for_user, m_dataStruct);
|
||||
StateManager::push_state(confirm);
|
||||
const std::string query = stringutil::get_formatted_string(confirmFormat, nickname);
|
||||
TaskConfirm::create_and_push(query, true, tasks::useroptions::create_all_save_data_for_user, m_dataStruct);
|
||||
}
|
||||
|
||||
void UserOptionState::delete_all_save_data()
|
||||
@@ -170,150 +162,8 @@ void UserOptionState::delete_all_save_data()
|
||||
const uint8_t saveType = m_user->get_account_save_type();
|
||||
if (saveType == FsSaveDataType_System || saveType == FsSaveDataType_SystemBcat) { return; }
|
||||
|
||||
const char *confirmFormat = strings::get_by_name(strings::names::USEROPTION_CONFS, 2);
|
||||
const char *nickname = m_user->get_nickname();
|
||||
|
||||
const char *confirmFormat = strings::get_by_name(strings::names::USEROPTION_CONFS, 2);
|
||||
const char *nickname = m_user->get_nickname();
|
||||
const std::string queryString = stringutil::get_formatted_string(confirmFormat, nickname);
|
||||
|
||||
auto confirm = std::make_shared<TaskConfirm>(queryString, true, delete_all_save_data_for_user, m_dataStruct);
|
||||
StateManager::push_state(confirm);
|
||||
}
|
||||
|
||||
static void backup_all_for_user(sys::ProgressTask *task, std::shared_ptr<UserOptionState::DataStruct> dataStruct)
|
||||
{
|
||||
if (error::is_null(task)) { return; }
|
||||
|
||||
data::User *user = dataStruct->user;
|
||||
|
||||
const bool exportZip = config::get_by_key(config::keys::EXPORT_TO_ZIP) || config::get_by_key(config::keys::AUTO_UPLOAD);
|
||||
const bool autoUpload = config::get_by_key(config::keys::AUTO_UPLOAD);
|
||||
const size_t titleCount = user->get_total_data_entries();
|
||||
for (size_t i = 0; i < titleCount; i++)
|
||||
{
|
||||
const FsSaveDataInfo *saveInfo = user->get_save_info_at(i);
|
||||
data::TitleInfo *titleInfo = data::get_title_info_by_id(saveInfo->application_id);
|
||||
if (error::is_null(saveInfo) || error::is_null(titleInfo)) { continue; }
|
||||
|
||||
const fslib::Path targetDir{config::get_working_directory() / titleInfo->get_path_safe_title()};
|
||||
const bool targetExists = fslib::directory_exists(targetDir);
|
||||
const bool targetFailed = !targetExists && error::fslib(fslib::create_directories_recursively(targetDir));
|
||||
if (!targetExists && targetFailed) { continue; }
|
||||
|
||||
const bool mountFailed = error::fslib(fslib::open_save_data_with_save_info(fs::DEFAULT_SAVE_MOUNT, *saveInfo));
|
||||
const bool validSave = !mountFailed && fs::directory_has_contents(fs::DEFAULT_SAVE_ROOT);
|
||||
if (mountFailed || !validSave)
|
||||
{
|
||||
fslib::close_file_system(fs::DEFAULT_SAVE_MOUNT);
|
||||
continue;
|
||||
}
|
||||
|
||||
fs::SaveMetaData saveMeta{};
|
||||
const bool hasMeta = fs::fill_save_meta_data(saveInfo, saveMeta);
|
||||
|
||||
fslib::Path backupPath{targetDir / "AUTO - " + user->get_path_safe_nickname() + " - " + stringutil::get_date_string()};
|
||||
if (exportZip)
|
||||
{
|
||||
backupPath += ".zip";
|
||||
fs::MiniZip targetZip{backupPath};
|
||||
if (!targetZip.is_open())
|
||||
{
|
||||
fslib::close_file_system(fs::DEFAULT_SAVE_MOUNT);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (hasMeta && targetZip.open_new_file(fs::NAME_SAVE_META))
|
||||
{
|
||||
targetZip.write(&saveMeta, sizeof(fs::SaveMetaData));
|
||||
targetZip.close_current_file();
|
||||
}
|
||||
fs::copy_directory_to_zip(fs::DEFAULT_SAVE_ROOT, targetZip, task);
|
||||
}
|
||||
else
|
||||
{
|
||||
{
|
||||
fslib::File metaFile{backupPath / fs::NAME_SAVE_META, FsOpenMode_Create | FsOpenMode_Write};
|
||||
if (hasMeta && metaFile.is_open()) { metaFile.write(&saveMeta, sizeof(fs::SaveMetaData)); }
|
||||
fs::copy_directory(fs::DEFAULT_SAVE_ROOT, backupPath, task);
|
||||
}
|
||||
}
|
||||
}
|
||||
task->finished();
|
||||
}
|
||||
|
||||
static void create_all_save_data_for_user(sys::Task *task, std::shared_ptr<UserOptionState::DataStruct> dataStruct)
|
||||
{
|
||||
if (error::is_null(task)) { return; }
|
||||
|
||||
data::User *user = dataStruct->user;
|
||||
UserOptionState *spawningState = dataStruct->spawningState;
|
||||
|
||||
auto &titleInfoMap = data::get_title_info_map();
|
||||
const int popTicks = ui::PopMessageManager::DEFAULT_TICKS;
|
||||
const char *statusTemplate = strings::get_by_name(strings::names::USEROPTION_STATUS, 0);
|
||||
const char *popFailure = strings::get_by_name(strings::names::SAVECREATE_POPS, 1);
|
||||
const uint8_t saveType = user->get_account_save_type();
|
||||
|
||||
for (auto &[applicationID, titleInfo] : titleInfoMap)
|
||||
{
|
||||
|
||||
const bool hasType = titleInfo.has_save_data_type(saveType);
|
||||
if (!hasType) { continue; }
|
||||
|
||||
{
|
||||
const std::string status = stringutil::get_formatted_string(statusTemplate, titleInfo.get_title());
|
||||
task->set_status(status);
|
||||
}
|
||||
|
||||
const bool saveCreated = fs::create_save_data_for(user, &titleInfo);
|
||||
if (!saveCreated)
|
||||
{
|
||||
const char *title = titleInfo.get_title();
|
||||
const std::string pop = stringutil::get_formatted_string(popFailure, title);
|
||||
ui::PopMessageManager::push_message(popTicks, pop);
|
||||
}
|
||||
}
|
||||
spawningState->data_and_view_refresh_required();
|
||||
task->finished();
|
||||
}
|
||||
|
||||
static void delete_all_save_data_for_user(sys::Task *task, std::shared_ptr<UserOptionState::DataStruct> dataStruct)
|
||||
{
|
||||
if (error::is_null(task)) { return; }
|
||||
|
||||
data::User *user = dataStruct->user;
|
||||
UserOptionState *spawningState = dataStruct->spawningState;
|
||||
const char *statusTemplate = strings::get_by_name(strings::names::USEROPTION_STATUS, 1); // Borrowed. No duplication.
|
||||
const char *popFailed = strings::get_by_name(strings::names::SAVECREATE_POPS, 2);
|
||||
const int popTicks = ui::PopMessageManager::DEFAULT_TICKS;
|
||||
const size_t totalDataEntries = user->get_total_data_entries();
|
||||
std::vector<uint64_t> applicationIDs;
|
||||
|
||||
// Check this quick just in case.
|
||||
if (user->get_account_save_type() == FsSaveDataType_System) { TASK_FINISH_RETURN(task); }
|
||||
|
||||
for (size_t i = 0; i < totalDataEntries; i++)
|
||||
{
|
||||
const FsSaveDataInfo *saveInfo = user->get_save_info_at(i);
|
||||
if (error::is_null(saveInfo) || saveInfo->save_data_type == FsSaveDataType_System) { continue; }
|
||||
|
||||
{
|
||||
const uint64_t applicationID = user->get_application_id_at(i);
|
||||
data::TitleInfo *titleInfo = data::get_title_info_by_id(applicationID);
|
||||
const char *title = titleInfo->get_title();
|
||||
const std::string status = stringutil::get_formatted_string(statusTemplate, title);
|
||||
task->set_status(status);
|
||||
}
|
||||
|
||||
const bool saveDeleted = fs::delete_save_data(saveInfo);
|
||||
if (!saveDeleted)
|
||||
{
|
||||
ui::PopMessageManager::push_message(popTicks, popFailed);
|
||||
continue;
|
||||
}
|
||||
applicationIDs.push_back(saveInfo->application_id);
|
||||
}
|
||||
|
||||
for (uint64_t &applicationID : applicationIDs) { user->erase_save_info_by_id(applicationID); }
|
||||
spawningState->data_and_view_refresh_required();
|
||||
task->finished();
|
||||
TaskConfirm::create_and_push(queryString, true, tasks::useroptions::delete_all_save_data_for_user, m_dataStruct);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user