1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-07-17 01:32:21 +02:00

Implemented auto-update

This commit is contained in:
nordsoft
2022-08-25 05:09:20 +04:00
parent 28063cfcf3
commit 744afdbde2
3 changed files with 91 additions and 1 deletions

View File

@ -363,7 +363,7 @@
"type" : "object",
"default": {},
"additionalProperties" : false,
"required" : [ "repositoryURL", "enableInstalledMods", "autoCheckRepositories", "updateOnStartup" ],
"required" : [ "repositoryURL", "enableInstalledMods", "autoCheckRepositories", "updateOnStartup", "updateConfigUrl" ],
"properties" : {
"repositoryURL" : {
"type" : "array",
@ -385,6 +385,10 @@
"updateOnStartup" : {
"type" : "boolean",
"default" : true
},
"updateConfigUrl" : {
"type" : "string",
"default" : "https://raw.githubusercontent.com/Nordsoft91/vcmi-autoupdate/main/autoUpdate.json"
}
}
}

View File

@ -2,6 +2,11 @@
#include "ui_updatedialog_moc.h"
#include "../lib/CConfigHandler.h"
#include "../lib/GameConstants.h"
#include <QNetworkAccessManager>
#include <QNetworkReply>
#include <QNetworkRequest>
UpdateDialog::UpdateDialog(QWidget *parent) :
QDialog(parent),
@ -13,6 +18,49 @@ UpdateDialog::UpdateDialog(QWidget *parent) :
if(settings["launcher"]["updateOnStartup"].Bool() == true)
ui->checkOnStartup->setCheckState(Qt::CheckState::Checked);
currentVersion = QString::fromStdString(GameConstants::VCMI_VERSION);
ui->currentVersion->setText(currentVersion);
#ifdef VCMI_WINDOWS
platformParameter = "windows";
#elif defined(VCMI_MAC)
platformParameter = "macos";
#elif defined(VCMI_IOS)
platformParameter = "ios";
#elif defined(VCMI_ANDROID)
platformParameter = "android";
#elif define(VCMI_UNIX)
platformParameter = "linux";
#endif
QString url = QString::fromStdString(settings["launcher"]["updateConfigUrl"].String());
QScopedPointer<QNetworkAccessManager> manager(new QNetworkAccessManager);
QNetworkReply *response = manager->get(QNetworkRequest(QUrl(url)));
QObject::connect(response, &QNetworkReply::finished, [&, response]{
response->deleteLater();
response->manager()->deleteLater();
if(response->error() != QNetworkReply::NoError)
{
ui->plainTextEdit->setPlainText(response->errorString());
return;
}
//auto const contentType = response->header(QNetworkRequest::ContentTypeHeader).toString();
//htmlContent = contentType;
auto byteArray = response->readAll();
JsonNode node(byteArray.constData(), byteArray.size());
loadFromJson(node);
//QString html;
//for(int i = 0; i < 200 && response->isReadable(); ++i) //just limit somehow to avoid long queries
//html += QString::fromUtf8(response->readLine(256));
}) && manager.take();
}
UpdateDialog::~UpdateDialog()
@ -35,3 +83,34 @@ void UpdateDialog::on_checkOnStartup_stateChanged(int state)
node->Bool() = (state == 2 ? true : false);
}
void UpdateDialog::loadFromJson(const JsonNode & node)
{
if(node.getType() != JsonNode::JsonType::DATA_STRUCT ||
node["updateType"].getType() != JsonNode::JsonType::DATA_STRING ||
node["version"].getType() != JsonNode::JsonType::DATA_STRING ||
node["changeLog"].getType() != JsonNode::JsonType::DATA_STRING ||
node.getType() != JsonNode::JsonType::DATA_STRUCT) //we need at least one link - other are optional
{
ui->plainTextEdit->setPlainText("Cannot read JSON from url or incorrect JSON data");
return;
}
if(node["updateType"].String() == "minor")
ui->versionLabel->setStyleSheet("QLabel { background-color : gray; color : black; }");
if(node["updateType"].String() == "major")
ui->versionLabel->setStyleSheet("QLabel { background-color : yellow; color : black; }");
if(node["updateType"].String() == "critical")
ui->versionLabel->setStyleSheet("QLabel { background-color : red; color : black; }");
ui->versionLabel->setText(QString::fromStdString(node["version"].String()));
ui->plainTextEdit->setPlainText(QString::fromStdString(node["changeLog"].String()));
QString downloadLink = QString::fromStdString(node["downloadLinks"]["other"].String());
if(node["downloadLinks"][platformParameter].getType() == JsonNode::JsonType::DATA_STRING)
downloadLink = QString::fromStdString(node["downloadLinks"][platformParameter].String());
QString downloadHtml("<a href=\"");
downloadHtml += downloadLink + "\">link</a>";
ui->downloadLink->setText(downloadHtml);
}

View File

@ -3,6 +3,8 @@
#include <QDialog>
class JsonNode;
namespace Ui {
class UpdateDialog;
}
@ -22,6 +24,11 @@ private slots:
private:
Ui::UpdateDialog *ui;
QString currentVersion;
std::string platformParameter = "other";
void loadFromJson(const JsonNode & node);
};
#endif // UPDATEDIALOG_MOC_H