-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathosx.cpp
213 lines (195 loc) · 6.78 KB
/
osx.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
/* Unvanquished Updater
* Copyright (C) Unvanquished Developers
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* 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 for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "system.h"
#include "settings.h"
#include "quazip/quazip/JlCompress.h"
#include <QDir>
#include <QFile>
#include <QDebug>
#include <QCoreApplication>
#include <QFileInfo>
#include <QProcess>
namespace {
// Enter path of file within an app, and get the
// path of the app. Returns empty string if not within
// an app.
QString extractAppPath(const QString& path) {
QFileInfo info(path);
if (info.canonicalFilePath().contains(".app")) {
QDir path = info.absoluteDir();
while (!path.isRoot() && !path.absolutePath().endsWith(".app")) {
path.cdUp();
}
if (path.isRoot()) {
qDebug() << "Failed to find .app name.";
return "";
}
return path.absolutePath();
}
return "";
}
}
namespace Sys {
QString archiveName()
{
return "macos-amd64.zip";
}
QString defaultInstallPath()
{
return QDir::homePath() + "/Games/Unvanquished";
}
bool validateInstallPath(const QString&)
{
// The default install location is not in the Unvanquished homepath, so the problem which
// can occur on Linux is not likely to arise.
return true;
}
bool installShortcuts()
{
QDir applications(QDir::homePath() + "/Applications");
if (!applications.exists()) {
if (!applications.mkpath(".")) {
qDebug() << "can't create ~/Applications";
return false;
}
}
Settings settings;
QString linkPath = applications.absoluteFilePath("Unvanquished.app");
QFile::remove(linkPath);
if (!QFile::link(settings.installPath() + QDir::separator() + "updater.app", linkPath)) {
qDebug() << "failed to create Applications link";
return false;
}
return true;
}
bool installUpdater(const QString& installPath) {
QString currentAppPath = extractAppPath(QCoreApplication::applicationFilePath());
if (currentAppPath.isEmpty()) return false;
QDir src(currentAppPath);
QDir dest(installPath + QDir::separator() + "updater.app");
if (src == dest) {
qDebug() << "Updater already in install location";
return true;
}
if (dest.exists()) {
qDebug() << "Deleting updater in install path";
if (!dest.removeRecursively()) {
return false;
}
}
qDebug() << "Copying updater from" << src.absolutePath();
int ret = QProcess::execute("/bin/cp", {QString("-R"), src.path(), dest.path()});
qDebug() << "cp returned" << ret;
return ret == 0;
}
bool updateUpdater(const QString& updaterArchive, const QString&)
{
QString currentAppPath = extractAppPath(QCoreApplication::applicationFilePath());
if (currentAppPath.isEmpty()) return false;
QDir currentApp(currentAppPath);
QDir backupUpdater(currentAppPath + ".bak");
if (backupUpdater.exists()) {
if (!backupUpdater.removeRecursively()) {
qDebug() << "Error deleting old update. Please update manually";
return false;
}
}
QDir extractDir = backupUpdater;
extractDir.cdUp();
if (!extractDir.rename(currentApp.dirName(), backupUpdater.dirName())) {
qDebug() << "Could not rename backup. Please manually update.";
return false;
}
QStringList extractedFiles = JlCompress::extractDir(updaterArchive, extractDir.absolutePath());
if (extractedFiles.size() < 1) {
return false;
}
// Extract the .app name.
// Since it's an .app file, it must contain an Info.plist. Use
// this file to determine the .app name in the zip.
// NOTE: We could have hard coded this, but there is no guarantee
// that the person running this app is running the app that has
// the same name as the one in the zip.
constexpr char kFileInApp[] = "Info.plist";
QStringList infos = extractedFiles.filter(kFileInApp);
if (infos.size() < 1) {
qDebug() << "Error finding " << kFileInApp;
return false;
}
QString extractedAppPath = "";
for (int i = 0; i < infos.size() && extractedAppPath.isEmpty(); ++i) {
extractedAppPath = extractAppPath(infos[i]);
}
if (extractedAppPath.isEmpty()) return false;
QDir extractedApp(extractedAppPath);
if (extractedApp.dirName() != currentApp.dirName()) {
if (!extractDir.rename(extractedApp.dirName(), currentApp.dirName())) {
qDebug() << "Could not rename update. Please manually update.";
return false;
}
}
// TODO: Maybe don't use system? startDetached didn't work for me...
// TODO: pass connectUrl
int i = system((QString("/usr/bin/open -F -n \"%1\"").arg(currentAppPath)).toStdString().c_str());
if (i != 0) {
qDebug() << "Error launching new updater. Please try manually.";
return false;
}
QCoreApplication::quit();
return true;
}
QString updaterArchiveName()
{
return "UnvUpdaterOSX.zip";
}
std::string getCertStore()
{
return ""; // Not used on OSX.
}
void initApplicationName()
{
QCoreApplication::setOrganizationName("Unvanquished Development");
QCoreApplication::setApplicationName("Unvanquished Updater");
}
// Settings are stored in "~/Library/Preferences/net.unvanquished.Unvanquished Updater.plist"
// After deleting/changing the file, you must run `sudo killall cfprefsd` for it to take effect
QSettings* makePersistentSettings(QObject* parent)
{
return new QSettings(parent);
}
QString getGameCommand(const QString& installPath)
{
return "/usr/bin/open " +
QuoteQProcessCommandArgument(installPath + QDir::separator() + "Unvanquished.app") +
" --args";
}
QString startGame(const QString& commandLine, bool, const QString&)
{
if (commandLine.startsWith("/usr/bin/open ")) {
// Get the return code of `open` to see whether the app was started successfully
int ret = QProcess::execute(commandLine);
qDebug() << "/usr/bin/open returned" << ret;
return ret == 0 ? "" : QString("/usr/bin/open returned %1").arg(ret);
} else {
return QProcess::startDetached(commandLine) ? "" : "startDetached failed";
}
}
ElevationResult RelaunchElevated(const QString&)
{
return ElevationResult::UNNEEDED;
}
} // namespace Sys