Merge pull request #90 from gizmo98/patch-2
[libretro-ppsspp.git] / Core / Loaders.cpp
blob0aa7173f4e80e46e95e4247ac42ecca311e42bfe
1 // Copyright (c) 2012- PPSSPP Project.
3 // This program is free software: you can redistribute it and/or modify
4 // it under the terms of the GNU General Public License as published by
5 // the Free Software Foundation, version 2.0 or later versions.
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License 2.0 for more details.
12 // A copy of the GPL 2.0 should have been included with the program.
13 // If not, see http://www.gnu.org/licenses/
15 // Official git repository and contact information can be found at
16 // https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
18 #include <algorithm>
19 #include <cstdio>
21 #include "file/file_util.h"
23 #include "Core/FileLoaders/CachingFileLoader.h"
24 #include "Core/FileLoaders/DiskCachingFileLoader.h"
25 #include "Core/FileLoaders/HTTPFileLoader.h"
26 #include "Core/FileLoaders/LocalFileLoader.h"
27 #include "Core/FileLoaders/RetryingFileLoader.h"
28 #include "Core/FileSystems/MetaFileSystem.h"
29 #include "Core/PSPLoaders.h"
30 #include "Core/MemMap.h"
31 #include "Core/Loaders.h"
32 #include "Core/System.h"
33 #include "Core/ELF/PBPReader.h"
34 #include "Core/ELF/ParamSFO.h"
36 FileLoader *ConstructFileLoader(const std::string &filename) {
37 if (filename.find("http://") == 0 || filename.find("https://") == 0)
38 return new CachingFileLoader(new RetryingFileLoader(new HTTPFileLoader(filename)));
39 return new LocalFileLoader(filename);
42 // TODO : improve, look in the file more
43 IdentifiedFileType Identify_File(FileLoader *fileLoader)
45 if (fileLoader == nullptr) {
46 ERROR_LOG(LOADER, "Invalid fileLoader");
47 return FILETYPE_ERROR;
49 if (fileLoader->Path().size() == 0) {
50 ERROR_LOG(LOADER, "Invalid filename %s", fileLoader->Path().c_str());
51 return FILETYPE_ERROR;
54 if (!fileLoader->Exists()) {
55 return FILETYPE_ERROR;
58 std::string extension = fileLoader->Extension();
59 if (!strcasecmp(extension.c_str(), ".iso"))
61 // may be a psx iso, they have 2352 byte sectors. You never know what some people try to open
62 if ((fileLoader->FileSize() % 2352) == 0)
64 unsigned char sync[12];
65 fileLoader->ReadAt(0, 12, sync);
67 // each sector in a mode2 image starts with these 12 bytes
68 if (memcmp(sync,"\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00",12) == 0)
70 return FILETYPE_ISO_MODE2;
73 // maybe it also just happened to have that size,
75 return FILETYPE_PSP_ISO;
77 else if (!strcasecmp(extension.c_str(),".cso"))
79 return FILETYPE_PSP_ISO;
81 else if (!strcasecmp(extension.c_str(),".ppst"))
83 return FILETYPE_PPSSPP_SAVESTATE;
86 // First, check if it's a directory with an EBOOT.PBP in it.
87 if (fileLoader->IsDirectory()) {
88 std::string filename = fileLoader->Path();
89 if (filename.size() > 4) {
90 FileInfo fileInfo;
91 // Check for existence of EBOOT.PBP, as required for "Directory games".
92 if (getFileInfo((filename + "/EBOOT.PBP").c_str(), &fileInfo)) {
93 if (fileInfo.exists) {
94 return FILETYPE_PSP_PBP_DIRECTORY;
98 // check if it's a disc directory
99 if (getFileInfo((filename + "/PSP_GAME").c_str(), &fileInfo)) {
100 if (fileInfo.exists) {
101 return FILETYPE_PSP_DISC_DIRECTORY;
105 // Not that, okay, let's guess it's a savedata directory if it has a param.sfo...
106 if (getFileInfo((filename + "/PARAM.SFO").c_str(), &fileInfo)) {
107 if (fileInfo.exists) {
108 return FILETYPE_PSP_SAVEDATA_DIRECTORY;
113 return FILETYPE_NORMAL_DIRECTORY;
116 u32_le id;
118 size_t readSize = fileLoader->ReadAt(0, 4, 1, &id);
119 if (readSize != 1) {
120 return FILETYPE_ERROR;
123 u32 psar_offset = 0, psar_id = 0;
124 u32 _id = id;
125 switch (_id) {
126 case 'PBP\x00':
127 fileLoader->ReadAt(0x24, 4, 1, &psar_offset);
128 fileLoader->ReadAt(psar_offset, 4, 1, &psar_id);
129 break;
130 case '!raR':
131 return FILETYPE_ARCHIVE_RAR;
132 case '\x04\x03KP':
133 case '\x06\x05KP':
134 case '\x08\x07KP':
135 return FILETYPE_ARCHIVE_ZIP;
138 if (id == 'FLE\x7F') {
139 std::string filename = fileLoader->Path();
140 // There are a few elfs misnamed as pbp (like Trig Wars), accept that.
141 if (!strcasecmp(extension.c_str(), ".plf") || strstr(filename.c_str(),"BOOT.BIN") ||
142 !strcasecmp(extension.c_str(), ".elf") || !strcasecmp(extension.c_str(), ".prx") ||
143 !strcasecmp(extension.c_str(), ".pbp")) {
144 return FILETYPE_PSP_ELF;
146 return FILETYPE_UNKNOWN_ELF;
148 else if (id == 'PBP\x00') {
149 // Do this PS1 eboot check FIRST before checking other eboot types.
150 // It seems like some are malformed and slip through the PSAR check below.
151 // TODO: Change PBPReader to read FileLoader objects?
152 std::string filename = fileLoader->Path();
153 PBPReader pbp(filename.c_str());
154 if (pbp.IsValid()) {
155 if (!pbp.IsELF()) {
156 size_t sfoSize;
157 u8 *sfoData = pbp.GetSubFile(PBP_PARAM_SFO, &sfoSize);
159 recursive_mutex _lock;
160 lock_guard lock(_lock);
161 ParamSFOData paramSFO;
162 paramSFO.ReadSFO(sfoData, sfoSize);
163 // PS1 Eboots are supposed to use "ME" as their PARAM SFO category.
164 // If they don't, and they're still malformed (e.g. PSISOIMG0000 isn't found), there's nothing we can do.
165 if (paramSFO.GetValueString("CATEGORY") == "ME")
166 return FILETYPE_PSP_PS1_PBP;
168 delete[] sfoData;
172 if (psar_id == 'MUPN') {
173 return FILETYPE_PSP_ISO_NP;
175 // PS1 PSAR begins with "PSISOIMG0000"
176 if (psar_id == 'SISP') {
177 return FILETYPE_PSP_PS1_PBP;
180 // Let's check if we got pointed to a PBP within such a directory.
181 // If so we just move up and return the directory itself as the game.
182 std::string path = getDir(filename);
183 // If loading from memstick...
184 size_t pos = path.find("/PSP/GAME/");
185 if (pos != std::string::npos) {
186 filename = path;
187 return FILETYPE_PSP_PBP_DIRECTORY;
189 return FILETYPE_PSP_PBP;
191 else if (!strcasecmp(extension.c_str(),".pbp")) {
192 ERROR_LOG(LOADER, "A PBP with the wrong magic number?");
193 return FILETYPE_PSP_PBP;
194 } else if (!strcasecmp(extension.c_str(),".bin")) {
195 return FILETYPE_UNKNOWN_BIN;
196 } else if (!strcasecmp(extension.c_str(),".zip")) {
197 return FILETYPE_ARCHIVE_ZIP;
198 } else if (!strcasecmp(extension.c_str(),".rar")) {
199 return FILETYPE_ARCHIVE_RAR;
200 } else if (!strcasecmp(extension.c_str(),".r00")) {
201 return FILETYPE_ARCHIVE_RAR;
202 } else if (!strcasecmp(extension.c_str(),".r01")) {
203 return FILETYPE_ARCHIVE_RAR;
204 } else if (!strcasecmp(extension.substr(1).c_str(), ".7z")) {
205 return FILETYPE_ARCHIVE_7Z;
207 return FILETYPE_UNKNOWN;
210 bool LoadFile(FileLoader **fileLoaderPtr, std::string *error_string) {
211 FileLoader *&fileLoader = *fileLoaderPtr;
212 // Note that this can modify filename!
213 switch (Identify_File(fileLoader)) {
214 case FILETYPE_PSP_PBP_DIRECTORY:
216 std::string filename = fileLoader->Path();
217 std::string ebootFilename = filename + "/EBOOT.PBP";
219 // Switch fileLoader to the EBOOT.
220 delete fileLoader;
221 fileLoader = ConstructFileLoader(ebootFilename);
223 if (fileLoader->Exists()) {
224 INFO_LOG(LOADER, "File is a PBP in a directory!");
225 IdentifiedFileType ebootType = Identify_File(fileLoader);
226 if (ebootType == FILETYPE_PSP_ISO_NP) {
227 InitMemoryForGameISO(fileLoader);
228 pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");
229 return Load_PSP_ISO(fileLoader, error_string);
231 else if (ebootType == FILETYPE_PSP_PS1_PBP) {
232 *error_string = "PS1 EBOOTs are not supported by PPSSPP.";
233 return false;
235 std::string path = filename;
236 size_t pos = path.find("/PSP/GAME/");
237 if (pos != std::string::npos)
238 pspFileSystem.SetStartingDirectory("ms0:" + path.substr(pos));
239 return Load_PSP_ELF_PBP(fileLoader, error_string);
240 } else {
241 *error_string = "No EBOOT.PBP, misidentified game";
242 return false;
246 case FILETYPE_PSP_PBP:
247 case FILETYPE_PSP_ELF:
249 INFO_LOG(LOADER,"File is an ELF or loose PBP!");
250 return Load_PSP_ELF_PBP(fileLoader, error_string);
253 case FILETYPE_PSP_ISO:
254 case FILETYPE_PSP_ISO_NP:
255 case FILETYPE_PSP_DISC_DIRECTORY: // behaves the same as the mounting is already done by now
256 pspFileSystem.SetStartingDirectory("disc0:/PSP_GAME/USRDIR");
257 return Load_PSP_ISO(fileLoader, error_string);
259 case FILETYPE_PSP_PS1_PBP:
260 *error_string = "PS1 EBOOTs are not supported by PPSSPP.";
261 break;
263 case FILETYPE_ERROR:
264 ERROR_LOG(LOADER, "Could not read file");
265 *error_string = "Error reading file";
266 break;
268 case FILETYPE_ARCHIVE_RAR:
269 #ifdef WIN32
270 *error_string = "RAR file detected (Require WINRAR)";
271 #else
272 *error_string = "RAR file detected (Require UnRAR)";
273 #endif
274 break;
276 case FILETYPE_ARCHIVE_ZIP:
277 #ifdef WIN32
278 *error_string = "ZIP file detected (Require WINRAR)";
279 #else
280 *error_string = "ZIP file detected (Require UnRAR)";
281 #endif
282 break;
284 case FILETYPE_ARCHIVE_7Z:
285 #ifdef WIN32
286 *error_string = "7z file detected (Require 7-Zip)";
287 #else
288 *error_string = "7z file detected (Require 7-Zip)";
289 #endif
290 break;
292 case FILETYPE_ISO_MODE2:
293 *error_string = "PSX game image detected.";
294 break;
296 case FILETYPE_NORMAL_DIRECTORY:
297 ERROR_LOG(LOADER, "Just a directory.");
298 *error_string = "Just a directory.";
299 break;
301 case FILETYPE_PPSSPP_SAVESTATE:
302 *error_string = "This is a saved state, not a game."; // Actually, we could make it load it...
303 break;
305 case FILETYPE_PSP_SAVEDATA_DIRECTORY:
306 *error_string = "This is save data, not a game."; // Actually, we could make it load it...
307 break;
309 case FILETYPE_UNKNOWN_BIN:
310 case FILETYPE_UNKNOWN_ELF:
311 case FILETYPE_UNKNOWN:
312 default:
313 ERROR_LOG(LOADER, "Failed to identify file");
314 *error_string = "Failed to identify file";
315 break;
317 return false;