CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
hrydgard

CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!

GitHub Repository: hrydgard/ppsspp
Path: blob/master/Core/Dialog/PSPGamedataInstallDialog.cpp
Views: 1401
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 "Common/System/OSD.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
const u32 ERROR_UTILITY_GAMEDATA_MEMSTRICK_REMOVED = 0x80111901;
45
const u32 ERROR_UTILITY_GAMEDATA_MEMSTRICK_WRITE_PROTECTED = 0x80111903;
46
const u32 ERROR_UTILITY_GAMEDATA_INVALID_MODE = 0x80111908;
47
48
static const std::string SFO_FILENAME = "PARAM.SFO";
49
50
namespace
51
{
52
std::vector<std::string> GetPSPFileList (const std::string &dirpath) {
53
std::vector<std::string> FileList;
54
auto Fileinfos = pspFileSystem.GetDirListing(dirpath);
55
FileList.reserve(Fileinfos.size());
56
57
for (auto it = Fileinfos.begin(); it != Fileinfos.end(); ++it) {
58
std::string info = (*it).name;
59
FileList.push_back(info);
60
}
61
return FileList;
62
}
63
}
64
65
PSPGamedataInstallDialog::PSPGamedataInstallDialog(UtilityDialogType type) : PSPDialog(type) {
66
}
67
68
PSPGamedataInstallDialog::~PSPGamedataInstallDialog() {
69
}
70
71
int PSPGamedataInstallDialog::Init(u32 paramAddr) {
72
if (GetStatus() != SCE_UTILITY_STATUS_NONE) {
73
ERROR_LOG_REPORT(Log::sceUtility, "A game install request is already running, not starting a new one");
74
return SCE_ERROR_UTILITY_INVALID_STATUS;
75
}
76
77
param.ptr = paramAddr;
78
inFileNames = GetPSPFileList("disc0:/PSP_GAME/INSDIR");
79
numFiles = (int)inFileNames.size();
80
readFiles = 0;
81
progressValue = 0;
82
allFilesSize = 0;
83
allReadSize = 0;
84
currentInputFile = 0;
85
currentOutputFile = 0;
86
87
for (std::string filename : inFileNames) {
88
allFilesSize += pspFileSystem.GetFileInfo("disc0:/PSP_GAME/INSDIR/" + filename).size;
89
}
90
91
if (allFilesSize == 0) {
92
ERROR_LOG_REPORT(Log::sceUtility, "Game install with no files / data");
93
// TODO: What happens here?
94
return -1;
95
}
96
97
int size = Memory::Read_U32(paramAddr);
98
if (size != 1424 && size != 1432) {
99
ERROR_LOG_REPORT(Log::sceUtility, "sceGamedataInstallInitStart: invalid param size %d", size);
100
return SCE_ERROR_UTILITY_INVALID_PARAM_SIZE;
101
}
102
103
memset(&request, 0, sizeof(request));
104
// Only copy the right size to support different request format
105
Memory::Memcpy(&request, paramAddr, size, "sceGamedataInstallInitStart");
106
InitCommon();
107
108
ChangeStatusInit(GAMEDATA_INIT_DELAY_US);
109
return 0;
110
}
111
112
int PSPGamedataInstallDialog::Update(int animSpeed) {
113
if (GetStatus() != SCE_UTILITY_STATUS_RUNNING)
114
return SCE_ERROR_UTILITY_INVALID_STATUS;
115
116
if (param->mode >= 2) {
117
param->common.result = ERROR_UTILITY_GAMEDATA_INVALID_MODE;
118
param.NotifyWrite("DialogResult");
119
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
120
WARN_LOG_REPORT(Log::sceUtility, "sceUtilityGamedataInstallUpdate: invalid mode %d", param->mode);
121
return 0;
122
}
123
124
UpdateCommon();
125
126
// TODO: param->mode == 1 should show a prompt to confirm, then a progress bar.
127
// Any other mode (i.e. 0 or negative) should proceed and show no UI.
128
129
// TODO: This should return error codes in some cases, like write failure.
130
// request.common.result must be updated for errors as well.
131
132
if (readFiles < numFiles) {
133
if (currentInputFile != 0 && currentOutputFile != 0) {
134
// Continue copying, this will close once done automatically.
135
CopyCurrentFileData();
136
} else {
137
OpenNextFile();
138
}
139
140
UpdateProgress();
141
} else {
142
WriteSfoFile();
143
144
// TODO: What is this? Should one of these update per file or anything?
145
param->unknownResult1 = readFiles;
146
param->unknownResult2 = readFiles;
147
param.NotifyWrite("DialogResult");
148
149
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
150
}
151
return 0;
152
}
153
154
void PSPGamedataInstallDialog::OpenNextFile() {
155
std::string inputFileName = "disc0:/PSP_GAME/INSDIR/" + inFileNames[readFiles];
156
std::string outputFileName = GetGameDataInstallFileName(&request, inFileNames[readFiles]);
157
158
currentInputFile = pspFileSystem.OpenFile(inputFileName, FILEACCESS_READ);
159
if (currentInputFile < 0) {
160
// TODO: Generate an error code?
161
ERROR_LOG_REPORT(Log::sceUtility, "Unable to read from install file: %s", inFileNames[readFiles].c_str());
162
++readFiles;
163
currentInputFile = 0;
164
return;
165
}
166
currentOutputFile = pspFileSystem.OpenFile(outputFileName, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));
167
if (currentOutputFile < 0) {
168
// TODO: Generate an error code?
169
ERROR_LOG(Log::sceUtility, "Unable to write to install file: %s", inFileNames[readFiles].c_str());
170
pspFileSystem.CloseFile(currentInputFile);
171
currentInputFile = 0;
172
currentOutputFile = 0;
173
++readFiles;
174
return;
175
}
176
177
currentInputBytesLeft = (u32)pspFileSystem.GetFileInfo(inputFileName).size;
178
}
179
180
void PSPGamedataInstallDialog::CopyCurrentFileData() {
181
u8 buffer[GAMEDATA_BYTES_PER_READ];
182
for (u32 i = 0; i < GAMEDATA_READS_PER_UPDATE; ++i) {
183
if (currentInputBytesLeft <= 0) {
184
break;
185
}
186
187
const u32 bytesToRead = std::min(GAMEDATA_BYTES_PER_READ, currentInputBytesLeft);
188
size_t readSize = pspFileSystem.ReadFile(currentInputFile, buffer, bytesToRead);
189
if (readSize > 0) {
190
pspFileSystem.WriteFile(currentOutputFile, buffer, readSize);
191
currentInputBytesLeft -= (u32)readSize;
192
allReadSize += readSize;
193
} else {
194
break;
195
}
196
}
197
198
if (currentInputBytesLeft <= 0) {
199
CloseCurrentFile();
200
}
201
}
202
203
void PSPGamedataInstallDialog::CloseCurrentFile() {
204
if (currentOutputFile >= 0)
205
pspFileSystem.CloseFile(currentOutputFile);
206
currentOutputFile = 0;
207
208
if (currentInputFile >= 0)
209
pspFileSystem.CloseFile(currentInputFile);
210
currentInputFile = 0;
211
212
++readFiles;
213
}
214
215
void PSPGamedataInstallDialog::WriteSfoFile() {
216
ParamSFOData sfoFile;
217
std::string sfopath = GetGameDataInstallFileName(&request, SFO_FILENAME);
218
std::vector<u8> sfoFileData;
219
if (pspFileSystem.ReadEntireFile(sfopath, sfoFileData) >= 0) {
220
sfoFile.ReadSFO(sfoFileData);
221
}
222
223
// Update based on the just-saved data.
224
sfoFile.SetValue("TITLE", param->sfoParam.title, 128);
225
sfoFile.SetValue("SAVEDATA_TITLE", param->sfoParam.savedataTitle, 128);
226
sfoFile.SetValue("SAVEDATA_DETAIL", param->sfoParam.detail, 1024);
227
sfoFile.SetValue("PARENTAL_LEVEL", param->sfoParam.parentalLevel, 4);
228
// TODO: Verify category.
229
sfoFile.SetValue("CATEGORY", "MS", 4);
230
sfoFile.SetValue("SAVEDATA_DIRECTORY", std::string(param->gameName) + param->dataName, 64);
231
232
// TODO: Maybe there should be other things in the SFO file? Needs testing.
233
234
u8 *sfoData;
235
size_t sfoSize;
236
sfoFile.WriteSFO(&sfoData,&sfoSize);
237
238
int handle = pspFileSystem.OpenFile(sfopath, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));
239
if (handle >= 0) {
240
pspFileSystem.WriteFile(handle, sfoData, sfoSize);
241
pspFileSystem.CloseFile(handle);
242
}
243
244
delete[] sfoData;
245
}
246
247
int PSPGamedataInstallDialog::Abort() {
248
param->common.result = 1;
249
param.NotifyWrite("DialogResult");
250
251
// TODO: Delete the files or anything?
252
return PSPDialog::Shutdown();
253
}
254
255
int PSPGamedataInstallDialog::Shutdown(bool force) {
256
if (GetStatus() != SCE_UTILITY_STATUS_FINISHED && !force)
257
return SCE_ERROR_UTILITY_INVALID_STATUS;
258
259
return PSPDialog::Shutdown(force);
260
}
261
262
std::string PSPGamedataInstallDialog::GetGameDataInstallFileName(const SceUtilityGamedataInstallParam *param, const std::string &filename) {
263
if (!param)
264
return "";
265
std::string GameDataInstallPath = saveBasePath + param->gameName + param->dataName + "/";
266
if (!pspFileSystem.GetFileInfo(GameDataInstallPath).exists)
267
pspFileSystem.MkDir(GameDataInstallPath);
268
269
return GameDataInstallPath + filename;
270
}
271
272
void PSPGamedataInstallDialog::UpdateProgress() {
273
// Update progress bar(if there is).
274
// We only should update progress[0] here as the max progress value is 100.
275
if (allFilesSize != 0)
276
progressValue = (int)((allReadSize * 100) / allFilesSize);
277
else
278
progressValue = 100;
279
280
if (param->mode == PSP_UTILITY_GAMEDATA_MODE_SHOW_PROGRESS) {
281
RenderProgress(progressValue);
282
}
283
284
param->progress = progressValue;
285
param.NotifyWrite("DialogResult");
286
}
287
288
void PSPGamedataInstallDialog::RenderProgress(int percentage) {
289
StartDraw();
290
291
float barWidth = 380;
292
float barX = (480 - barWidth) / 2;
293
float barWidthDone = barWidth * percentage / 100;
294
float barH = 10.0;
295
float barY = 272 / 2 - barH / 2;
296
297
PPGeDrawRect(barX - 3, barY - 3, barX + barWidth + 3, barY + barH + 3, 0x30000000);
298
PPGeDrawRect(barX, barY, barX + barWidth, barY + barH, 0xFF707070);
299
PPGeDrawRect(barX, barY, barX + barWidthDone, barY + barH, 0xFFE0E0E0);
300
301
auto di = GetI18NCategory(I18NCat::DIALOG);
302
303
fadeValue = 255;
304
PPGeStyle textStyle = FadedStyle(PPGeAlign::BOX_HCENTER, 0.6f);
305
306
PPGeDrawText(di->T("Installing..."), 480 / 2, barY + barH + 10, textStyle);
307
308
EndDraw();
309
}
310
311
void PSPGamedataInstallDialog::DoState(PointerWrap &p) {
312
auto s = p.Section("PSPGamedataInstallDialog", 0, 4);
313
if (!s)
314
return;
315
316
// This was included in version 1 and higher.
317
PSPDialog::DoState(p);
318
Do(p, request);
319
320
// This was included in version 2 and higher, but for BC reasons we use 3+.
321
if (s >= 3) {
322
Do(p, param.ptr);
323
Do(p, inFileNames);
324
Do(p, numFiles);
325
Do(p, readFiles);
326
Do(p, allFilesSize);
327
Do(p, allReadSize);
328
Do(p, progressValue);
329
} else {
330
param.ptr = 0;
331
}
332
333
if (s >= 4) {
334
Do(p, currentInputFile);
335
Do(p, currentInputBytesLeft);
336
Do(p, currentOutputFile);
337
} else {
338
currentInputFile = 0;
339
currentInputBytesLeft = 0;
340
currentOutputFile = 0;
341
}
342
}
343
344