-
Notifications
You must be signed in to change notification settings - Fork 7
/
main.cpp
231 lines (203 loc) · 8.41 KB
/
main.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
/* 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 <QApplication>
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QDateTime>
#include <QQmlApplicationEngine>
#include <QFile>
#include <QFontDatabase>
#include <QQmlContext>
#include <QDebug>
#include <QMessageBox>
#include <QRegularExpression>
#include "iconsimageprovider.h"
#include "iconthemeimageprovider.h"
#include "gamelauncher.h"
#include "qmldownloader.h"
#include "settings.h"
#include "splashcontroller.h"
#include "system.h"
namespace {
QFile logFile;
void LogMessageHandler(QtMsgType, const QMessageLogContext&, const QString& msg) {
static const QString timeFormat = "yyyy-MM-dd hh:mm:ssZ ";
logFile.write(QDateTime::currentDateTimeUtc().toString(timeFormat).toUtf8());
logFile.write(msg.toUtf8());
logFile.write("\n");
logFile.flush();
}
void LogSettings() {
std::unique_ptr<QSettings> settings(Sys::makePersistentSettings(nullptr));
QStringList storedKeys = settings->allKeys();
qDebug() << storedKeys.size() << "stored settings:";
for (const auto& key : storedKeys) {
qDebug() << " -" << key << "=" << settings->value(key).toString();
}
}
struct CommandLineOptions {
QString logFilename;
QString ariaLogFilename;
int splashMilliseconds = 3000;
RelaunchCommand relaunchCommand = RelaunchCommand::NONE;
QString updateUpdaterVersion;
QString connectUrl;
};
static void argParseError(const QString& message)
{
QMessageBox::critical(nullptr, "Unvanquished Updater", message);
exit(1);
}
QString getURLFromPositionalOptions(const QCommandLineParser& parser)
{
QStringList args = parser.positionalArguments();
if (args.empty()) {
return "";
} else if (args.size() > 1) {
argParseError("Too many command line arguments.");
}
QString url = args[0];
QRegularExpression scheme("^[a-zA-Z][-+.a-zA-Z]*://");
url.replace(scheme, ""); // Strip scheme
int passwordEnd = url.indexOf('@') + 1; // relies on -1 => not found
if (passwordEnd >= url.size()) {
argParseError("URL argument is empty");
}
// Square brackets are shell globbing metacharacters but may be used in IPv6 addresses.
// Take advantage of a Daemon parsing quirk that it will still be understood without the
// leading '['.
if (url[passwordEnd] == '[') {
url.remove(passwordEnd, 1);
}
// If a custom command is used, we have to consider the pessimistic case
// of a command line like bash -c "%command%"
// so metacharacters from any common shells must be forbidden.
// What we need to accept are domain names and IP addresses.
// Also throw in '@' for limited support of the password feature (unv://[email protected])
QRegularExpression forbiddenChar(R"regex([^a-zA-Z0-9./\-\]_:@])regex");
int forbiddenIndex = url.indexOf(forbiddenChar);
if (forbiddenIndex >= 0) {
argParseError("URL \"" + args[0] + "\" contains forbidden character '"
+ url[forbiddenIndex] + "'");
}
return "unv://" + url;
}
CommandLineOptions getCommandLineOptions(const QApplication& app) {
QCommandLineOption logFileNameOption("logfile");
logFileNameOption.setValueName("filename");
QCommandLineOption ariaLogFilenameOption("arialogfile");
ariaLogFilenameOption.setValueName("filename");
QCommandLineOption splashMsOption("splashms");
splashMsOption.setValueName("duration in milliseconds");
QCommandLineOption internalCommandOption("internalcommand");
internalCommandOption.setValueName("command");
QCommandLineParser optionParser;
optionParser.addHelpOption();
optionParser.addVersionOption();
optionParser.addOption(logFileNameOption);
optionParser.addOption(ariaLogFilenameOption);
optionParser.addOption(splashMsOption);
optionParser.addOption(internalCommandOption);
optionParser.addPositionalArgument("URL", "address of Unvanquished server to connect to", "[URL]");
optionParser.process(app);
CommandLineOptions options;
options.connectUrl = getURLFromPositionalOptions(optionParser);
options.logFilename = optionParser.value(logFileNameOption);
options.ariaLogFilename = optionParser.value(ariaLogFilenameOption);
int splashMs = optionParser.value(splashMsOption).toInt();
if (splashMs > 0) {
options.splashMilliseconds = splashMs;
}
if (optionParser.isSet(internalCommandOption)) {
QString command = optionParser.value(internalCommandOption);
if (command == "playnow") {
options.relaunchCommand = RelaunchCommand::PLAY_NOW;
} else if (command == "updategame") {
options.relaunchCommand = RelaunchCommand::UPDATE_GAME;
} else if (command.startsWith("updateupdater:")) {
options.relaunchCommand = RelaunchCommand::UPDATE_UPDATER;
options.updateUpdaterVersion = command.section(':', 1);
} else {
argParseError("Invalid --internalcommand option: " + command);
}
}
return options;
}
} // namespace
int main(int argc, char *argv[])
{
Sys::initApplicationName();
QCoreApplication::setApplicationVersion(GIT_VERSION);
QCoreApplication::setOrganizationDomain("unvanquished.net");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QApplication app(argc, argv);
// The font is already needed to display our arg parsing error on Linux
int fontId = QFontDatabase::addApplicationFont(":resources/Roboto-Regular.ttf");
if (fontId == -1) {
qDebug() << "Failed to register Roboto font";
} else {
QFont font("Roboto");
font.setPointSize(10);
app.setFont(font);
}
CommandLineOptions options = getCommandLineOptions(app);
if (!options.logFilename.isEmpty()) {
logFile.setFileName(options.logFilename);
if (logFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
qInstallMessageHandler(&LogMessageHandler);
} else {
qDebug() << "Failed to open log file for writing";
}
}
if (!options.connectUrl.isEmpty()) {
qDebug() << "Will connect to URL:" << options.connectUrl;
}
qDebug() << "Git version:" << GIT_VERSION;
LogSettings();
try {
qDebug() << "Testing exception handling...";
throw 1;
} catch(int) {
qDebug() << "Exception handling works";
}
app.setWindowIcon(QIcon(":resources/updater.png"));
Settings settings;
GameLauncher gameLauncher(options.connectUrl, settings);
if (options.relaunchCommand == RelaunchCommand::PLAY_NOW) {
gameLauncher.startGame(/*useConnectUrl=*/ true, /*failIfWindowsAdmin=*/ true);
return 0;
}
SplashController splashController(
options.relaunchCommand, options.updateUpdaterVersion, options.connectUrl, settings);
splashController.checkForUpdate();
QmlDownloader downloader(options.ariaLogFilename, options.connectUrl, settings);
QQmlApplicationEngine engine;
engine.addImportPath(QLatin1String("qrc:/"));
engine.addImageProvider(QLatin1String("fluidicons"), new IconsImageProvider());
engine.addImageProvider(QLatin1String("fluidicontheme"), new IconThemeImageProvider());
auto* context = engine.rootContext();
context->setContextProperty("updaterSettings", &settings);
context->setContextProperty("gameLauncher", &gameLauncher);
context->setContextProperty("splashController", &splashController);
context->setContextProperty("downloader", &downloader);
context->setContextProperty("splashMilliseconds", options.splashMilliseconds);
// This is done in order to use the DownloadState enum
qmlRegisterUncreatableType<QmlDownloader>(
"QmlDownloader", 1, 0, "QmlDownloader", "QmlDownloader not constructible");
engine.load(QUrl(QLatin1String("qrc:/splash.qml")));
return app.exec();
}