ppsspp/UI/GameInfoCache.cpp

894 lines
27 KiB
C++
Raw Normal View History

// Copyright (c) 2013- 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.
// 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 "Common/Common.h"
#include <string>
#include <map>
#include <memory>
2013-09-30 20:00:41 +00:00
#include <algorithm>
#include "Common/GPU/thin3d.h"
#include "Common/Thread/ThreadManager.h"
#include "Common/File/VFS/VFS.h"
#include "Common/File/FileUtil.h"
#include "Common/File/Path.h"
#include "Common/Render/ManagedTexture.h"
#include "Common/StringUtils.h"
#include "Common/TimeUtil.h"
#include "Core/FileSystems/ISOFileSystem.h"
#include "Core/FileSystems/DirectoryFileSystem.h"
#include "Core/FileSystems/VirtualDiscFileSystem.h"
2013-04-08 19:46:41 +00:00
#include "Core/ELF/PBPReader.h"
#include "Core/SaveState.h"
#include "Core/System.h"
#include "Core/Loaders.h"
#include "Core/Util/GameManager.h"
#include "Core/Config.h"
#include "UI/GameInfoCache.h"
GameInfoCache *g_gameInfoCache;
void GameInfoTex::Clear() {
if (!data.empty()) {
data.clear();
dataLoaded = false;
}
if (texture) {
texture->Release();
texture = nullptr;
}
}
GameInfo::GameInfo() {
// here due to a forward decl.
fileType = IdentifiedFileType::UNKNOWN;
}
GameInfo::~GameInfo() {
std::lock_guard<std::mutex> guard(lock);
sndDataLoaded = false;
icon.Clear();
pic0.Clear();
pic1.Clear();
fileLoader.reset();
}
bool GameInfo::Delete() {
switch (fileType) {
case IdentifiedFileType::PSP_ISO:
case IdentifiedFileType::PSP_ISO_NP:
{
// Just delete the one file (TODO: handle two-disk games as well somehow).
Path fileToRemove = filePath_;
2024-01-22 09:08:50 +00:00
INFO_LOG(SYSTEM, "Deleting file %s", fileToRemove.c_str());
2015-09-22 21:02:02 +00:00
File::Delete(fileToRemove);
g_Config.RemoveRecent(filePath_.ToString());
return true;
}
case IdentifiedFileType::PSP_PBP_DIRECTORY:
case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:
{
// TODO: This could be handled by Core/Util/GameManager too somehow.
2021-05-09 16:38:48 +00:00
Path directoryToRemove = ResolvePBPDirectory(filePath_);
2024-01-22 09:08:50 +00:00
INFO_LOG(SYSTEM, "Deleting directory %s", directoryToRemove.c_str());
if (!File::DeleteDirRecursively(directoryToRemove)) {
ERROR_LOG(SYSTEM, "Failed to delete file");
return false;
}
g_Config.CleanRecent();
return true;
}
case IdentifiedFileType::PSP_ELF:
case IdentifiedFileType::UNKNOWN_BIN:
case IdentifiedFileType::UNKNOWN_ELF:
case IdentifiedFileType::UNKNOWN_ISO:
case IdentifiedFileType::ARCHIVE_RAR:
case IdentifiedFileType::ARCHIVE_ZIP:
case IdentifiedFileType::ARCHIVE_7Z:
case IdentifiedFileType::PPSSPP_GE_DUMP:
{
const Path &fileToRemove = filePath_;
2024-01-22 09:08:50 +00:00
INFO_LOG(SYSTEM, "Deleting file %s", fileToRemove.c_str());
2015-09-22 21:02:02 +00:00
File::Delete(fileToRemove);
g_Config.RemoveRecent(filePath_.ToString());
return true;
}
case IdentifiedFileType::PPSSPP_SAVESTATE:
{
const Path &ppstPath = filePath_;
2024-01-22 09:08:50 +00:00
INFO_LOG(SYSTEM, "Deleting file %s", ppstPath.c_str());
File::Delete(ppstPath);
2021-05-11 21:58:54 +00:00
const Path screenshotPath = filePath_.WithReplacedExtension(".ppst", ".jpg");
if (File::Exists(screenshotPath)) {
File::Delete(screenshotPath);
}
return true;
}
default:
2024-01-22 09:08:50 +00:00
INFO_LOG(SYSTEM, "Don't know how to delete this type of file: %s", filePath_.c_str());
return false;
}
}
u64 GameInfo::GetGameSizeOnDiskInBytes() {
switch (fileType) {
case IdentifiedFileType::PSP_PBP_DIRECTORY:
case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:
return File::ComputeRecursiveDirectorySize(ResolvePBPDirectory(filePath_));
case IdentifiedFileType::PSP_DISC_DIRECTORY:
return File::ComputeRecursiveDirectorySize(GetFileLoader()->GetPath());
default:
return GetFileLoader()->FileSize();
}
}
u64 GameInfo::GetGameSizeUncompressedInBytes() {
switch (fileType) {
case IdentifiedFileType::PSP_PBP_DIRECTORY:
case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:
return File::ComputeRecursiveDirectorySize(ResolvePBPDirectory(filePath_));
case IdentifiedFileType::PSP_DISC_DIRECTORY:
return File::ComputeRecursiveDirectorySize(GetFileLoader()->GetPath());
default:
{
BlockDevice *blockDevice = constructBlockDevice(GetFileLoader().get());
if (blockDevice) {
u64 size = blockDevice->GetUncompressedSize();
delete blockDevice;
return size;
} else {
return GetFileLoader()->FileSize();
}
}
}
}
// Not too meaningful if the object itself is a savedata directory...
std::vector<Path> GameInfo::GetSaveDataDirectories() {
Path memc = GetSysDirectory(DIRECTORY_SAVEDATA);
std::vector<File::FileInfo> dirs;
File::GetFilesInDir(memc, &dirs);
std::vector<Path> directories;
if (id.size() < 5) {
return directories;
}
for (size_t i = 0; i < dirs.size(); i++) {
if (startsWith(dirs[i].name, id)) {
directories.push_back(dirs[i].fullName);
}
}
return directories;
}
u64 GameInfo::GetSaveDataSizeInBytes() {
if (fileType == IdentifiedFileType::PSP_SAVEDATA_DIRECTORY || fileType == IdentifiedFileType::PPSSPP_SAVESTATE) {
2015-06-12 09:40:50 +00:00
return 0;
}
std::vector<Path> saveDataDir = GetSaveDataDirectories();
u64 totalSize = 0;
u64 filesSizeInDir = 0;
for (size_t j = 0; j < saveDataDir.size(); j++) {
std::vector<File::FileInfo> fileInfo;
File::GetFilesInDir(saveDataDir[j], &fileInfo);
2021-09-11 13:11:56 +00:00
for (auto const &file : fileInfo) {
if (!file.isDirectory)
filesSizeInDir += file.size;
}
if (filesSizeInDir < 0xA00000) {
// HACK: Generally the savedata size in a dir shouldn't be more than 10MB.
totalSize += filesSizeInDir;
}
filesSizeInDir = 0;
}
return totalSize;
}
u64 GameInfo::GetInstallDataSizeInBytes() {
if (fileType == IdentifiedFileType::PSP_SAVEDATA_DIRECTORY || fileType == IdentifiedFileType::PPSSPP_SAVESTATE) {
return 0;
}
std::vector<Path> saveDataDir = GetSaveDataDirectories();
u64 totalSize = 0;
u64 filesSizeInDir = 0;
for (size_t j = 0; j < saveDataDir.size(); j++) {
std::vector<File::FileInfo> fileInfo;
File::GetFilesInDir(saveDataDir[j], &fileInfo);
for (auto const &file : fileInfo) {
// TODO: Might want to recurse here? Don't know games that use directories
// for install-data though.
if (!file.isDirectory)
filesSizeInDir += file.size;
}
if (filesSizeInDir >= 0xA00000) {
// HACK: Generally the savedata size in a dir shouldn't be more than 10MB.
// This is probably GameInstall data.
totalSize += filesSizeInDir;
}
filesSizeInDir = 0;
}
return totalSize;
}
bool GameInfo::LoadFromPath(const Path &gamePath) {
{
std::lock_guard<std::mutex> guard(lock);
// No need to rebuild if we already have it loaded.
if (filePath_ == gamePath) {
return true;
}
}
{
std::lock_guard<std::mutex> guard(loaderLock);
fileLoader.reset(ConstructFileLoader(gamePath));
if (!fileLoader)
return false;
}
std::lock_guard<std::mutex> guard(lock);
filePath_ = gamePath;
// This is a fallback title, while we're loading / if unable to load.
title = filePath_.GetFilename();
return true;
}
std::shared_ptr<FileLoader> GameInfo::GetFileLoader() {
if (filePath_.empty()) {
// Happens when workqueue tries to figure out priorities,
// because Priority() calls GetFileLoader()... gnarly.
return fileLoader;
}
std::lock_guard<std::mutex> guard(loaderLock);
if (!fileLoader) {
FileLoader *loader = ConstructFileLoader(filePath_);
fileLoader.reset(loader);
return fileLoader;
}
return fileLoader;
}
void GameInfo::DisposeFileLoader() {
std::lock_guard<std::mutex> guard(loaderLock);
fileLoader.reset();
}
bool GameInfo::DeleteAllSaveData() {
_assert_(hasFlags & GameInfoFlags::PARAM_SFO); // so we know we have the ID.
std::vector<Path> saveDataDir = GetSaveDataDirectories();
for (size_t j = 0; j < saveDataDir.size(); j++) {
std::vector<File::FileInfo> fileInfo;
File::GetFilesInDir(saveDataDir[j], &fileInfo);
for (size_t i = 0; i < fileInfo.size(); i++) {
File::Delete(fileInfo[i].fullName);
}
File::DeleteDir(saveDataDir[j]);
}
return true;
}
void GameInfo::ParseParamSFO() {
title = paramSFO.GetValueString("TITLE");
id = paramSFO.GetValueString("DISC_ID");
id_version = id + "_" + paramSFO.GetValueString("DISC_VERSION");
disc_total = paramSFO.GetValueInt("DISC_TOTAL");
disc_number = paramSFO.GetValueInt("DISC_NUMBER");
// region = paramSFO.GetValueInt("REGION"); // Always seems to be 32768?
region = GAMEREGION_OTHER;
if (id_version.size() >= 4) {
std::string regStr = id_version.substr(0, 4);
// Guesswork
switch (regStr[2]) {
case 'E': region = GAMEREGION_EUROPE; break;
case 'U': region = GAMEREGION_USA; break;
case 'J': region = GAMEREGION_JAPAN; break;
case 'H': region = GAMEREGION_HONGKONG; break;
case 'A': region = GAMEREGION_ASIA; break;
2020-02-23 07:33:44 +00:00
case 'K': region = GAMEREGION_KOREA; break;
}
/*
if (regStr == "NPEZ" || regStr == "NPEG" || regStr == "ULES" || regStr == "UCES" ||
regStr == "NPEX") {
region = GAMEREGION_EUROPE;
} else if (regStr == "NPUG" || regStr == "NPUZ" || regStr == "ULUS" || regStr == "UCUS") {
region = GAMEREGION_USA;
} else if (regStr == "NPJH" || regStr == "NPJG" || regStr == "ULJM"|| regStr == "ULJS") {
region = GAMEREGION_JAPAN;
} else if (regStr == "NPHG") {
region = GAMEREGION_HONGKONG;
} else if (regStr == "UCAS") {
region = GAMEREGION_CHINA;
}*/
}
}
std::string GameInfo::GetTitle() {
std::lock_guard<std::mutex> guard(lock);
if (hasFlags & GameInfoFlags::PARAM_SFO) {
return title;
} else {
return filePath_.GetFilename();
}
}
void GameInfo::SetTitle(const std::string &newTitle) {
std::lock_guard<std::mutex> guard(lock);
title = newTitle;
}
void GameInfo::FinishPendingTextureLoads(Draw::DrawContext *draw) {
if (draw && icon.dataLoaded && !icon.texture) {
SetupTexture(draw, icon);
}
if (draw && pic0.dataLoaded && !pic0.texture) {
SetupTexture(draw, pic0);
}
if (draw && pic1.dataLoaded && !pic1.texture) {
SetupTexture(draw, pic1);
}
}
void GameInfo::SetupTexture(Draw::DrawContext *thin3d, GameInfoTex &tex) {
using namespace Draw;
// TODO: Use TempImage to semi-load the image in the worker task, then here we
// could just call CreateTextureFromTempImage.
tex.texture = CreateTextureFromFileData(thin3d, (const uint8_t *)tex.data.data(), (int)tex.data.size(), ImageFileType::DETECT, false, GetTitle().c_str());
if (tex.texture) {
tex.timeLoaded = time_now_d();
} else {
ERROR_LOG(G3D, "Failed creating texture (%s) from %d-byte file", GetTitle().c_str(), (int)tex.data.size());
}
}
static bool ReadFileToString(IFileSystem *fs, const char *filename, std::string *contents, std::mutex *mtx) {
PSPFileInfo info = fs->GetFileInfo(filename);
if (!info.exists) {
return false;
}
int handle = fs->OpenFile(filename, FILEACCESS_READ);
if (handle < 0) {
return false;
}
if (mtx) {
std::string data;
data.resize(info.size);
size_t readSize = fs->ReadFile(handle, (u8 *)data.data(), info.size);
fs->CloseFile(handle);
if (readSize != info.size) {
return false;
}
std::lock_guard<std::mutex> lock(*mtx);
*contents = std::move(data);
} else {
contents->resize(info.size);
size_t readSize = fs->ReadFile(handle, (u8 *)contents->data(), info.size);
fs->CloseFile(handle);
if (readSize != info.size) {
return false;
}
}
return true;
}
static bool ReadLocalFileToString(const Path &path, std::string *contents, std::mutex *mtx) {
std::string data;
if (!File::ReadBinaryFileToString(path, &data)) {
return false;
}
if (mtx) {
std::lock_guard<std::mutex> lock(*mtx);
*contents = std::move(data);
} else {
*contents = std::move(data);
}
return true;
}
static bool ReadVFSToString(const char *filename, std::string *contents, std::mutex *mtx) {
2015-12-24 20:45:07 +00:00
size_t sz;
uint8_t *data = g_VFS.ReadFile(filename, &sz);
2015-12-24 20:45:07 +00:00
if (data) {
if (mtx) {
std::lock_guard<std::mutex> lock(*mtx);
2015-12-24 20:45:07 +00:00
*contents = std::string((const char *)data, sz);
} else {
*contents = std::string((const char *)data, sz);
}
} else {
return false;
2015-12-24 20:45:07 +00:00
}
delete [] data;
return true;
2015-12-24 20:45:07 +00:00
}
class GameInfoWorkItem : public Task {
public:
GameInfoWorkItem(const Path &gamePath, std::shared_ptr<GameInfo> &info, GameInfoFlags flags)
: gamePath_(gamePath), info_(info), flags_(flags) {}
~GameInfoWorkItem() {
info_->DisposeFileLoader();
}
TaskType Type() const override {
return TaskType::IO_BLOCKING;
}
TaskPriority Priority() const override {
switch (gamePath_.Type()) {
case PathType::NATIVE:
case PathType::CONTENT_URI:
return TaskPriority::NORMAL;
default:
// Remote/network access.
return TaskPriority::LOW;
}
}
void Run() override {
2021-06-12 20:16:03 +00:00
// An early-return will result in the destructor running, where we can set
// flags like working and pending.
if (!info_->LoadFromPath(gamePath_)) {
2021-06-12 20:16:03 +00:00
return;
}
2021-09-11 13:11:56 +00:00
// In case of a remote file, check if it actually exists before locking.
2023-05-03 23:24:31 +00:00
if (!info_->GetFileLoader() || !info_->GetFileLoader()->Exists()) {
2021-06-12 20:16:03 +00:00
return;
}
std::string errorString;
if (flags_ & GameInfoFlags::FILE_TYPE) {
info_->fileType = Identify_File(info_->GetFileLoader().get(), &errorString);
info_->hasConfig = g_Config.hasGameConfig(info_->id);
}
switch (info_->fileType) {
case IdentifiedFileType::PSP_PBP:
case IdentifiedFileType::PSP_PBP_DIRECTORY:
{
auto pbpLoader = info_->GetFileLoader();
if (info_->fileType == IdentifiedFileType::PSP_PBP_DIRECTORY) {
2021-05-09 16:38:48 +00:00
Path ebootPath = ResolvePBPFile(gamePath_);
if (ebootPath != gamePath_) {
2021-05-09 16:38:48 +00:00
pbpLoader.reset(ConstructFileLoader(ebootPath));
}
}
PBPReader pbp(pbpLoader.get());
if (!pbp.IsValid()) {
if (pbp.IsELF()) {
goto handleELF;
}
ERROR_LOG(LOADER, "invalid pbp '%s'\n", pbpLoader->GetPath().c_str());
2021-06-12 20:16:03 +00:00
return;
}
// First, PARAM.SFO.
if (flags_ & GameInfoFlags::PARAM_SFO) {
std::vector<u8> sfoData;
if (pbp.GetSubFile(PBP_PARAM_SFO, &sfoData)) {
std::lock_guard<std::mutex> lock(info_->lock);
info_->paramSFO.ReadSFO(sfoData);
info_->ParseParamSFO();
// Assuming PSP_PBP_DIRECTORY without ID or with disc_total < 1 in GAME dir must be homebrew
if ((info_->id.empty() || !info_->disc_total)
&& gamePath_.FilePathContainsNoCase("PSP/GAME/")
&& info_->fileType == IdentifiedFileType::PSP_PBP_DIRECTORY) {
info_->id = g_paramSFO.GenerateFakeID(gamePath_);
info_->id_version = info_->id + "_1.00";
info_->region = GAMEREGION_MAX + 1; // Homebrew
}
}
}
// Then, ICON0.PNG.
if (flags_ & GameInfoFlags::ICON) {
if (pbp.GetSubFileSize(PBP_ICON0_PNG) > 0) {
std::lock_guard<std::mutex> lock(info_->lock);
pbp.GetSubFileAsString(PBP_ICON0_PNG, &info_->icon.data);
} else {
Path screenshot_jpg = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.jpg");
Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png");
// Try using png/jpg screenshots first
if (File::Exists(screenshot_png))
ReadLocalFileToString(screenshot_png, &info_->icon.data, &info_->lock);
else if (File::Exists(screenshot_jpg))
ReadLocalFileToString(screenshot_jpg, &info_->icon.data, &info_->lock);
else
// Read standard icon
ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock);
}
info_->icon.dataLoaded = true;
}
if (flags_ & GameInfoFlags::BG) {
2014-06-22 01:29:54 +00:00
if (pbp.GetSubFileSize(PBP_PIC0_PNG) > 0) {
std::string data;
pbp.GetSubFileAsString(PBP_PIC0_PNG, &data);
std::lock_guard<std::mutex> lock(info_->lock);
info_->pic0.data = std::move(data);
info_->pic0.dataLoaded = true;
2014-06-22 01:29:54 +00:00
}
if (pbp.GetSubFileSize(PBP_PIC1_PNG) > 0) {
std::string data;
pbp.GetSubFileAsString(PBP_PIC1_PNG, &data);
std::lock_guard<std::mutex> lock(info_->lock);
info_->pic1.data = std::move(data);
info_->pic1.dataLoaded = true;
}
}
if (flags_ & GameInfoFlags::SND) {
2014-06-22 01:38:26 +00:00
if (pbp.GetSubFileSize(PBP_SND0_AT3) > 0) {
std::string data;
pbp.GetSubFileAsString(PBP_SND0_AT3, &data);
std::lock_guard<std::mutex> lock(info_->lock);
info_->sndFileData = std::move(data);
2014-06-22 01:38:26 +00:00
info_->sndDataLoaded = true;
}
}
}
break;
case IdentifiedFileType::PSP_ELF:
handleELF:
// An elf on its own has no usable information, no icons, no nothing.
if (flags_ & GameInfoFlags::PARAM_SFO) {
info_->id = g_paramSFO.GenerateFakeID(gamePath_);
info_->id_version = info_->id + "_1.00";
info_->region = GAMEREGION_MAX + 1; // Homebrew
}
if (flags_ & GameInfoFlags::ICON) {
std::string id = g_paramSFO.GenerateFakeID(gamePath_);
// Due to the dependency of the BASIC info, we fetch it already here.
Path screenshot_jpg = GetSysDirectory(DIRECTORY_SCREENSHOT) / (id + "_00000.jpg");
Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (id + "_00000.png");
// Try using png/jpg screenshots first
2018-02-26 08:26:07 +00:00
if (File::Exists(screenshot_png)) {
ReadLocalFileToString(screenshot_png, &info_->icon.data, &info_->lock);
2018-02-26 08:26:07 +00:00
} else if (File::Exists(screenshot_jpg)) {
ReadLocalFileToString(screenshot_jpg, &info_->icon.data, &info_->lock);
2018-02-26 08:26:07 +00:00
} else {
// Read standard icon
2018-02-26 08:26:07 +00:00
VERBOSE_LOG(LOADER, "Loading unknown.png because there was an ELF");
ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock);
}
info_->icon.dataLoaded = true;
}
break;
case IdentifiedFileType::PSP_SAVEDATA_DIRECTORY:
{
SequentialHandleAllocator handles;
2021-05-09 16:38:48 +00:00
VirtualDiscFileSystem umd(&handles, gamePath_);
if (flags_ & GameInfoFlags::PARAM_SFO) {
// Alright, let's fetch the PARAM.SFO.
std::string paramSFOcontents;
if (ReadFileToString(&umd, "/PARAM.SFO", &paramSFOcontents, 0)) {
std::lock_guard<std::mutex> lock(info_->lock);
info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size());
info_->ParseParamSFO();
}
}
if (flags_ & GameInfoFlags::ICON) {
ReadFileToString(&umd, "/ICON0.PNG", &info_->icon.data, &info_->lock);
info_->icon.dataLoaded = true;
}
if (flags_ & GameInfoFlags::BG) {
ReadFileToString(&umd, "/PIC1.PNG", &info_->pic1.data, &info_->lock);
info_->pic1.dataLoaded = true;
}
break;
}
case IdentifiedFileType::PPSSPP_SAVESTATE:
{
if (flags_ & GameInfoFlags::PARAM_SFO) {
info_->SetTitle(SaveState::GetTitle(gamePath_));
}
// Let's use the screenshot as an icon, too.
if (flags_ & GameInfoFlags::ICON) {
Path screenshotPath = gamePath_.WithReplacedExtension(".ppst", ".jpg");
if (ReadLocalFileToString(screenshotPath, &info_->icon.data, &info_->lock)) {
info_->icon.dataLoaded = true;
} else {
ERROR_LOG(G3D, "Error loading screenshot data: '%s'", screenshotPath.c_str());
}
}
break;
}
case IdentifiedFileType::PPSSPP_GE_DUMP:
{
if (flags_ & GameInfoFlags::ICON) {
Path screenshotPath = gamePath_.WithReplacedExtension(".ppdmp", ".png");
// Let's use the comparison screenshot as an icon, if it exists.
if (ReadLocalFileToString(screenshotPath, &info_->icon.data, &info_->lock)) {
info_->icon.dataLoaded = true;
}
}
break;
}
case IdentifiedFileType::PSP_DISC_DIRECTORY:
2013-07-23 15:35:43 +00:00
{
SequentialHandleAllocator handles;
2021-05-09 16:38:48 +00:00
VirtualDiscFileSystem umd(&handles, gamePath_);
2013-07-23 15:35:43 +00:00
// Alright, let's fetch the PARAM.SFO.
if (flags_ & GameInfoFlags::PARAM_SFO) {
std::string paramSFOcontents;
if (ReadFileToString(&umd, "/PSP_GAME/PARAM.SFO", &paramSFOcontents, 0)) {
std::lock_guard<std::mutex> lock(info_->lock);
info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size());
info_->ParseParamSFO();
}
2013-07-23 15:35:43 +00:00
}
ReadFileToString(&umd, "/PSP_GAME/ICON0.PNG", &info_->icon.data, &info_->lock);
info_->icon.dataLoaded = true;
if (flags_ & GameInfoFlags::BG) {
ReadFileToString(&umd, "/PSP_GAME/PIC0.PNG", &info_->pic0.data, &info_->lock);
info_->pic0.dataLoaded = true;
ReadFileToString(&umd, "/PSP_GAME/PIC1.PNG", &info_->pic1.data, &info_->lock);
info_->pic1.dataLoaded = true;
2013-07-23 15:35:43 +00:00
}
if (flags_ & GameInfoFlags::SND) {
2014-06-22 01:38:26 +00:00
ReadFileToString(&umd, "/PSP_GAME/SND0.AT3", &info_->sndFileData, &info_->lock);
info_->pic1.dataLoaded = true;
2014-06-22 01:38:26 +00:00
}
2013-07-23 15:35:43 +00:00
break;
}
case IdentifiedFileType::PSP_ISO:
case IdentifiedFileType::PSP_ISO_NP:
{
SequentialHandleAllocator handles;
// Let's assume it's an ISO.
// TODO: This will currently read in the whole directory tree. Not really necessary for just a
// few files.
auto fl = info_->GetFileLoader();
if (!fl) {
2021-06-12 20:16:03 +00:00
return;
}
BlockDevice *bd = constructBlockDevice(info_->GetFileLoader().get());
if (!bd) {
2021-06-12 20:16:03 +00:00
return;
}
ISOFileSystem umd(&handles, bd);
// Alright, let's fetch the PARAM.SFO.
if (flags_ & GameInfoFlags::PARAM_SFO) {
std::string paramSFOcontents;
if (ReadFileToString(&umd, "/PSP_GAME/PARAM.SFO", &paramSFOcontents, nullptr)) {
{
std::lock_guard<std::mutex> lock(info_->lock);
info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size());
info_->ParseParamSFO();
}
2014-06-22 01:38:26 +00:00
}
}
if (flags_ & GameInfoFlags::BG) {
info_->pic0.dataLoaded = ReadFileToString(&umd, "/PSP_GAME/PIC0.PNG", &info_->pic0.data, &info_->lock);
info_->pic1.dataLoaded = ReadFileToString(&umd, "/PSP_GAME/PIC1.PNG", &info_->pic1.data, &info_->lock);
}
if (flags_ & GameInfoFlags::SND) {
info_->sndDataLoaded = ReadFileToString(&umd, "/PSP_GAME/SND0.AT3", &info_->sndFileData, &info_->lock);
}
// Fall back to unknown icon if ISO is broken/is a homebrew ISO, override is allowed though
if (flags_ & GameInfoFlags::ICON) {
if (!ReadFileToString(&umd, "/PSP_GAME/ICON0.PNG", &info_->icon.data, &info_->lock)) {
Path screenshot_jpg = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.jpg");
Path screenshot_png = GetSysDirectory(DIRECTORY_SCREENSHOT) / (info_->id + "_00000.png");
// Try using png/jpg screenshots first
if (File::Exists(screenshot_png))
info_->icon.dataLoaded = ReadLocalFileToString(screenshot_png, &info_->icon.data, &info_->lock);
else if (File::Exists(screenshot_jpg))
info_->icon.dataLoaded = ReadLocalFileToString(screenshot_jpg, &info_->icon.data, &info_->lock);
else {
DEBUG_LOG(LOADER, "Loading unknown.png because no icon was found");
info_->icon.dataLoaded = ReadVFSToString("unknown.png", &info_->icon.data, &info_->lock);
}
} else {
info_->icon.dataLoaded = true;
}
}
break;
}
case IdentifiedFileType::ARCHIVE_ZIP:
if (flags_ & GameInfoFlags::ICON) {
ReadVFSToString("zip.png", &info_->icon.data, &info_->lock);
info_->icon.dataLoaded = true;
}
2013-08-20 14:52:36 +00:00
break;
case IdentifiedFileType::ARCHIVE_RAR:
if (flags_ & GameInfoFlags::ICON) {
ReadVFSToString("rargray.png", &info_->icon.data, &info_->lock);
info_->icon.dataLoaded = true;
}
break;
case IdentifiedFileType::ARCHIVE_7Z:
if (flags_ & GameInfoFlags::ICON) {
ReadVFSToString("7z.png", &info_->icon.data, &info_->lock);
info_->icon.dataLoaded = true;
}
break;
case IdentifiedFileType::NORMAL_DIRECTORY:
2013-08-20 14:52:36 +00:00
default:
break;
}
if (flags_ & GameInfoFlags::SIZE) {
std::lock_guard<std::mutex> lock(info_->lock);
info_->gameSizeOnDisk = info_->GetGameSizeOnDiskInBytes();
info_->saveDataSize = info_->GetSaveDataSizeInBytes();
info_->installDataSize = info_->GetInstallDataSizeInBytes();
}
if (flags_ & GameInfoFlags::UNCOMPRESSED_SIZE) {
info_->gameSizeUncompressed = info_->GetGameSizeUncompressedInBytes();
}
// Time to update the flags.
std::unique_lock<std::mutex> lock(info_->lock);
info_->hasFlags |= flags_;
info_->pendingFlags &= ~flags_;
// INFO_LOG(SYSTEM, "Completed writing info for %s", info_->GetTitle().c_str());
}
private:
Path gamePath_;
std::shared_ptr<GameInfo> info_;
GameInfoFlags flags_{};
DISALLOW_COPY_AND_ASSIGN(GameInfoWorkItem);
};
GameInfoCache::GameInfoCache() {
Init();
}
GameInfoCache::~GameInfoCache() {
Clear();
Shutdown();
}
void GameInfoCache::Init() {}
void GameInfoCache::Shutdown() {
CancelAll();
}
void GameInfoCache::Clear() {
CancelAll();
2013-04-14 11:01:01 +00:00
info_.clear();
}
void GameInfoCache::CancelAll() {
for (auto info : info_) {
// GetFileLoader will create one if there isn't one already.
// Avoid that by checking.
if (info.second->HasFileLoader()) {
auto fl = info.second->GetFileLoader();
if (fl) {
fl->Cancel();
}
}
}
}
void GameInfoCache::FlushBGs() {
for (auto iter = info_.begin(); iter != info_.end(); iter++) {
std::lock_guard<std::mutex> lock(iter->second->lock);
iter->second->pic0.Clear();
iter->second->pic1.Clear();
if (!iter->second->sndFileData.empty()) {
iter->second->sndFileData.clear();
iter->second->sndDataLoaded = false;
}
iter->second->hasFlags &= ~(GameInfoFlags::BG | GameInfoFlags::SND);
}
}
2015-06-12 12:46:34 +00:00
void GameInfoCache::PurgeType(IdentifiedFileType fileType) {
for (auto iter = info_.begin(); iter != info_.end();) {
auto &info = iter->second;
// TODO: Find a better way to wait here.
while (info->pendingFlags != (GameInfoFlags)0) {
sleep_ms(1);
}
if (info->fileType == fileType) {
iter = info_.erase(iter);
} else {
iter++;
2015-06-12 12:46:34 +00:00
}
}
}
// Call on the main thread ONLY - that is from stuff called from NativeFrame.
// Can also be called from the audio thread for menu background music, but that cannot request images!
std::shared_ptr<GameInfo> GameInfoCache::GetInfo(Draw::DrawContext *draw, const Path &gamePath, GameInfoFlags wantFlags) {
const std::string &pathStr = gamePath.ToString();
// This is always needed to determine the method to get the other info, so make sure it's computed first.
wantFlags |= GameInfoFlags::FILE_TYPE;
mapLock_.lock();
auto iter = info_.find(pathStr);
if (iter != info_.end()) {
// There's already a structure about this game. Let's check.
std::shared_ptr<GameInfo> info = iter->second;
mapLock_.unlock();
info->FinishPendingTextureLoads(draw);
info->lastAccessedTime = time_now_d();
GameInfoFlags wanted = (GameInfoFlags)0;
{
// Careful now!
std::unique_lock<std::mutex> lock(info->lock);
GameInfoFlags hasFlags = info->hasFlags | info->pendingFlags; // We don't want to re-fetch data that we have, so or in pendingFlags.
wanted = (GameInfoFlags)((int)wantFlags & ~(int)hasFlags); // & is reserved for testing. ugh.
info->pendingFlags |= wanted;
}
if (wanted != (GameInfoFlags)0) {
// We're missing info that we want. Go get it!
GameInfoWorkItem *item = new GameInfoWorkItem(gamePath, info, wanted);
g_threadManager.EnqueueTask(item);
}
return info;
}
std::shared_ptr<GameInfo> info = std::make_shared<GameInfo>();
info->pendingFlags = wantFlags;
info->lastAccessedTime = time_now_d();
info_.insert(std::make_pair(pathStr, info));
mapLock_.unlock();
// Just get all the stuff we wanted.
GameInfoWorkItem *item = new GameInfoWorkItem(gamePath, info, wantFlags);
g_threadManager.EnqueueTask(item);
return info;
}