Begin generic SaveImportState

This commit is contained in:
J-D-K 2025-09-24 01:57:26 -04:00
parent 0aef58c064
commit a78dd3456a
6 changed files with 141 additions and 8 deletions

View File

@ -0,0 +1,53 @@
#pragma once
#include "StateManager.hpp"
#include "appstates/BaseState.hpp"
#include "data/data.hpp"
#include "fslib.hpp"
#include "ui/ui.hpp"
#include <memory>
class SaveImportState final : public BaseState
{
public:
/// @brief Constructs a new SaveImportState.
SaveImportState(data::User *user);
static inline std::shared_ptr<SaveImportState> create(data::User *user)
{
return std::make_shared<SaveImportState>(user);
}
static inline std::shared_ptr<SaveImportState> create_and_push(data::User *user)
{
auto newState = SaveImportState::create(user);
StateManager::push_state(newState);
return newState;
}
void update() override;
void render() override;
private:
/// @brief Pointer to the target user.
data::User *m_user{};
/// @brief
fslib::Directory m_saveDir{};
/// @brief Menu shared by all instances.
static inline std::shared_ptr<ui::Menu> sm_saveMenu{};
/// @brief Panel shared by all instances.
static inline std::shared_ptr<ui::SlideOutPanel> sm_slidePanel{};
/// @brief Ensures the menu and panel are initialized correctly.
void initialize_static_members();
/// @brief Opens and
void initialize_directory_menu();
/// @brief Cleans up and deactivates the state.
void deactivate_state();
};

View File

@ -91,6 +91,9 @@ class UserOptionState final : public BaseState
/// @brief Deletes all save data for the user.
void delete_all_save_data();
/// @brief Creates and pushes the save import state.
void create_push_save_import();
/// @brief Performs operations to reset static members and marks the state for purging.
void deactivate_state();
};

View File

@ -278,7 +278,8 @@
"0: Dump all for `%s`",
"1: Create Save Data for `%s`",
"2: Create All Save Data for `%s`",
"3: Delete All Save Data for `%s`"
"3: Delete All Save Data for `%s`",
"4: Import Save Data for `%s`"
],
"WebDavStrings": [
"0: WebDav successfully started!",
@ -289,4 +290,4 @@
"1: No [B]",
"2: OK [A]"
]
}
}

View File

@ -0,0 +1,64 @@
#include "appstates/SaveImportState.hpp"
#include "config/config.hpp"
#include "input.hpp"
SaveImportState::SaveImportState(data::User *user)
: m_user(user)
, m_saveDir()
{
SaveImportState::initialize_static_members();
SaveImportState::initialize_directory_menu();
}
void SaveImportState::update()
{
const bool hasFocus = BaseState::has_focus();
sm_slidePanel->update(hasFocus);
if (!sm_slidePanel->is_open()) { return; }
const bool aPressed = input::button_pressed(HidNpadButton_A);
const bool bPressed = input::button_pressed(HidNpadButton_B);
if (aPressed) {}
else if (bPressed) { sm_slidePanel->close(); }
else if (sm_slidePanel->is_closed()) { SaveImportState::deactivate_state(); }
}
void SaveImportState::render()
{
const bool hasFocus = BaseState::has_focus();
sm_slidePanel->clear_target();
sm_slidePanel->render(sdl::Texture::Null, hasFocus);
}
void SaveImportState::initialize_static_members()
{
static constexpr int SIZE_PANEL_WIDTH = 512;
if (sm_saveMenu && sm_slidePanel) { return; }
sm_saveMenu = ui::Menu::create(8, 8, 492, 22, 720);
sm_slidePanel = ui::SlideOutPanel::create(SIZE_PANEL_WIDTH, ui::SlideOutPanel::Side::Right);
sm_slidePanel->push_new_element(sm_saveMenu);
}
void SaveImportState::initialize_directory_menu()
{
const fslib::Path saveDirPath{config::get_working_directory() / "saves"};
m_saveDir.open(saveDirPath);
if (!m_saveDir.is_open()) { return; }
for (const fslib::DirectoryEntry &entry : m_saveDir) { sm_saveMenu->add_option(entry.get_filename()); }
}
void SaveImportState::deactivate_state()
{
sm_saveMenu->reset();
sm_slidePanel->reset();
BaseState::deactivate();
}

View File

@ -5,6 +5,7 @@
#include "appstates/MainMenuState.hpp"
#include "appstates/ProgressState.hpp"
#include "appstates/SaveCreateState.hpp"
#include "appstates/SaveImportState.hpp"
#include "appstates/TaskState.hpp"
#include "config/config.hpp"
#include "data/data.hpp"
@ -28,7 +29,8 @@ namespace
BACKUP_ALL,
CREATE_SAVE,
CREATE_ALL_SAVE,
DELETE_ALL_SAVE
DELETE_ALL_SAVE,
IMPORT_SAVE_DATA
};
} // namespace
@ -64,10 +66,11 @@ void UserOptionState::update()
switch (selected)
{
case BACKUP_ALL: UserOptionState::backup_all(); break;
case CREATE_SAVE: UserOptionState::create_save_create(); break;
case CREATE_ALL_SAVE: UserOptionState::create_all_save_data(); break;
case DELETE_ALL_SAVE: UserOptionState::delete_all_save_data(); break;
case BACKUP_ALL: UserOptionState::backup_all(); break;
case CREATE_SAVE: UserOptionState::create_save_create(); break;
case CREATE_ALL_SAVE: UserOptionState::create_all_save_data(); break;
case DELETE_ALL_SAVE: UserOptionState::delete_all_save_data(); break;
case IMPORT_SAVE_DATA: UserOptionState::create_push_save_import(); break;
}
}
else if (bPressed) { sm_menuPanel->close(); }
@ -159,6 +162,15 @@ void UserOptionState::delete_all_save_data()
ConfirmTask::create_push_fade(queryString, true, tasks::useroptions::delete_all_save_data_for_user, m_dataStruct);
}
void UserOptionState::create_push_save_import()
{
const fslib::Path saveDirPath{config::get_working_directory() / "saves"};
if (!fs::directory_has_contents(saveDirPath)) { return; }
sm_menuPanel->hide();
SaveImportState::create_and_push(m_user);
}
void UserOptionState::deactivate_state()
{
sm_menuPanel->clear_elements();

View File

@ -33,6 +33,7 @@ void logger::initialize()
void logger::log(const char *format, ...) noexcept
{
static std::mutex logLock{};
static const fslib::Path logPath{PATH_JKSV_LOG};
std::array<char, VA_BUFFER_SIZE> vaBuffer = {0};
std::va_list vaList{};
@ -46,7 +47,6 @@ void logger::log(const char *format, ...) noexcept
std::strftime(timeDateBuffer.data(), TIME_BUFFER, "[%c] ", &localTime);
std::lock_guard<std::mutex> logGuard(logLock);
const fslib::Path logPath{PATH_JKSV_LOG};
fslib::File logFile(logPath, FsOpenMode_Append);
if (logFile.is_open() && logFile.get_size() >= SIZE_LOG_LIMIT)
{