save memory saze to save slot instead of global

This commit is contained in:
mailwl 2025-05-16 18:21:03 +03:00
parent 289e9fb5ab
commit 8e972e81f4
3 changed files with 10 additions and 16 deletions

View File

@ -40,11 +40,11 @@ struct SlotData {
std::filesystem::path folder_path; std::filesystem::path folder_path;
PSF sfo; PSF sfo;
std::vector<u8> memory_cache; std::vector<u8> memory_cache;
size_t memory_cache_size{};
}; };
static std::mutex g_slot_mtx; static std::mutex g_slot_mtx;
static std::unordered_map<u32, SlotData> g_attached_slots; static std::unordered_map<u32, SlotData> g_attached_slots;
static size_t g_memory_size = 0;
void PersistMemory(u32 slot_id, bool lock) { void PersistMemory(u32 slot_id, bool lock) {
std::unique_lock lck{g_slot_mtx, std::defer_lock}; std::unique_lock lck{g_slot_mtx, std::defer_lock};
@ -98,7 +98,8 @@ std::filesystem::path GetSavePath(OrbisUserServiceUserId user_id, u32 slot_id,
return SaveInstance::MakeDirSavePath(user_id, Common::ElfInfo::Instance().GameSerial(), dir); 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}; std::lock_guard lck{g_slot_mtx};
const auto save_dir = GetSavePath(user_id, slot_id, game_serial); const auto save_dir = GetSavePath(user_id, slot_id, game_serial);
@ -110,6 +111,7 @@ size_t SetupSaveMemory(OrbisUserServiceUserId user_id, u32 slot_id, std::string_
.folder_path = save_dir, .folder_path = save_dir,
.sfo = {}, .sfo = {},
.memory_cache = {}, .memory_cache = {},
.memory_cache_size = memory_size,
}; };
SaveInstance::SetupDefaultParamSFO(data.sfo, GetSaveDir(slot_id), std::string{game_serial}); SaveInstance::SetupDefaultParamSFO(data.sfo, GetSaveDir(slot_id), std::string{game_serial});
@ -197,8 +199,8 @@ void ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset) {
auto& data = g_attached_slots[slot_id]; auto& data = g_attached_slots[slot_id];
auto& memory = data.memory_cache; auto& memory = data.memory_cache;
if (memory.empty()) { // Load file if (memory.empty()) { // Load file
memory.resize(g_memory_size); memory.resize(data.memory_cache_size);
memset(memory.data(), 0, g_memory_size); memset(memory.data(), 0, data.memory_cache_size);
IOFile f{data.folder_path / FilenameSaveDataMemory, Common::FS::FileAccessMode::Read}; IOFile f{data.folder_path / FilenameSaveDataMemory, Common::FS::FileAccessMode::Read};
if (f.IsOpen()) { if (f.IsOpen()) {
f.Seek(0); f.Seek(0);
@ -224,9 +226,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::NewRequest(data.user_id, data.game_serial, GetSaveDir(slot_id),
Backup::OrbisSaveDataEventType::__DO_NOT_SAVE); Backup::OrbisSaveDataEventType::__DO_NOT_SAVE);
} }
void SetMemorySize(size_t memory_size) {
g_memory_size = memory_size;
}
} // namespace Libraries::SaveData::SaveMemory } // namespace Libraries::SaveData::SaveMemory

View File

@ -23,7 +23,8 @@ void PersistMemory(u32 slot_id, bool lock = true);
std::string_view game_serial); std::string_view game_serial);
// returns the size of the save memory if exists // 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. // 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); void SetIcon(u32 slot_id, void* buf = nullptr, size_t buf_size = 0);
@ -41,6 +42,4 @@ void ReadMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset);
void WriteMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset); void WriteMemory(u32 slot_id, void* buf, size_t buf_size, int64_t offset);
void SetMemorySize(size_t memory_size);
} // namespace Libraries::SaveData::SaveMemory } // namespace Libraries::SaveData::SaveMemory

View File

@ -1565,8 +1565,6 @@ Error PS4_SYSV_ABI sceSaveDataSetupSaveDataMemory2(const OrbisSaveDataMemorySetu
slot_id = setupParam->slotId; slot_id = setupParam->slotId;
} }
SaveMemory::SetMemorySize(setupParam->memorySize);
const auto& save_path = SaveMemory::GetSavePath(setupParam->userId, slot_id, g_game_serial); const auto& save_path = SaveMemory::GetSavePath(setupParam->userId, slot_id, g_game_serial);
for (const auto& instance : g_mount_slots) { for (const auto& instance : g_mount_slots) {
if (instance.has_value() && instance->GetSavePath() == save_path) { if (instance.has_value() && instance->GetSavePath() == save_path) {
@ -1575,8 +1573,8 @@ Error PS4_SYSV_ABI sceSaveDataSetupSaveDataMemory2(const OrbisSaveDataMemorySetu
} }
try { try {
size_t existed_size = size_t existed_size = SaveMemory::SetupSaveMemory(setupParam->userId, slot_id,
SaveMemory::SetupSaveMemory(setupParam->userId, slot_id, g_game_serial); g_game_serial, setupParam->memorySize);
if (existed_size == 0) { // Just created if (existed_size == 0) { // Just created
if (g_fw_ver >= ElfInfo::FW_45 && setupParam->initParam != nullptr) { if (g_fw_ver >= ElfInfo::FW_45 && setupParam->initParam != nullptr) {
auto& sfo = SaveMemory::GetParamSFO(slot_id); auto& sfo = SaveMemory::GetParamSFO(slot_id);