Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
hrydgard
GitHub Repository: hrydgard/ppsspp
Path: blob/master/UI/InstallZipScreen.cpp
3185 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
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
#include "Core/Config.h"
27
#include "Core/System.h"
28
#include "Core/Util/GameManager.h"
29
#include "Core/Loaders.h"
30
#include "UI/InstallZipScreen.h"
31
#include "UI/MainScreen.h"
32
#include "UI/OnScreenDisplay.h"
33
#include "UI/SavedataScreen.h"
34
#include "UI/EmuScreen.h"
35
36
InstallZipScreen::InstallZipScreen(const Path &zipPath) : zipPath_(zipPath) {
37
g_GameManager.ResetInstallError();
38
}
39
40
void InstallZipScreen::CreateViews() {
41
using namespace UI;
42
43
File::FileInfo fileInfo;
44
File::GetFileInfo(zipPath_, &fileInfo);
45
46
auto di = GetI18NCategory(I18NCat::DIALOG);
47
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
48
auto er = GetI18NCategory(I18NCat::ERRORS);
49
auto ga = GetI18NCategory(I18NCat::GAME);
50
51
Margins actionMenuMargins(0, 100, 15, 0);
52
53
root_ = new LinearLayout(ORIENT_HORIZONTAL);
54
55
ViewGroup *leftColumn = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(1.0f, Margins(12)));
56
ViewGroup *rightColumnItems = new LinearLayout(ORIENT_VERTICAL, new LinearLayoutParams(300, FILL_PARENT, actionMenuMargins));
57
root_->Add(leftColumn);
58
root_->Add(rightColumnItems);
59
60
std::string shortFilename = zipPath_.GetFilename();
61
62
// TODO: Do in the background?
63
struct zip *zipFile = ZipOpenPath(zipPath_);
64
65
bool showDeleteCheckbox = false;
66
returnToHomebrew_ = false;
67
installChoice_ = nullptr;
68
playChoice_ = nullptr;
69
doneView_ = nullptr;
70
existingSaveView_ = nullptr;
71
destFolders_.clear();
72
73
std::vector<Path> destOptions;
74
75
if (zipFile) {
76
DetectZipFileContents(zipFile, &zipFileInfo_); // Even if this fails, it sets zipInfo->contents.
77
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE || zipFileInfo_.contents == ZipFileContents::PSP_GAME_DIR) {
78
std::string_view question = iz->T("Install game from ZIP file?");
79
80
leftColumn->Add(new TextView(question));
81
leftColumn->Add(new TextView(shortFilename));
82
if (!zipFileInfo_.contentName.empty()) {
83
leftColumn->Add(new TextView(zipFileInfo_.contentName));
84
}
85
86
doneView_ = leftColumn->Add(new TextView(""));
87
88
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE) {
89
const bool isInDownloads = File::IsProbablyInDownloadsFolder(zipPath_);
90
Path parent;
91
if (!isInDownloads && zipPath_.CanNavigateUp()) {
92
parent = zipPath_.NavigateUp();
93
destFolders_.push_back(parent);
94
}
95
if (g_Config.currentDirectory.IsLocalType() && File::Exists(g_Config.currentDirectory) && g_Config.currentDirectory != parent) {
96
destFolders_.push_back(g_Config.currentDirectory);
97
}
98
destFolders_.push_back(g_Config.memStickDirectory);
99
} else {
100
destFolders_.push_back(GetSysDirectory(DIRECTORY_GAME));
101
}
102
103
installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));
104
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
105
106
// NOTE: We detect PBP isos (like demos) as game dirs currently. Can't play them directly.
107
if (zipFileInfo_.contents == ZipFileContents::ISO_FILE) {
108
playChoice_ = rightColumnItems->Add(new Choice(ga->T("Play")));
109
playChoice_->OnClick.Handle(this, &InstallZipScreen::OnPlay);
110
}
111
112
returnToHomebrew_ = true;
113
showDeleteCheckbox = true;
114
} else if (zipFileInfo_.contents == ZipFileContents::TEXTURE_PACK) {
115
std::string_view question = iz->T("Install textures from ZIP file?");
116
leftColumn->Add(new TextView(question));
117
leftColumn->Add(new TextView(shortFilename));
118
119
doneView_ = leftColumn->Add(new TextView(""));
120
121
installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));
122
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
123
124
showDeleteCheckbox = true;
125
} else if (zipFileInfo_.contents == ZipFileContents::SAVE_DATA) {
126
std::string_view question = iz->T("Import savedata from ZIP file");
127
leftColumn->Add(new TextView(question))->SetBig(true);
128
leftColumn->Add(new TextView(zipFileInfo_.gameTitle + ": " + zipFileInfo_.savedataDir));
129
130
Path savedataDir = GetSysDirectory(DIRECTORY_SAVEDATA);
131
bool overwrite = !CanExtractWithoutOverwrite(zipFile, savedataDir, 50);
132
133
destFolders_.push_back(savedataDir);
134
135
if (overwrite) {
136
leftColumn->Add(new NoticeView(NoticeLevel::WARN, di->T("Confirm Overwrite"), ""));
137
}
138
139
int columnWidth = 300;
140
141
LinearLayout *compareColumns = leftColumn->Add(new LinearLayout(UI::ORIENT_HORIZONTAL, new LinearLayoutParams(FILL_PARENT, WRAP_CONTENT)));
142
LinearLayout *leftCompare = new LinearLayout(UI::ORIENT_VERTICAL);
143
leftCompare->Add(new TextView(iz->T("Data to import")));
144
compareColumns->Add(leftCompare);
145
leftCompare->Add(new SavedataView(*screenManager()->getUIContext(), Path(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY,
146
zipFileInfo_.gameTitle, zipFileInfo_.savedataTitle, zipFileInfo_.savedataDetails, NiceSizeFormat(zipFileInfo_.totalFileSize), zipFileInfo_.mTime, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));
147
148
// Check for potential overwrite at destination, and ask the user if it's OK to overwrite.
149
if (overwrite) {
150
savedataToOverwrite_ = savedataDir / zipFileInfo_.savedataDir;
151
std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);
152
153
LinearLayout *rightCompare = new LinearLayout(UI::ORIENT_VERTICAL);
154
rightCompare->Add(new TextView(iz->T("Existing data")));
155
156
compareColumns->Add(rightCompare);
157
existingSaveView_ = rightCompare->Add(new SavedataView(*screenManager()->getUIContext(), ginfo.get(), IdentifiedFileType::PSP_SAVEDATA_DIRECTORY, false, new LinearLayoutParams(columnWidth, WRAP_CONTENT)));
158
if (System_GetPropertyBool(SYSPROP_CAN_SHOW_FILE)) {
159
rightCompare->Add(new Button(di->T("Show in folder")))->OnClick.Add([=](UI::EventParams &) {
160
System_ShowFileInFolder(savedataToOverwrite_);
161
return UI::EVENT_DONE;
162
});
163
}
164
}
165
166
installChoice_ = rightColumnItems->Add(new Choice(iz->T("Install")));
167
installChoice_->OnClick.Handle(this, &InstallZipScreen::OnInstall);
168
169
doneView_ = leftColumn->Add(new TextView(""));
170
showDeleteCheckbox = true;
171
} else if (zipFileInfo_.contents == ZipFileContents::FRAME_DUMP) {
172
leftColumn->Add(new TextView(zipFileInfo_.contentName));
173
// It's a frame dump, add a play button!
174
playChoice_ = rightColumnItems->Add(new Choice(ga->T("Play")));
175
playChoice_->OnClick.Handle(this, &InstallZipScreen::OnPlay);
176
} else {
177
leftColumn->Add(new TextView(iz->T("Zip file does not contain PSP software"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));
178
}
179
ZipClose(zipFile);
180
} else {
181
leftColumn->Add(new TextView(er->T("The file is not a valid zip file"), ALIGN_LEFT, false, new AnchorLayoutParams(10, 10, NONE, NONE)));
182
}
183
184
if (destFolders_.size() > 1) {
185
leftColumn->Add(new TextView(iz->T("Install into folder")));
186
for (int i = 0; i < (int)destFolders_.size(); i++) {
187
leftColumn->Add(new RadioButton(&destFolderChoice_, i, destFolders_[i].ToVisualString()));
188
}
189
} else if (destFolders_.size() == 1 && zipFileInfo_.contents != ZipFileContents::SAVE_DATA) {
190
leftColumn->Add(new TextView(StringFromFormat("%s %s", iz->T_cstr("Install into folder:"), destFolders_[0].ToVisualString().c_str())));
191
}
192
193
// OK so that EmuScreen will handle it right.
194
backChoice_ = rightColumnItems->Add(new Choice(di->T("Back")));
195
backChoice_->OnClick.Handle<UIScreen>(this, &UIScreen::OnOK);
196
197
if (showDeleteCheckbox) {
198
rightColumnItems->Add(new CheckBox(&deleteZipFile_, iz->T("Delete ZIP file")));
199
}
200
}
201
202
bool InstallZipScreen::key(const KeyInput &key) {
203
// Ignore all key presses during download and installation to avoid user escape
204
if (g_GameManager.GetState() == GameManagerState::IDLE) {
205
return UIDialogScreen::key(key);
206
}
207
return false;
208
}
209
210
UI::EventReturn InstallZipScreen::OnInstall(UI::EventParams &params) {
211
ZipFileTask task;
212
task.url = zipPath_;
213
task.fileName = zipPath_;
214
task.deleteAfter = deleteZipFile_;
215
task.zipFileInfo = zipFileInfo_;
216
if (!destFolders_.empty() && destFolderChoice_ < destFolders_.size()) {
217
task.destination = destFolders_[destFolderChoice_];
218
}
219
if (g_GameManager.InstallZipOnThread(task)) {
220
installStarted_ = true;
221
if (playChoice_) {
222
playChoice_->SetEnabled(false); // need to exit this screen to played the installed one. We could make this smarter.
223
}
224
if (installChoice_) {
225
installChoice_->SetEnabled(false);
226
}
227
}
228
return UI::EVENT_DONE;
229
}
230
231
UI::EventReturn InstallZipScreen::OnPlay(UI::EventParams &params) {
232
screenManager()->switchScreen(new EmuScreen(zipPath_));
233
return UI::EVENT_DONE;
234
}
235
236
void InstallZipScreen::update() {
237
auto iz = GetI18NCategory(I18NCat::INSTALLZIP);
238
239
using namespace UI;
240
if (g_GameManager.GetState() != GameManagerState::IDLE) {
241
if (backChoice_) {
242
backChoice_->SetEnabled(false);
243
}
244
} else {
245
if (backChoice_) {
246
backChoice_->SetEnabled(true);
247
}
248
std::string err = g_GameManager.GetInstallError();
249
if (!err.empty()) {
250
if (doneView_)
251
doneView_->SetText(iz->T(err));
252
} else if (installStarted_) {
253
if (doneView_) {
254
doneView_->SetText(iz->T("Installed!"));
255
}
256
MainScreen::showHomebrewTab = returnToHomebrew_;
257
}
258
}
259
260
if (existingSaveView_) {
261
std::shared_ptr<GameInfo> ginfo = g_gameInfoCache->GetInfo(screenManager()->getDrawContext(), savedataToOverwrite_, GameInfoFlags::FILE_TYPE | GameInfoFlags::PARAM_SFO | GameInfoFlags::ICON | GameInfoFlags::SIZE);
262
existingSaveView_->UpdateGame(ginfo.get());
263
}
264
UIDialogScreenWithBackground::update();
265
}
266
267