forked from gb2dev/HLA-NoVR-Launcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
178 lines (148 loc) · 6.34 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
#include <QFont>
#include <QFontDatabase>
#include <QGuiApplication>
#include <QJsonDocument>
#include <QJsonObject>
#ifdef Q_OS_WIN
#include <QAudioDevice>
#include <QMediaDevices>
#endif
#include <QProcess>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QQuickStyle>
#include "gamemenu.h"
#include "launcher.h"
bool noUpdate = true;
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(context);
QFile outFile(noUpdate ? "log.txt" : "log_update.txt");
outFile.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream textStream(&outFile);
textStream << msg << Qt::endl;
}
int main(int argc, char *argv[])
{
qputenv("QT_ENABLE_HIGHDPI_SCALING", "0");
QQuickStyle::setStyle("Fusion");
QGuiApplication app(argc, argv);
app.setOrganizationName("HLA-NoVR");
app.setApplicationName("Launcher");
app.setFont(QFont("Dinish"));
app.setApplicationVersion(VERSION);
QFontDatabase::addApplicationFont("Dinish-Regular.otf");
QFontDatabase::addApplicationFont("Dinish-Bold.otf");
QFontDatabase::addApplicationFont("Dinish-Italic.otf");
QQmlApplicationEngine engine;
QUrl url;
noUpdate = app.arguments().contains("-noupdate");
QFile outFile(noUpdate ? "log.txt" : "log_update.txt");
outFile.resize(0);
qInstallMessageHandler(customMessageHandler);
if (noUpdate) {
QFile("HLA-NoVR-Launcher-Installer.exe").remove();
QQmlContext *rootContext = engine.rootContext();
rootContext->setContextProperty("applicationDirPath", app.applicationDirPath());
Launcher *launcher = new Launcher(&app);
launcher->engine = &engine;
rootContext->setContextProperty("launcher", launcher);
#ifdef Q_OS_WIN
QAudioDevice info(QMediaDevices::defaultAudioOutput());
rootContext->setContextProperty("audioWarning", info.maximumChannelCount() > 2);
#else
rootContext->setContextProperty("audioWarning", false);
#endif
GameMenu *gameMenu = new GameMenu(&app);
rootContext->setContextProperty("gameMenu", gameMenu);
url = QUrl(u"qrc:/HLA-NoVR-Launcher/Launcher.qml"_qs);
} else {
QNetworkAccessManager *networkManager = new QNetworkAccessManager(&app);
QNetworkRequest versionInfoRequest(QUrl("https://api.github.com/repos/bfeber/HLA-NoVR-Launcher/releases/latest"));
QNetworkReply *versionInfoReply = networkManager->get(versionInfoRequest);
QObject::connect(versionInfoReply, &QNetworkReply::finished, qApp, [versionInfoReply, networkManager]() {
versionInfoReply->deleteLater();
if (versionInfoReply->error()) {
qDebug() << "Can't find newest version";
#ifdef Q_OS_WIN
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments() << "-noupdate");
#else
QProcess::startDetached("/bin/bash", {"update.sh"});
#endif
qApp->quit();
return;
}
QJsonDocument doc = QJsonDocument::fromJson(versionInfoReply->readAll());
if (doc.isNull()) {
#ifdef Q_OS_WIN
qDebug() << "Invalid newest version";
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments() << "-noupdate");
#else
QProcess::startDetached("/bin/bash", {"update.sh"});
#endif
qApp->quit();
return;
} else {
if (VERSION != doc.object().value("tag_name").toString()) {
QNetworkRequest launcherRequest(QUrl("https://github.com/bfeber/HLA-NoVR-Launcher/releases/latest/download/HLA-NoVR-Launcher-Installer.exe"));
QNetworkReply *launcherReply = networkManager->get(launcherRequest);
QObject::connect(launcherReply, &QNetworkReply::finished, launcherReply, [launcherReply]() {
QFile file("HLA-NoVR-Launcher-Installer.exe");
if (launcherReply->error() || !file.open(QIODevice::WriteOnly)) {
qDebug() << "Invalid update file";
launcherReply->deleteLater();
QFile("HLA-NoVR-Launcher-Installer.exe").remove();
#ifdef Q_OS_WIN
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments() << "-noupdate");
#else
QProcess::startDetached("/bin/bash", {"update.sh"});
#endif
qApp->quit();
return;
}
file.write(launcherReply->readAll());
file.close();
launcherReply->deleteLater();
QProcess *install = new QProcess;
#ifdef Q_OS_WIN
qDebug() << "Installing update";
install->setProgram("HLA-NoVR-Launcher-Installer.exe");
install->setArguments({"/silent", "/norestartapplications"});
#else
install->setProgram("unzip");
install->setArguments({"-o", "HLA-NoVR-Launcher-Linux.zip", "-d", "Update"});
#endif
install->start();
QObject::connect(install, &QProcess::finished, [=](int exitCode, QProcess::ExitStatus exitStatus = QProcess::NormalExit) {
qDebug() << "Update installed: " + QString::number(exitCode);
#ifdef Q_OS_WIN
#else
QProcess::startDetached("/bin/bash", {"update.sh"});
#endif
qApp->quit();
return;
});
});
} else {
#ifdef Q_OS_WIN
qApp->quit();
QProcess::startDetached(qApp->arguments()[0], qApp->arguments() << "-noupdate");
#else
QProcess::startDetached("/bin/bash", {"update.sh"});
#endif
qApp->quit();
return;
}
}
});
url = QUrl(u"qrc:/HLA-NoVR-Launcher/Updating.qml"_qs);
}
QObject::connect(&engine, &QQmlApplicationEngine::objectCreationFailed,
&app, []() { QCoreApplication::exit(-1); },
Qt::QueuedConnection);
engine.load(url);
return app.exec();
}