Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Dialog/PSPGamedataInstallDialog.cpp
3186 views
1
// Copyright (c) 2012- PPSSPP Project.
2
3
// This program is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU General Public License as published by
5
// the Free Software Foundation, version 2.0 or later versions.
6
7
// This program is distributed in the hope that it will be useful,
8
// but WITHOUT ANY WARRANTY; without even the implied warranty of
9
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
// GNU General Public License 2.0 for more details.
11
12
// A copy of the GPL 2.0 should have been included with the program.
13
// If not, see http://www.gnu.org/licenses/
14
15
// Official git repository and contact information can be found at
16
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
17
18
#include <algorithm>
19
#include "Common/CommonTypes.h"
20
#include "Common/Serialize/Serializer.h"
21
#include "Common/Serialize/SerializeFuncs.h"
22
#include "Core/HLE/ErrorCodes.h"
23
#include "Core/ELF/ParamSFO.h"
24
#include "Core/MemMapHelpers.h"
25
#include "Core/Reporting.h"
26
#include "Core/System.h"
27
#include "Core/FileSystems/MetaFileSystem.h"
28
#include "Core/Dialog/PSPGamedataInstallDialog.h"
29
#include "Common/Data/Text/I18n.h"
30
#include "UI/OnScreenDisplay.h"
31
32
std::string saveBasePath = "ms0:/PSP/SAVEDATA/";
33
34
// Guesses.
35
const static int GAMEDATA_INIT_DELAY_US = 200000;
36
const static int GAMEDATA_SHUTDOWN_DELAY_US = 2000;
37
const static u32 GAMEDATA_BYTES_PER_READ = 32768;
38
// TODO: Could adjust based on real-time into frame? Or eat cycles?
39
// If this is too high, some games (e.g. Senjou no Valkyria 3) will lag.
40
const static u32 GAMEDATA_READS_PER_UPDATE = 20;
41
42
const u32 PSP_UTILITY_GAMEDATA_MODE_SHOW_PROGRESS = 1;
43
44
static const std::string SFO_FILENAME = "PARAM.SFO";
45
46
namespace
47
{
48
std::vector<std::string> GetPSPFileList (const std::string &dirpath) {
49
std::vector<std::string> FileList;
50
auto Fileinfos = pspFileSystem.GetDirListing(dirpath);
51
FileList.reserve(Fileinfos.size());
52
53
for (auto it = Fileinfos.begin(); it != Fileinfos.end(); ++it) {
54
std::string info = (*it).name;
55
FileList.push_back(info);
56
}
57
return FileList;
58
}
59
}
60
61
PSPGamedataInstallDialog::PSPGamedataInstallDialog(UtilityDialogType type) : PSPDialog(type) {
62
}
63
64
PSPGamedataInstallDialog::~PSPGamedataInstallDialog() {
65
}
66
67
int PSPGamedataInstallDialog::Init(u32 paramAddr) {
68
if (GetStatus() != SCE_UTILITY_STATUS_NONE) {
69
ERROR_LOG_REPORT(Log::sceUtility, "A game install request is already running, not starting a new one");
70
return SCE_ERROR_UTILITY_INVALID_STATUS;
71
}
72
73
param.ptr = paramAddr;
74
inFileNames = GetPSPFileList("disc0:/PSP_GAME/INSDIR");
75
numFiles = (int)inFileNames.size();
76
readFiles = 0;
77
progressValue = 0;
78
allFilesSize = 0;
79
allReadSize = 0;
80
currentInputFile = 0;
81
currentOutputFile = 0;
82
83
for (std::string filename : inFileNames) {
84
allFilesSize += pspFileSystem.GetFileInfo("disc0:/PSP_GAME/INSDIR/" + filename).size;
85
}
86
87
if (allFilesSize == 0) {
88
ERROR_LOG_REPORT(Log::sceUtility, "Game install with no files / data");
89
// Getting a lot of reports of this from patched football games. Can probably ignore. https://report.ppsspp.org/logs/kind/793
90
return -1;
91
}
92
93
int size = Memory::Read_U32(paramAddr);
94
if (size != 1424 && size != 1432) {
95
ERROR_LOG_REPORT(Log::sceUtility, "sceGamedataInstallInitStart: invalid param size %d", size);
96
return SCE_ERROR_UTILITY_INVALID_PARAM_SIZE;
97
}
98
99
memset(&request, 0, sizeof(request));
100
// Only copy the right size to support different request format
101
Memory::Memcpy(&request, paramAddr, size, "sceGamedataInstallInitStart");
102
InitCommon();
103
104
ChangeStatusInit(GAMEDATA_INIT_DELAY_US);
105
return 0;
106
}
107
108
int PSPGamedataInstallDialog::Update(int animSpeed) {
109
if (GetStatus() != SCE_UTILITY_STATUS_RUNNING)
110
return SCE_ERROR_UTILITY_INVALID_STATUS;
111
112
if (param->mode >= 2) {
113
param->common.result = SCE_ERROR_UTILITY_GAMEDATA_INVALID_MODE;
114
param.NotifyWrite("DialogResult");
115
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
116
WARN_LOG_REPORT(Log::sceUtility, "sceUtilityGamedataInstallUpdate: invalid mode %d", param->mode);
117
return 0;
118
}
119
120
UpdateCommon();
121
122
// TODO: param->mode == 1 should show a prompt to confirm, then a progress bar.
123
// Any other mode (i.e. 0 or negative) should proceed and show no UI.
124
125
// TODO: This should return error codes in some cases, like write failure.
126
// request.common.result must be updated for errors as well.
127
128
if (readFiles < numFiles) {
129
if (currentInputFile != 0 && currentOutputFile != 0) {
130
// Continue copying, this will close once done automatically.
131
CopyCurrentFileData();
132
} else {
133
OpenNextFile();
134
}
135
136
UpdateProgress();
137
} else {
138
WriteSfoFile();
139
140
// TODO: What is this? Should one of these update per file or anything?
141
param->unknownResult1 = readFiles;
142
param->unknownResult2 = readFiles;
143
param.NotifyWrite("DialogResult");
144
145
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
146
}
147
return 0;
148
}
149
150
void PSPGamedataInstallDialog::OpenNextFile() {
151
std::string inputFileName = "disc0:/PSP_GAME/INSDIR/" + inFileNames[readFiles];
152
std::string outputFileName = GetGameDataInstallFileName(&request, inFileNames[readFiles]);
153
154
currentInputFile = pspFileSystem.OpenFile(inputFileName, FILEACCESS_READ);
155
if (currentInputFile < 0) {
156
// TODO: Generate an error code?
157
ERROR_LOG_REPORT(Log::sceUtility, "Unable to read from install file: %s", inFileNames[readFiles].c_str());
158
++readFiles;
159
currentInputFile = 0;
160
return;
161
}
162
currentOutputFile = pspFileSystem.OpenFile(outputFileName, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));
163
if (currentOutputFile < 0) {
164
// TODO: Generate an error code?
165
ERROR_LOG(Log::sceUtility, "Unable to write to install file: %s", inFileNames[readFiles].c_str());
166
pspFileSystem.CloseFile(currentInputFile);
167
currentInputFile = 0;
168
currentOutputFile = 0;
169
++readFiles;
170
return;
171
}
172
173
currentInputBytesLeft = (u32)pspFileSystem.GetFileInfo(inputFileName).size;
174
}
175
176
void PSPGamedataInstallDialog::CopyCurrentFileData() {
177
u8 buffer[GAMEDATA_BYTES_PER_READ];
178
for (u32 i = 0; i < GAMEDATA_READS_PER_UPDATE; ++i) {
179
if (currentInputBytesLeft <= 0) {
180
break;
181
}
182
183
const u32 bytesToRead = std::min(GAMEDATA_BYTES_PER_READ, currentInputBytesLeft);
184
size_t readSize = pspFileSystem.ReadFile(currentInputFile, buffer, bytesToRead);
185
if (readSize > 0) {
186
pspFileSystem.WriteFile(currentOutputFile, buffer, readSize);
187
currentInputBytesLeft -= (u32)readSize;
188
allReadSize += readSize;
189
} else {
190
break;
191
}
192
}
193
194
if (currentInputBytesLeft <= 0) {
195
CloseCurrentFile();
196
}
197
}
198
199
void PSPGamedataInstallDialog::CloseCurrentFile() {
200
if (currentOutputFile >= 0)
201
pspFileSystem.CloseFile(currentOutputFile);
202
currentOutputFile = 0;
203
204
if (currentInputFile >= 0)
205
pspFileSystem.CloseFile(currentInputFile);
206
currentInputFile = 0;
207
208
++readFiles;
209
}
210
211
void PSPGamedataInstallDialog::WriteSfoFile() {
212
ParamSFOData sfoFile;
213
std::string sfopath = GetGameDataInstallFileName(&request, SFO_FILENAME);
214
std::vector<u8> sfoFileData;
215
if (pspFileSystem.ReadEntireFile(sfopath, sfoFileData) >= 0) {
216
sfoFile.ReadSFO(sfoFileData);
217
}
218
219
// Update based on the just-saved data.
220
sfoFile.SetValue("TITLE", param->sfoParam.title, 128);
221
sfoFile.SetValue("SAVEDATA_TITLE", param->sfoParam.savedataTitle, 128);
222
sfoFile.SetValue("SAVEDATA_DETAIL", param->sfoParam.detail, 1024);
223
sfoFile.SetValue("PARENTAL_LEVEL", param->sfoParam.parentalLevel, 4);
224
// TODO: Verify category.
225
sfoFile.SetValue("CATEGORY", "MS", 4);
226
sfoFile.SetValue("SAVEDATA_DIRECTORY", std::string(param->gameName) + param->dataName, 64);
227
228
// TODO: Maybe there should be other things in the SFO file? Needs testing.
229
230
u8 *sfoData;
231
size_t sfoSize;
232
sfoFile.WriteSFO(&sfoData,&sfoSize);
233
234
int handle = pspFileSystem.OpenFile(sfopath, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));
235
if (handle >= 0) {
236
pspFileSystem.WriteFile(handle, sfoData, sfoSize);
237
pspFileSystem.CloseFile(handle);
238
}
239
240
delete[] sfoData;
241
}
242
243
int PSPGamedataInstallDialog::Abort() {
244
param->common.result = 1;
245
param.NotifyWrite("DialogResult");
246
247
// TODO: Delete the files or anything?
248
return PSPDialog::Shutdown();
249
}
250
251
int PSPGamedataInstallDialog::Shutdown(bool force) {
252
if (GetStatus() != SCE_UTILITY_STATUS_FINISHED && !force)
253
return SCE_ERROR_UTILITY_INVALID_STATUS;
254
255
return PSPDialog::Shutdown(force);
256
}
257
258
std::string PSPGamedataInstallDialog::GetGameDataInstallFileName(const SceUtilityGamedataInstallParam *param, const std::string &filename) {
259
if (!param)
260
return "";
261
std::string GameDataInstallPath = saveBasePath + param->gameName + param->dataName + "/";
262
if (!pspFileSystem.GetFileInfo(GameDataInstallPath).exists)
263
pspFileSystem.MkDir(GameDataInstallPath);
264
265
return GameDataInstallPath + filename;
266
}
267
268
void PSPGamedataInstallDialog::UpdateProgress() {
269
// Update progress bar(if there is).
270
// We only should update progress[0] here as the max progress value is 100.
271
if (allFilesSize != 0)
272
progressValue = (int)((allReadSize * 100) / allFilesSize);
273
else
274
progressValue = 100;
275
276
if (param->mode == PSP_UTILITY_GAMEDATA_MODE_SHOW_PROGRESS) {
277
RenderProgress(progressValue);
278
}
279
280
param->progress = progressValue;
281
param.NotifyWrite("DialogResult");
282
}
283
284
void PSPGamedataInstallDialog::RenderProgress(int percentage) {
285
StartDraw();
286
287
float barWidth = 380;
288
float barX = (480 - barWidth) / 2;
289
float barWidthDone = barWidth * percentage / 100;
290
float barH = 10.0;
291
float barY = 272 / 2 - barH / 2;
292
293
PPGeDrawRect(barX - 3, barY - 3, barX + barWidth + 3, barY + barH + 3, 0x30000000);
294
PPGeDrawRect(barX, barY, barX + barWidth, barY + barH, 0xFF707070);
295
PPGeDrawRect(barX, barY, barX + barWidthDone, barY + barH, 0xFFE0E0E0);
296
297
auto di = GetI18NCategory(I18NCat::DIALOG);
298
299
fadeValue = 255;
300
PPGeStyle textStyle = FadedStyle(PPGeAlign::BOX_HCENTER, 0.6f);
301
302
PPGeDrawText(di->T("Installing..."), 480 / 2, barY + barH + 10, textStyle);
303
304
EndDraw();
305
}
306
307
void PSPGamedataInstallDialog::DoState(PointerWrap &p) {
308
auto s = p.Section("PSPGamedataInstallDialog", 0, 4);
309
if (!s)
310
return;
311
312
// This was included in version 1 and higher.
313
PSPDialog::DoState(p);
314
Do(p, request);
315
316
// This was included in version 2 and higher, but for BC reasons we use 3+.
317
if (s >= 3) {
318
Do(p, param.ptr);
319
Do(p, inFileNames);
320
Do(p, numFiles);
321
Do(p, readFiles);
322
Do(p, allFilesSize);
323
Do(p, allReadSize);
324
Do(p, progressValue);
325
} else {
326
param.ptr = 0;
327
}
328
329
if (s >= 4) {
330
Do(p, currentInputFile);
331
Do(p, currentInputBytesLeft);
332
Do(p, currentOutputFile);
333
} else {
334
currentInputFile = 0;
335
currentInputBytesLeft = 0;
336
currentOutputFile = 0;
337
}
338
}
339
340