TitleOptionState: Add system save protections.

This commit is contained in:
J-D-K 2025-06-09 14:58:10 -04:00
parent 12deaa343c
commit 5d14f26495
8 changed files with 72 additions and 22 deletions

View File

@ -32,7 +32,7 @@ include $(DEVKITPRO)/libnx/switch_rules
#---------------------------------------------------------------------------------
TARGET := JKSV
BUILD := build
SOURCES := source source/appstates source/ui source/data source/system source/fs
SOURCES := source source/appstates source/ui source/data source/system source/fs source/curl
DATA := data
INCLUDES := include ./Libraries/FsLib/Switch/FsLib/include ./Libraries/SDLLib/SDL/include
EXEFS_SRC := exefs_src

View File

@ -28,6 +28,19 @@ namespace curl
/// @brief Exits libcurl
void exit(void);
/// @brief Inline templated function to wrap curl_easy_setopt and make using curl::Handle slightly easier.
/// @tparam Option Templated type of the option. This is a headache so let the compiler figure it out.
/// @tparam Value Templated type of the value to set the option too. See above.
/// @param handle curl::Handle the option is being set for.
/// @param option CURLOPT to set.
/// @param value Value to set the CURLOPT to.
/// @return CURLcode returned from curl_easy_setopt.
template <typename Option, typename Value>
static inline CURLcode set_option(curl::Handle &handle, Option option, Value value)
{
return curl_easy_setopt(handle.get(), option, value);
}
/// @brief Inline function that returns a self cleaning curl handle.
/// @return Curl handle.
static inline curl::Handle new_handle(void)
@ -67,19 +80,6 @@ namespace curl
return true;
}
/// @brief Inline templated function to wrap curl_easy_setopt and make using curl::Handle slightly easier.
/// @tparam Option Templated type of the option. This is a headache so let the compiler figure it out.
/// @tparam Value Templated type of the value to set the option too. See above.
/// @param handle curl::Handle the option is being set for.
/// @param option CURLOPT to set.
/// @param value Value to set the CURLOPT to.
/// @return CURLcode returned from curl_easy_setopt.
template <typename Option, typename Value>
static inline CURLcode set_option(curl::Handle &handle, Option option, Value value)
{
return curl_easy_setopt(handle.get(), option, value);
}
/// @brief Inline wrapper function to make adding to HeaderList simpler.
/// @param headerList Header list to append to.
/// @param header Header to append.
@ -125,7 +125,7 @@ namespace curl
/// @param count Element count.
/// @param target File to write the data to.
/// @return Number of bytes written to the file.
size_t write_data_to_file(const char *buffer, size_t size, size_t count, fslib::File &target);
size_t write_data_to_file(const char *buffer, size_t size, size_t count, fslib::File *target);
/// @brief Gets the value of a header from an array of headers.
/// @param array Array of headers to search.

View File

@ -21,4 +21,10 @@ namespace fs
/// @param journalSize Size of the journaling space.
/// @return True on success. False on failure.
bool extend_save_data(const FsSaveDataInfo *saveInfo, int64_t size, int64_t journalSize);
/// @brief Returns whether or not the saveInfo passed is system type.
/// @param saveInfo FsSaveDataInfo to check.
/// @return True if it is. False if it isn't.
/// @note The config setting overrides this.
bool is_system_save_data(const FsSaveDataInfo *saveInfo);
} // namespace fs

View File

@ -147,7 +147,8 @@
"Error resetting save data!",
"Save data successfully reset!",
"SVI file exported successfully!",
"Error exporting SVI file!"
"Error exporting SVI file!",
"This option is unavailable for system saves!"
],
"TitleOptionConfirmations": [
"Are you sure you want to add #%s# to your blacklist? Once you do this, it will no longer appear on any title list or selection.",

View File

@ -2,6 +2,7 @@
#include "appstates/MainMenuState.hpp"
#include "colors.hpp"
#include "config.hpp"
#include "curl/curl.hpp"
#include "data/data.hpp"
#include "fslib.hpp"
#include "input.hpp"
@ -22,10 +23,8 @@ namespace
{
/// @brief Build month.
constexpr uint8_t BUILD_MON = 5;
/// @brief Build day.
constexpr uint8_t BUILD_DAY = 31;
/// @brief Year.
constexpr uint16_t BUILD_YEAR = 2025;
} // namespace
@ -70,6 +69,7 @@ JKSV::JKSV(void)
ABORT_ON_FAILURE(initialize_service(setInitialize, "Set"));
ABORT_ON_FAILURE(initialize_service(setsysInitialize, "SetSys"));
ABORT_ON_FAILURE(initialize_service(socketInitializeDefault, "Socket"));
ABORT_ON_FAILURE(curl::initialize());
// Input doesn't have anything to return.
input::initialize();
@ -127,6 +127,7 @@ JKSV::~JKSV()
// Try to save config first.
config::save();
curl::exit();
socketExit();
setsysExit();
setExit();

View File

@ -144,6 +144,15 @@ void TitleOptionState::update(void)
case RESET_SAVE_DATA:
{
// Need to check this first. For safety.
FsSaveDataInfo *saveInfo = m_targetUser->get_save_info_by_id(m_titleInfo->get_application_id());
if (!config::get_by_key(config::keys::ALLOW_WRITING_TO_SYSTEM) || fs::is_system_save_data(saveInfo))
{
ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_MESSAGE_TICKS,
strings::get_by_name(strings::names::TITLE_OPTION_POPS, 6));
return;
}
// String
std::string confirmString = stringutil::get_formatted_string(
strings::get_by_name(strings::names::TITLE_OPTION_CONFIRMATIONS, 2),
@ -166,6 +175,14 @@ void TitleOptionState::update(void)
case DELETE_SAVE_FROM_SYSTEM:
{
FsSaveDataInfo *saveInfo = m_targetUser->get_save_info_by_id(m_titleInfo->get_application_id());
if (!config::get_by_key(config::keys::ALLOW_WRITING_TO_SYSTEM) || fs::is_system_save_data(saveInfo))
{
ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_MESSAGE_TICKS,
strings::get_by_name(strings::names::TITLE_OPTION_POPS, 6));
return;
}
// String
std::string confirmString = stringutil::get_formatted_string(
strings::get_by_name(strings::names::TITLE_OPTION_CONFIRMATIONS, 3),
@ -190,6 +207,14 @@ void TitleOptionState::update(void)
case EXTEND_CONTAINER:
{
FsSaveDataInfo *saveInfo = m_targetUser->get_save_info_by_id(m_titleInfo->get_application_id());
if (!config::get_by_key(config::keys::ALLOW_WRITING_TO_SYSTEM) || fs::is_system_save_data(saveInfo))
{
ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_MESSAGE_TICKS,
strings::get_by_name(strings::names::TITLE_OPTION_POPS, 6));
return;
}
// Data
std::shared_ptr<TargetStruct> data = std::make_shared<TargetStruct>();
data->m_targetUser = m_targetUser;
@ -202,6 +227,12 @@ void TitleOptionState::update(void)
case EXPORT_SVI:
{
// This type of save data can't have this exported anyway.
FsSaveDataInfo *saveInfo = m_targetUser->get_save_info_by_id(m_titleInfo->get_application_id());
if (fs::is_system_save_data(saveInfo))
{
return;
}
export_svi_file(m_titleInfo);
}
break;
@ -459,3 +490,10 @@ static void export_svi_file(data::TitleInfo *titleInfo)
ui::PopMessageManager::push_message(ui::PopMessageManager::DEFAULT_MESSAGE_TICKS,
strings::get_by_name(strings::names::TITLE_OPTION_POPS, 4));
}
static bool is_system_save_data(const FsSaveDataInfo *saveInfo)
{
// The config setting will override this
return config::get_by_key(config::keys::ALLOW_WRITING_TO_SYSTEM) ||
saveInfo->save_data_type == FsSaveDataType_System || saveInfo->save_data_type == FsSaveDataType_SystemBcat;
}

View File

@ -1,4 +1,4 @@
#include "curl.hpp"
#include "curl/curl.hpp"
#include "stringutil.hpp"
namespace
@ -82,7 +82,7 @@ void curl::prepare_get(curl::Handle &curl)
// Setup basic request.
curl::set_option(curl, CURLOPT_HTTPGET, 1L);
curl::set_option(curl, CURLOPT_ACCEPT_ENCODING, "") // I think this is how you set the defaults for this?
curl::set_option(curl, CURLOPT_ACCEPT_ENCODING, ""); // I think this is how you set the defaults for this?
}
void curl::prepare_post(curl::Handle &curl)
@ -99,6 +99,5 @@ void curl::prepare_upload(curl::Handle &curl)
curl::set_option(curl, CURLOPT_UPLOAD, 1L);
curl::set_option(curl, CURLOPT_UPLOAD_BUFFERSIZE, SIZE_UPLOAD_BUFFER);
curl::set_option(curl, CURLOPT_ACCEPT_ENCODING, "") // Not really sure this will have any affect here...
curl::set_option(curl, CURLOPT_ACCEPT_ENCODING, ""); // Not really sure this will have any affect here...
}
`

View File

@ -75,3 +75,8 @@ bool fs::extend_save_data(const FsSaveDataInfo *saveInfo, int64_t size, int64_t
}
return true;
}
bool fs::is_system_save_data(const FsSaveDataInfo *saveInfo)
{
return saveInfo->save_data_type == FsSaveDataType_System || saveInfo->save_data_type == FsSaveDataType_SystemBcat;
}