mirror of
https://github.com/J-D-K/JKSV.git
synced 2026-03-22 01:34:13 -05:00
319 lines
13 KiB
C++
319 lines
13 KiB
C++
#include "appstates/BackupMenuState.hpp"
|
|
|
|
#include "StateManager.hpp"
|
|
#include "appstates/ConfirmState.hpp"
|
|
#include "appstates/ProgressState.hpp"
|
|
#include "colors.hpp"
|
|
#include "config.hpp"
|
|
#include "error.hpp"
|
|
#include "fs/fs.hpp"
|
|
#include "fslib.hpp"
|
|
#include "input.hpp"
|
|
#include "keyboard.hpp"
|
|
#include "remote/remote.hpp"
|
|
#include "sdl.hpp"
|
|
#include "strings.hpp"
|
|
#include "stringutil.hpp"
|
|
#include "system/system.hpp"
|
|
#include "tasks/backup.hpp"
|
|
#include "ui/PopMessageManager.hpp"
|
|
#include "ui/TextScroll.hpp"
|
|
|
|
#include <cstring>
|
|
|
|
namespace
|
|
{
|
|
/// @brief This is the length allotted for naming backups.
|
|
constexpr size_t SIZE_NAME_LENGTH = 0x80;
|
|
|
|
/// @brief This is just so there isn't random .zip comparisons everywhere.
|
|
constexpr const char *STRING_ZIP_EXT = ".zip";
|
|
|
|
// These make some things cleaner and easier to type.
|
|
using TaskConfirm = ConfirmState<sys::Task, TaskState, BackupMenuState::DataStruct>;
|
|
using ProgressConfirm = ConfirmState<sys::ProgressTask, ProgressState, BackupMenuState::DataStruct>;
|
|
} // namespace
|
|
|
|
BackupMenuState::BackupMenuState(data::User *user, data::TitleInfo *titleInfo)
|
|
: m_user(user)
|
|
, m_titleInfo(titleInfo)
|
|
, m_saveType(m_user->get_account_save_type())
|
|
, m_directoryPath(config::get_working_directory() / m_titleInfo->get_path_safe_title())
|
|
, m_directoryListing(m_directoryPath)
|
|
, m_dataStruct(std::make_shared<BackupMenuState::DataStruct>())
|
|
, m_controlGuide(strings::get_by_name(strings::names::CONTROL_GUIDES, 2))
|
|
{
|
|
BackupMenuState::initialize_static_members();
|
|
BackupMenuState::ensure_target_directory();
|
|
BackupMenuState::initialize_remote_storage();
|
|
BackupMenuState::initialize_task_data();
|
|
BackupMenuState::initialize_info_string();
|
|
BackupMenuState::save_data_check();
|
|
BackupMenuState::refresh();
|
|
}
|
|
|
|
BackupMenuState::~BackupMenuState()
|
|
{
|
|
fslib::close_file_system(fs::DEFAULT_SAVE_MOUNT);
|
|
|
|
sm_slidePanel->clear_elements();
|
|
sm_slidePanel->reset();
|
|
|
|
remote::Storage *remote = remote::get_remote_storage();
|
|
if (remote && remote->is_initialized()) { remote->return_to_root(); }
|
|
}
|
|
|
|
void BackupMenuState::update()
|
|
{
|
|
const bool hasFocus = BaseState::has_focus();
|
|
const int selected = sm_backupMenu->get_selected();
|
|
const bool aPressed = input::button_pressed(HidNpadButton_A);
|
|
const bool bPressed = input::button_pressed(HidNpadButton_B);
|
|
const bool xPressed = input::button_pressed(HidNpadButton_X);
|
|
const bool yPressed = input::button_pressed(HidNpadButton_Y);
|
|
const bool zrPressed = input::button_pressed(HidNpadButton_ZR);
|
|
|
|
const bool newSelected = selected == 0;
|
|
const bool newBackup = aPressed && newSelected && m_saveHasData;
|
|
const bool overwriteBackup = aPressed && !newSelected && m_saveHasData;
|
|
const bool restoreBackup = yPressed && !newSelected;
|
|
const bool deleteBackup = xPressed && !newSelected;
|
|
const bool uploadBackup = zrPressed && !newSelected;
|
|
const bool popEmpty = aPressed && !m_saveHasData;
|
|
|
|
if (newBackup) { BackupMenuState::name_and_create_backup(); }
|
|
else if (overwriteBackup) { BackupMenuState::confirm_overwrite(); }
|
|
else if (restoreBackup) { BackupMenuState::confirm_restore(); }
|
|
else if (deleteBackup) { BackupMenuState::confirm_delete(); }
|
|
else if (uploadBackup) { BackupMenuState::upload_backup(); }
|
|
else if (popEmpty) { BackupMenuState::pop_save_empty(); }
|
|
else if (bPressed) { sm_slidePanel->close(); }
|
|
else if (sm_slidePanel->is_closed()) { BaseState::deactivate(); }
|
|
|
|
m_titleScroll.update(hasFocus);
|
|
sm_slidePanel->update(hasFocus);
|
|
sm_backupMenu->update(hasFocus);
|
|
}
|
|
|
|
void BackupMenuState::render()
|
|
{
|
|
const bool hasFocus = BaseState::has_focus();
|
|
SDL_Texture *target = sm_slidePanel->get_target();
|
|
|
|
sm_slidePanel->clear_target();
|
|
m_titleScroll.render(target, hasFocus);
|
|
|
|
sdl::render_line(target, 10, 42, sm_panelWidth - 10, 42, colors::WHITE);
|
|
sdl::render_line(target, 10, 648, sm_panelWidth - 10, 648, colors::WHITE);
|
|
sdl::text::render(target, 32, 673, 22, sdl::text::NO_TEXT_WRAP, colors::WHITE, m_controlGuide);
|
|
|
|
sm_menuRenderTarget->clear(colors::TRANSPARENT);
|
|
sm_backupMenu->render(sm_menuRenderTarget->get(), hasFocus);
|
|
sm_menuRenderTarget->render(target, 0, 43);
|
|
|
|
sm_slidePanel->render(NULL, hasFocus);
|
|
}
|
|
|
|
void BackupMenuState::refresh()
|
|
{
|
|
remote::Storage *remote = remote::get_remote_storage();
|
|
m_directoryListing.open(m_directoryPath);
|
|
if (!m_directoryListing) { return; }
|
|
|
|
sm_backupMenu->reset();
|
|
m_menuEntries.clear();
|
|
|
|
sm_backupMenu->add_option(strings::get_by_name(strings::names::BACKUPMENU_MENU, 0));
|
|
m_menuEntries.push_back({MenuEntryType::Null, 0});
|
|
|
|
if (remote && remote->is_initialized())
|
|
{
|
|
const std::string_view prefix = remote->get_prefix();
|
|
remote::Storage::DirectoryListing listing = remote->get_directory_listing();
|
|
int index{};
|
|
for (const remote::Item *item : listing)
|
|
{
|
|
const std::string_view name = item->get_name();
|
|
const std::string option = stringutil::get_formatted_string("%s %s", prefix.data(), name.data());
|
|
sm_backupMenu->add_option(option);
|
|
m_menuEntries.push_back({MenuEntryType::Remote, index++});
|
|
}
|
|
}
|
|
|
|
const int64_t listingCount = m_directoryListing.get_count();
|
|
for (int64_t i = 0; i < listingCount; i++)
|
|
{
|
|
sm_backupMenu->add_option(m_directoryListing[i]);
|
|
m_menuEntries.push_back({MenuEntryType::Local, static_cast<int>(i)});
|
|
}
|
|
}
|
|
|
|
void BackupMenuState::save_data_written()
|
|
{
|
|
if (!m_saveHasData) { m_saveHasData = true; }
|
|
}
|
|
|
|
void BackupMenuState::initialize_static_members()
|
|
{
|
|
constexpr int SDL_TEX_FLAGS = SDL_TEXTUREACCESS_STATIC | SDL_TEXTUREACCESS_TARGET;
|
|
if (sm_backupMenu && sm_slidePanel && sm_menuRenderTarget && sm_panelWidth) { return; }
|
|
|
|
sm_panelWidth = sdl::text::get_width(22, m_controlGuide) + 64;
|
|
sm_backupMenu = std::make_shared<ui::Menu>(8, 8, sm_panelWidth - 14, 24, 600);
|
|
sm_slidePanel = std::make_unique<ui::SlideOutPanel>(sm_panelWidth, ui::SlideOutPanel::Side::Right);
|
|
sm_menuRenderTarget = sdl::TextureManager::create_load_texture("backupMenuTarget", sm_panelWidth, 600, SDL_TEX_FLAGS);
|
|
}
|
|
|
|
void BackupMenuState::ensure_target_directory()
|
|
{
|
|
// If this is enabled, don't bother.
|
|
const bool autoUpload = config::get_by_key(config::keys::AUTO_UPLOAD);
|
|
const bool directoryNeeded = !autoUpload && !fslib::directory_exists(m_directoryPath);
|
|
const bool directoryCreate = directoryNeeded && fslib::create_directory(m_directoryPath);
|
|
}
|
|
|
|
void BackupMenuState::initialize_task_data()
|
|
{
|
|
m_dataStruct->user = m_user;
|
|
m_dataStruct->titleInfo = m_titleInfo;
|
|
m_dataStruct->spawningState = this;
|
|
}
|
|
|
|
void BackupMenuState::initialize_info_string()
|
|
{
|
|
const char *nickname = m_user->get_nickname();
|
|
const char *title = m_titleInfo->get_title();
|
|
const std::string infoString = stringutil::get_formatted_string("`%s` - %s", nickname, title);
|
|
m_titleScroll.create(infoString, 22, sm_panelWidth, 8, true, colors::WHITE);
|
|
}
|
|
|
|
void BackupMenuState::save_data_check()
|
|
{
|
|
fslib::Directory saveRoot{fs::DEFAULT_SAVE_ROOT};
|
|
m_saveHasData = saveRoot.get_count() > 0;
|
|
}
|
|
|
|
void BackupMenuState::initialize_remote_storage()
|
|
{
|
|
remote::Storage *remote = remote::get_remote_storage();
|
|
if (error::is_null(remote) || !remote->is_initialized()) { return; }
|
|
|
|
const bool supportsUtf8 = remote->supports_utf8();
|
|
const std::string_view remoteTitle = supportsUtf8 ? m_titleInfo->get_title() : m_titleInfo->get_path_safe_title();
|
|
const bool remoteDirExists = remote->directory_exists(remoteTitle);
|
|
const bool remoteDirCreated = !remoteDirExists && remote->create_directory(remoteTitle);
|
|
if (!remoteDirExists && !remoteDirCreated) { return; }
|
|
|
|
const remote::Item *remoteDir = remote->get_directory_by_name(remoteTitle);
|
|
if (!remoteDir) { return; }
|
|
|
|
remote->change_directory(remoteDir);
|
|
}
|
|
|
|
void BackupMenuState::name_and_create_backup()
|
|
{
|
|
const bool autoName = config::get_by_key(config::keys::AUTO_NAME_BACKUPS);
|
|
const bool exportZip = config::get_by_key(config::keys::EXPORT_TO_ZIP) || config::get_by_key(config::keys::AUTO_UPLOAD);
|
|
const bool zrHeld = input::button_held(HidNpadButton_ZR);
|
|
const char *keyboardHeader = strings::get_by_name(strings::names::KEYBOARD, 0);
|
|
const bool autoNamed = (autoName || zrHeld); // This can be eval'd here.
|
|
char name[SIZE_NAME_LENGTH + 1] = {0};
|
|
|
|
std::snprintf(name, SIZE_NAME_LENGTH, "%s - %s", m_user->get_path_safe_nickname(), stringutil::get_date_string().c_str());
|
|
|
|
const bool named = autoNamed || keyboard::get_input(SwkbdType_QWERTY, name, keyboardHeader, name, SIZE_NAME_LENGTH);
|
|
if (!named) { return; }
|
|
|
|
fslib::Path target{m_directoryPath / name};
|
|
const bool hasZipExt = std::strstr(target.full_path(), STRING_ZIP_EXT); // This might not be the best check.
|
|
if (exportZip && !hasZipExt) { target += STRING_ZIP_EXT; }
|
|
else if (!exportZip && !hasZipExt)
|
|
{
|
|
const bool targetExists = fslib::directory_exists(target);
|
|
const bool targetCreated = !targetExists && fslib::create_directory(target);
|
|
}
|
|
auto newBackupTask =
|
|
std::make_shared<ProgressState>(tasks::backup::create_new_backup, m_user, m_titleInfo, target, this, true);
|
|
StateManager::push_state(newBackupTask);
|
|
}
|
|
|
|
void BackupMenuState::confirm_overwrite()
|
|
{
|
|
const int selected = sm_backupMenu->get_selected();
|
|
const MenuEntry &entry = m_menuEntries.at(selected);
|
|
const bool holdRequired = config::get_by_key(config::keys::HOLD_FOR_OVERWRITE);
|
|
const char *confirmTemplate = strings::get_by_name(strings::names::BACKUPMENU_CONFS, 0);
|
|
m_dataStruct->path = m_directoryPath / m_directoryListing[entry.index];
|
|
|
|
const std::string query = stringutil::get_formatted_string(confirmTemplate, m_directoryListing[entry.index]);
|
|
auto confirm = std::make_shared<ProgressConfirm>(query, holdRequired, tasks::backup::overwrite_backup, m_dataStruct);
|
|
StateManager::push_state(confirm);
|
|
}
|
|
|
|
void BackupMenuState::confirm_restore()
|
|
{
|
|
const int selected = sm_backupMenu->get_selected();
|
|
const MenuEntry &entry = m_menuEntries.at(selected);
|
|
const int popTicks = ui::PopMessageManager::DEFAULT_MESSAGE_TICKS;
|
|
const bool holdRequired = config::get_by_key(config::keys::HOLD_FOR_RESTORATION);
|
|
const char *confirmTemplate = strings::get_by_name(strings::names::BACKUPMENU_CONFS, 1);
|
|
const char *popBackupEmpty = strings::get_by_name(strings::names::BACKUPMENU_POPS, 1);
|
|
const char *popSysNotAllowed = strings::get_by_name(strings::names::BACKUPMENU_POPS, 6);
|
|
|
|
const bool isSystem = BackupMenuState::is_system_save_data();
|
|
const bool allowSystem = config::get_by_key(config::keys::ALLOW_WRITING_TO_SYSTEM);
|
|
const bool isValidRestore = !isSystem || allowSystem;
|
|
if (!isValidRestore)
|
|
{
|
|
ui::PopMessageManager::push_message(popTicks, popSysNotAllowed);
|
|
return;
|
|
}
|
|
|
|
const fslib::Path target = m_directoryPath / m_directoryListing[entry.index];
|
|
const bool targetIsDirectory = fslib::directory_exists(target);
|
|
const bool backupIsGood = targetIsDirectory ? fs::directory_has_contents(target) : fs::zip_has_contents(target);
|
|
if (!backupIsGood)
|
|
{
|
|
ui::PopMessageManager::push_message(popTicks, popBackupEmpty);
|
|
return;
|
|
}
|
|
|
|
m_dataStruct->path = target;
|
|
const std::string query = stringutil::get_formatted_string(confirmTemplate, m_directoryListing[entry.index]);
|
|
auto confirm = std::make_shared<ProgressConfirm>(query, holdRequired, tasks::backup::restore_backup, m_dataStruct);
|
|
StateManager::push_state(confirm);
|
|
}
|
|
|
|
void BackupMenuState::confirm_delete()
|
|
{
|
|
const int selected = sm_backupMenu->get_selected();
|
|
const MenuEntry &entry = m_menuEntries.at(selected);
|
|
const bool holdRequired = config::get_by_key(config::keys::HOLD_FOR_DELETION);
|
|
const char *confirmTemplate = strings::get_by_name(strings::names::BACKUPMENU_CONFS, 2);
|
|
m_dataStruct->path = m_directoryPath / m_directoryListing[entry.index];
|
|
|
|
const std::string query = stringutil::get_formatted_string(confirmTemplate, m_directoryListing[entry.index]);
|
|
auto confirm = std::make_shared<TaskConfirm>(query, holdRequired, tasks::backup::delete_backup, m_dataStruct);
|
|
|
|
StateManager::push_state(confirm);
|
|
}
|
|
|
|
void BackupMenuState::upload_backup()
|
|
{
|
|
const int selected = sm_backupMenu->get_selected();
|
|
const MenuEntry &entry = m_menuEntries.at(selected);
|
|
if (entry.type != BackupMenuState::MenuEntryType::Local) { return; }
|
|
|
|
m_dataStruct->path = m_directoryPath / m_directoryListing[entry.index];
|
|
|
|
auto upload = std::make_shared<ProgressState>(tasks::backup::upload_backup, m_dataStruct);
|
|
StateManager::push_state(upload);
|
|
}
|
|
|
|
void BackupMenuState::pop_save_empty()
|
|
{
|
|
const int ticks = ui::PopMessageManager::DEFAULT_MESSAGE_TICKS;
|
|
const char *popEmpty = strings::get_by_name(strings::names::BACKUPMENU_POPS, 0);
|
|
ui::PopMessageManager::push_message(ticks, popEmpty);
|
|
}
|