2013-03-30 14:44:10 +00:00
|
|
|
// 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 <string>
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
#include "base/timeutil.h"
|
|
|
|
#include "base/stringutil.h"
|
|
|
|
#include "image/png_load.h"
|
2013-04-13 19:24:07 +00:00
|
|
|
#include "thread/prioritizedworkqueue.h"
|
2013-03-30 14:44:10 +00:00
|
|
|
#include "GameInfoCache.h"
|
|
|
|
#include "Core/FileSystems/ISOFileSystem.h"
|
|
|
|
#include "Core/FileSystems/DirectoryFileSystem.h"
|
2013-04-08 19:46:41 +00:00
|
|
|
#include "Core/ELF/PBPReader.h"
|
2013-03-30 14:44:10 +00:00
|
|
|
|
2013-03-30 18:23:20 +00:00
|
|
|
GameInfoCache g_gameInfoCache;
|
|
|
|
|
2013-03-30 14:44:10 +00:00
|
|
|
|
2013-04-13 20:29:42 +00:00
|
|
|
static bool ReadFileToString(IFileSystem *fs, const char *filename, std::string *contents, recursive_mutex *mtx) {
|
2013-03-30 14:44:10 +00:00
|
|
|
PSPFileInfo info = fs->GetFileInfo(filename);
|
2013-04-01 10:35:02 +00:00
|
|
|
if (!info.exists) {
|
2013-03-30 14:44:10 +00:00
|
|
|
return false;
|
2013-04-01 10:35:02 +00:00
|
|
|
}
|
2013-03-30 14:44:10 +00:00
|
|
|
|
|
|
|
int handle = fs->OpenFile(filename, FILEACCESS_READ);
|
2013-04-01 10:35:02 +00:00
|
|
|
if (!handle) {
|
2013-03-30 14:44:10 +00:00
|
|
|
return false;
|
2013-04-01 10:35:02 +00:00
|
|
|
}
|
2013-03-30 14:44:10 +00:00
|
|
|
|
2013-04-13 20:29:42 +00:00
|
|
|
if (mtx) {
|
|
|
|
lock_guard lock(*mtx);
|
|
|
|
contents->resize(info.size);
|
|
|
|
fs->ReadFile(handle, (u8 *)contents->data(), info.size);
|
|
|
|
} else {
|
|
|
|
contents->resize(info.size);
|
|
|
|
fs->ReadFile(handle, (u8 *)contents->data(), info.size);
|
|
|
|
}
|
2013-03-30 14:44:10 +00:00
|
|
|
fs->CloseFile(handle);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:24:07 +00:00
|
|
|
|
|
|
|
class GameInfoWorkItem : public PrioritizedWorkQueueItem {
|
|
|
|
public:
|
|
|
|
GameInfoWorkItem(const std::string &gamePath, GameInfo *info)
|
|
|
|
: gamePath_(gamePath), info_(info) {
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void run() {
|
|
|
|
// A game can be either an UMD or a directory under ms0:/PSP/GAME .
|
|
|
|
if (startsWith(gamePath_, "ms0:/PSP/GAME")) {
|
|
|
|
return;
|
|
|
|
// TODO: The case of these extensions is not perfect.
|
|
|
|
} else if (endsWith(gamePath_, ".PBP")) {
|
|
|
|
PBPReader pbp(gamePath_.c_str());
|
|
|
|
if (!pbp.IsValid())
|
|
|
|
return;
|
2013-04-13 20:39:19 +00:00
|
|
|
info_->fileType = FILETYPE_PSP_PBP;
|
2013-04-13 19:24:07 +00:00
|
|
|
// First, PARAM.SFO.
|
|
|
|
size_t sfoSize;
|
|
|
|
u8 *sfoData = pbp.GetSubFile(PBP_PARAM_SFO, &sfoSize);
|
|
|
|
{
|
|
|
|
lock_guard lock(info_->lock);
|
|
|
|
info_->paramSFO.ReadSFO(sfoData, sfoSize);
|
|
|
|
info_->title = info_->paramSFO.GetValueString("TITLE");
|
|
|
|
}
|
|
|
|
delete [] sfoData;
|
|
|
|
|
|
|
|
// Then, ICON0.PNG.
|
|
|
|
{
|
|
|
|
lock_guard lock(info_->lock);
|
|
|
|
if (pbp.GetSubFileSize(PBP_ICON0_PNG) > 0) {
|
|
|
|
pbp.GetSubFileAsString(PBP_ICON0_PNG, &info_->iconTextureData);
|
|
|
|
} else {
|
|
|
|
// We should load a default image here.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (info_->wantBG) {
|
|
|
|
{
|
|
|
|
lock_guard lock(info_->lock);
|
|
|
|
if (pbp.GetSubFileSize(PBP_PIC1_PNG) > 0)
|
|
|
|
pbp.GetSubFileAsString(PBP_PIC1_PNG, &info_->pic1TextureData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (endsWith(gamePath_, ".elf") || endsWith(gamePath_, ".prx")) {
|
2013-04-13 20:39:19 +00:00
|
|
|
info_->fileType = FILETYPE_PSP_ELF;
|
2013-04-13 19:24:07 +00:00
|
|
|
return;
|
|
|
|
} else {
|
2013-04-13 20:39:19 +00:00
|
|
|
info_->fileType = FILETYPE_PSP_ISO;
|
2013-04-13 19:24:07 +00:00
|
|
|
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.
|
|
|
|
BlockDevice *bd = constructBlockDevice(gamePath_.c_str());
|
|
|
|
if (!bd)
|
|
|
|
return; // nothing to do here..
|
|
|
|
ISOFileSystem umd(&handles, bd, "/PSP_GAME");
|
|
|
|
|
|
|
|
// Alright, let's fetch the PARAM.SFO.
|
|
|
|
std::string paramSFOcontents;
|
2013-04-13 20:29:42 +00:00
|
|
|
if (ReadFileToString(&umd, "/PSP_GAME/PARAM.SFO", ¶mSFOcontents, 0)) {
|
2013-04-13 19:24:07 +00:00
|
|
|
lock_guard lock(info_->lock);
|
|
|
|
info_->paramSFO.ReadSFO((const u8 *)paramSFOcontents.data(), paramSFOcontents.size());
|
|
|
|
info_->title = info_->paramSFO.GetValueString("TITLE");
|
|
|
|
}
|
|
|
|
|
2013-04-13 20:29:42 +00:00
|
|
|
{
|
|
|
|
ReadFileToString(&umd, "/PSP_GAME/ICON0.PNG", &info_->iconTextureData, &info_->lock);
|
|
|
|
}
|
|
|
|
|
2013-04-13 19:24:07 +00:00
|
|
|
if (info_->wantBG) {
|
|
|
|
{
|
2013-04-13 20:29:42 +00:00
|
|
|
ReadFileToString(&umd, "/PSP_GAME/PIC0.PNG", &info_->pic0TextureData, &info_->lock);
|
2013-04-13 19:24:07 +00:00
|
|
|
}
|
|
|
|
{
|
2013-04-13 20:29:42 +00:00
|
|
|
ReadFileToString(&umd, "/PSP_GAME/PIC1.PNG", &info_->pic1TextureData, &info_->lock);
|
2013-04-13 19:24:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual float priority() {
|
|
|
|
return info_->lastAccessedTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::string gamePath_;
|
|
|
|
GameInfo *info_;
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(GameInfoWorkItem);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
GameInfoCache::~GameInfoCache() {
|
2013-04-14 09:58:28 +00:00
|
|
|
Clear();
|
2013-04-13 19:24:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameInfoCache::Init() {
|
|
|
|
gameInfoWQ_ = new PrioritizedWorkQueue();
|
|
|
|
ProcessWorkQueueOnThreadWhile(gameInfoWQ_);
|
|
|
|
}
|
|
|
|
|
|
|
|
void GameInfoCache::Shutdown() {
|
|
|
|
StopProcessingWorkQueue(gameInfoWQ_);
|
|
|
|
}
|
|
|
|
|
2013-03-30 14:44:10 +00:00
|
|
|
void GameInfoCache::Save()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:35:02 +00:00
|
|
|
void GameInfoCache::Load() {
|
2013-03-30 14:44:10 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:35:02 +00:00
|
|
|
void GameInfoCache::Decimate() {
|
2013-03-30 14:44:10 +00:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2013-04-14 09:58:28 +00:00
|
|
|
void GameInfoCache::Clear() {
|
|
|
|
gameInfoWQ_->Flush();
|
|
|
|
for (auto iter = info_.begin(); iter != info_.end(); iter++) {
|
|
|
|
lock_guard lock(iter->second->lock);
|
|
|
|
if (!iter->second->pic0TextureData.empty()) {
|
|
|
|
iter->second->pic0TextureData.clear();
|
|
|
|
}
|
|
|
|
if (iter->second->pic0Texture) {
|
|
|
|
delete iter->second->pic0Texture;
|
|
|
|
iter->second->pic0Texture = 0;
|
|
|
|
}
|
|
|
|
if (!iter->second->pic1TextureData.empty()) {
|
|
|
|
iter->second->pic1TextureData.clear();
|
|
|
|
}
|
|
|
|
if (iter->second->pic1Texture) {
|
|
|
|
delete iter->second->pic1Texture;
|
|
|
|
iter->second->pic1Texture = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-01 10:35:02 +00:00
|
|
|
void GameInfoCache::FlushBGs() {
|
|
|
|
for (auto iter = info_.begin(); iter != info_.end(); iter++) {
|
|
|
|
lock_guard lock(iter->second->lock);
|
|
|
|
if (!iter->second->pic0TextureData.empty()) {
|
|
|
|
iter->second->pic0TextureData.clear();
|
|
|
|
}
|
|
|
|
if (iter->second->pic0Texture) {
|
|
|
|
delete iter->second->pic0Texture;
|
|
|
|
iter->second->pic0Texture = 0;
|
|
|
|
}
|
|
|
|
if (!iter->second->pic1TextureData.empty()) {
|
|
|
|
iter->second->pic1TextureData.clear();
|
|
|
|
}
|
|
|
|
if (iter->second->pic1Texture) {
|
|
|
|
delete iter->second->pic1Texture;
|
|
|
|
iter->second->pic1Texture = 0;
|
|
|
|
}
|
|
|
|
}
|
2013-03-30 14:44:10 +00:00
|
|
|
}
|
|
|
|
|
2013-04-13 19:24:07 +00:00
|
|
|
void GameInfoCache::Add(const std::string &key, GameInfo *info_) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-30 14:44:10 +00:00
|
|
|
// This may run off-main-thread and we thus can't use the global
|
|
|
|
// pspFileSystem (well, we could with synchronization but there might not
|
|
|
|
// even be a game running).
|
|
|
|
GameInfo *GameInfoCache::GetInfo(const std::string &gamePath, bool wantBG) {
|
|
|
|
auto iter = info_.find(gamePath);
|
|
|
|
if (iter != info_.end()) {
|
2013-03-30 18:23:20 +00:00
|
|
|
GameInfo *info = iter->second;
|
2013-03-31 00:04:46 +00:00
|
|
|
if (!info->wantBG && wantBG) {
|
2013-04-13 20:29:42 +00:00
|
|
|
// Need to start over. We'll just add a new work item.
|
|
|
|
delete info; // Hm, how dangerous is this? There might be a race condition here.
|
2013-03-31 00:04:46 +00:00
|
|
|
goto again;
|
|
|
|
}
|
2013-04-13 20:29:42 +00:00
|
|
|
{
|
|
|
|
lock_guard lock(info->lock);
|
|
|
|
if (info->iconTextureData.size()) {
|
|
|
|
info->iconTexture = new Texture();
|
|
|
|
// TODO: We could actually do the PNG decoding as well on the async thread.
|
|
|
|
// We'd have to split up Texture->LoadPNG though, creating some intermediate Image class maybe.
|
|
|
|
if (info->iconTexture->LoadPNG((const u8 *)info->iconTextureData.data(), info->iconTextureData.size(), false)) {
|
|
|
|
info->timeIconWasLoaded = time_now_d();
|
|
|
|
}
|
|
|
|
info->iconTextureData.clear();
|
2013-03-30 18:23:20 +00:00
|
|
|
}
|
|
|
|
}
|
2013-04-13 20:29:42 +00:00
|
|
|
{
|
|
|
|
lock_guard lock(info->lock);
|
|
|
|
if (info->pic0TextureData.size()) {
|
|
|
|
info->pic0Texture = new Texture();
|
|
|
|
if (info->pic0Texture->LoadPNG((const u8 *)info->pic0TextureData.data(), info->pic0TextureData.size(), false)) {
|
|
|
|
info->timePic0WasLoaded = time_now_d();
|
|
|
|
}
|
|
|
|
info->pic0TextureData.clear();
|
2013-03-30 18:23:20 +00:00
|
|
|
}
|
2013-04-01 10:35:02 +00:00
|
|
|
}
|
2013-04-13 20:29:42 +00:00
|
|
|
{
|
|
|
|
lock_guard lock(info->lock);
|
|
|
|
if (info->pic1TextureData.size()) {
|
|
|
|
info->pic1Texture = new Texture();
|
|
|
|
if (info->pic1Texture->LoadPNG((const u8 *)info->pic1TextureData.data(), info->pic1TextureData.size(), false)) {
|
|
|
|
info->timePic1WasLoaded = time_now_d();
|
|
|
|
}
|
|
|
|
info->pic1TextureData.clear();
|
2013-04-01 10:35:02 +00:00
|
|
|
}
|
2013-03-30 18:23:20 +00:00
|
|
|
}
|
2013-03-30 14:44:10 +00:00
|
|
|
iter->second->lastAccessedTime = time_now_d();
|
|
|
|
return iter->second;
|
|
|
|
}
|
|
|
|
|
2013-03-31 00:04:46 +00:00
|
|
|
again:
|
|
|
|
|
2013-04-13 19:24:07 +00:00
|
|
|
GameInfo *info = new GameInfo();
|
|
|
|
info->wantBG = wantBG;
|
2013-03-30 14:44:10 +00:00
|
|
|
|
2013-04-13 19:24:07 +00:00
|
|
|
GameInfoWorkItem *item = new GameInfoWorkItem(gamePath, info);
|
|
|
|
gameInfoWQ_->Add(item);
|
2013-03-30 14:44:10 +00:00
|
|
|
|
2013-04-13 19:24:07 +00:00
|
|
|
info_[gamePath] = info;
|
|
|
|
return info;
|
2013-03-30 14:44:10 +00:00
|
|
|
}
|