Fix infinite update (#5187)

This commit is contained in:
DioEgizio 2026-03-16 18:10:50 +00:00 committed by GitHub
commit ab72536248
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -30,6 +30,7 @@
#include <QProgressDialog> #include <QProgressDialog>
#include <QSettings> #include <QSettings>
#include <QTimer> #include <QTimer>
#include <algorithm>
#include <memory> #include <memory>
#include "StringUtils.h" #include "StringUtils.h"
@ -43,29 +44,30 @@ class PrismExternalUpdater::Private {
QDir appDir; QDir appDir;
QDir dataDir; QDir dataDir;
QTimer updateTimer; QTimer updateTimer;
bool allowBeta; bool allowBeta{};
bool autoCheck; bool autoCheck{};
double updateInterval; double updateInterval{};
QDateTime lastCheck; QDateTime lastCheck;
std::unique_ptr<QSettings> settings; std::unique_ptr<QSettings> settings;
QWidget* parent; QWidget* parent{};
}; };
PrismExternalUpdater::PrismExternalUpdater(QWidget* parent, const QString& appDir, const QString& dataDir) PrismExternalUpdater::PrismExternalUpdater(QWidget* parent, const QString& appDir, const QString& dataDir)
: priv(new PrismExternalUpdater::Private())
{ {
priv = new PrismExternalUpdater::Private();
priv->appDir = QDir(appDir); priv->appDir = QDir(appDir);
priv->dataDir = QDir(dataDir); priv->dataDir = QDir(dataDir);
auto settings_file = priv->dataDir.absoluteFilePath("prismlauncher_update.cfg"); auto settings_file = priv->dataDir.absoluteFilePath("prismlauncher_update.cfg");
priv->settings = std::make_unique<QSettings>(settings_file, QSettings::Format::IniFormat); priv->settings = std::make_unique<QSettings>(settings_file, QSettings::Format::IniFormat);
priv->allowBeta = priv->settings->value("allow_beta", false).toBool(); priv->allowBeta = priv->settings->value("allow_beta", false).toBool();
priv->autoCheck = priv->settings->value("auto_check", false).toBool(); priv->autoCheck = priv->settings->value("auto_check", false).toBool();
bool interval_ok; bool interval_ok = false;
// default once per day // default once per day
priv->updateInterval = priv->settings->value("update_interval", 86400).toInt(&interval_ok); priv->updateInterval = priv->settings->value("update_interval", 86400).toInt(&interval_ok);
if (!interval_ok) if (!interval_ok) {
priv->updateInterval = 86400; priv->updateInterval = 86400;
}
auto last_check = priv->settings->value("last_check"); auto last_check = priv->settings->value("last_check");
if (!last_check.isNull() && last_check.isValid()) { if (!last_check.isNull() && last_check.isValid()) {
priv->lastCheck = QDateTime::fromString(last_check.toString(), Qt::ISODate); priv->lastCheck = QDateTime::fromString(last_check.toString(), Qt::ISODate);
@ -73,15 +75,16 @@ PrismExternalUpdater::PrismExternalUpdater(QWidget* parent, const QString& appDi
priv->parent = parent; priv->parent = parent;
connectTimer(); connectTimer();
resetAutoCheckTimer(); resetAutoCheckTimer();
if (priv->updateInterval == 0) { // "On Launch" if (priv->updateInterval == 0) { // "On Launch"
checkForUpdates(false); checkForUpdates(false);
} }
} }
PrismExternalUpdater::~PrismExternalUpdater() PrismExternalUpdater::~PrismExternalUpdater()
{ {
if (priv->updateTimer.isActive()) if (priv->updateTimer.isActive()) {
priv->updateTimer.stop(); priv->updateTimer.stop();
}
disconnectTimer(); disconnectTimer();
priv->settings->sync(); priv->settings->sync();
delete priv; delete priv;
@ -115,8 +118,9 @@ void PrismExternalUpdater::checkForUpdates(bool triggeredByUser)
#endif #endif
QStringList args = { "--check-only", "--dir", priv->dataDir.absolutePath(), "--debug" }; QStringList args = { "--check-only", "--dir", priv->dataDir.absolutePath(), "--debug" };
if (priv->allowBeta) if (priv->allowBeta) {
args.append("--pre-release"); args.append("--pre-release");
}
proc.start(priv->appDir.absoluteFilePath(exe_name), args); proc.start(priv->appDir.absoluteFilePath(exe_name), args);
auto result_start = proc.waitForStarted(5000); auto result_start = proc.waitForStarted(5000);
@ -268,20 +272,24 @@ void PrismExternalUpdater::setBetaAllowed(bool allowed)
void PrismExternalUpdater::resetAutoCheckTimer() void PrismExternalUpdater::resetAutoCheckTimer()
{ {
if (priv->autoCheck && priv->updateInterval > 0) { if (priv->autoCheck && priv->updateInterval > 0) {
int timeoutDuration = 0;
auto now = QDateTime::currentDateTime(); auto now = QDateTime::currentDateTime();
qint64 timeoutMs = 0;
if (priv->lastCheck.isValid()) { if (priv->lastCheck.isValid()) {
auto diff = priv->lastCheck.secsTo(now); qint64 diff = priv->lastCheck.secsTo(now);
auto secs_left = priv->updateInterval - diff; qint64 secs_left = std::max<qint64>(priv->updateInterval - diff, 0);
if (secs_left < 0) timeoutMs = secs_left * 1000;
secs_left = 0;
timeoutDuration = secs_left * 1000; // to msec
} }
qDebug() << "Auto update timer starting," << timeoutDuration / 1000 << "seconds left";
priv->updateTimer.start(timeoutDuration); 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 { } else {
if (priv->updateTimer.isActive()) if (priv->updateTimer.isActive()) {
priv->updateTimer.stop(); priv->updateTimer.stop();
}
} }
} }
@ -332,7 +340,7 @@ void PrismExternalUpdater::offerUpdate(const QString& version_name, const QStrin
priv->settings->sync(); priv->settings->sync();
return; return;
} }
case UpdateAvailableDialog::DontInstall: { default: {
return; return;
} }
} }
@ -353,10 +361,13 @@ void PrismExternalUpdater::performUpdate(const QString& version_tag)
#endif #endif
QStringList args = { "--dir", priv->dataDir.absolutePath(), "--install-version", version_tag }; QStringList args = { "--dir", priv->dataDir.absolutePath(), "--install-version", version_tag };
if (priv->allowBeta) if (priv->allowBeta) {
args.append("--pre-release"); args.append("--pre-release");
}
auto result = proc.startDetached(priv->appDir.absoluteFilePath(exe_name), args); proc.setProgram(priv->appDir.absoluteFilePath(exe_name));
proc.setArguments(args);
auto result = proc.startDetached();
if (!result) { if (!result) {
qDebug() << "Failed to start updater:" << proc.error() << proc.errorString(); qDebug() << "Failed to start updater:" << proc.error() << proc.errorString();
} }