2013-11-20 13:42:48 +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/.
|
|
|
|
|
2013-12-29 22:44:35 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <set>
|
2013-12-31 05:37:19 +00:00
|
|
|
#include <cstring>
|
2013-12-29 22:44:35 +00:00
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
#include "file/file_util.h"
|
2013-12-05 16:49:37 +00:00
|
|
|
#include "ext/libzip/zip.h"
|
|
|
|
#include "thread/thread.h"
|
2013-11-20 15:36:58 +00:00
|
|
|
#include "util/text/utf8.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
|
|
|
|
#include "Common/Log.h"
|
|
|
|
#include "Common/FileUtil.h"
|
2013-11-20 15:36:58 +00:00
|
|
|
#include "Core/Config.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
#include "Core/System.h"
|
|
|
|
#include "Core/Util/GameManager.h"
|
2015-03-04 12:59:09 +00:00
|
|
|
#include "i18n/i18n.h"
|
2013-11-20 13:42:48 +00:00
|
|
|
|
|
|
|
GameManager g_GameManager;
|
|
|
|
|
2013-12-05 12:01:00 +00:00
|
|
|
GameManager::GameManager()
|
|
|
|
: installInProgress_(false), installProgress_(0.0f) {
|
|
|
|
}
|
|
|
|
|
2013-11-20 15:36:58 +00:00
|
|
|
std::string GameManager::GetTempFilename() const {
|
|
|
|
#ifdef _WIN32
|
|
|
|
wchar_t tempPath[MAX_PATH];
|
|
|
|
GetTempPath(MAX_PATH, tempPath);
|
|
|
|
wchar_t buffer[MAX_PATH];
|
|
|
|
GetTempFileName(tempPath, L"PSP", 1, buffer);
|
|
|
|
return ConvertWStringToUTF8(buffer);
|
|
|
|
#else
|
|
|
|
return g_Config.externalDirectory + "/ppsspp.dl";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
bool GameManager::IsGameInstalled(std::string name) {
|
2013-11-20 15:36:58 +00:00
|
|
|
std::string pspGame = GetSysDirectory(DIRECTORY_GAME);
|
|
|
|
return File::Exists(pspGame + name);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool GameManager::DownloadAndInstall(std::string storeZipUrl) {
|
|
|
|
if (curDownload_.get() != 0) {
|
|
|
|
ERROR_LOG(HLE, "Can only process one download at a time");
|
|
|
|
return false;
|
|
|
|
}
|
2013-12-05 12:01:00 +00:00
|
|
|
if (installInProgress_) {
|
|
|
|
ERROR_LOG(HLE, "Can't download when an install is in progress (yet)");
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2013-11-20 15:36:58 +00:00
|
|
|
std::string filename = GetTempFilename();
|
2013-11-29 15:33:17 +00:00
|
|
|
curDownload_ = g_DownloadManager.StartDownload(storeZipUrl, filename);
|
2013-11-20 13:42:48 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-11-20 18:48:44 +00:00
|
|
|
bool GameManager::Uninstall(std::string name) {
|
|
|
|
if (name.empty()) {
|
|
|
|
ERROR_LOG(HLE, "Cannot remove an empty-named game");
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-20 15:36:58 +00:00
|
|
|
std::string gameDir = GetSysDirectory(DIRECTORY_GAME) + name;
|
|
|
|
INFO_LOG(HLE, "Deleting %s", gameDir.c_str());
|
|
|
|
if (!File::Exists(gameDir)) {
|
|
|
|
ERROR_LOG(HLE, "Game %s not installed, cannot uninstall", name.c_str());
|
2013-11-20 18:48:44 +00:00
|
|
|
return false;
|
2013-11-20 15:36:58 +00:00
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
|
2013-11-20 15:36:58 +00:00
|
|
|
bool success = File::DeleteDirRecursively(gameDir);
|
|
|
|
if (success) {
|
|
|
|
INFO_LOG(HLE, "Successfully deleted game %s", name.c_str());
|
|
|
|
g_Config.CleanRecent();
|
2013-11-20 18:48:44 +00:00
|
|
|
return true;
|
2013-11-20 15:36:58 +00:00
|
|
|
} else {
|
|
|
|
ERROR_LOG(HLE, "Failed to delete game %s", name.c_str());
|
2013-11-20 18:48:44 +00:00
|
|
|
return false;
|
2013-11-20 15:36:58 +00:00
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GameManager::Update() {
|
|
|
|
if (curDownload_.get() && curDownload_->Done()) {
|
2013-11-20 15:36:58 +00:00
|
|
|
INFO_LOG(HLE, "Download completed! Status = %i", curDownload_->ResultCode());
|
2013-11-29 16:34:21 +00:00
|
|
|
std::string zipName = curDownload_->outfile();
|
2013-11-20 15:36:58 +00:00
|
|
|
if (curDownload_->ResultCode() == 200) {
|
|
|
|
if (!File::Exists(zipName)) {
|
|
|
|
ERROR_LOG(HLE, "Downloaded file %s does not exist :(", zipName.c_str());
|
|
|
|
curDownload_.reset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Install the game!
|
|
|
|
InstallGame(zipName);
|
|
|
|
// Doesn't matter if the install succeeds or not, we delete the temp file to not squander space.
|
|
|
|
// TODO: Handle disk full?
|
|
|
|
deleteFile(zipName.c_str());
|
2013-11-29 16:34:21 +00:00
|
|
|
} else {
|
|
|
|
ERROR_LOG(HLE, "Expected HTTP status code 200, got status code %i. Install cancelled.", curDownload_->ResultCode());
|
|
|
|
deleteFile(zipName.c_str());
|
2013-11-20 15:36:58 +00:00
|
|
|
}
|
2013-11-29 15:33:17 +00:00
|
|
|
curDownload_.reset();
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:49:37 +00:00
|
|
|
bool GameManager::InstallGame(std::string zipfile, bool deleteAfter) {
|
2013-12-05 12:01:00 +00:00
|
|
|
if (installInProgress_) {
|
|
|
|
ERROR_LOG(HLE, "Cannot have two installs in progress at the same time");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-03-04 12:59:09 +00:00
|
|
|
I18NCategory *s = GetI18NCategory("System");
|
2013-12-05 12:01:00 +00:00
|
|
|
installInProgress_ = true;
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
std::string pspGame = GetSysDirectory(DIRECTORY_GAME);
|
|
|
|
INFO_LOG(HLE, "Installing %s into %s", zipfile.c_str(), pspGame.c_str());
|
|
|
|
|
2013-11-20 15:36:58 +00:00
|
|
|
if (!File::Exists(zipfile)) {
|
|
|
|
ERROR_LOG(HLE, "ZIP file %s doesn't exist", zipfile.c_str());
|
2013-12-05 12:01:00 +00:00
|
|
|
return false;
|
2013-11-20 15:36:58 +00:00
|
|
|
}
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
int error;
|
2013-11-20 15:36:58 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
struct zip *z = zip_open(ConvertUTF8ToWString(zipfile).c_str(), 0, &error);
|
2013-12-05 14:18:26 +00:00
|
|
|
#elif defined(__SYMBIAN32__)
|
2013-12-05 14:35:45 +00:00
|
|
|
// If zipfile is non-ascii, this may not function correctly. Other options?
|
|
|
|
struct zip *z = zip_open(std::wstring(zipfile.begin(), zipfile.end()).c_str(), 0, &error);
|
2013-11-20 15:36:58 +00:00
|
|
|
#else
|
2013-11-20 13:42:48 +00:00
|
|
|
struct zip *z = zip_open(zipfile.c_str(), 0, &error);
|
2013-11-20 15:36:58 +00:00
|
|
|
#endif
|
2013-11-20 13:42:48 +00:00
|
|
|
if (!z) {
|
|
|
|
ERROR_LOG(HLE, "Failed to open ZIP file %s, error code=%i", zipfile.c_str(), error);
|
2013-12-05 12:01:00 +00:00
|
|
|
return false;
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int numFiles = zip_get_num_files(z);
|
|
|
|
|
2013-11-20 15:36:58 +00:00
|
|
|
// First, find all the directories, and precreate them before we fill in with files.
|
2013-11-20 15:50:09 +00:00
|
|
|
// Also, verify that this is a PSP zip file with the correct layout.
|
|
|
|
bool isPSP = false;
|
2013-11-29 16:34:21 +00:00
|
|
|
int stripChars = 0;
|
2013-12-05 13:11:35 +00:00
|
|
|
|
2013-11-20 15:36:58 +00:00
|
|
|
for (int i = 0; i < numFiles; i++) {
|
|
|
|
const char *fn = zip_get_name(z, i, 0);
|
2013-11-20 15:50:09 +00:00
|
|
|
std::string zippedName = fn;
|
|
|
|
if (zippedName.find("EBOOT.PBP") != std::string::npos) {
|
|
|
|
int slashCount = 0;
|
2013-12-05 12:22:06 +00:00
|
|
|
int lastSlashLocation = -1;
|
|
|
|
int slashLocation = -1;
|
2013-11-20 15:50:09 +00:00
|
|
|
for (size_t i = 0; i < zippedName.size(); i++) {
|
2013-11-29 16:34:21 +00:00
|
|
|
if (zippedName[i] == '/') {
|
2013-11-20 15:50:09 +00:00
|
|
|
slashCount++;
|
2013-11-29 16:34:21 +00:00
|
|
|
slashLocation = lastSlashLocation;
|
2013-12-08 20:02:37 +00:00
|
|
|
lastSlashLocation = (int)i;
|
2013-11-29 16:34:21 +00:00
|
|
|
}
|
2013-11-20 15:50:09 +00:00
|
|
|
}
|
2013-11-29 16:34:21 +00:00
|
|
|
if (slashCount >= 1 && (!isPSP || slashLocation < stripChars + 1)) {
|
|
|
|
stripChars = slashLocation + 1;
|
2013-11-20 15:50:09 +00:00
|
|
|
isPSP = true;
|
|
|
|
} else {
|
|
|
|
INFO_LOG(HLE, "Wrong number of slashes (%i) in %s", slashCount, zippedName.c_str());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isPSP) {
|
2013-11-29 16:34:21 +00:00
|
|
|
ERROR_LOG(HLE, "File not a PSP game, no EBOOT.PBP found.");
|
2013-12-06 14:29:14 +00:00
|
|
|
installProgress_ = 0.0f;
|
|
|
|
installInProgress_ = false;
|
2015-03-04 12:59:09 +00:00
|
|
|
installError_ = s->T("Not a PSP game");
|
2013-12-06 14:29:14 +00:00
|
|
|
InstallDone();
|
2013-12-05 12:01:00 +00:00
|
|
|
return false;
|
2013-11-20 15:36:58 +00:00
|
|
|
}
|
|
|
|
|
2013-12-05 12:22:06 +00:00
|
|
|
size_t allBytes = 0, bytesCopied = 0;
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
// Create all the directories in one pass
|
2013-12-05 13:11:35 +00:00
|
|
|
std::set<std::string> createdDirs;
|
2013-11-20 13:42:48 +00:00
|
|
|
for (int i = 0; i < numFiles; i++) {
|
|
|
|
const char *fn = zip_get_name(z, i, 0);
|
2013-11-20 15:50:09 +00:00
|
|
|
std::string zippedName = fn;
|
2013-11-29 16:34:21 +00:00
|
|
|
std::string outFilename = pspGame + zippedName.substr(stripChars);
|
2013-12-05 12:41:23 +00:00
|
|
|
bool isDir = *outFilename.rbegin() == '/';
|
2013-12-05 13:11:35 +00:00
|
|
|
if (!isDir && outFilename.find("/") != std::string::npos) {
|
|
|
|
outFilename = outFilename.substr(0, outFilename.rfind('/'));
|
|
|
|
}
|
|
|
|
if (createdDirs.find(outFilename) == createdDirs.end()) {
|
2013-11-20 13:42:48 +00:00
|
|
|
File::CreateFullPath(outFilename.c_str());
|
2013-12-05 13:11:35 +00:00
|
|
|
createdDirs.insert(outFilename);
|
|
|
|
}
|
2013-12-31 05:48:36 +00:00
|
|
|
if (!isDir && strchr(fn, '/') != 0) {
|
2013-12-05 12:22:06 +00:00
|
|
|
struct zip_stat zstat;
|
|
|
|
if (zip_stat_index(z, i, 0, &zstat) >= 0) {
|
|
|
|
allBytes += zstat.size;
|
|
|
|
}
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now, loop through again in a second pass, writing files.
|
2013-12-06 11:49:57 +00:00
|
|
|
std::vector<std::string> createdFiles;
|
2013-11-20 13:42:48 +00:00
|
|
|
for (int i = 0; i < numFiles; i++) {
|
|
|
|
const char *fn = zip_get_name(z, i, 0);
|
2013-11-20 15:36:58 +00:00
|
|
|
// Note that we do NOT write files that are not in a directory, to avoid random
|
|
|
|
// README files etc.
|
2013-12-31 05:48:36 +00:00
|
|
|
if (strchr(fn, '/') != 0) {
|
2013-11-20 13:42:48 +00:00
|
|
|
struct zip_stat zstat;
|
2013-12-05 12:22:06 +00:00
|
|
|
zip_stat_index(z, i, 0, &zstat);
|
2013-11-20 13:42:48 +00:00
|
|
|
size_t size = zstat.size;
|
|
|
|
|
2013-11-29 16:34:21 +00:00
|
|
|
fn += stripChars;
|
|
|
|
|
2013-11-20 13:42:48 +00:00
|
|
|
std::string outFilename = pspGame + fn;
|
2013-12-05 12:41:23 +00:00
|
|
|
bool isDir = *outFilename.rbegin() == '/';
|
2013-12-05 12:22:06 +00:00
|
|
|
if (isDir)
|
|
|
|
continue;
|
2013-12-05 12:01:00 +00:00
|
|
|
|
2013-12-05 12:22:06 +00:00
|
|
|
if (i < 10) {
|
|
|
|
INFO_LOG(HLE, "Writing %i bytes to %s", (int)size, outFilename.c_str());
|
|
|
|
}
|
2013-12-05 12:01:00 +00:00
|
|
|
|
2013-12-05 12:22:06 +00:00
|
|
|
zip_file *zf = zip_fopen_index(z, i, 0);
|
|
|
|
FILE *f = fopen(outFilename.c_str(), "wb");
|
|
|
|
if (f) {
|
|
|
|
size_t pos = 0;
|
|
|
|
const size_t blockSize = 1024 * 128;
|
|
|
|
u8 *buffer = new u8[blockSize];
|
|
|
|
while (pos < size) {
|
|
|
|
size_t bs = std::min(blockSize, pos - size);
|
|
|
|
zip_fread(zf, buffer, bs);
|
|
|
|
size_t written = fwrite(buffer, 1, bs, f);
|
|
|
|
if (written != bs) {
|
|
|
|
ERROR_LOG(HLE, "Wrote %i bytes out of %i - Disk full?", (int)written, (int)bs);
|
2013-12-06 11:49:57 +00:00
|
|
|
delete [] buffer;
|
|
|
|
buffer = 0;
|
|
|
|
fclose(f);
|
|
|
|
zip_fclose(zf);
|
|
|
|
deleteFile(outFilename.c_str());
|
|
|
|
// Yes it's a goto. Sue me. I think it's appropriate here.
|
|
|
|
goto bail;
|
2013-12-05 12:22:06 +00:00
|
|
|
}
|
|
|
|
pos += bs;
|
|
|
|
|
|
|
|
bytesCopied += bs;
|
|
|
|
installProgress_ = (float)bytesCopied / (float)allBytes;
|
|
|
|
// printf("Progress: %f\n", installProgress_);
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
2013-12-05 12:22:06 +00:00
|
|
|
zip_fclose(zf);
|
|
|
|
fclose(f);
|
2013-12-06 11:49:57 +00:00
|
|
|
createdFiles.push_back(outFilename);
|
2013-11-20 13:42:48 +00:00
|
|
|
delete [] buffer;
|
2013-12-05 12:22:06 +00:00
|
|
|
} else {
|
|
|
|
ERROR_LOG(HLE, "Failed to open file for writing");
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-06 14:29:14 +00:00
|
|
|
INFO_LOG(HLE, "Extracted %i files (%i bytes / %i).", numFiles, (int)bytesCopied, (int)allBytes);
|
2013-11-20 13:42:48 +00:00
|
|
|
|
|
|
|
zip_close(z);
|
2013-12-06 11:49:57 +00:00
|
|
|
z = 0;
|
2013-12-05 12:01:00 +00:00
|
|
|
installProgress_ = 1.0f;
|
|
|
|
installInProgress_ = false;
|
2013-12-06 11:49:57 +00:00
|
|
|
installError_ = "";
|
2013-12-05 16:49:37 +00:00
|
|
|
if (deleteAfter) {
|
|
|
|
deleteFile(zipfile.c_str());
|
|
|
|
}
|
|
|
|
InstallDone();
|
2013-12-05 12:22:06 +00:00
|
|
|
return true;
|
2013-12-06 11:49:57 +00:00
|
|
|
|
|
|
|
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;
|
2015-03-04 12:59:09 +00:00
|
|
|
installError_ = s->T("Storage full");
|
2013-12-06 11:49:57 +00:00
|
|
|
if (deleteAfter) {
|
|
|
|
deleteFile(zipfile.c_str());
|
|
|
|
}
|
|
|
|
for (size_t i = 0; i < createdFiles.size(); i++) {
|
|
|
|
deleteFile(createdFiles[i].c_str());
|
|
|
|
}
|
|
|
|
for (auto iter = createdDirs.begin(); iter != createdDirs.end(); ++iter) {
|
|
|
|
deleteDir(iter->c_str());
|
|
|
|
}
|
|
|
|
InstallDone();
|
|
|
|
return false;
|
2013-11-20 13:42:48 +00:00
|
|
|
}
|
2013-12-05 13:11:35 +00:00
|
|
|
|
2013-12-05 16:49:37 +00:00
|
|
|
bool GameManager::InstallGameOnThread(std::string zipFile, bool deleteAfter) {
|
2013-12-05 13:11:35 +00:00
|
|
|
if (installInProgress_) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-12-05 16:49:37 +00:00
|
|
|
installThread_.reset(new std::thread(std::bind(&GameManager::InstallGame, this, zipFile, deleteAfter)));
|
|
|
|
installThread_->detach();
|
2013-12-05 13:11:35 +00:00
|
|
|
return true;
|
|
|
|
}
|
2013-12-05 16:49:37 +00:00
|
|
|
|
|
|
|
void GameManager::InstallDone() {
|
|
|
|
if (installThread_.get() != 0) {
|
|
|
|
installThread_.reset();
|
|
|
|
}
|
|
|
|
}
|