Path: blob/master/Core/Dialog/PSPGamedataInstallDialog.cpp
3186 views
// Copyright (c) 2012- PPSSPP Project.12// This program is free software: you can redistribute it and/or modify3// it under the terms of the GNU General Public License as published by4// the Free Software Foundation, version 2.0 or later versions.56// This program is distributed in the hope that it will be useful,7// but WITHOUT ANY WARRANTY; without even the implied warranty of8// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the9// GNU General Public License 2.0 for more details.1011// A copy of the GPL 2.0 should have been included with the program.12// If not, see http://www.gnu.org/licenses/1314// Official git repository and contact information can be found at15// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.1617#include <algorithm>18#include "Common/CommonTypes.h"19#include "Common/Serialize/Serializer.h"20#include "Common/Serialize/SerializeFuncs.h"21#include "Core/HLE/ErrorCodes.h"22#include "Core/ELF/ParamSFO.h"23#include "Core/MemMapHelpers.h"24#include "Core/Reporting.h"25#include "Core/System.h"26#include "Core/FileSystems/MetaFileSystem.h"27#include "Core/Dialog/PSPGamedataInstallDialog.h"28#include "Common/Data/Text/I18n.h"29#include "UI/OnScreenDisplay.h"3031std::string saveBasePath = "ms0:/PSP/SAVEDATA/";3233// Guesses.34const static int GAMEDATA_INIT_DELAY_US = 200000;35const static int GAMEDATA_SHUTDOWN_DELAY_US = 2000;36const static u32 GAMEDATA_BYTES_PER_READ = 32768;37// TODO: Could adjust based on real-time into frame? Or eat cycles?38// If this is too high, some games (e.g. Senjou no Valkyria 3) will lag.39const static u32 GAMEDATA_READS_PER_UPDATE = 20;4041const u32 PSP_UTILITY_GAMEDATA_MODE_SHOW_PROGRESS = 1;4243static const std::string SFO_FILENAME = "PARAM.SFO";4445namespace46{47std::vector<std::string> GetPSPFileList (const std::string &dirpath) {48std::vector<std::string> FileList;49auto Fileinfos = pspFileSystem.GetDirListing(dirpath);50FileList.reserve(Fileinfos.size());5152for (auto it = Fileinfos.begin(); it != Fileinfos.end(); ++it) {53std::string info = (*it).name;54FileList.push_back(info);55}56return FileList;57}58}5960PSPGamedataInstallDialog::PSPGamedataInstallDialog(UtilityDialogType type) : PSPDialog(type) {61}6263PSPGamedataInstallDialog::~PSPGamedataInstallDialog() {64}6566int PSPGamedataInstallDialog::Init(u32 paramAddr) {67if (GetStatus() != SCE_UTILITY_STATUS_NONE) {68ERROR_LOG_REPORT(Log::sceUtility, "A game install request is already running, not starting a new one");69return SCE_ERROR_UTILITY_INVALID_STATUS;70}7172param.ptr = paramAddr;73inFileNames = GetPSPFileList("disc0:/PSP_GAME/INSDIR");74numFiles = (int)inFileNames.size();75readFiles = 0;76progressValue = 0;77allFilesSize = 0;78allReadSize = 0;79currentInputFile = 0;80currentOutputFile = 0;8182for (std::string filename : inFileNames) {83allFilesSize += pspFileSystem.GetFileInfo("disc0:/PSP_GAME/INSDIR/" + filename).size;84}8586if (allFilesSize == 0) {87ERROR_LOG_REPORT(Log::sceUtility, "Game install with no files / data");88// Getting a lot of reports of this from patched football games. Can probably ignore. https://report.ppsspp.org/logs/kind/79389return -1;90}9192int size = Memory::Read_U32(paramAddr);93if (size != 1424 && size != 1432) {94ERROR_LOG_REPORT(Log::sceUtility, "sceGamedataInstallInitStart: invalid param size %d", size);95return SCE_ERROR_UTILITY_INVALID_PARAM_SIZE;96}9798memset(&request, 0, sizeof(request));99// Only copy the right size to support different request format100Memory::Memcpy(&request, paramAddr, size, "sceGamedataInstallInitStart");101InitCommon();102103ChangeStatusInit(GAMEDATA_INIT_DELAY_US);104return 0;105}106107int PSPGamedataInstallDialog::Update(int animSpeed) {108if (GetStatus() != SCE_UTILITY_STATUS_RUNNING)109return SCE_ERROR_UTILITY_INVALID_STATUS;110111if (param->mode >= 2) {112param->common.result = SCE_ERROR_UTILITY_GAMEDATA_INVALID_MODE;113param.NotifyWrite("DialogResult");114ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);115WARN_LOG_REPORT(Log::sceUtility, "sceUtilityGamedataInstallUpdate: invalid mode %d", param->mode);116return 0;117}118119UpdateCommon();120121// TODO: param->mode == 1 should show a prompt to confirm, then a progress bar.122// Any other mode (i.e. 0 or negative) should proceed and show no UI.123124// TODO: This should return error codes in some cases, like write failure.125// request.common.result must be updated for errors as well.126127if (readFiles < numFiles) {128if (currentInputFile != 0 && currentOutputFile != 0) {129// Continue copying, this will close once done automatically.130CopyCurrentFileData();131} else {132OpenNextFile();133}134135UpdateProgress();136} else {137WriteSfoFile();138139// TODO: What is this? Should one of these update per file or anything?140param->unknownResult1 = readFiles;141param->unknownResult2 = readFiles;142param.NotifyWrite("DialogResult");143144ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);145}146return 0;147}148149void PSPGamedataInstallDialog::OpenNextFile() {150std::string inputFileName = "disc0:/PSP_GAME/INSDIR/" + inFileNames[readFiles];151std::string outputFileName = GetGameDataInstallFileName(&request, inFileNames[readFiles]);152153currentInputFile = pspFileSystem.OpenFile(inputFileName, FILEACCESS_READ);154if (currentInputFile < 0) {155// TODO: Generate an error code?156ERROR_LOG_REPORT(Log::sceUtility, "Unable to read from install file: %s", inFileNames[readFiles].c_str());157++readFiles;158currentInputFile = 0;159return;160}161currentOutputFile = pspFileSystem.OpenFile(outputFileName, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));162if (currentOutputFile < 0) {163// TODO: Generate an error code?164ERROR_LOG(Log::sceUtility, "Unable to write to install file: %s", inFileNames[readFiles].c_str());165pspFileSystem.CloseFile(currentInputFile);166currentInputFile = 0;167currentOutputFile = 0;168++readFiles;169return;170}171172currentInputBytesLeft = (u32)pspFileSystem.GetFileInfo(inputFileName).size;173}174175void PSPGamedataInstallDialog::CopyCurrentFileData() {176u8 buffer[GAMEDATA_BYTES_PER_READ];177for (u32 i = 0; i < GAMEDATA_READS_PER_UPDATE; ++i) {178if (currentInputBytesLeft <= 0) {179break;180}181182const u32 bytesToRead = std::min(GAMEDATA_BYTES_PER_READ, currentInputBytesLeft);183size_t readSize = pspFileSystem.ReadFile(currentInputFile, buffer, bytesToRead);184if (readSize > 0) {185pspFileSystem.WriteFile(currentOutputFile, buffer, readSize);186currentInputBytesLeft -= (u32)readSize;187allReadSize += readSize;188} else {189break;190}191}192193if (currentInputBytesLeft <= 0) {194CloseCurrentFile();195}196}197198void PSPGamedataInstallDialog::CloseCurrentFile() {199if (currentOutputFile >= 0)200pspFileSystem.CloseFile(currentOutputFile);201currentOutputFile = 0;202203if (currentInputFile >= 0)204pspFileSystem.CloseFile(currentInputFile);205currentInputFile = 0;206207++readFiles;208}209210void PSPGamedataInstallDialog::WriteSfoFile() {211ParamSFOData sfoFile;212std::string sfopath = GetGameDataInstallFileName(&request, SFO_FILENAME);213std::vector<u8> sfoFileData;214if (pspFileSystem.ReadEntireFile(sfopath, sfoFileData) >= 0) {215sfoFile.ReadSFO(sfoFileData);216}217218// Update based on the just-saved data.219sfoFile.SetValue("TITLE", param->sfoParam.title, 128);220sfoFile.SetValue("SAVEDATA_TITLE", param->sfoParam.savedataTitle, 128);221sfoFile.SetValue("SAVEDATA_DETAIL", param->sfoParam.detail, 1024);222sfoFile.SetValue("PARENTAL_LEVEL", param->sfoParam.parentalLevel, 4);223// TODO: Verify category.224sfoFile.SetValue("CATEGORY", "MS", 4);225sfoFile.SetValue("SAVEDATA_DIRECTORY", std::string(param->gameName) + param->dataName, 64);226227// TODO: Maybe there should be other things in the SFO file? Needs testing.228229u8 *sfoData;230size_t sfoSize;231sfoFile.WriteSFO(&sfoData,&sfoSize);232233int handle = pspFileSystem.OpenFile(sfopath, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));234if (handle >= 0) {235pspFileSystem.WriteFile(handle, sfoData, sfoSize);236pspFileSystem.CloseFile(handle);237}238239delete[] sfoData;240}241242int PSPGamedataInstallDialog::Abort() {243param->common.result = 1;244param.NotifyWrite("DialogResult");245246// TODO: Delete the files or anything?247return PSPDialog::Shutdown();248}249250int PSPGamedataInstallDialog::Shutdown(bool force) {251if (GetStatus() != SCE_UTILITY_STATUS_FINISHED && !force)252return SCE_ERROR_UTILITY_INVALID_STATUS;253254return PSPDialog::Shutdown(force);255}256257std::string PSPGamedataInstallDialog::GetGameDataInstallFileName(const SceUtilityGamedataInstallParam *param, const std::string &filename) {258if (!param)259return "";260std::string GameDataInstallPath = saveBasePath + param->gameName + param->dataName + "/";261if (!pspFileSystem.GetFileInfo(GameDataInstallPath).exists)262pspFileSystem.MkDir(GameDataInstallPath);263264return GameDataInstallPath + filename;265}266267void PSPGamedataInstallDialog::UpdateProgress() {268// Update progress bar(if there is).269// We only should update progress[0] here as the max progress value is 100.270if (allFilesSize != 0)271progressValue = (int)((allReadSize * 100) / allFilesSize);272else273progressValue = 100;274275if (param->mode == PSP_UTILITY_GAMEDATA_MODE_SHOW_PROGRESS) {276RenderProgress(progressValue);277}278279param->progress = progressValue;280param.NotifyWrite("DialogResult");281}282283void PSPGamedataInstallDialog::RenderProgress(int percentage) {284StartDraw();285286float barWidth = 380;287float barX = (480 - barWidth) / 2;288float barWidthDone = barWidth * percentage / 100;289float barH = 10.0;290float barY = 272 / 2 - barH / 2;291292PPGeDrawRect(barX - 3, barY - 3, barX + barWidth + 3, barY + barH + 3, 0x30000000);293PPGeDrawRect(barX, barY, barX + barWidth, barY + barH, 0xFF707070);294PPGeDrawRect(barX, barY, barX + barWidthDone, barY + barH, 0xFFE0E0E0);295296auto di = GetI18NCategory(I18NCat::DIALOG);297298fadeValue = 255;299PPGeStyle textStyle = FadedStyle(PPGeAlign::BOX_HCENTER, 0.6f);300301PPGeDrawText(di->T("Installing..."), 480 / 2, barY + barH + 10, textStyle);302303EndDraw();304}305306void PSPGamedataInstallDialog::DoState(PointerWrap &p) {307auto s = p.Section("PSPGamedataInstallDialog", 0, 4);308if (!s)309return;310311// This was included in version 1 and higher.312PSPDialog::DoState(p);313Do(p, request);314315// This was included in version 2 and higher, but for BC reasons we use 3+.316if (s >= 3) {317Do(p, param.ptr);318Do(p, inFileNames);319Do(p, numFiles);320Do(p, readFiles);321Do(p, allFilesSize);322Do(p, allReadSize);323Do(p, progressValue);324} else {325param.ptr = 0;326}327328if (s >= 4) {329Do(p, currentInputFile);330Do(p, currentInputBytesLeft);331Do(p, currentOutputFile);332} else {333currentInputFile = 0;334currentInputBytesLeft = 0;335currentOutputFile = 0;336}337}338339340