Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Loaders.cpp
3185 views
1
// Copyright (c) 2012- PPSSPP Project.
2
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.
6
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.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
20
#include "Common/File/FileUtil.h"
21
#include "Common/File/Path.h"
22
#include "Common/StringUtils.h"
23
#include "Common/Data/Text/I18n.h"
24
#include "Core/FileLoaders/CachingFileLoader.h"
25
#include "Core/FileLoaders/DiskCachingFileLoader.h"
26
#include "Core/FileLoaders/HTTPFileLoader.h"
27
#include "Core/FileLoaders/LocalFileLoader.h"
28
#include "Core/FileLoaders/RetryingFileLoader.h"
29
#include "Core/FileLoaders/ZipFileLoader.h"
30
#include "Core/FileSystems/MetaFileSystem.h"
31
#include "Core/PSPLoaders.h"
32
#include "Core/MemMap.h"
33
#include "Core/Loaders.h"
34
#include "Core/Core.h"
35
#include "Core/System.h"
36
#include "Core/ELF/PBPReader.h"
37
#include "Core/ELF/ParamSFO.h"
38
#include "Core/Util/GameManager.h"
39
40
FileLoader *ConstructFileLoader(const Path &filename) {
41
if (filename.Type() == PathType::HTTP) {
42
FileLoader *baseLoader = new RetryingFileLoader(new HTTPFileLoader(filename));
43
// For headless, avoid disk caching since it's usually used for tests that might mutate.
44
if (!PSP_CoreParameter().headLess) {
45
baseLoader = new DiskCachingFileLoader(baseLoader);
46
}
47
return new CachingFileLoader(baseLoader);
48
}
49
return new LocalFileLoader(filename);
50
}
51
52
// TODO : improve, look in the file more
53
// Does not take ownership.
54
IdentifiedFileType Identify_File(FileLoader *fileLoader, std::string *errorString) {
55
errorString->clear();
56
if (fileLoader == nullptr) {
57
*errorString = "Invalid fileLoader";
58
return IdentifiedFileType::ERROR_IDENTIFYING;
59
}
60
if (fileLoader->GetPath().size() == 0) {
61
*errorString = "Invalid filename " + fileLoader->GetPath().ToString();
62
return IdentifiedFileType::ERROR_IDENTIFYING;
63
}
64
65
if (!fileLoader->Exists()) {
66
*errorString = "IdentifyFile: File doesn't exist: " + fileLoader->GetPath().ToString();
67
return IdentifiedFileType::ERROR_IDENTIFYING;
68
}
69
70
std::string extension = fileLoader->GetFileExtension();
71
if (extension == ".iso") {
72
// may be a psx iso, they have 2352 byte sectors. You never know what some people try to open
73
if ((fileLoader->FileSize() % 2352) == 0) {
74
unsigned char sync[12];
75
fileLoader->ReadAt(0, 12, sync);
76
77
// each sector in a mode2 image starts with these 12 bytes
78
if (memcmp(sync,"\x00\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x00", 12) == 0) {
79
*errorString = "ISO in Mode 2: Not a PSP game";
80
return IdentifiedFileType::ISO_MODE2;
81
}
82
83
// 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.
84
}
85
return IdentifiedFileType::PSP_ISO;
86
} else if (extension == ".cso" || extension == ".chd") {
87
return IdentifiedFileType::PSP_ISO;
88
} else if (extension == ".ppst") {
89
return IdentifiedFileType::PPSSPP_SAVESTATE;
90
} else if (extension == ".ppdmp") {
91
char data[8]{};
92
fileLoader->ReadAt(0, 8, data);
93
if (memcmp(data, "PPSSPPGE", 8) == 0) {
94
return IdentifiedFileType::PPSSPP_GE_DUMP;
95
}
96
}
97
98
// First, check if it's a directory with an EBOOT.PBP in it.
99
if (fileLoader->IsDirectory()) {
100
Path filename = fileLoader->GetPath();
101
if (filename.size() > 4) {
102
// Check for existence of EBOOT.PBP, as required for "Directory games".
103
if (File::Exists(filename / "EBOOT.PBP")) {
104
return IdentifiedFileType::PSP_PBP_DIRECTORY;
105
}
106
107
// check if it's a disc directory
108
if (File::Exists(filename / "PSP_GAME")) {
109
return IdentifiedFileType::PSP_DISC_DIRECTORY;
110
}
111
112
// Not that, okay, let's guess it's a savedata directory if it has a param.sfo...
113
if (File::Exists(filename / "PARAM.SFO")) {
114
return IdentifiedFileType::PSP_SAVEDATA_DIRECTORY;
115
}
116
}
117
118
return IdentifiedFileType::NORMAL_DIRECTORY;
119
}
120
121
// OK, quick methods of identification for common types failed. Moving on to more expensive methods,
122
// starting by reading the first few bytes.
123
// This can be necessary for weird Android content storage path types, see issue #17462
124
125
u32_le id;
126
127
size_t readSize = fileLoader->ReadAt(0, 4, 1, &id);
128
if (readSize != 1) {
129
*errorString = "Failed to read identification bytes";
130
return IdentifiedFileType::ERROR_IDENTIFYING;
131
}
132
133
u32_le psar_offset = 0, psar_id = 0;
134
u32 _id = id;
135
if (!memcmp(&_id, "PK\x03\x04", 4) || !memcmp(&_id, "PK\x05\x06", 4) || !memcmp(&_id, "PK\x07\x08", 4)) {
136
return IdentifiedFileType::ARCHIVE_ZIP;
137
} else if (!memcmp(&_id, "\x00PBP", 4)) {
138
fileLoader->ReadAt(0x24, 4, 1, &psar_offset);
139
fileLoader->ReadAt(psar_offset, 4, 1, &psar_id);
140
// Fall through to the below if chain.
141
} else if (!memcmp(&_id, "Rar!", 4)) {
142
return IdentifiedFileType::ARCHIVE_RAR;
143
} else if (!memcmp(&_id, "\x37\x7A\xBC\xAF", 4)) {
144
return IdentifiedFileType::ARCHIVE_7Z;
145
} else if (!memcmp(&_id, "\0\0\0\0", 4)) {
146
// All zeroes. ISO files start like this but their 16th 2048-byte sector contains metadata.
147
if (fileLoader->FileSize() > 0x8100) {
148
char buffer[16];
149
fileLoader->ReadAt(0x8000, sizeof(buffer), buffer);
150
if (!memcmp(buffer + 1, "CD001", 5)) {
151
// It's an ISO file.
152
if (!memcmp(buffer + 8, "PSP GAME", 8)) {
153
return IdentifiedFileType::PSP_ISO;
154
}
155
return IdentifiedFileType::UNKNOWN_ISO;
156
}
157
}
158
} else if (!memcmp(&_id, "CISO", 4)) {
159
// CISO are not used for many other kinds of ISO so let's just guess it's a PSP one and let it
160
// fail later...
161
return IdentifiedFileType::PSP_ISO;
162
} else if (!memcmp(&_id, "MCom", 4)) {
163
size_t readSize = fileLoader->ReadAt(4, 4, 1, &_id);
164
if (!memcmp(&_id, "prHD", 4)) {
165
return IdentifiedFileType::PSP_ISO; // CHD file
166
}
167
}
168
169
if (id == 'FLE\x7F') {
170
Path filename = fileLoader->GetPath();
171
// There are a few elfs misnamed as pbp (like Trig Wars), accept that. Also accept extension-less paths.
172
if (extension == ".plf" || strstr(filename.GetFilename().c_str(), "BOOT.BIN") ||
173
extension == ".elf" || extension == ".prx" || extension == ".pbp" || extension == "") {
174
return IdentifiedFileType::PSP_ELF;
175
}
176
return IdentifiedFileType::UNKNOWN_ELF;
177
} else if (id == 'PBP\x00') {
178
// Do this PS1 eboot check FIRST before checking other eboot types.
179
// It seems like some are malformed and slip through the PSAR check below.
180
PBPReader pbp(fileLoader);
181
if (pbp.IsValid() && !pbp.IsELF()) {
182
std::vector<u8> sfoData;
183
if (pbp.GetSubFile(PBP_PARAM_SFO, &sfoData)) {
184
ParamSFOData paramSFO;
185
paramSFO.ReadSFO(sfoData);
186
// PS1 Eboots are supposed to use "ME" as their PARAM SFO category.
187
// If they don't, and they're still malformed (e.g. PSISOIMG0000 isn't found), there's nothing we can do.
188
if (paramSFO.GetValueString("CATEGORY") == "ME")
189
return IdentifiedFileType::PSP_PS1_PBP;
190
}
191
}
192
193
if (psar_id == 'MUPN') {
194
return IdentifiedFileType::PSP_ISO_NP;
195
}
196
// PS1 PSAR begins with "PSISOIMG0000"
197
if (psar_id == 'SISP') {
198
return IdentifiedFileType::PSP_PS1_PBP;
199
}
200
201
// Let's check if we got pointed to a PBP within such a directory.
202
// If so we just move up and return the directory itself as the game.
203
// If loading from memstick...
204
if (fileLoader->GetPath().FilePathContainsNoCase("PSP/GAME/")) {
205
return IdentifiedFileType::PSP_PBP_DIRECTORY;
206
}
207
return IdentifiedFileType::PSP_PBP;
208
} else if (extension == ".pbp") {
209
ERROR_LOG(Log::Loader, "A PBP with the wrong magic number?");
210
return IdentifiedFileType::PSP_PBP;
211
} else if (extension == ".bin") {
212
return IdentifiedFileType::UNKNOWN_BIN;
213
} else if (extension == ".zip") {
214
return IdentifiedFileType::ARCHIVE_ZIP;
215
} else if (extension == ".rar") {
216
return IdentifiedFileType::ARCHIVE_RAR;
217
} else if (extension == ".r00") {
218
return IdentifiedFileType::ARCHIVE_RAR;
219
} else if (extension == ".r01") {
220
return IdentifiedFileType::ARCHIVE_RAR;
221
} else if (extension == ".7z") {
222
return IdentifiedFileType::ARCHIVE_7Z;
223
}
224
return IdentifiedFileType::UNKNOWN;
225
}
226
227
FileLoader *ResolveFileLoaderTarget(FileLoader *fileLoader) {
228
std::string errorString;
229
IdentifiedFileType type = Identify_File(fileLoader, &errorString);
230
if (type == IdentifiedFileType::PSP_PBP_DIRECTORY) {
231
const Path ebootFilename = ResolvePBPFile(fileLoader->GetPath());
232
if (ebootFilename != fileLoader->GetPath()) {
233
// Switch fileLoader to the actual EBOOT.
234
delete fileLoader;
235
fileLoader = ConstructFileLoader(ebootFilename);
236
}
237
} else if (type == IdentifiedFileType::ARCHIVE_ZIP) {
238
// Handle zip files, take automatic action depending on contents.
239
// Can also return nullptr.
240
ZipFileLoader *zipLoader = new ZipFileLoader(fileLoader);
241
242
ZipFileInfo zipFileInfo{};
243
DetectZipFileContents(zipLoader->GetZip(), &zipFileInfo);
244
245
switch (zipFileInfo.contents) {
246
case ZipFileContents::ISO_FILE:
247
case ZipFileContents::FRAME_DUMP:
248
{
249
zipLoader->Initialize(zipFileInfo.isoFileIndex);
250
return zipLoader;
251
}
252
default:
253
{
254
// Nothing runnable in file. Take the original loader back and return it.
255
fileLoader = zipLoader->Steal();
256
delete zipLoader;
257
return fileLoader;
258
}
259
}
260
}
261
return fileLoader;
262
}
263
264
Path ResolvePBPDirectory(const Path &filename) {
265
if (filename.GetFilename() == "EBOOT.PBP") {
266
return filename.NavigateUp();
267
} else {
268
return filename;
269
}
270
}
271
272
Path ResolvePBPFile(const Path &filename) {
273
if (filename.GetFilename() != "EBOOT.PBP") {
274
return filename / "EBOOT.PBP";
275
} else {
276
return filename;
277
}
278
}
279
280
bool UmdReplace(const Path &filepath, FileLoader **fileLoader, std::string &error) {
281
IFileSystem *currentUMD = pspFileSystem.GetSystem("disc0:");
282
283
if (!currentUMD) {
284
error = "has no disc";
285
return false;
286
}
287
288
FileLoader *loadedFile = ConstructFileLoader(filepath);
289
290
if (!loadedFile || !loadedFile->Exists()) {
291
error = loadedFile ? (loadedFile->GetPath().ToVisualString() + " doesn't exist") : "no loaded file";
292
delete loadedFile;
293
return false;
294
}
295
UpdateLoadedFile(loadedFile);
296
297
loadedFile = ResolveFileLoaderTarget(loadedFile);
298
299
*fileLoader = loadedFile;
300
301
std::string errorString;
302
IdentifiedFileType type = Identify_File(loadedFile, &errorString);
303
304
switch (type) {
305
case IdentifiedFileType::PSP_ISO:
306
case IdentifiedFileType::PSP_ISO_NP:
307
case IdentifiedFileType::PSP_DISC_DIRECTORY:
308
if (!MountGameISO(loadedFile, &error)) {
309
error = "mounting the replaced ISO failed: " + error;
310
return false;
311
}
312
break;
313
default:
314
error = "Unsupported file type: " + std::to_string((int)type) + " " + errorString;
315
return false;
316
break;
317
}
318
return true;
319
}
320
321
// Close the return value with ZipClose (if non-null, of course).
322
struct zip *ZipOpenPath(const Path &fileName) {
323
int error = 0;
324
// Need to special case for content URI here, similar to OpenCFile.
325
struct zip *z;
326
#if PPSSPP_PLATFORM(ANDROID)
327
if (fileName.Type() == PathType::CONTENT_URI) {
328
int fd = File::OpenFD(fileName, File::OPEN_READ);
329
z = zip_fdopen(fd, 0, &error);
330
} else
331
#endif
332
{ // continuation of above else in the ifdef
333
z = zip_open(fileName.c_str(), 0, &error);
334
}
335
336
if (!z) {
337
ERROR_LOG(Log::HLE, "Failed to open ZIP file '%s', error code=%i", fileName.c_str(), error);
338
}
339
return z;
340
}
341
342
void ZipClose(struct zip *z) {
343
if (z)
344
zip_close(z);
345
}
346
347
bool DetectZipFileContents(const Path &fileName, ZipFileInfo *info) {
348
struct zip *z = ZipOpenPath(fileName);
349
if (!z) {
350
info->contents = ZipFileContents::UNKNOWN;
351
return false;
352
}
353
DetectZipFileContents(z, info);
354
zip_close(z);
355
return true;
356
}
357
358
static int countSlashes(const std::string &fileName, int *slashLocation) {
359
int slashCount = 0;
360
int lastSlashLocation = -1;
361
if (slashLocation) {
362
*slashLocation = -1;
363
}
364
for (size_t i = 0; i < fileName.size(); i++) {
365
if (fileName[i] == '/') {
366
slashCount++;
367
if (slashLocation) {
368
*slashLocation = lastSlashLocation;
369
lastSlashLocation = (int)i;
370
}
371
}
372
}
373
374
return slashCount;
375
}
376
377
inline char asciitolower(char in) {
378
if (in <= 'Z' && in >= 'A')
379
return in - ('Z' - 'z');
380
return in;
381
}
382
383
static bool ZipExtractFileToMemory(struct zip *z, int fileIndex, std::string *data) {
384
struct zip_stat zstat;
385
zip_stat_index(z, fileIndex, 0, &zstat);
386
if (zstat.size == 0) {
387
data->clear();
388
return true;
389
}
390
391
size_t readSize = zstat.size;
392
data->resize(readSize);
393
394
zip_file *zf = zip_fopen_index(z, fileIndex, 0);
395
if (!zf) {
396
ERROR_LOG(Log::HLE, "Failed to zip_fopen_index file %d from zip", fileIndex);
397
return false;
398
}
399
400
zip_int64_t retval = zip_fread(zf, data->data(), readSize);
401
zip_fclose(zf);
402
403
if (retval < 0 || retval < (int)readSize) {
404
ERROR_LOG(Log::HLE, "Failed to read %d bytes from zip (%d) - archive corrupt?", (int)readSize, (int)retval);
405
return false;
406
} else {
407
return true;
408
}
409
}
410
411
void DetectZipFileContents(struct zip *z, ZipFileInfo *info) {
412
int numFiles = zip_get_num_files(z);
413
_dbg_assert_(numFiles >= 0);
414
415
// Verify that this is a PSP zip file with the correct layout. We also try
416
// to detect simple zipped ISO files, those we'll just "install" to the current
417
// directory of the Games tab (where else?).
418
bool isPSPMemstickGame = false;
419
bool isZippedISO = false;
420
bool isTexturePack = false;
421
bool isFrameDump = false;
422
int stripChars = 0;
423
int isoFileIndex = -1;
424
int stripCharsTexturePack = -1;
425
int textureIniIndex = -1;
426
int filesInRoot = 0;
427
int directoriesInRoot = 0;
428
bool hasParamSFO = false;
429
bool hasIcon0PNG = false;
430
s64 totalFileSize = 0;
431
432
// TODO: It might be cleaner to write separate detection functions, but this big loop doing it all at once
433
// is quite convenient and makes it easy to add shared heuristics.
434
for (int i = 0; i < numFiles; i++) {
435
const char *fn = zip_get_name(z, i, 0);
436
437
zip_stat_t stat{};
438
zip_stat_index(z, i, 0, &stat);
439
totalFileSize += stat.size;
440
441
std::string zippedName = fn;
442
std::transform(zippedName.begin(), zippedName.end(), zippedName.begin(),
443
[](unsigned char c) { return asciitolower(c); }); // Not using std::tolower to avoid Turkish I->ı conversion.
444
// Ignore macos metadata stuff
445
if (startsWith(zippedName, "__macosx/")) {
446
continue;
447
}
448
if (endsWith(zippedName, "/")) {
449
// A directory. Not all zips bother including these.
450
continue;
451
}
452
453
int prevSlashLocation = -1;
454
int slashCount = countSlashes(zippedName, &prevSlashLocation);
455
if (zippedName.find("eboot.pbp") != std::string::npos) {
456
if (slashCount >= 1 && (!isPSPMemstickGame || prevSlashLocation < stripChars + 1)) {
457
stripChars = prevSlashLocation + 1;
458
isPSPMemstickGame = true;
459
} else {
460
INFO_LOG(Log::HLE, "Wrong number of slashes (%i) in '%s'", slashCount, fn);
461
}
462
// TODO: Extract icon and param.sfo from the pbp to be able to display it on the install screen.
463
} else if (endsWith(zippedName, ".iso") || endsWith(zippedName, ".cso") || endsWith(zippedName, ".chd")) {
464
if (slashCount <= 1) {
465
// We only do this if the ISO file is in the root or one level down.
466
isZippedISO = true;
467
INFO_LOG(Log::HLE, "ISO found in zip: %s", zippedName.c_str());
468
if (isoFileIndex != -1) {
469
INFO_LOG(Log::HLE, "More than one ISO file found in zip. Ignoring additional ones.");
470
} else {
471
isoFileIndex = i;
472
info->contentName = zippedName;
473
}
474
}
475
} else if (zippedName.find("textures.ini") != std::string::npos) {
476
int slashLocation = (int)zippedName.find_last_of('/');
477
if (stripCharsTexturePack == -1 || slashLocation < stripCharsTexturePack + 1) {
478
stripCharsTexturePack = slashLocation + 1;
479
isTexturePack = true;
480
textureIniIndex = i;
481
}
482
} else if (endsWith(zippedName, ".ppdmp")) {
483
isFrameDump = true;
484
isoFileIndex = i;
485
info->contentName = zippedName;
486
} else if (endsWith(zippedName, "/param.sfo")) {
487
// Get the game name so we can display it.
488
std::string paramSFOContents;
489
if (ZipExtractFileToMemory(z, i, &paramSFOContents)) {
490
ParamSFOData sfo;
491
if (sfo.ReadSFO((const u8 *)paramSFOContents.data(), paramSFOContents.size())) {
492
if (sfo.HasKey("TITLE")) {
493
info->gameTitle = sfo.GetValueString("TITLE");
494
info->savedataTitle = sfo.GetValueString("SAVEDATA_TITLE");
495
char buff[20];
496
strftime(buff, 20, "%Y-%m-%d %H:%M:%S", localtime(&stat.mtime));
497
info->mTime = buff;
498
info->savedataDetails = sfo.GetValueString("SAVEDATA_DETAIL");
499
info->savedataDir = sfo.GetValueString("SAVEDATA_DIRECTORY"); // should also be parsable from the path.
500
hasParamSFO = true;
501
}
502
}
503
}
504
} else if (endsWith(zippedName, "/icon0.png")) {
505
hasIcon0PNG = true;
506
}
507
if (slashCount == 0) {
508
filesInRoot++;
509
}
510
}
511
512
info->stripChars = stripChars;
513
info->numFiles = numFiles;
514
info->isoFileIndex = isoFileIndex;
515
info->textureIniIndex = textureIniIndex;
516
info->ignoreMetaFiles = false;
517
info->totalFileSize = totalFileSize;
518
519
// Priority ordering for detecting the various kinds of zip file content.s
520
if (isPSPMemstickGame) {
521
info->contents = ZipFileContents::PSP_GAME_DIR;
522
} else if (isZippedISO) {
523
info->contents = ZipFileContents::ISO_FILE;
524
} else if (isTexturePack) {
525
info->stripChars = stripCharsTexturePack;
526
info->ignoreMetaFiles = true;
527
info->contents = ZipFileContents::TEXTURE_PACK;
528
} else if (stripChars == 0 && filesInRoot == 0 && hasParamSFO && hasIcon0PNG) {
529
// As downloaded from GameFAQs, for example.
530
info->contents = ZipFileContents::SAVE_DATA;
531
} else if (isFrameDump) {
532
info->contents = ZipFileContents::FRAME_DUMP;
533
} else {
534
info->contents = ZipFileContents::UNKNOWN;
535
}
536
}
537
538