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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
/* SPDX-FileCopyrightText: 2026 Project Tick
* SPDX-FileContributor: Project Tick
* SPDX-License-Identifier: GPL-3.0-or-later
*
* MeshMC - A Custom Launcher for Minecraft
* Copyright (C) 2026 Project Tick
*
* 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 "UpdateChecker.h"
#include <QDebug>
#include <QDir>
#include <QJsonDocument>
#include <QJsonObject>
#include <QXmlStreamReader>
#include "BuildConfig.h"
#include "FileSystem.h"
#include "net/Download.h"
// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------
bool UpdateChecker::isPortableMode()
{
// portable.txt lives next to the application binary.
return QFile::exists(FS::PathCombine(QCoreApplication::applicationDirPath(),
"portable.txt"));
}
bool UpdateChecker::isAppImage()
{
return !qEnvironmentVariable("APPIMAGE").isEmpty();
}
QString UpdateChecker::currentVersion()
{
return QString("%1.%2.%3")
.arg(BuildConfig.VERSION_MAJOR)
.arg(BuildConfig.VERSION_MINOR)
.arg(BuildConfig.VERSION_HOTFIX);
}
QString UpdateChecker::normalizeVersion(const QString& v)
{
QString out = v.trimmed();
if (out.startsWith('v', Qt::CaseInsensitive))
out.remove(0, 1);
return out;
}
int UpdateChecker::compareVersions(const QString& v1, const QString& v2)
{
const QStringList parts1 = v1.split('.');
const QStringList parts2 = v2.split('.');
const int len = std::max(parts1.size(), parts2.size());
for (int i = 0; i < len; ++i) {
const int a = (i < parts1.size()) ? parts1.at(i).toInt() : 0;
const int b = (i < parts2.size()) ? parts2.at(i).toInt() : 0;
if (a != b)
return a - b;
}
return 0;
}
// ---------------------------------------------------------------------------
// Public
// ---------------------------------------------------------------------------
UpdateChecker::UpdateChecker(shared_qobject_ptr<QNetworkAccessManager> nam,
QObject* parent)
: QObject(parent), m_network(nam)
{
}
bool UpdateChecker::isUpdaterSupported()
{
if (!BuildConfig.UPDATER_ENABLED)
return false;
#if defined(Q_OS_LINUX) || defined(Q_OS_FREEBSD)
// On Linux/BSD: disable unless this is a portable install and not an
// AppImage.
if (isAppImage())
return false;
if (!isPortableMode())
return false;
#endif
return true;
}
void UpdateChecker::checkForUpdate(bool notifyNoUpdate)
{
if (!isUpdaterSupported()) {
qDebug() << "UpdateChecker: updater not supported on this "
"platform/mode. Skipping.";
return;
}
if (m_checking) {
qDebug() << "UpdateChecker: check already in progress, ignoring.";
return;
}
qDebug() << "UpdateChecker: starting dual-source update check.";
m_checking = true;
m_feedData.clear();
m_githubData.clear();
m_checkJob.reset(new NetJob("Update Check", m_network));
m_checkJob->addNetAction(Net::Download::makeByteArray(
QUrl(BuildConfig.UPDATER_FEED_URL), &m_feedData));
m_checkJob->addNetAction(Net::Download::makeByteArray(
QUrl(BuildConfig.UPDATER_GITHUB_API_URL), &m_githubData));
connect(m_checkJob.get(), &NetJob::succeeded,
[this, notifyNoUpdate]() { onDownloadsFinished(notifyNoUpdate); });
connect(m_checkJob.get(), &NetJob::failed, this,
&UpdateChecker::onDownloadsFailed);
m_checkJob->start();
}
// ---------------------------------------------------------------------------
// Private slots
// ---------------------------------------------------------------------------
void UpdateChecker::onDownloadsFinished(bool notifyNoUpdate)
{
m_checking = false;
m_checkJob.reset();
// ---- Parse the RSS feed -----------------------------------------------
// We look for the first <item> whose <projt:channel> == "stable".
// From that item we read <projt:version> and the <projt:asset> whose
// name attribute contains BuildConfig.BUILD_ARTIFACT.
QString feedVersion;
QString downloadUrl;
QString releaseNotes;
{
QXmlStreamReader xml(m_feedData);
m_feedData.clear();
bool insideItem = false;
bool isStable = false;
QString itemVersion;
QString itemUrl;
QString itemNotes;
// We iterate forward and take the FIRST stable item we encounter
// (the feed lists newest first).
while (!xml.atEnd() && !xml.hasError()) {
xml.readNext();
if (xml.isStartElement()) {
const QStringView name = xml.name();
if (name == u"item") {
insideItem = true;
isStable = false;
itemVersion.clear();
itemUrl.clear();
itemNotes.clear();
} else if (insideItem) {
if (xml.namespaceUri() ==
u"https://projecttick.org/ns/projt-launcher/feed") {
if (name == u"version") {
itemVersion = xml.readElementText().trimmed();
} else if (name == u"channel") {
isStable =
(xml.readElementText().trimmed() == "stable");
} else if (name == u"asset") {
const QString assetName =
xml.attributes().value("name").toString();
const QString assetUrl =
xml.attributes().value("url").toString();
if (!BuildConfig.BUILD_ARTIFACT.isEmpty() &&
assetName.contains(BuildConfig.BUILD_ARTIFACT,
Qt::CaseInsensitive)) {
itemUrl = assetUrl;
}
}
} else if (name == u"description" &&
xml.namespaceUri().isEmpty()) {
itemNotes =
xml.readElementText(
QXmlStreamReader::IncludeChildElements)
.trimmed();
}
}
} else if (xml.isEndElement() && xml.name() == u"item" &&
insideItem) {
insideItem = false;
if (isStable && !itemVersion.isEmpty()) {
// First stable item wins.
feedVersion = normalizeVersion(itemVersion);
downloadUrl = itemUrl;
releaseNotes = itemNotes;
break;
}
}
}
if (xml.hasError()) {
emit checkFailed(
tr("Failed to parse update feed: %1").arg(xml.errorString()));
return;
}
}
if (feedVersion.isEmpty()) {
emit checkFailed(
tr("No stable release entry found in the update feed."));
return;
}
if (downloadUrl.isEmpty()) {
qWarning() << "UpdateChecker: feed has version" << feedVersion
<< "but no asset matching BUILD_ARTIFACT '"
<< BuildConfig.BUILD_ARTIFACT << "'";
// We can still report an update even without a direct URL —
// the UpdateDialog will inform the user.
}
// ---- Parse the GitHub releases JSON -----------------------------------
// Expect the GitHub REST API format: { "tag_name": "vX.Y.Z", ... }
QString githubVersion;
{
QJsonParseError jsonError;
const QJsonDocument doc =
QJsonDocument::fromJson(m_githubData, &jsonError);
m_githubData.clear();
if (jsonError.error != QJsonParseError::NoError || !doc.isObject()) {
emit checkFailed(tr("Failed to parse GitHub releases response: %1")
.arg(jsonError.errorString()));
return;
}
const QString tag = doc.object().value("tag_name").toString().trimmed();
if (tag.isEmpty()) {
emit checkFailed(
tr("GitHub releases response contained no tag_name field."));
return;
}
githubVersion = normalizeVersion(tag);
}
qDebug() << "UpdateChecker: feed version =" << feedVersion
<< "| github version =" << githubVersion
<< "| current =" << currentVersion();
// ---- Cross-check both sources -----------------------------------------
if (feedVersion != githubVersion) {
qDebug() << "UpdateChecker: feed and GitHub disagree on version — no "
"update reported.";
if (notifyNoUpdate)
emit noUpdateFound();
return;
}
// ---- Compare against the running version ------------------------------
if (compareVersions(feedVersion, currentVersion()) <= 0) {
qDebug() << "UpdateChecker: already up to date.";
if (notifyNoUpdate)
emit noUpdateFound();
return;
}
qDebug() << "UpdateChecker: update available:" << feedVersion;
UpdateAvailableStatus status;
status.version = feedVersion;
status.downloadUrl = downloadUrl;
status.releaseNotes = releaseNotes;
emit updateAvailable(status);
}
void UpdateChecker::onDownloadsFailed(QString reason)
{
m_checking = false;
m_checkJob.reset();
m_feedData.clear();
m_githubData.clear();
qCritical() << "UpdateChecker: download failed:" << reason;
emit checkFailed(reason);
}
|