summaryrefslogtreecommitdiff
path: root/archived/projt-launcher/launcher/updater/ProjTExternalUpdater.cpp
blob: 2b6ef190157c08ed25e4030d3b85e50a188bf801 (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// SPDX-License-Identifier: GPL-3.0-only
// SPDX-FileCopyrightText: 2026 Project Tick
// SPDX-FileContributor: Project Tick Team
/*
 *  ProjT Launcher - Minecraft Launcher
 *  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, version 3.
 *
 *  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, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * === Upstream License Block (Do Not Modify) ==============================
 *
 *
 *
 *
 *
 *
 *  Prism Launcher - Minecraft Launcher
 *  Copyright (C) 2023 Rachel Powers <508861+Ryex@users.noreply.github.com>
 *
 *  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, version 3.
 *
 *  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, write to the Free Software Foundation,
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 *
 *
 * ======================================================================== */

#include "ProjTExternalUpdater.h"
#include <QCoreApplication>
#include <QDateTime>
#include <QDebug>
#include <QDir>
#include <QMessageBox>
#include <QProcess>
#include <QProgressDialog>
#include <QSettings>
#include <QTimer>
#include <algorithm>
#include <memory>

#include "StringUtils.h"

#include "BuildConfig.h"

#include "ui/dialogs/UpdateAvailableDialog.h"

class ProjTExternalUpdater::Private
{
  public:
	QDir appDir;
	QDir dataDir;
	QTimer updateTimer;
	bool allowBeta{};
	bool autoCheck{};
	double updateInterval{};
	QDateTime lastCheck;
	std::unique_ptr<QSettings> settings;

	QWidget* parent{};
};

ProjTExternalUpdater::ProjTExternalUpdater(QWidget* parent, const QString& appDir, const QString& dataDir)
	: priv(new ProjTExternalUpdater::Private())
{
	priv->appDir	   = QDir(appDir);
	priv->dataDir	   = QDir(dataDir);
	auto settings_file = priv->dataDir.absoluteFilePath("projtlauncher_update.cfg");
	priv->settings	   = std::make_unique<QSettings>(settings_file, QSettings::Format::IniFormat);
	priv->allowBeta	   = priv->settings->value("allow_beta", false).toBool();
	priv->autoCheck	   = priv->settings->value("auto_check", false).toBool();
	bool interval_ok = false;
	// default once per day
	priv->updateInterval = priv->settings->value("update_interval", 86400).toInt(&interval_ok);
	if (!interval_ok)
		priv->updateInterval = 86400;
	auto last_check = priv->settings->value("last_check");
	if (!last_check.isNull() && last_check.isValid())
	{
		priv->lastCheck = QDateTime::fromString(last_check.toString(), Qt::ISODate);
	}
	priv->parent = parent;
	connectTimer();
	resetAutoCheckTimer();
	if (priv->updateInterval == 0)
		checkForUpdates(false);
}

ProjTExternalUpdater::~ProjTExternalUpdater()
{
	if (priv->updateTimer.isActive())
		priv->updateTimer.stop();
	disconnectTimer();
	priv->settings->sync();
	delete priv;
}

void ProjTExternalUpdater::checkForUpdates()
{
	checkForUpdates(true);
}

void ProjTExternalUpdater::checkForUpdates(bool triggeredByUser)
{
	QProgressDialog progress(tr("Checking for updates..."), "", 0, 0, priv->parent);
	progress.setCancelButton(nullptr);
	progress.adjustSize();
	if (triggeredByUser)
		progress.show();
	QCoreApplication::processEvents();

	QProcess proc;
	auto exe_name = QStringLiteral("%1_updater").arg(BuildConfig.LAUNCHER_APP_BINARY_NAME);
#if defined Q_OS_WIN32
	exe_name.append(".exe");

	auto env = QProcessEnvironment::systemEnvironment();
	env.insert("__COMPAT_LAYER", "RUNASINVOKER");
	proc.setProcessEnvironment(env);
#else
	exe_name = QString("bin/%1").arg(exe_name);
#endif

	QStringList args = { "--check-only", "--dir", priv->dataDir.absolutePath(), "--debug" };
	if (priv->allowBeta)
		args.append("--pre-release");

	proc.start(priv->appDir.absoluteFilePath(exe_name), args);
	auto result_start = proc.waitForStarted(5000);
	if (!result_start)
	{
		auto err = proc.error();
		qDebug() << "Failed to start updater after 5 seconds."
				 << "reason:" << err << proc.errorString();
		auto msgBox = QMessageBox(QMessageBox::Information,
								  tr("Update Check Failed"),
								  tr("Failed to start after 5 seconds\nReason: %1.").arg(proc.errorString()),
								  QMessageBox::Ok,
								  priv->parent);
		msgBox.setMinimumWidth(460);
		msgBox.adjustSize();
		msgBox.exec();
		priv->lastCheck = QDateTime::currentDateTime();
		priv->settings->setValue("last_check", priv->lastCheck.toString(Qt::ISODate));
		priv->settings->sync();
		resetAutoCheckTimer();
		return;
	}
	QCoreApplication::processEvents();

	auto result_finished = proc.waitForFinished(60000);
	if (!result_finished)
	{
		proc.kill();
		auto err	= proc.error();
		auto output = proc.readAll();
		qDebug() << "Updater failed to close after 60 seconds."
				 << "reason:" << err << proc.errorString();
		auto msgBox = QMessageBox(QMessageBox::Information,
								  tr("Update Check Failed"),
								  tr("Updater failed to close 60 seconds\nReason: %1.").arg(proc.errorString()),
								  QMessageBox::Ok,
								  priv->parent);
		msgBox.setDetailedText(output);
		msgBox.setMinimumWidth(460);
		msgBox.adjustSize();
		msgBox.exec();
		priv->lastCheck = QDateTime::currentDateTime();
		priv->settings->setValue("last_check", priv->lastCheck.toString(Qt::ISODate));
		priv->settings->sync();
		resetAutoCheckTimer();
		return;
	}

	auto exit_code = proc.exitCode();

	auto std_output = proc.readAllStandardOutput();
	auto std_error	= proc.readAllStandardError();

	progress.hide();
	QCoreApplication::processEvents();

	switch (exit_code)
	{
		case 0:
			// no update available
			if (triggeredByUser)
			{
				qDebug() << "No update available";
				auto msgBox = QMessageBox(QMessageBox::Information,
										  tr("No Update Available"),
										  tr("You are running the latest version."),
										  QMessageBox::Ok,
										  priv->parent);
				msgBox.setMinimumWidth(460);
				msgBox.adjustSize();
				msgBox.exec();
			}
			break;
		case 1:
			// there was an error
			{
				qDebug() << "Updater subprocess error" << qPrintable(std_error);
				auto msgBox = QMessageBox(QMessageBox::Warning,
										  tr("Update Check Error"),
										  tr("There was an error running the update check."),
										  QMessageBox::Ok,
										  priv->parent);
				msgBox.setDetailedText(QString(std_error));
				msgBox.setMinimumWidth(460);
				msgBox.adjustSize();
				msgBox.exec();
			}
			break;
		case 100:
		case 101:
			// update or migration available
			{
				auto [first_line, remainder1]	 = StringUtils::splitFirst(std_output, '\n');
				auto [second_line, remainder2]	 = StringUtils::splitFirst(remainder1, '\n');
				auto [third_line, release_notes] = StringUtils::splitFirst(remainder2, '\n');
				auto version_name				 = StringUtils::splitFirst(first_line, ": ").second.trimmed();
				auto version_tag				 = StringUtils::splitFirst(second_line, ": ").second.trimmed();
				auto release_timestamp =
					QDateTime::fromString(StringUtils::splitFirst(third_line, ": ").second.trimmed(), Qt::ISODate);
				if (exit_code == 100)
					qDebug() << "Update available:" << version_name << version_tag << release_timestamp;
				else
					qDebug() << "Migration available:" << version_name << version_tag << release_timestamp;
				qDebug() << "Update release notes:" << release_notes;

				offerUpdate(version_name, version_tag, release_notes, exit_code == 101);
			}
			break;
		default:
			// unknown error code
			{
				qDebug() << "Updater exited with unknown code" << exit_code;
				auto msgBox = QMessageBox(
					QMessageBox::Information,
					tr("Unknown Update Error"),
					tr("The updater exited with an unknown condition.\nExit Code: %1").arg(QString::number(exit_code)),
					QMessageBox::Ok,
					priv->parent);
				auto detail_txt = tr("StdOut: %1\nStdErr: %2").arg(QString(std_output)).arg(QString(std_error));
				msgBox.setDetailedText(detail_txt);
				msgBox.setMinimumWidth(460);
				msgBox.adjustSize();
				msgBox.exec();
			}
	}
	priv->lastCheck = QDateTime::currentDateTime();
	priv->settings->setValue("last_check", priv->lastCheck.toString(Qt::ISODate));
	priv->settings->sync();
	resetAutoCheckTimer();
}

bool ProjTExternalUpdater::getAutomaticallyChecksForUpdates()
{
	return priv->autoCheck;
}

double ProjTExternalUpdater::getUpdateCheckInterval()
{
	return priv->updateInterval;
}

bool ProjTExternalUpdater::getBetaAllowed()
{
	return priv->allowBeta;
}

void ProjTExternalUpdater::setAutomaticallyChecksForUpdates(bool check)
{
	priv->autoCheck = check;
	priv->settings->setValue("auto_check", check);
	priv->settings->sync();
	resetAutoCheckTimer();
}

void ProjTExternalUpdater::setUpdateCheckInterval(double seconds)
{
	priv->updateInterval = seconds;
	priv->settings->setValue("update_interval", seconds);
	priv->settings->sync();
	resetAutoCheckTimer();
}

void ProjTExternalUpdater::setBetaAllowed(bool allowed)
{
	priv->allowBeta = allowed;
	priv->settings->setValue("allow_beta", allowed);
	priv->settings->sync();
}

void ProjTExternalUpdater::resetAutoCheckTimer()
{
	if (priv->autoCheck && priv->updateInterval > 0)
	{
		qint64 timeoutMs = 0;
		auto now		 = QDateTime::currentDateTime();
		if (priv->lastCheck.isValid())
		{
			qint64 diff		= priv->lastCheck.secsTo(now);
			qint64 secs_left = std::max<qint64>(priv->updateInterval - diff, 0);
			timeoutMs		= secs_left * 1000;
		}
		timeoutMs = std::min(timeoutMs, static_cast<qint64>(INT_MAX));

		qDebug() << "Auto update timer starting," << timeoutMs / 1000 << "seconds left";
		priv->updateTimer.start(static_cast<int>(timeoutMs));
	}
	else
	{
		if (priv->updateTimer.isActive())
			priv->updateTimer.stop();
	}
}

void ProjTExternalUpdater::connectTimer()
{
	connect(&priv->updateTimer, &QTimer::timeout, this, &ProjTExternalUpdater::autoCheckTimerFired);
}

void ProjTExternalUpdater::disconnectTimer()
{
	disconnect(&priv->updateTimer, &QTimer::timeout, this, &ProjTExternalUpdater::autoCheckTimerFired);
}

void ProjTExternalUpdater::autoCheckTimerFired()
{
	qDebug() << "Auto update Timer fired";
	checkForUpdates(false);
}

void ProjTExternalUpdater::offerUpdate(const QString& version_name,
									   const QString& version_tag,
									   const QString& release_notes,
									   bool isMigration)
{
	priv->settings->beginGroup("skip");
	auto should_skip = priv->settings->value(version_tag, false).toBool();
	priv->settings->endGroup();

	if (should_skip)
	{
		auto msgBox = QMessageBox(QMessageBox::Information,
								  tr("No Update Available"),
								  tr("There are no new updates available."),
								  QMessageBox::Ok,
								  priv->parent);
		msgBox.setMinimumWidth(460);
		msgBox.adjustSize();
		msgBox.exec();
		return;
	}

	UpdateAvailableDialog dlg(BuildConfig.printableVersionString(),
							  version_name,
							  release_notes,
							  isMigration ? UpdateAvailableDialog::Mode::Migration
										  : UpdateAvailableDialog::Mode::Update);

	auto result = dlg.exec();
	qDebug() << "offer dlg result" << result;
	switch (result)
	{
		case UpdateAvailableDialog::Install:
		{
			performUpdate(version_tag);
			return;
		}
		case UpdateAvailableDialog::Skip:
		{
			priv->settings->beginGroup("skip");
			priv->settings->setValue(version_tag, true);
			priv->settings->endGroup();
			priv->settings->sync();
			return;
		}
		default: return;
	}
}

void ProjTExternalUpdater::performUpdate(const QString& version_tag)
{
	QProcess proc;
	auto exe_name = QStringLiteral("%1_updater").arg(BuildConfig.LAUNCHER_APP_BINARY_NAME);
#if defined Q_OS_WIN32
	exe_name.append(".exe");

	auto env = QProcessEnvironment::systemEnvironment();
	env.insert("__COMPAT_LAYER", "RUNASINVOKER");
	proc.setProcessEnvironment(env);
#else
	exe_name = QString("bin/%1").arg(exe_name);
#endif

	QStringList args = { "--dir", priv->dataDir.absolutePath(), "--install-version", version_tag };
	if (priv->allowBeta)
		args.append("--pre-release");

	proc.setProgram(priv->appDir.absoluteFilePath(exe_name));
	proc.setArguments(args);
	auto result = proc.startDetached();
	if (!result)
	{
		qDebug() << "Failed to start updater:" << proc.error() << proc.errorString();
	}
	QCoreApplication::exit();
}