Fix reading not existing savedata (#2941)

* Fix reading not existing savedata

* alloc save memory instead return error

* save memory saze to save slot instead of global

* remove unused enum

* remove unneeded memory clean
This commit is contained in:
mailwl 2025-05-16 19:22:47 +03:00 committed by GitHub
parent 4d769d9c7e
commit ffd31589cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 44 additions and 34 deletions

View file

@ -409,6 +409,7 @@ set(SYSTEM_LIBS src/core/libraries/system/commondialog.cpp
src/core/libraries/save_data/save_memory.h
src/core/libraries/save_data/savedata.cpp
src/core/libraries/save_data/savedata.h
src/core/libraries/save_data/savedata_error.h
src/core/libraries/save_data/dialog/savedatadialog.cpp
src/core/libraries/save_data/dialog/savedatadialog.h
src/core/libraries/save_data/dialog/savedatadialog_ui.cpp

View file

@ -10,15 +10,14 @@
#include <utility>
#include <fmt/format.h>
#include <core/libraries/system/msgdialog_ui.h>
#include "common/assert.h"
#include "boost/icl/concept/interval.hpp"
#include "common/elf_info.h"
#include "common/logging/log.h"
#include "common/path_util.h"
#include "common/singleton.h"
#include "common/thread.h"
#include "core/file_sys/fs.h"
#include "core/libraries/system/msgdialog_ui.h"
#include "save_instance.h"
using Common::FS::IOFile;
@ -35,11 +34,12 @@ namespace Libraries::SaveData::SaveMemory {
static Core::FileSys::MntPoints* g_mnt = Common::Singleton<Core::FileSys::MntPoints>::Instance();
struct SlotData {
OrbisUserServiceUserId user_id;
OrbisUserServiceUserId user_id{};
std::string game_serial;
std::filesystem::path folder_path;
PSF sfo;
std::vector<u8> memory_cache;
size_t memory_cache_size{};
};
static std::mutex g_slot_mtx;
@ -97,7 +97,8 @@ std::filesystem::path GetSavePath(OrbisUserServiceUserId user_id, u32 slot_id,
return SaveInstance::MakeDirSavePath(user_id, Common::ElfInfo::Instance().GameSerial(), dir);
}
size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_view game_serial) {
size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_view game_serial,
size_t memory_size) {
std::lock_guard lck{g_slot_mtx};
const auto save_dir = GetSavePath(user_id, slot_id, game_serial);
@ -109,6 +110,7 @@ size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_
.folder_path = save_dir,
.sfo = {},
.memory_cache = {},
.memory_cache_size = memory_size,
};
SaveInstance::SetupDefaultParamSFO(data.sfo, GetSaveDir(slot_id), std::string{game_serial});
@ -196,9 +198,9 @@ void ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
auto& data = g_attached_slots[slot_id];
auto& memory = data.memory_cache;
if (memory.empty()) { // Load file
memory.resize(data.memory_cache_size);
IOFile f{data.folder_path / FilenameSaveDataMemory, Common::FS::FileAccessMode::Read};
if (f.IsOpen()) {
memory.resize(f.GetSize());
f.Seek(0);
f.ReadSpan(std::span{memory});
}
@ -222,5 +224,4 @@ void WriteMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
Backup::NewRequest(data.user_id, data.game_serial, GetSaveDir(slot_id),
Backup::OrbisSaveDataEventType::__DO_NOT_SAVE);
}
} // namespace Libraries::SaveData::SaveMemory

View file

@ -4,13 +4,13 @@
#pragma once
#include <vector>
#include "save_backup.h"
#include "core/libraries/save_data/save_backup.h"
class PSF;
namespace Libraries::SaveData {
using OrbisUserServiceUserId = s32;
}
} // namespace Libraries::SaveData
namespace Libraries::SaveData::SaveMemory {
@ -22,7 +22,8 @@ void PersistMemory(u32 slot_id, bool lock = true);
std::string_view game_serial);
// returns the size of the save memory if exists
size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_view game_serial);
size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_view game_serial,
size_t memory_size);
// Write the icon. Set buf to null to read the standard icon.
void SetIcon(u32 slot_id, void* buf = nullptr, size_t buf_size = 0);

View file

@ -5,7 +5,6 @@
#include <thread>
#include <vector>
#include <core/libraries/system/msgdialog_ui.h>
#include <magic_enum/magic_enum.hpp>
#include "common/assert.h"
@ -20,7 +19,9 @@
#include "core/libraries/error_codes.h"
#include "core/libraries/libs.h"
#include "core/libraries/save_data/savedata.h"
#include "core/libraries/save_data/savedata_error.h"
#include "core/libraries/system/msgdialog.h"
#include "core/libraries/system/msgdialog_ui.h"
#include "save_backup.h"
#include "save_instance.h"
#include "save_memory.h"
@ -33,27 +34,6 @@ using Common::ElfInfo;
namespace Libraries::SaveData {
enum class Error : u32 {
OK = 0,
USER_SERVICE_NOT_INITIALIZED = 0x80960002,
PARAMETER = 0x809F0000,
NOT_INITIALIZED = 0x809F0001,
OUT_OF_MEMORY = 0x809F0002,
BUSY = 0x809F0003,
NOT_MOUNTED = 0x809F0004,
EXISTS = 0x809F0007,
NOT_FOUND = 0x809F0008,
NO_SPACE_FS = 0x809F000A,
INTERNAL = 0x809F000B,
MOUNT_FULL = 0x809F000C,
BAD_MOUNTED = 0x809F000D,
BROKEN = 0x809F000F,
INVALID_LOGIN_USER = 0x809F0011,
MEMORY_NOT_READY = 0x809F0012,
BACKUP_BUSY = 0x809F0013,
BUSY_FOR_SAVING = 0x809F0016,
};
enum class OrbisSaveDataSaveDataMemoryOption : u32 {
NONE = 0,
SET_PARAM = 1 << 0,
@ -1593,8 +1573,8 @@ Error PS4_SYSV_ABI sceSaveDataSetupSaveDataMemory2(const OrbisSaveDataMemorySetu
}
try {
size_t existed_size =
SaveMemory::SetupSaveMemory(setupParam->userId, slot_id, g_game_serial);
size_t existed_size = SaveMemory::SetupSaveMemory(setupParam->userId, slot_id,
g_game_serial, setupParam->memorySize);
if (existed_size == 0) { // Just created
if (g_fw_ver >= ElfInfo::FW_45 && setupParam->initParam != nullptr) {
auto& sfo = SaveMemory::GetParamSFO(slot_id);

View file

@ -0,0 +1,27 @@
// SPDX-FileCopyrightText: Copyright 2024 shadPS4 Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#pragma once
namespace Libraries::SaveData {
enum class Error : u32 {
OK = 0,
USER_SERVICE_NOT_INITIALIZED = 0x80960002,
PARAMETER = 0x809F0000,
NOT_INITIALIZED = 0x809F0001,
OUT_OF_MEMORY = 0x809F0002,
BUSY = 0x809F0003,
NOT_MOUNTED = 0x809F0004,
EXISTS = 0x809F0007,
NOT_FOUND = 0x809F0008,
NO_SPACE_FS = 0x809F000A,
INTERNAL = 0x809F000B,
MOUNT_FULL = 0x809F000C,
BAD_MOUNTED = 0x809F000D,
BROKEN = 0x809F000F,
INVALID_LOGIN_USER = 0x809F0011,
MEMORY_NOT_READY = 0x809F0012,
BACKUP_BUSY = 0x809F0013,
BUSY_FOR_SAVING = 0x809F0016,
};
} // namespace Libraries::SaveData