Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/InstallZipScreen.cpp
5654 views
1
// Copyright (c) 2013- 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 "Common/UI/UI.h"
19
#include "Common/UI/View.h"
20
#include "Common/UI/ViewGroup.h"
21
#include "Common/UI/Notice.h"
22
#include "Common/StringUtils.h"
23
#include "Common/File/FileUtil.h"
24
#include "Common/Data/Text/I18n.h"
25
#include "Common/Data/Text/Parsers.h"
26
27
#include "Core/Config.h"
28
#include "Core/System.h"
29
#include "Core/Util/GameManager.h"
30
#include "Core/Util/PathUtil.h"
31
#include "Core/Loaders.h"
32
33
#include "UI/InstallZipScreen.h"
34
#include "UI/MainScreen.h"
35
#include "UI/OnScreenDisplay.h"
36
#include "UI/SavedataScreen.h"
37
#include "UI/EmuScreen.h"
38
39
InstallZipScreen::InstallZipScreen(const Path &zipPath) : UITwoPaneBaseDialogScreen(Path(), TwoPaneFlags::SettingsToTheRight | TwoPaneFlags::ContentsCanScroll), zipPath_(zipPath) {
40
g_GameManager.ResetInstallError();
41
ZipContainer zipFile = ZipOpenPath(zipPath_);
42
if (zipFile) {
43
DetectZipFileContents(zipFile, &zipFileInfo_); // Even if this fails, it sets zipInfo->contents.
44
ZipClose(zipFile);
45
}
46
}
47
48
std::string_view InstallZipScreen::GetTitle() const {
49
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
50
return iz->T("ZIP file");
51
}
52
53
void InstallZipScreen::CreateSettingsViews(UI::ViewGroup *parent) {
54
using namespace UI;
55
56
auto di = GetI18NCategory(I18NCat::DIALOG);
57
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
58
auto er = GetI18NCategory(I18NCat::ERRORS);
59
auto ga = GetI18NCategory(I18NCat::GAME);
60
61
std::string shortFilename = zipPath_.GetFilename();
62
63
bool showDeleteCheckbox = false;
64
returnToHomebrew_ = false;
65
installChoice_ = nullptr;
66
playChoice_ = nullptr;
67
doneView_ = nullptr;
68
existingSaveView_ = nullptr;
69
70
std::vector<Path> destOptions;
71
72
switch (zipFileInfo_.contents) {
73
case ZipFileContents::ISO_FILE:
74
case ZipFileContents::PSP_GAME_DIR:
75
installChoice_ = parent->Add(new Choice(iz->T("Install"), ImageID("I_FOLDER_UPLOAD")));
76
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
77
78
// NOTE: We detect PBP isos (like demos) as game dirs currently. Can't play them directly.
79
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE) {
80
playChoice_ = parent->Add(new Choice(ga->T("Play"), ImageID("I_PLAY")));
81
playChoice_->OnClick.Handle(this, &InstallZipScreen::OnPlay);
82
}
83
84
returnToHomebrew_ = true;
85
showDeleteCheckbox = true;
86
break;
87
case ZipFileContents::TEXTURE_PACK:
88
case ZipFileContents::SAVE_DATA:
89
case ZipFileContents::SAVE_STATES:
90
installChoice_ = parent->Add(new Choice(iz->T("Install"), ImageID("I_FOLDER_UPLOAD")));
91
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
92
showDeleteCheckbox = true;
93
break;
94
case ZipFileContents::FRAME_DUMP:
95
// It's a frame dump, add a play button!
96
playChoice_ = parent->Add(new Choice(ga->T("Play"), ImageID("I_PLAY")));
97
playChoice_->OnClick.Handle(this, &InstallZipScreen::OnPlay);
98
break;
99
default:
100
// Nothing to do!
101
break;
102
}
103
104
if (showDeleteCheckbox) {
105
parent->Add(new Spacer(12.0f));
106
parent->Add(new CheckBox(&deleteZipFile_, iz->T("Delete ZIP file")));
107
}
108
}
109
110
void InstallZipScreen::CreateContentViews(UI::ViewGroup *parent) {
111
using namespace UI;
112
113
LinearLayout *leftColumn = parent->Add(new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT, Margins(8))));
114
115
auto di = GetI18NCategory(I18NCat::DIALOG);
116
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
117
auto er = GetI18NCategory(I18NCat::ERRORS);
118
auto ga = GetI18NCategory(I18NCat::GAME);
119
120
std::string shortFilename = zipPath_.GetFilename();
121
122
bool showDeleteCheckbox = false;
123
returnToHomebrew_ = false;
124
installChoice_ = nullptr;
125
playChoice_ = nullptr;
126
doneView_ = nullptr;
127
existingSaveView_ = nullptr;
128
destFolders_.clear();
129
130
std::vector<Path> destOptions;
131
bool overwrite = false;
132
switch (zipFileInfo_.contents) {
133
case ZipFileContents::ISO_FILE:
134
case ZipFileContents::PSP_GAME_DIR:
135
{
136
std::string_view question = iz->T("Install game from ZIP file?");
137
138
leftColumn->Add(new TextView(question));
139
leftColumn->Add(new TextView(shortFilename));
140
if (!zipFileInfo_.contentName.empty()) {
141
leftColumn->Add(new TextView(zipFileInfo_.contentName));
142
}
143
144
doneView_ = leftColumn->Add(new TextView(""));
145
146
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE) {
147
const bool isInDownloads = File::IsProbablyInDownloadsFolder(zipPath_);
148
Path parent;
149
if (!isInDownloads && zipPath_.CanNavigateUp()) {
150
parent = zipPath_.NavigateUp();
151
destFolders_.push_back(parent);
152
}
153
if (g_Config.currentDirectory.IsLocalType() && File::Exists(g_Config.currentDirectory) && g_Config.currentDirectory != parent) {
154
destFolders_.push_back(g_Config.currentDirectory);
155
}
156
destFolders_.push_back(g_Config.memStickDirectory);
157
} else {
158
destFolders_.push_back(GetSysDirectory(DIRECTORY_GAME));
159
}
160
161
returnToHomebrew_ = true;
162
showDeleteCheckbox = true;
163
break;
164
}
165
case ZipFileContents::TEXTURE_PACK:
166
{
167
std::string_view question = iz->T("Install textures from ZIP file?");
168
leftColumn->Add(new TextView(question));
169
leftColumn->Add(new TextView(shortFilename));
170
171
doneView_ = leftColumn->Add(new TextView(""));
172
173
showDeleteCheckbox = true;
174
break;
175
}
176
case ZipFileContents::SAVE_STATES:
177
{
178
std::string_view question = iz->T("Import savestates from ZIP file");
179
leftColumn->Add(new TextView(question))->SetBig(true);
180
leftColumn->Add(new TextView(GetFriendlyPath(zipPath_)));
181
leftColumn->Add(new TextView(zipFileInfo_.gameTitle));
182
183
Path savestateDir = GetSysDirectory(DIRECTORY_SAVESTATE);
184
ZipContainer zipFile = ZipOpenPath(zipPath_);
185
overwrite = !CanExtractWithoutOverwrite(zipFile, savestateDir, 50);
186
ZipClose(zipFile);
187
188
destFolders_.push_back(savestateDir);
189
190
// TODO: Use the GameInfoCache to display data about the game if available.
191
doneView_ = leftColumn->Add(new TextView(""));
192
showDeleteCheckbox = true;
193
break;
194
}
195
case ZipFileContents::SAVE_DATA:
196
{
197
std::string_view question = iz->T("Import savedata from ZIP file");
198
leftColumn->Add(new TextView(question))->SetBig(true);
199
leftColumn->Add(new TextView(zipFileInfo_.gameTitle + ": " + zipFileInfo_.savedataDir));
200
201
Path savedataDir = GetSysDirectory(DIRECTORY_SAVEDATA);
202
ZipContainer zipFile = ZipOpenPath(zipPath_);
203
overwrite = !CanExtractWithoutOverwrite(zipFile, savedataDir, 50);
204
ZipClose(zipFile);
205
206
destFolders_.push_back(savedataDir);
207
int columnWidth = 300;
208
209
LinearLayout *compareColumns = leftColumn->Add(new LinearLayout(ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
210
LinearLayout *leftCompare = new LinearLayout(ORIENT_VERTICAL);
211
leftCompare->Add(new TextView(iz->T("Data to import")));
212
compareColumns->Add(leftCompare);
213
leftCompare->Add(new SavedataView(*screenManager()->getUIContext(), Path(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY,
214
zipFileInfo_.gameTitle, zipFileInfo_.savedataTitle, zipFileInfo_.savedataDetails, NiceSizeFormat(zipFileInfo_.totalFileSize), zipFileInfo_.mTime, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));
215
216
// Check for potential overwrite at destination, and ask the user if it's OK to overwrite.
217
if (overwrite) {
218
savedataToOverwrite_ = savedataDir / zipFileInfo_.savedataDir;
219
std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);
220
221
LinearLayout *rightCompare = new LinearLayout(ORIENT_VERTICAL);
222
rightCompare->Add(new TextView(iz->T("Existing data")));
223
224
compareColumns->Add(rightCompare);
225
existingSaveView_ = rightCompare->Add(new SavedataView(*screenManager()->getUIContext(), ginfo.get(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));
226
if (System_GetPropertyBool(SYSPROP_CAN_SHOW_FILE)) {
227
rightCompare->Add(new Button(di->T("Show in folder")))->OnClick.Add([=](UI::EventParams &) {
228
System_ShowFileInFolder(savedataToOverwrite_);
229
});
230
}
231
}
232
233
doneView_ = leftColumn->Add(new TextView(""));
234
showDeleteCheckbox = true;
235
break;
236
}
237
case ZipFileContents::FRAME_DUMP:
238
leftColumn->Add(new TextView(zipFileInfo_.contentName));
239
// It's a frame dump, add a play button!
240
break;
241
case ZipFileContents::EXTRACTED_GAME:
242
// We can do something smarter here later.
243
leftColumn->Add(new TextView(GetFriendlyPath(zipPath_)));
244
leftColumn->Add(new TextView(er->T("File format not supported")));
245
break;
246
case ZipFileContents::UNKNOWN:
247
leftColumn->Add(new TextView(iz->T("Zip file does not contain PSP software"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));
248
break;
249
default:
250
leftColumn->Add(new TextView(er->T("The file is not a valid zip file"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));
251
break;
252
}
253
254
if (destFolders_.size() > 1) {
255
leftColumn->Add(new TextView(iz->T("Install into folder")));
256
for (int i = 0; i < (int)destFolders_.size(); i++) {
257
leftColumn->Add(new RadioButton(&destFolderChoice_, i, GetFriendlyPath(destFolders_[i])));
258
}
259
} else if (destFolders_.size() == 1 && zipFileInfo_.contents != ZipFileContents::SAVE_DATA) {
260
leftColumn->Add(new TextView(iz->T("Install into folder")));
261
leftColumn->Add(new TextView(GetFriendlyPath(destFolders_[0])))->SetAlign(FLAG_WRAP_TEXT);
262
}
263
264
if (overwrite) {
265
leftColumn->Add(new NoticeView(NoticeLevel::WARN, di->T("Confirm Overwrite"), ""));
266
}
267
}
268
269
void InstallZipScreen::BeforeCreateViews() {
270
File::FileInfo fileInfo;
271
File::GetFileInfo(zipPath_, &fileInfo);
272
}
273
274
bool InstallZipScreen::key(const KeyInput &key) {
275
// Ignore all key presses during download and installation to avoid user escape
276
if (g_GameManager.GetState() == GameManagerState::IDLE) {
277
return UIDialogScreen::key(key);
278
}
279
return false;
280
}
281
282
void InstallZipScreen::OnInstall(UI::EventParams &params) {
283
ZipFileTask task;
284
task.url = zipPath_;
285
task.fileName = zipPath_;
286
task.deleteAfter = deleteZipFile_;
287
task.zipFileInfo = zipFileInfo_;
288
if (!destFolders_.empty() && destFolderChoice_ < destFolders_.size()) {
289
task.destination = destFolders_[destFolderChoice_];
290
}
291
if (g_GameManager.InstallZipOnThread(task)) {
292
installStarted_ = true;
293
if (playChoice_) {
294
playChoice_->SetEnabled(false); // need to exit this screen to played the installed one. We could make this smarter.
295
}
296
if (installChoice_) {
297
installChoice_->SetEnabled(false);
298
}
299
}
300
}
301
302
void InstallZipScreen::OnPlay(UI::EventParams &params) {
303
screenManager()->switchScreen(new EmuScreen(zipPath_));
304
}
305
306
void InstallZipScreen::update() {
307
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
308
309
using namespace UI;
310
if (g_GameManager.GetState() != GameManagerState::IDLE) {
311
if (backChoice_) {
312
backChoice_->SetEnabled(false);
313
}
314
} else {
315
if (backChoice_) {
316
backChoice_->SetEnabled(true);
317
}
318
std::string err = g_GameManager.GetInstallError();
319
if (!err.empty()) {
320
if (doneView_)
321
doneView_->SetText(iz->T(err));
322
} else if (installStarted_) {
323
if (doneView_) {
324
doneView_->SetText(iz->T("Installed!"));
325
}
326
MainScreen::showHomebrewTab = returnToHomebrew_;
327
}
328
}
329
330
if (existingSaveView_) {
331
std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);
332
existingSaveView_->UpdateGame(ginfo.get());
333
}
334
UIBaseDialogScreen::update();
335
}
336
337