Skip to content

Commit

Permalink
UI: Allow installing texture packs from zips.
Browse files Browse the repository at this point in the history
Requires the textures.ini to have a [games] section.
  • Loading branch information
unknownbrackets committed Jul 15, 2019
1 parent b4d9d03 commit 8aed212
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 25 deletions.
203 changes: 182 additions & 21 deletions Core/Util/GameManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,11 @@
#include "Common/FileUtil.h"
#include "Common/StringUtils.h"
#include "Core/Config.h"
#include "Core/Loaders.h"
#include "Core/ELF/ParamSFO.h"
#include "Core/ELF/PBPReader.h"
#include "Core/System.h"
#include "Core/FileSystems/ISOFileSystem.h"
#include "Core/Util/GameManager.h"
#include "i18n/i18n.h"

Expand Down Expand Up @@ -171,8 +175,11 @@ ZipFileContents DetectZipFileContents(struct zip *z, ZipFileInfo *info) {
// directory of the Games tab (where else?).
bool isPSPMemstickGame = false;
bool isZippedISO = false;
bool isTexturePack = false;
int stripChars = 0;
int isoFileIndex = -1;
int stripCharsTexturePack = -1;
int textureIniIndex = -1;

for (int i = 0; i < numFiles; i++) {
const char *fn = zip_get_name(z, i, 0);
Expand All @@ -198,17 +205,33 @@ ZipFileContents DetectZipFileContents(struct zip *z, ZipFileInfo *info) {
isZippedISO = true;
isoFileIndex = i;
}
} else if (zippedName.find("textures.ini") != std::string::npos) {
int slashCount = 0;
int slashLocation = -1;
countSlashes(zippedName, &slashLocation, &slashCount);
if (stripCharsTexturePack == -1 || slashLocation < stripCharsTexturePack + 1) {
stripCharsTexturePack = slashLocation + 1;
isTexturePack = true;
textureIniIndex = i;
}
}
}

info->stripChars = stripChars;
info->numFiles = numFiles;
info->isoFileIndex = isoFileIndex;
info->textureIniIndex = textureIniIndex;
info->ignoreMetaFiles = false;

// If a ZIP is detected as both, let's let the memstick game interpretation prevail.
if (isPSPMemstickGame) {
return ZipFileContents::PSP_GAME_DIR;
} else if (isZippedISO) {
return ZipFileContents::ISO_FILE;
} else if (isTexturePack) {
info->stripChars = stripCharsTexturePack;
info->ignoreMetaFiles = true;
return ZipFileContents::TEXTURE_PACK;
} else {
return ZipFileContents::UNKNOWN;
}
Expand Down Expand Up @@ -236,7 +259,7 @@ bool GameManager::InstallGame(const std::string &url, const std::string &fileNam
installInProgress_ = true;

std::string pspGame = GetSysDirectory(DIRECTORY_GAME);
INFO_LOG(HLE, "Installing '%s' into '%s'", fileName.c_str(), pspGame.c_str());
std::string dest = pspGame;
int error = 0;
#ifdef _WIN32
struct zip *z = zip_open(ConvertUTF8ToWString(fileName).c_str(), 0, &error);
Expand All @@ -252,22 +275,148 @@ bool GameManager::InstallGame(const std::string &url, const std::string &fileNam
ZipFileContents contents = DetectZipFileContents(z, &info);
switch (contents) {
case ZipFileContents::PSP_GAME_DIR:
INFO_LOG(HLE, "Installing '%s' into '%s'", fileName.c_str(), pspGame.c_str());
// InstallMemstickGame contains code to close z.
return InstallMemstickGame(z, fileName, pspGame, info.numFiles, info.stripChars, deleteAfter);
return InstallMemstickGame(z, fileName, pspGame, info, false, deleteAfter);
case ZipFileContents::ISO_FILE:
INFO_LOG(HLE, "Installing '%s' into its containing directory", fileName.c_str());
// InstallZippedISO contains code to close z.
return InstallZippedISO(z, info.isoFileIndex, fileName, deleteAfter);
case ZipFileContents::TEXTURE_PACK:
// InstallMemstickGame contains code to close z, and works for textures too.
if (DetectTexturePackDest(z, info.textureIniIndex, &dest)) {
INFO_LOG(HLE, "Installing '%s' into '%s'", fileName.c_str(), dest.c_str());
File::CreateFullPath(dest);
File::CreateEmptyFile(dest + "/.nomedia");
return InstallMemstickGame(z, fileName, dest, info, true, deleteAfter);
}
return false;
default:
ERROR_LOG(HLE, "File not a PSP game, no EBOOT.PBP found.");
installProgress_ = 0.0f;
installInProgress_ = false;
installError_ = sy->T("Not a PSP game");
InstallDone();
SetInstallError(sy->T("Not a PSP game"));
if (deleteAfter)
File::Delete(fileName);
return false;
}
}

bool GameManager::DetectTexturePackDest(struct zip *z, int iniIndex, std::string *dest) {
I18NCategory *iz = GetI18NCategory("InstallZip");

struct zip_stat zstat;
zip_stat_index(z, iniIndex, 0, &zstat);

if (zstat.size >= 32 * 1024 * 1024) {
SetInstallError(iz->T("Texture pack doesn't support install"));
return false;
}

std::string buffer;
buffer.resize(zstat.size);
zip_file *zf = zip_fopen_index(z, iniIndex, 0);
if (zip_fread(zf, &buffer[0], buffer.size()) != (ssize_t)zstat.size) {
SetInstallError(iz->T("Zip archive corrupt"));
return false;
}

IniFile ini;
std::stringstream sstream(buffer);
ini.Load(sstream);

auto games = ini.GetOrCreateSection("games")->ToMap();
if (games.empty()) {
SetInstallError(iz->T("Texture pack doesn't support install"));
return false;
}

std::string gameID = games.begin()->first;
if (games.size() > 1) {
// Check for any supported game on their recent list and use that instead.
for (const std::string &path : g_Config.recentIsos) {
std::string recentID = GetGameID(path);
if (games.find(recentID) != games.end()) {
gameID = recentID;
break;
}
}
}

std::string pspTextures = GetSysDirectory(DIRECTORY_TEXTURES);
*dest = pspTextures + gameID + "/";
return true;
}

void GameManager::SetInstallError(const std::string &err) {
installProgress_ = 0.0f;
installInProgress_ = false;
installError_ = err;
InstallDone();
}

std::string GameManager::GetGameID(const std::string &path) const {
auto loader = ConstructFileLoader(path);
std::string id;

switch (Identify_File(loader)) {
case IdentifiedFileType::PSP_PBP_DIRECTORY:
delete loader;
loader = ConstructFileLoader(ResolvePBPFile(path));
id = GetPBPGameID(loader);
break;

case IdentifiedFileType::PSP_PBP:
id = GetPBPGameID(loader);
break;

case IdentifiedFileType::PSP_ISO:
case IdentifiedFileType::PSP_ISO_NP:
id = GetISOGameID(loader);
break;

default:
id.clear();
break;
}

delete loader;
return id;
}

std::string GameManager::GetPBPGameID(FileLoader *loader) const {
PBPReader pbp(loader);
std::vector<u8> sfoData;
if (pbp.GetSubFile(PBP_PARAM_SFO, &sfoData)) {
ParamSFOData sfo;
sfo.ReadSFO(sfoData);
return sfo.GetValueString("DISC_ID");
}
return "";
}

std::string GameManager::GetISOGameID(FileLoader *loader) const {
SequentialHandleAllocator handles;
BlockDevice *bd = constructBlockDevice(loader);
if (!bd) {
return "";
}
ISOFileSystem umd(&handles, bd);

PSPFileInfo info = umd.GetFileInfo("/PSP_GAME/PARAM.SFO");
int handle = 0;
if (info.exists) {
handle = umd.OpenFile("/PSP_GAME/PARAM.SFO", FILEACCESS_READ);
}

std::string sfoData;
sfoData.resize(info.size);
umd.ReadFile(handle, (u8 *)&sfoData[0], info.size);
umd.CloseFile(handle);

ParamSFOData sfo;
sfo.ReadSFO((const u8 *)sfoData.data(), sfoData.size());
return sfo.GetValueString("DISC_ID");
}

bool GameManager::ExtractFile(struct zip *z, int file_index, std::string outFilename, size_t *bytesCopied, size_t allBytes) {
struct zip_stat zstat;
zip_stat_index(z, file_index, 0, &zstat);
Expand Down Expand Up @@ -319,18 +468,33 @@ bool GameManager::ExtractFile(struct zip *z, int file_index, std::string outFile
}
}

bool GameManager::InstallMemstickGame(struct zip *z, std::string zipfile, std::string pspGame, int numFiles, int stripChars, bool deleteAfter) {
bool GameManager::InstallMemstickGame(struct zip *z, const std::string &zipfile, const std::string &dest, const ZipFileInfo &info, bool allowRoot, bool deleteAfter) {
size_t allBytes = 0;
size_t bytesCopied = 0;

I18NCategory *sy = GetI18NCategory("System");

auto fileAllowed = [&](const char *fn) {
if (!allowRoot && strchr(fn, '/') == 0)
return false;

const char *basefn = strrchr(fn, '/');
basefn = basefn ? basefn + 1 : fn;

if (info.ignoreMetaFiles) {
if (basefn[0] == '.' || !strcmp(basefn, "Thumbs.db") || !strcmp(basefn, "desktop.ini"))
return false;
}

return true;
};

// Create all the directories first in one pass
std::set<std::string> createdDirs;
for (int i = 0; i < numFiles; i++) {
for (int i = 0; i < info.numFiles; i++) {
const char *fn = zip_get_name(z, i, 0);
std::string zippedName = fn;
std::string outFilename = pspGame + zippedName.substr(stripChars);
std::string outFilename = dest + zippedName.substr(info.stripChars);
bool isDir = *outFilename.rbegin() == '/';
if (!isDir && outFilename.find("/") != std::string::npos) {
outFilename = outFilename.substr(0, outFilename.rfind('/'));
Expand All @@ -339,7 +503,7 @@ bool GameManager::InstallMemstickGame(struct zip *z, std::string zipfile, std::s
File::CreateFullPath(outFilename.c_str());
createdDirs.insert(outFilename);
}
if (!isDir && strchr(fn, '/') != 0) {
if (!isDir && fileAllowed(fn)) {
struct zip_stat zstat;
if (zip_stat_index(z, i, 0, &zstat) >= 0) {
allBytes += zstat.size;
Expand All @@ -349,13 +513,13 @@ bool GameManager::InstallMemstickGame(struct zip *z, std::string zipfile, std::s

// Now, loop through again in a second pass, writing files.
std::vector<std::string> createdFiles;
for (int i = 0; i < numFiles; i++) {
for (int i = 0; i < info.numFiles; i++) {
const char *fn = zip_get_name(z, i, 0);
// Note that we do NOT write files that are not in a directory, to avoid random
// README files etc.
if (strchr(fn, '/') != 0) {
fn += stripChars;
std::string outFilename = pspGame + fn;
if (fileAllowed(fn)) {
fn += info.stripChars;
std::string outFilename = dest + fn;
bool isDir = *outFilename.rbegin() == '/';
if (isDir)
continue;
Expand All @@ -367,10 +531,10 @@ bool GameManager::InstallMemstickGame(struct zip *z, std::string zipfile, std::s
}
}
}
INFO_LOG(HLE, "Extracted %i files (%i bytes / %i).", numFiles, (int)bytesCopied, (int)allBytes);
INFO_LOG(HLE, "Extracted %i files (%i bytes / %i).", info.numFiles, (int)bytesCopied, (int)allBytes);

zip_close(z);
z = 0;
z = nullptr;
installProgress_ = 1.0f;
installInProgress_ = false;
installError_ = "";
Expand All @@ -381,19 +545,16 @@ bool GameManager::InstallMemstickGame(struct zip *z, std::string zipfile, std::s
return true;

bail:
zip_close(z);
// We end up here if disk is full or couldn't write to storage for some other reason.
installProgress_ = 0.0f;
installInProgress_ = false;
installError_ = sy->T("Storage full");
zip_close(z);
// We don't delete the original in this case. Try to delete the files we created so far.
for (size_t i = 0; i < createdFiles.size(); i++) {
File::Delete(createdFiles[i].c_str());
}
for (auto iter = createdDirs.begin(); iter != createdDirs.end(); ++iter) {
File::DeleteDir(iter->c_str());
}
InstallDone();
SetInstallError(sy->T("Storage full"));
return false;
}

Expand Down
12 changes: 11 additions & 1 deletion Core/Util/GameManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ enum class GameManagerState {
};

struct zip;
class FileLoader;
struct ZipFileInfo;

class GameManager {
public:
Expand Down Expand Up @@ -70,13 +72,18 @@ class GameManager {

private:
bool InstallGame(const std::string &url, const std::string &tempFileName, bool deleteAfter);
bool InstallMemstickGame(struct zip *z, std::string zipFile, std::string pspGame, int numFiles, int stripChars, bool deleteAfter);
bool InstallMemstickGame(struct zip *z, const std::string &zipFile, const std::string &pspGame, const ZipFileInfo &info, bool allowRoot, bool deleteAfter);
bool InstallZippedISO(struct zip *z, int isoFileIndex, std::string zipfile, bool deleteAfter);
bool InstallRawISO(const std::string &zipFile, const std::string &originalName, bool deleteAfter);
void InstallDone();
bool ExtractFile(struct zip *z, int file_index, std::string outFilename, size_t *bytesCopied, size_t allBytes);
bool DetectTexturePackDest(struct zip *z, int iniIndex, std::string *dest);
void SetInstallError(const std::string &err);

std::string GetTempFilename() const;
std::string GetGameID(const std::string &path) const;
std::string GetPBPGameID(FileLoader *loader) const;
std::string GetISOGameID(FileLoader *loader) const;
std::shared_ptr<http::Download> curDownload_;
std::shared_ptr<std::thread> installThread_;
bool installInProgress_;
Expand All @@ -90,12 +97,15 @@ enum class ZipFileContents {
UNKNOWN,
PSP_GAME_DIR,
ISO_FILE,
TEXTURE_PACK,
};

struct ZipFileInfo {
int numFiles;
int stripChars; // for PSP game
int isoFileIndex; // for ISO
int textureIniIndex; // for textures
bool ignoreMetaFiles;
};

ZipFileContents DetectZipFileContents(struct zip *z, ZipFileInfo *info);
Expand Down
Loading

0 comments on commit 8aed212

Please sign in to comment.