savedata: auto-restore backup if needed

This commit is contained in:
Vinicius Rangel 2025-01-30 03:59:52 -03:00
parent 2dcd6093ab
commit 9fa5014d16
No known key found for this signature in database
GPG Key ID: A5B154D904B761D9
3 changed files with 18 additions and 7 deletions

View File

@ -10,6 +10,7 @@
#include "common/path_util.h"
#include "common/singleton.h"
#include "core/file_sys/fs.h"
#include "save_backup.h"
#include "save_instance.h"
constexpr auto OrbisSaveDataBlocksMin2 = 96; // 3MiB
@ -140,7 +141,8 @@ SaveInstance& SaveInstance::operator=(SaveInstance&& other) noexcept {
return *this;
}
void SaveInstance::SetupAndMount(bool read_only, bool copy_icon, bool ignore_corrupt) {
void SaveInstance::SetupAndMount(bool read_only, bool copy_icon, bool ignore_corrupt,
bool dont_restore_backup) {
if (mounted) {
UNREACHABLE_MSG("Save instance is already mounted");
}
@ -159,13 +161,21 @@ void SaveInstance::SetupAndMount(bool read_only, bool copy_icon, bool ignore_cor
}
exists = true;
} else {
std::optional<fs::filesystem_error> err;
if (!ignore_corrupt && fs::exists(corrupt_file_path)) {
throw fs::filesystem_error("Corrupted save data", corrupt_file_path,
err = fs::filesystem_error("Corrupted save data", corrupt_file_path,
std::make_error_code(std::errc::illegal_byte_sequence));
} else if (!param_sfo.Open(param_sfo_path)) {
err = fs::filesystem_error("Failed to read param.sfo", param_sfo_path,
std::make_error_code(std::errc::illegal_byte_sequence));
}
if (!param_sfo.Open(param_sfo_path)) {
throw fs::filesystem_error("Failed to read param.sfo", param_sfo_path,
std::make_error_code(std::errc::illegal_byte_sequence));
if (err.has_value()) {
if (dont_restore_backup) {
throw err.value();
}
if (Backup::Restore(save_path)) {
return SetupAndMount(read_only, copy_icon, ignore_corrupt, true);
}
}
}

View File

@ -78,7 +78,8 @@ public:
SaveInstance& operator=(const SaveInstance& other) = delete;
SaveInstance& operator=(SaveInstance&& other) noexcept;
void SetupAndMount(bool read_only = false, bool copy_icon = false, bool ignore_corrupt = false);
void SetupAndMount(bool read_only = false, bool copy_icon = false, bool ignore_corrupt = false,
bool dont_restore_backup = false);
void Umount();

View File

@ -126,7 +126,7 @@ size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_
const auto memory = save_dir / FilenameSaveDataMemory;
if (fs::exists(memory)) {
return {fs::file_size(memory)};
return fs::file_size(memory);
}
return 0;