Propagate errors from file identification upwards better.

This commit is contained in:
Henrik Rydgård 2021-08-07 11:54:45 +02:00
parent af3ad2ca03
commit 43ba908225
8 changed files with 58 additions and 30 deletions

View file

@ -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;
}

View file

@ -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:

View file

@ -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";

View file

@ -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));

View file

@ -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:

View file

@ -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) {

View file

@ -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<FileLoader>(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<FileLoader>(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);

View file

@ -37,13 +37,16 @@ public:
std::vector<std::string> 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;