From 43ba9082251cf5a6a794f844bb9c7f297334c8cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 7 Aug 2021 11:54:45 +0200 Subject: [PATCH 1/2] Propagate errors from file identification upwards better. --- Core/Loaders.cpp | 58 +++++++++++++++++++++------------ Core/Loaders.h | 2 +- Core/System.cpp | 9 +++-- Core/Util/GameManager.cpp | 3 +- UI/GameInfoCache.cpp | 4 ++- UI/MainScreen.cpp | 3 +- UI/SavedataScreen.cpp | 6 ++-- android/jni/AndroidContentURI.h | 3 ++ 8 files changed, 58 insertions(+), 30 deletions(-) diff --git a/Core/Loaders.cpp b/Core/Loaders.cpp index 226cb597c3..9eccacc215 100644 --- a/Core/Loaders.cpp +++ b/Core/Loaders.cpp @@ -59,17 +59,19 @@ FileLoader *ConstructFileLoader(const Path &filename) { } // TODO : improve, look in the file more -IdentifiedFileType Identify_File(FileLoader *fileLoader) { +IdentifiedFileType Identify_File(FileLoader *fileLoader, std::string *errorString) { + *errorString = ""; if (fileLoader == nullptr) { - ERROR_LOG(LOADER, "Invalid fileLoader"); + *errorString = "Invalid fileLoader"; return IdentifiedFileType::ERROR_IDENTIFYING; } if (fileLoader->GetPath().size() == 0) { - ERROR_LOG(LOADER, "Invalid filename %s", fileLoader->GetPath().c_str()); + *errorString = "Invalid filename " + fileLoader->GetPath().ToString(); return IdentifiedFileType::ERROR_IDENTIFYING; } if (!fileLoader->Exists()) { + *errorString = "IdentifyFile: File doesn't exist" + fileLoader->GetPath().ToString(); return IdentifiedFileType::ERROR_IDENTIFYING; } @@ -82,10 +84,11 @@ IdentifiedFileType Identify_File(FileLoader *fileLoader) { // each sector in a mode2 image starts with these 12 bytes if (memcmp(sync,"\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 12) == 0) { + *errorString = "ISO in Mode 2: Not a PSP game"; return IdentifiedFileType::ISO_MODE2; } - // maybe it also just happened to have that size, + // maybe it also just happened to have that size, let's assume it's a PSP ISO and error out later if it's not. } return IdentifiedFileType::PSP_ISO; } else if (extension == ".cso") { @@ -127,7 +130,7 @@ IdentifiedFileType Identify_File(FileLoader *fileLoader) { size_t readSize = fileLoader->ReadAt(0, 4, 1, &id); if (readSize != 1) { - ERROR_LOG(LOADER, "Failed to read identification bytes"); + *errorString = "Failed to read identification bytes"; return IdentifiedFileType::ERROR_IDENTIFYING; } @@ -201,7 +204,8 @@ IdentifiedFileType Identify_File(FileLoader *fileLoader) { } FileLoader *ResolveFileLoaderTarget(FileLoader *fileLoader) { - IdentifiedFileType type = Identify_File(fileLoader); + std::string errorString; + IdentifiedFileType type = Identify_File(fileLoader, &errorString); if (type == IdentifiedFileType::PSP_PBP_DIRECTORY) { const Path ebootFilename = ResolvePBPFile(fileLoader->GetPath()); if (ebootFilename != fileLoader->GetPath()) { @@ -232,14 +236,15 @@ Path ResolvePBPFile(const Path &filename) { bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { FileLoader *&fileLoader = *fileLoaderPtr; // Note that this can modify filename! - switch (Identify_File(fileLoader)) { + IdentifiedFileType type = Identify_File(fileLoader, error_string); + switch (type) { case IdentifiedFileType::PSP_PBP_DIRECTORY: { // TODO: Perhaps we should/can never get here now? fileLoader = ResolveFileLoaderTarget(fileLoader); if (fileLoader->Exists()) { INFO_LOG(LOADER, "File is a PBP in a directory!"); - IdentifiedFileType ebootType = Identify_File(fileLoader); + IdentifiedFileType ebootType = Identify_File(fileLoader, error_string); if (ebootType == IdentifiedFileType::PSP_ISO_NP) { InitMemoryForGameISO(fileLoader); pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR"); @@ -249,6 +254,10 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { *error_string = "PS1 EBOOTs are not supported by PPSSPP."; coreState = CORE_BOOT_ERROR; return false; + } else if (ebootType == IdentifiedFileType::ERROR_IDENTIFYING) { + // IdentifyFile will have written to errorString. + coreState = CORE_BOOT_ERROR; + return false; } std::string dir = fileLoader->GetPath().GetDirectory(); @@ -268,7 +277,7 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { case IdentifiedFileType::PSP_PBP: case IdentifiedFileType::PSP_ELF: { - INFO_LOG(LOADER,"File is an ELF or loose PBP!"); + INFO_LOG(LOADER, "File is an ELF or loose PBP!"); return Load_PSP_ELF_PBP(fileLoader, error_string); } @@ -282,13 +291,6 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { *error_string = "PS1 EBOOTs are not supported by PPSSPP."; break; - case IdentifiedFileType::ERROR_IDENTIFYING: - ERROR_LOG(LOADER, "Could not read file enough to identify it"); - *error_string = fileLoader ? fileLoader->LatestError() : ""; - if (error_string->empty()) - *error_string = "Error reading file"; - break; - case IdentifiedFileType::ARCHIVE_RAR: #ifdef WIN32 *error_string = "RAR file detected (Require WINRAR)"; @@ -336,9 +338,20 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { case IdentifiedFileType::UNKNOWN_BIN: case IdentifiedFileType::UNKNOWN_ELF: case IdentifiedFileType::UNKNOWN: + ERROR_LOG(LOADER, "Unknown file type: %s (%s)", fileLoader->GetPath().c_str(), error_string->c_str()); + *error_string = "Unknown file type: " + fileLoader->GetPath().ToString(); + break; + + case IdentifiedFileType::ERROR_IDENTIFYING: + *error_string = *error_string + ": " + (fileLoader ? fileLoader->LatestError() : ""); + if (error_string->empty()) + *error_string = "Error reading file"; + ERROR_LOG(LOADER, "Error while identifying file: %s", error_string->c_str()); + break; + default: - ERROR_LOG(LOADER, "Failed to identify file: %s", fileLoader->GetPath().c_str()); - *error_string = "Failed to identify file"; + *error_string = StringFromFormat("Unhandled identified file type %d", (int)type); + ERROR_LOG(LOADER, "%s", error_string->c_str()); break; } @@ -347,7 +360,7 @@ bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) { } bool UmdReplace(const Path &filepath, std::string &error) { - IFileSystem* currentUMD = pspFileSystem.GetSystem("disc0:"); + IFileSystem *currentUMD = pspFileSystem.GetSystem("disc0:"); if (!currentUMD) { error = "has no disc"; @@ -364,7 +377,10 @@ bool UmdReplace(const Path &filepath, std::string &error) { UpdateLoadedFile(loadedFile); loadedFile = ResolveFileLoaderTarget(loadedFile); - IdentifiedFileType type = Identify_File(loadedFile); + + + std::string errorString; + IdentifiedFileType type = Identify_File(loadedFile, &errorString); switch (type) { case IdentifiedFileType::PSP_ISO: @@ -377,7 +393,7 @@ bool UmdReplace(const Path &filepath, std::string &error) { break; default: - error = "Unsupported file type:" + std::to_string((int)type); + error = "Unsupported file type: " + std::to_string((int)type) + " " + errorString; return false; break; } diff --git a/Core/Loaders.h b/Core/Loaders.h index cbf4329547..131c82451b 100644 --- a/Core/Loaders.h +++ b/Core/Loaders.h @@ -144,7 +144,7 @@ FileLoader *ResolveFileLoaderTarget(FileLoader *fileLoader); Path ResolvePBPDirectory(const Path &filename); Path ResolvePBPFile(const Path &filename); -IdentifiedFileType Identify_File(FileLoader *fileLoader); +IdentifiedFileType Identify_File(FileLoader *fileLoader, std::string *errorString); class FileLoaderFactory { public: diff --git a/Core/System.cpp b/Core/System.cpp index d11fb469fe..87392b22eb 100644 --- a/Core/System.cpp +++ b/Core/System.cpp @@ -216,7 +216,7 @@ bool DiscIDFromGEDumpPath(const Path &path, FileLoader *fileLoader, std::string } } -bool CPU_Init() { +bool CPU_Init(std::string *errorString) { coreState = CORE_POWERUP; currentMIPS = &mipsr4k; @@ -237,7 +237,8 @@ bool CPU_Init() { loadedFile = new RamCachingFileLoader(loadedFile); } #endif - IdentifiedFileType type = Identify_File(loadedFile); + + IdentifiedFileType type = Identify_File(loadedFile, errorString); // TODO: Put this somewhere better? if (!coreParameter.mountIso.empty()) { @@ -277,6 +278,8 @@ bool CPU_Init() { allowPlugins = false; break; default: + // Can we even get here? + WARN_LOG(LOADER, "CPU_Init didn't recognize file. %s", errorString->c_str()); break; } @@ -413,7 +416,7 @@ bool PSP_InitStart(const CoreParameter &coreParam, std::string *error_string) { pspIsIniting = true; PSP_SetLoading("Loading game..."); - if (!CPU_Init()) { + if (!CPU_Init(&coreParameter.errorString)) { *error_string = coreParameter.errorString; if (error_string->empty()) { *error_string = "Failed initializing CPU/Memory"; diff --git a/Core/Util/GameManager.cpp b/Core/Util/GameManager.cpp index 96688e9cdd..26b0581c95 100644 --- a/Core/Util/GameManager.cpp +++ b/Core/Util/GameManager.cpp @@ -399,7 +399,8 @@ std::string GameManager::GetGameID(const Path &path) const { auto loader = ConstructFileLoader(path); std::string id; - switch (Identify_File(loader)) { + std::string errorString; + switch (Identify_File(loader, &errorString)) { case IdentifiedFileType::PSP_PBP_DIRECTORY: delete loader; loader = ConstructFileLoader(ResolvePBPFile(path)); diff --git a/UI/GameInfoCache.cpp b/UI/GameInfoCache.cpp index 6fac432f99..3d8db8fd7c 100644 --- a/UI/GameInfoCache.cpp +++ b/UI/GameInfoCache.cpp @@ -356,8 +356,10 @@ public: return; } + std::string errorString; + info_->working = true; - info_->fileType = Identify_File(info_->GetFileLoader().get()); + info_->fileType = Identify_File(info_->GetFileLoader().get(), &errorString); switch (info_->fileType) { case IdentifiedFileType::PSP_PBP: case IdentifiedFileType::PSP_PBP_DIRECTORY: diff --git a/UI/MainScreen.cpp b/UI/MainScreen.cpp index ef5847c7c4..2bde92b1af 100644 --- a/UI/MainScreen.cpp +++ b/UI/MainScreen.cpp @@ -74,7 +74,8 @@ bool LaunchFile(ScreenManager *screenManager, const Path &path) { return false; } - IdentifiedFileType type = Identify_File(loader); + std::string errorString; + IdentifiedFileType type = Identify_File(loader, &errorString); delete loader; switch (type) { diff --git a/UI/SavedataScreen.cpp b/UI/SavedataScreen.cpp index 97fe8cb6d1..1c44d8127c 100644 --- a/UI/SavedataScreen.cpp +++ b/UI/SavedataScreen.cpp @@ -426,7 +426,8 @@ bool SavedataBrowser::ByFilename(const UI::View *v1, const UI::View *v2) { static time_t GetTotalSize(const SavedataButton *b) { auto fileLoader = std::unique_ptr(ConstructFileLoader(b->GamePath())); - switch (Identify_File(fileLoader.get())) { + std::string errorString; + switch (Identify_File(fileLoader.get(), &errorString)) { case IdentifiedFileType::PSP_PBP_DIRECTORY: case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY: return File::GetDirectoryRecursiveSize(ResolvePBPDirectory(b->GamePath()), nullptr, File::GETFILES_GETHIDDEN); @@ -449,7 +450,8 @@ static time_t GetDateSeconds(const SavedataButton *b) { auto fileLoader = std::unique_ptr(ConstructFileLoader(b->GamePath())); tm datetm; bool success; - if (Identify_File(fileLoader.get()) == IdentifiedFileType::PSP_SAVEDATA_DIRECTORY) { + std::string errorString; + if (Identify_File(fileLoader.get(), &errorString) == IdentifiedFileType::PSP_SAVEDATA_DIRECTORY) { success = File::GetModifTime(b->GamePath() / "PARAM.SFO", datetm); } else { success = File::GetModifTime(b->GamePath(), datetm); diff --git a/android/jni/AndroidContentURI.h b/android/jni/AndroidContentURI.h index 92e77afb62..880675bcb9 100644 --- a/android/jni/AndroidContentURI.h +++ b/android/jni/AndroidContentURI.h @@ -37,13 +37,16 @@ public: std::vector parts; SplitString(components, '/', parts); if (parts.size() == 3) { + // Single file URI. provider = parts[0]; if (parts[1] != "tree") { return false; } root = UriDecode(parts[2]); + // file empty signals this type. return true; } else if (parts.size() == 5) { + // Tree URI provider = parts[0]; if (parts[1] != "tree") { return false; From ed7053329ab26f56e2dc257557bac37c4b88025b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sat, 7 Aug 2021 12:17:31 +0200 Subject: [PATCH 2/2] Fix some mis-handling of single file URI. Fixes the Load... button. --- android/jni/AndroidContentURI.h | 40 +++++++++++++++++++++++---------- unittest/UnitTest.cpp | 31 +++++++++++++++---------- 2 files changed, 47 insertions(+), 24 deletions(-) diff --git a/android/jni/AndroidContentURI.h b/android/jni/AndroidContentURI.h index 880675bcb9..07977a2d71 100644 --- a/android/jni/AndroidContentURI.h +++ b/android/jni/AndroidContentURI.h @@ -4,6 +4,7 @@ #include "Common/StringUtils.h" #include "Common/Net/URL.h" +#include "Common/Log.h" // Utility to deal with Android storage URIs of the forms: // content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO @@ -39,12 +40,20 @@ public: if (parts.size() == 3) { // Single file URI. provider = parts[0]; - if (parts[1] != "tree") { + if (parts[1] == "tree") { + // Single directory URI. + // Not sure when we encounter these? + // file empty signals this type. + root = UriDecode(parts[2]); + return true; + } else if (parts[1] == "document") { + // root empty signals this type. + file = UriDecode(parts[2]); + return true; + } else { + // What's this? return false; } - root = UriDecode(parts[2]); - // file empty signals this type. - return true; } else if (parts.size() == 5) { // Tree URI provider = parts[0]; @@ -65,6 +74,11 @@ public: } AndroidContentURI WithRootFilePath(const std::string &filePath) { + if (root.empty()) { + ERROR_LOG(SYSTEM, "WithRootFilePath cannot be used with single file URIs."); + return *this; + } + AndroidContentURI uri = *this; uri.file = uri.root; if (!filePath.empty()) { @@ -106,11 +120,10 @@ public: return uri; } - bool IsTreeURI() const { - return file.empty(); - } - bool CanNavigateUp() const { + if (root.empty()) { + return false; + } return file.size() > root.size(); } @@ -145,7 +158,7 @@ public: } std::string GetLastPart() const { - if (IsTreeURI()) { + if (file.empty()) { // Can't do anything anyway. return std::string(); } @@ -182,7 +195,7 @@ public: } bool TreeContains(const AndroidContentURI &fileURI) { - if (!IsTreeURI()) { + if (root.empty()) { return false; } return startsWith(fileURI.file, root); @@ -192,8 +205,11 @@ public: if (file.empty()) { // Tree URI return StringFromFormat("content://%s/tree/%s", provider.c_str(), UriEncode(root).c_str()); + } else if (root.empty()) { + // Single file URI + return StringFromFormat("content://%s/document/%s", provider.c_str(), UriEncode(file).c_str()); } else { - // File URI + // File URI from Tree return StringFromFormat("content://%s/tree/%s/document/%s", provider.c_str(), UriEncode(root).c_str(), UriEncode(file).c_str()); } } @@ -208,6 +224,6 @@ public: } const std::string &RootPath() const { - return root; + return root.empty() ? file : root; } }; diff --git a/unittest/UnitTest.cpp b/unittest/UnitTest.cpp index f2ef8cdb88..b0585bc05b 100644 --- a/unittest/UnitTest.cpp +++ b/unittest/UnitTest.cpp @@ -622,33 +622,40 @@ static bool TestPath() { static bool TestAndroidContentURI() { static const char *treeURIString = "content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO"; static const char *directoryURIString = "content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO/document/primary%3APSP%20ISO"; - static const char *fileURIString = "content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO/document/primary%3APSP%20ISO%2FTekken%206.iso"; + static const char *fileTreeURIString = "content://com.android.externalstorage.documents/tree/primary%3APSP%20ISO/document/primary%3APSP%20ISO%2FTekken%206.iso"; + static const char *fileNonTreeString = "content://com.android.externalstorage.documents/document/primary%3APSP%2Fcrash_bad_execaddr.prx"; + AndroidContentURI treeURI; EXPECT_TRUE(treeURI.Parse(std::string(treeURIString))); AndroidContentURI dirURI; EXPECT_TRUE(dirURI.Parse(std::string(directoryURIString))); + AndroidContentURI fileTreeURI; + EXPECT_TRUE(fileTreeURI.Parse(std::string(fileTreeURIString))); + AndroidContentURI fileTreeURICopy; + EXPECT_TRUE(fileTreeURICopy.Parse(std::string(fileTreeURIString))); AndroidContentURI fileURI; - EXPECT_TRUE(fileURI.Parse(std::string(fileURIString))); + EXPECT_TRUE(fileURI.Parse(std::string(fileNonTreeString))); - std::string lastPart = dirURI.GetLastPart(); + EXPECT_EQ_STR(fileTreeURI.GetLastPart(), std::string("Tekken 6.iso")); - EXPECT_EQ_STR(fileURI.GetLastPart(), std::string("Tekken 6.iso")); + EXPECT_TRUE(treeURI.TreeContains(fileTreeURI)); - EXPECT_TRUE(treeURI.TreeContains(fileURI)); - - EXPECT_TRUE(fileURI.CanNavigateUp()); - fileURI.NavigateUp(); - EXPECT_FALSE(fileURI.CanNavigateUp()); + EXPECT_TRUE(fileTreeURI.CanNavigateUp()); + fileTreeURI.NavigateUp(); + EXPECT_FALSE(fileTreeURI.CanNavigateUp()); - EXPECT_EQ_STR(fileURI.FilePath(), fileURI.RootPath()); + EXPECT_EQ_STR(fileTreeURI.FilePath(), fileTreeURI.RootPath()); - EXPECT_EQ_STR(fileURI.ToString(), std::string(directoryURIString)); + EXPECT_EQ_STR(fileTreeURI.ToString(), std::string(directoryURIString)); std::string diff; - EXPECT_TRUE(dirURI.ComputePathTo(fileURI, diff)); + EXPECT_TRUE(dirURI.ComputePathTo(fileTreeURICopy, diff)); EXPECT_EQ_STR(diff, std::string("Tekken 6.iso")); + EXPECT_EQ_STR(fileURI.GetFileExtension(), std::string(".prx")); + EXPECT_FALSE(fileURI.CanNavigateUp()); + return true; }