forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSPGamedataInstallDialog.cpp
288 lines (238 loc) · 8.63 KB
/
PSPGamedataInstallDialog.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
// Copyright (c) 2012- 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/.
#include <algorithm>
#include "Common/Common.h"
#include "Common/Serialize/Serializer.h"
#include "Common/Serialize/SerializeFuncs.h"
#include "Core/ELF/ParamSFO.h"
#include "Core/MemMapHelpers.h"
#include "Core/Reporting.h"
#include "Core/System.h"
#include "Core/FileSystems/MetaFileSystem.h"
#include "Core/Dialog/PSPGamedataInstallDialog.h"
std::string saveBasePath = "ms0:/PSP/SAVEDATA/";
// Guesses.
const static int GAMEDATA_INIT_DELAY_US = 200000;
const static int GAMEDATA_SHUTDOWN_DELAY_US = 2000;
const static u32 GAMEDATA_BYTES_PER_READ = 32768;
// TODO: Could adjust based on real-time into frame? Or eat cycles?
// If this is too high, some games (e.g. Senjou no Valkyria 3) will lag.
const static u32 GAMEDATA_READS_PER_UPDATE = 20;
const u32 ERROR_UTILITY_GAMEDATA_MEMSTRICK_WRITE_PROTECTED = 0x80111903;
const u32 ERROR_UTILITY_GAMEDATA_MEMSTRICK_REMOVED = 0x80111901;
static const std::string SFO_FILENAME = "PARAM.SFO";
namespace
{
std::vector<std::string> GetPSPFileList (std::string dirpath) {
std::vector<std::string> FileList;
auto Fileinfos = pspFileSystem.GetDirListing(dirpath);
for (auto it = Fileinfos.begin(); it != Fileinfos.end(); ++it) {
std::string info = (*it).name;
FileList.push_back(info);
}
return FileList;
}
}
PSPGamedataInstallDialog::PSPGamedataInstallDialog() {
}
PSPGamedataInstallDialog::~PSPGamedataInstallDialog() {
}
int PSPGamedataInstallDialog::Init(u32 paramAddr) {
if (GetStatus() != SCE_UTILITY_STATUS_NONE) {
ERROR_LOG_REPORT(SCEUTILITY, "A game install request is already running, not starting a new one");
return SCE_ERROR_UTILITY_INVALID_STATUS;
}
param.ptr = paramAddr;
inFileNames = GetPSPFileList("disc0:/PSP_GAME/INSDIR");
numFiles = (int)inFileNames.size();
readFiles = 0;
progressValue = 0;
allFilesSize = 0;
allReadSize = 0;
currentInputFile = 0;
currentOutputFile = 0;
for (std::string filename : inFileNames) {
allFilesSize += pspFileSystem.GetFileInfo("disc0:/PSP_GAME/INSDIR/" + filename).size;
}
if (allFilesSize == 0) {
ERROR_LOG_REPORT(SCEUTILITY, "Game install with no files / data");
// TODO: What happens here?
return -1;
}
int size = Memory::Read_U32(paramAddr);
memset(&request, 0, sizeof(request));
// Only copy the right size to support different request format
Memory::Memcpy(&request, paramAddr, size);
ChangeStatusInit(GAMEDATA_INIT_DELAY_US);
return 0;
}
int PSPGamedataInstallDialog::Update(int animSpeed) {
if (GetStatus() != SCE_UTILITY_STATUS_RUNNING)
return SCE_ERROR_UTILITY_INVALID_STATUS;
// TODO: This should return error codes in some cases, like write failure.
// request.common.result must be updated for errors as well.
if (readFiles < numFiles) {
if (currentInputFile != 0 && currentOutputFile != 0) {
// Continue copying, this will close once done automatically.
CopyCurrentFileData();
} else {
OpenNextFile();
}
UpdateProgress();
} else {
WriteSfoFile();
// TODO: What is this? Should one of these update per file or anything?
request.unknownResult1 = readFiles;
request.unknownResult2 = readFiles;
Memory::WriteStruct(param.ptr, &request);
ChangeStatus(SCE_UTILITY_STATUS_FINISHED, 0);
}
return 0;
}
void PSPGamedataInstallDialog::OpenNextFile() {
std::string inputFileName = "disc0:/PSP_GAME/INSDIR/" + inFileNames[readFiles];
std::string outputFileName = GetGameDataInstallFileName(&request, inFileNames[readFiles]);
currentInputFile = pspFileSystem.OpenFile(inputFileName, FILEACCESS_READ);
if (currentInputFile < 0) {
// TODO: Generate an error code?
ERROR_LOG_REPORT(SCEUTILITY, "Unable to read from install file: %s", inFileNames[readFiles].c_str());
++readFiles;
currentInputFile = 0;
return;
}
currentOutputFile = pspFileSystem.OpenFile(outputFileName, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));
if (currentOutputFile < 0) {
// TODO: Generate an error code?
ERROR_LOG(SCEUTILITY, "Unable to write to install file: %s", inFileNames[readFiles].c_str());
pspFileSystem.CloseFile(currentInputFile);
currentInputFile = 0;
currentOutputFile = 0;
++readFiles;
return;
}
currentInputBytesLeft = (u32)pspFileSystem.GetFileInfo(inputFileName).size;
}
void PSPGamedataInstallDialog::CopyCurrentFileData() {
u8 buffer[GAMEDATA_BYTES_PER_READ];
for (u32 i = 0; i < GAMEDATA_READS_PER_UPDATE; ++i) {
if (currentInputBytesLeft <= 0) {
break;
}
const u32 bytesToRead = std::min(GAMEDATA_BYTES_PER_READ, currentInputBytesLeft);
size_t readSize = pspFileSystem.ReadFile(currentInputFile, buffer, bytesToRead);
if (readSize > 0) {
pspFileSystem.WriteFile(currentOutputFile, buffer, readSize);
currentInputBytesLeft -= (u32)readSize;
allReadSize += readSize;
} else {
break;
}
}
if (currentInputBytesLeft <= 0) {
CloseCurrentFile();
}
}
void PSPGamedataInstallDialog::CloseCurrentFile() {
if (currentOutputFile >= 0)
pspFileSystem.CloseFile(currentOutputFile);
currentOutputFile = 0;
if (currentInputFile >= 0)
pspFileSystem.CloseFile(currentInputFile);
currentInputFile = 0;
++readFiles;
}
void PSPGamedataInstallDialog::WriteSfoFile() {
ParamSFOData sfoFile;
std::string sfopath = GetGameDataInstallFileName(&request, SFO_FILENAME);
PSPFileInfo sfoInfo = pspFileSystem.GetFileInfo(sfopath);
if (sfoInfo.exists) {
std::vector<u8> sfoData;
if (pspFileSystem.ReadEntireFile(sfopath, sfoData) >= 0) {
sfoFile.ReadSFO(sfoData);
}
}
// Update based on the just-saved data.
sfoFile.SetValue("TITLE", param->sfoParam.title, 128);
sfoFile.SetValue("SAVEDATA_TITLE", param->sfoParam.savedataTitle, 128);
sfoFile.SetValue("SAVEDATA_DETAIL", param->sfoParam.detail, 1024);
sfoFile.SetValue("PARENTAL_LEVEL", param->sfoParam.parentalLevel, 4);
// TODO: Verify category.
sfoFile.SetValue("CATEGORY", "MS", 4);
sfoFile.SetValue("SAVEDATA_DIRECTORY", std::string(param->gameName) + param->dataName, 64);
// TODO: Maybe there should be other things in the SFO file? Needs testing.
u8 *sfoData;
size_t sfoSize;
sfoFile.WriteSFO(&sfoData,&sfoSize);
int handle = pspFileSystem.OpenFile(sfopath, (FileAccess)(FILEACCESS_WRITE | FILEACCESS_CREATE | FILEACCESS_TRUNCATE));
if (handle >= 0) {
pspFileSystem.WriteFile(handle, sfoData, sfoSize);
pspFileSystem.CloseFile(handle);
}
delete[] sfoData;
}
int PSPGamedataInstallDialog::Abort() {
// TODO: Delete the files or anything?
return PSPDialog::Shutdown();
}
int PSPGamedataInstallDialog::Shutdown(bool force) {
if (GetStatus() != SCE_UTILITY_STATUS_FINISHED && !force)
return SCE_ERROR_UTILITY_INVALID_STATUS;
return PSPDialog::Shutdown(force);
}
std::string PSPGamedataInstallDialog::GetGameDataInstallFileName(SceUtilityGamedataInstallParam *param, std::string filename){
if (!param)
return "";
std::string GameDataInstallPath = saveBasePath + param->gameName + param->dataName + "/";
if (!pspFileSystem.GetFileInfo(GameDataInstallPath).exists)
pspFileSystem.MkDir(GameDataInstallPath);
return GameDataInstallPath + filename;
}
void PSPGamedataInstallDialog::UpdateProgress() {
// Update progress bar(if there is).
// We only should update progress[0] here as the max progress value is 100.
if (allFilesSize != 0)
progressValue = (int)((allReadSize * 100) / allFilesSize);
else
progressValue = 100;
request.progress = progressValue;
Memory::WriteStruct(param.ptr, &request);
}
void PSPGamedataInstallDialog::DoState(PointerWrap &p) {
auto s = p.Section("PSPGamedataInstallDialog", 0, 4);
if (!s)
return;
// This was included in version 1 and higher.
PSPDialog::DoState(p);
Do(p, request);
// This was included in version 2 and higher, but for BC reasons we use 3+.
if (s >= 3) {
Do(p, param.ptr);
Do(p, inFileNames);
Do(p, numFiles);
Do(p, readFiles);
Do(p, allFilesSize);
Do(p, allReadSize);
Do(p, progressValue);
} else {
param.ptr = 0;
}
if (s >= 4) {
Do(p, currentInputFile);
Do(p, currentInputBytesLeft);
Do(p, currentOutputFile);
} else {
currentInputFile = 0;
currentInputBytesLeft = 0;
currentOutputFile = 0;
}
}