ppsspp/Core/Loaders.cpp

119 lines
3.0 KiB
C++
Raw Normal View History

2012-11-01 15:19:01 +00:00
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
2012-11-01 15:19:01 +00:00
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "file/file_util.h"
#include "MIPS/MIPS.h"
#include "MIPS/MIPSCodeUtils.h"
#include "HLE/HLE.h"
#include "HLE/sceKernelModule.h"
#include "PSPLoaders.h"
#include "MemMap.h"
#include "Loaders.h"
#include "System.h"
// TODO : improve, look in the file more
EmuFileType Identify_File(const char *filename)
{
//then: easy bulletproof IDs.
FILE *f = fopen(filename, "rb");
if (!f)
{
//File does not exists
return FILETYPE_ERROR;
}
u32 id;
size_t readSize = fread(&id,4,1,f);
2012-11-01 15:19:01 +00:00
fclose(f);
if(readSize != 1)
return FILETYPE_ERROR;
if (strlen(filename) < 5) {
ERROR_LOG(LOADER, "invalid filename %s", filename);
}
const char *extension = filename + strlen(filename) - 4;
2012-11-01 15:19:01 +00:00
if (id == 'FLE\x7F')
{
if (!strcasecmp(extension, ".plf") || strstr(filename,"BOOT.BIN") ||
!strcasecmp(extension, ".elf") || !strcasecmp(extension,".prx") )
2012-11-01 15:19:01 +00:00
{
return FILETYPE_PSP_ELF;
}
else
return FILETYPE_UNKNOWN_ELF;
2012-11-01 15:19:01 +00:00
}
else if (id == 'PBP\x00')
{
return FILETYPE_PSP_PBP;
}
else
{
if (!strcasecmp(extension,".pbp"))
2012-11-01 15:19:01 +00:00
{
return FILETYPE_PSP_PBP;
}
else if (!strcasecmp(extension,".iso"))
2012-11-01 15:19:01 +00:00
{
return FILETYPE_PSP_ISO;
}
else if (!strcasecmp(extension,".cso"))
2012-11-01 15:19:01 +00:00
{
return FILETYPE_PSP_ISO;
}
else if (!strcasecmp(extension,".bin"))
2012-11-01 15:19:01 +00:00
{
return FILETYPE_UNKNOWN_BIN;
}
}
return FILETYPE_UNKNOWN;
}
bool LoadFile(const char *filename, std::string *error_string)
{
2013-01-07 08:10:05 +00:00
INFO_LOG(LOADER,"Identifying file...");
2012-11-01 15:19:01 +00:00
switch (Identify_File(filename))
{
case FILETYPE_PSP_PBP:
case FILETYPE_PSP_ELF:
{
2013-01-07 08:10:05 +00:00
INFO_LOG(LOADER,"File is an ELF!");
2012-11-01 15:19:01 +00:00
std::string path = getDir(filename);
// If loading from memstick...
size_t pos = path.find("/PSP/GAME/");
if (pos != std::string::npos)
2013-01-10 11:27:10 +00:00
pspFileSystem.SetStartingDirectory("ms0:" + path.substr(pos));
2012-11-01 15:19:01 +00:00
return Load_PSP_ELF_PBP(filename, error_string);
}
case FILETYPE_PSP_ISO:
2013-01-10 11:27:10 +00:00
pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");
2012-11-01 15:19:01 +00:00
return Load_PSP_ISO(filename, error_string);
case FILETYPE_ERROR:
2013-03-30 08:11:53 +00:00
ERROR_LOG(LOADER, "Could not read file");
2012-11-01 15:19:01 +00:00
*error_string = "Error reading file";
break;
case FILETYPE_UNKNOWN_BIN:
case FILETYPE_UNKNOWN_ELF:
case FILETYPE_UNKNOWN:
default:
2013-01-07 08:10:05 +00:00
ERROR_LOG(LOADER, "Failed to identify file");
2012-11-01 15:19:01 +00:00
break;
}
return false;
}