From 8e2b73b05f7f8b03236a4ae4e68227e27c396360 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Sep 2021 19:32:37 +0200 Subject: [PATCH 1/6] Optimize MetaFileSystem::ReadEntireFile --- Core/Dialog/SavedataParam.cpp | 2 +- Core/FileSystems/MetaFileSystem.cpp | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 6af370b696..22879d326f 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -649,7 +649,7 @@ int SavedataParam::LoadSaveData(SceUtilitySavedataParam *param, const std::strin std::string filename = GetFileName(param); std::string filePath = dirPath + "/" + filename; s64 readSize; - INFO_LOG(SCEUTILITY,"Loading file with size %u in %s",param->dataBufSize,filePath.c_str()); + INFO_LOG(SCEUTILITY, "Loading file with size %u in %s", param->dataBufSize, filePath.c_str()); u8 *saveData = nullptr; int saveSize = -1; if (!ReadPSPFile(filePath, &saveData, saveSize, &readSize)) { diff --git a/Core/FileSystems/MetaFileSystem.cpp b/Core/FileSystems/MetaFileSystem.cpp index c726a87b51..7192630ae2 100644 --- a/Core/FileSystems/MetaFileSystem.cpp +++ b/Core/FileSystems/MetaFileSystem.cpp @@ -588,7 +588,9 @@ int MetaFileSystem::ReadEntireFile(const std::string &filename, std::vector if (handle < 0) return handle; - size_t dataSize = (size_t)GetFileInfo(filename).size; + SeekFile(handle, 0, FILEMOVE_END); + size_t dataSize = GetSeekPos(handle); + SeekFile(handle, 0, FILEMOVE_BEGIN); data.resize(dataSize); size_t result = ReadFile(handle, (u8 *)&data[0], dataSize); From 464af0723e30fc9bba698c9bc40405dffd67ed5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Sep 2021 19:32:48 +0200 Subject: [PATCH 2/6] Minor gradle upgrade, why not --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index a6beab90bd..a2dd2770f6 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:7.0.1' + classpath 'com.android.tools.build:gradle:7.0.2' } } From 5d53e59c1d5380c8c3984a191627809c49117a07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Sep 2021 19:33:28 +0200 Subject: [PATCH 3/6] Optimize ReadPSPFile (which should probably be replaced with ReadEntireFile anyway) --- Core/Dialog/SavedataParam.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 22879d326f..3d9b260ee4 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -66,9 +66,12 @@ namespace if (handle < 0) return false; - if(dataSize == -1) - { - dataSize = pspFileSystem.GetFileInfo(filename).size; + if (dataSize == -1) { + // Determine the size through seeking instead of querying. + pspFileSystem.SeekFile(handle, 0, FILEMOVE_END); + dataSize = pspFileSystem.GetSeekPos(handle); + pspFileSystem.SeekFile(handle, 0, FILEMOVE_BEGIN); + *data = new u8[(size_t)dataSize]; } From 36079b86d0f4155fddc476845fc0404b5f19591c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Sep 2021 19:53:19 +0200 Subject: [PATCH 4/6] Optimize away a couple of existence checks --- Core/Dialog/SavedataParam.cpp | 41 +++++++++++++---------------- Core/Dialog/SavedataParam.h | 2 +- Core/FileSystems/MetaFileSystem.cpp | 1 + 3 files changed, 21 insertions(+), 23 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 3d9b260ee4..14c6860f39 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -602,19 +602,19 @@ int SavedataParam::Load(SceUtilitySavedataParam *param, const std::string &saveD return isRWMode ? SCE_UTILITY_SAVEDATA_ERROR_RW_NO_DATA : SCE_UTILITY_SAVEDATA_ERROR_LOAD_NO_DATA; } - if (!pspFileSystem.GetFileInfo(sfoPath).exists) + // Load sfo + if (!LoadSFO(param, dirPath)) { return isRWMode ? SCE_UTILITY_SAVEDATA_ERROR_RW_DATA_BROKEN : SCE_UTILITY_SAVEDATA_ERROR_LOAD_DATA_BROKEN; + } if (fileName != "" && !pspFileSystem.GetFileInfo(filePath).exists) { return isRWMode ? SCE_UTILITY_SAVEDATA_ERROR_RW_FILE_NOT_FOUND : SCE_UTILITY_SAVEDATA_ERROR_LOAD_FILE_NOT_FOUND; } - LoadSFO(param, dirPath); // Load sfo - // Don't know what it is, but PSP always respond this and this unlock some game param->bind = 1021; - // Load another files,seems these are required by some games, e.g. Fushigi no Dungeon Fuurai no Shiren 4 Plus. + // Load another files, seems these are required by some games, e.g. Fushigi no Dungeon Fuurai no Shiren 4 Plus. // Load ICON0.PNG LoadFile(dirPath, ICON0_FILENAME, ¶m->icon0FileData); @@ -793,35 +793,33 @@ u32 SavedataParam::LoadNotCryptedSave(SceUtilitySavedataParam *param, u8 *data, return 0; } -void SavedataParam::LoadSFO(SceUtilitySavedataParam *param, const std::string& dirPath) { +bool SavedataParam::LoadSFO(SceUtilitySavedataParam *param, const std::string& dirPath) { ParamSFOData sfoFile; - std::string sfopath = dirPath+"/" + SFO_FILENAME; - PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath); - if (sfoInfo.exists) { - // Read sfo - std::vector sfoData; - if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) { - sfoFile.ReadSFO(sfoData); + std::string sfopath = dirPath + "/" + SFO_FILENAME; + std::vector sfoData; + if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) { + sfoFile.ReadSFO(sfoData); - // copy back info in request - strncpy(param->sfoParam.title,sfoFile.GetValueString("TITLE").c_str(),128); - strncpy(param->sfoParam.savedataTitle,sfoFile.GetValueString("SAVEDATA_TITLE").c_str(),128); - strncpy(param->sfoParam.detail,sfoFile.GetValueString("SAVEDATA_DETAIL").c_str(),1024); - param->sfoParam.parentalLevel = sfoFile.GetValueInt("PARENTAL_LEVEL"); - } + // copy back info in request + strncpy(param->sfoParam.title,sfoFile.GetValueString("TITLE").c_str(),128); + strncpy(param->sfoParam.savedataTitle,sfoFile.GetValueString("SAVEDATA_TITLE").c_str(),128); + strncpy(param->sfoParam.detail,sfoFile.GetValueString("SAVEDATA_DETAIL").c_str(),1024); + param->sfoParam.parentalLevel = sfoFile.GetValueInt("PARENTAL_LEVEL"); } + return true; } std::vector SavedataParam::GetSFOEntries(const std::string &dirPath) { std::vector result; const std::string sfoPath = dirPath + "/" + SFO_FILENAME; - if (!pspFileSystem.GetFileInfo(sfoPath).exists) - return result; ParamSFOData sfoFile; std::vector sfoData; - if (pspFileSystem.ReadEntireFile(dirPath + "/" + SFO_FILENAME, sfoData) >= 0) + if (pspFileSystem.ReadEntireFile(sfoPath, sfoData) >= 0) { sfoFile.ReadSFO(sfoData); + } else { + return result; + } const int FILE_LIST_COUNT_MAX = 99; u32 sfoFileListSize = 0; @@ -1164,7 +1162,6 @@ int SavedataParam::GetSizes(SceUtilitySavedataParam *param) NotifyMemInfo(MemBlockFlags::WRITE, param->utilityData.ptr, sizeof(SceUtilitySavedataUsedDataInfo), "SavedataGetSizes"); } return ret; - } bool SavedataParam::GetList(SceUtilitySavedataParam *param) diff --git a/Core/Dialog/SavedataParam.h b/Core/Dialog/SavedataParam.h index 7011d45e37..fc215a97b9 100644 --- a/Core/Dialog/SavedataParam.h +++ b/Core/Dialog/SavedataParam.h @@ -366,7 +366,7 @@ private: int LoadSaveData(SceUtilitySavedataParam *param, const std::string &saveDirName, const std::string& dirPath, bool secureMode); u32 LoadCryptedSave(SceUtilitySavedataParam *param, u8 *data, const u8 *saveData, int &saveSize, int prevCryptMode, const u8 *expectedHash, bool &saveDone); u32 LoadNotCryptedSave(SceUtilitySavedataParam *param, u8 *data, u8 *saveData, int &saveSize); - void LoadSFO(SceUtilitySavedataParam *param, const std::string& dirPath); + bool LoadSFO(SceUtilitySavedataParam *param, const std::string& dirPath); void LoadFile(const std::string& dirPath, const std::string& filename, PspUtilitySavedataFileData *fileData); int DecryptSave(unsigned int mode, unsigned char *data, int *dataLen, int *alignedLen, unsigned char *cryptkey, const u8 *expectedHash); diff --git a/Core/FileSystems/MetaFileSystem.cpp b/Core/FileSystems/MetaFileSystem.cpp index 7192630ae2..4ac716aa57 100644 --- a/Core/FileSystems/MetaFileSystem.cpp +++ b/Core/FileSystems/MetaFileSystem.cpp @@ -598,6 +598,7 @@ int MetaFileSystem::ReadEntireFile(const std::string &filename, std::vector if (result != dataSize) return SCE_KERNEL_ERROR_ERROR; + return 0; } From 71056e3486439847cc5e791d661ee27b274ee4ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Sep 2021 20:26:36 +0200 Subject: [PATCH 5/6] SFO return value fix, oops --- Core/Dialog/SavedataParam.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 14c6860f39..19cb39097c 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -805,8 +805,10 @@ bool SavedataParam::LoadSFO(SceUtilitySavedataParam *param, const std::string& d strncpy(param->sfoParam.savedataTitle,sfoFile.GetValueString("SAVEDATA_TITLE").c_str(),128); strncpy(param->sfoParam.detail,sfoFile.GetValueString("SAVEDATA_DETAIL").c_str(),1024); param->sfoParam.parentalLevel = sfoFile.GetValueInt("PARENTAL_LEVEL"); + return true; + } else { + return false; } - return true; } std::vector SavedataParam::GetSFOEntries(const std::string &dirPath) { From b05a74803dc3b52cf9f4b72c938d39e1f119f849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 11 Sep 2021 20:31:42 +0200 Subject: [PATCH 6/6] Remove another redundant check, comment fixes --- Core/Dialog/SavedataParam.cpp | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/Core/Dialog/SavedataParam.cpp b/Core/Dialog/SavedataParam.cpp index 19cb39097c..4e3ad43f3e 100644 --- a/Core/Dialog/SavedataParam.cpp +++ b/Core/Dialog/SavedataParam.cpp @@ -453,9 +453,7 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD // SAVE PARAM.SFO ParamSFOData sfoFile; - std::string sfopath = dirPath+"/" + SFO_FILENAME; - PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath); - if (sfoInfo.exists) // Read old sfo if exist + std::string sfopath = dirPath + "/" + SFO_FILENAME; { std::vector sfoData; if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) @@ -575,14 +573,12 @@ int SavedataParam::Save(SceUtilitySavedataParam* param, const std::string &saveD std::string pic1path = dirPath + "/" + PIC1_FILENAME; WritePSPFile(pic1path, param->pic1FileData.buf, param->pic1FileData.size); } - // Save SND if (param->snd0FileData.buf.IsValid()) { std::string snd0path = dirPath + "/" + SND0_FILENAME; WritePSPFile(snd0path, param->snd0FileData.buf, param->snd0FileData.size); } - return 0; } @@ -614,7 +610,7 @@ int SavedataParam::Load(SceUtilitySavedataParam *param, const std::string &saveD // Don't know what it is, but PSP always respond this and this unlock some game param->bind = 1021; - // Load another files, seems these are required by some games, e.g. Fushigi no Dungeon Fuurai no Shiren 4 Plus. + // Load other files, seems these are required by some games, e.g. Fushigi no Dungeon Fuurai no Shiren 4 Plus. // Load ICON0.PNG LoadFile(dirPath, ICON0_FILENAME, ¶m->icon0FileData); @@ -1180,11 +1176,11 @@ bool SavedataParam::GetList(SceUtilitySavedataParam *param) std::vector sfoFiles; std::vector allDir = pspFileSystem.GetDirListing(savePath); - std::string searchString = GetGameName(param)+GetSaveName(param); + std::string searchString = GetGameName(param) + GetSaveName(param); for (size_t i = 0; i < allDir.size() && validDir.size() < maxFile; i++) { std::string dirName = allDir[i].name; - if(PSPMatch(dirName, searchString)) + if (PSPMatch(dirName, searchString)) { validDir.push_back(allDir[i]); } @@ -1192,7 +1188,7 @@ bool SavedataParam::GetList(SceUtilitySavedataParam *param) PSPFileInfo sfoFile; for (size_t i = 0; i < validDir.size(); ++i) { - // GetFileName(param) == NUll here + // GetFileName(param) == null here // so use sfo files to set the date. sfoFile = pspFileSystem.GetFileInfo(savePath + validDir[i].name + "/" + SFO_FILENAME); sfoFiles.push_back(sfoFile);