2023-08-05 22:14:05 -04:00
|
|
|
// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-11-23 14:37:37 -05:00
|
|
|
#include "assetupdater.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
2022-08-15 11:14:37 -04:00
|
|
|
#include <QJsonArray>
|
2022-01-27 10:46:22 -05:00
|
|
|
#include <QJsonDocument>
|
2022-02-23 19:05:53 -05:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QStandardPaths>
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-03-01 16:43:49 -05:00
|
|
|
#include <JlCompress.h>
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
const QString dotnetRuntimePackageURL = "https://dotnetcli.azureedge.net/dotnet/Runtime/%1/dotnet-runtime-%1-win-x64.zip";
|
|
|
|
const QString dotnetDesktopPackageURL = "https://dotnetcli.azureedge.net/dotnet/WindowsDesktop/%1/windowsdesktop-runtime-%1-win-x64.zip";
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
AssetUpdater::AssetUpdater(Profile &profile, LauncherCore &launcher, QObject *parent)
|
|
|
|
: QObject(parent)
|
|
|
|
, launcher(launcher)
|
|
|
|
, m_profile(profile)
|
|
|
|
{
|
2021-11-23 14:37:37 -05:00
|
|
|
launcher.mgr->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
2023-08-18 14:52:06 -04:00
|
|
|
dalamudDir = dataDir.absoluteFilePath("dalamud");
|
|
|
|
dalamudAssetDir = dalamudDir.absoluteFilePath("assets");
|
|
|
|
dalamudRuntimeDir = dalamudDir.absoluteFilePath("runtime");
|
|
|
|
|
|
|
|
const auto createIfNeeded = [](const QDir &dir) {
|
|
|
|
if (!QDir().exists(dir.absolutePath()))
|
|
|
|
QDir().mkdir(dir.absolutePath());
|
|
|
|
};
|
|
|
|
|
|
|
|
createIfNeeded(dataDir);
|
|
|
|
createIfNeeded(dalamudDir);
|
|
|
|
createIfNeeded(dalamudAssetDir);
|
|
|
|
createIfNeeded(dalamudRuntimeDir);
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void AssetUpdater::update()
|
|
|
|
{
|
2022-02-25 18:08:57 -05:00
|
|
|
// non-dalamud users can bypass this process since it's not needed
|
2023-07-30 08:49:34 -04:00
|
|
|
if (!m_profile.dalamudEnabled()) {
|
2023-07-31 19:23:12 -04:00
|
|
|
Q_EMIT finishedUpdating();
|
2022-02-25 18:08:57 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Updating assets...");
|
2022-03-13 20:48:43 -04:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
// first, we want to collect all of the remote versions
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-02-23 19:05:53 -05:00
|
|
|
qInfo() << "Starting update sequence...";
|
2023-07-30 08:49:34 -04:00
|
|
|
// dialog->setLabelText("Checking for updates...");
|
2022-02-23 19:05:53 -05:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
// dalamud assets
|
|
|
|
{
|
2022-02-25 18:08:57 -05:00
|
|
|
// we want to prevent logging in before we actually check the version
|
2022-03-13 20:48:43 -04:00
|
|
|
dalamudAssetNeededFilenames.clear();
|
|
|
|
remoteDalamudAssetVersion = -1;
|
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
dalamudAssetNeededFilenames.append("dummy");
|
|
|
|
|
|
|
|
// first we want to fetch the list of assets required
|
2023-08-18 21:36:29 -04:00
|
|
|
QNetworkRequest request(dalamudAssetManifestUrl());
|
2022-02-25 18:08:57 -05:00
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
2023-07-30 08:49:34 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, [reply, this] {
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Checking for Dalamud asset updates...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2022-03-28 10:33:00 -04:00
|
|
|
// TODO: handle asset failure
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
2022-02-25 18:08:57 -05:00
|
|
|
|
|
|
|
qInfo() << "Dalamud asset remote version" << doc.object()["Version"].toInt();
|
2023-07-30 08:49:34 -04:00
|
|
|
qInfo() << "Dalamud asset local version" << m_profile.dalamudAssetVersion;
|
2022-02-25 18:08:57 -05:00
|
|
|
|
|
|
|
remoteDalamudAssetVersion = doc.object()["Version"].toInt();
|
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
remoteDalamudAssetArray = doc.object()["Assets"].toArray();
|
2022-02-25 18:08:57 -05:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
checkIfCheckingIsDone();
|
2022-02-25 18:08:57 -05:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-09-05 16:51:46 -04:00
|
|
|
// dalamud injector / net runtime
|
2022-03-13 20:44:57 -04:00
|
|
|
// they're all updated in unison, so there's no reason to have multiple checks
|
|
|
|
{
|
2023-08-18 21:36:29 -04:00
|
|
|
QNetworkRequest request(dalamudVersionManifestUrl(m_profile.dalamudChannel()));
|
2022-04-13 10:45:00 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
chosenChannel = m_profile.dalamudChannel();
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-03-13 20:48:43 -04:00
|
|
|
remoteDalamudVersion.clear();
|
|
|
|
remoteRuntimeVersion.clear();
|
|
|
|
|
2022-02-25 20:06:29 -05:00
|
|
|
auto reply = launcher.mgr->get(request);
|
2023-07-30 08:49:34 -04:00
|
|
|
connect(reply, &QNetworkReply::finished, [this, reply] {
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Checking for Dalamud updates...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
QByteArray str = reply->readAll();
|
|
|
|
// for some god forsaken reason, the version string comes back as raw
|
|
|
|
// bytes, ex: \xFF\xFE{\x00\"\x00""A\x00s\x00s\x00""e\x00m\x00 so we
|
|
|
|
// start at the first character of the json '{' and work our way up.
|
|
|
|
QString reassmbled;
|
|
|
|
for (int i = str.indexOf('{'); i < str.size(); i++) {
|
|
|
|
char t = str[i];
|
|
|
|
if (QChar(t).isPrint())
|
|
|
|
reassmbled += t;
|
|
|
|
}
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(reassmbled.toUtf8());
|
|
|
|
remoteDalamudVersion = doc["AssemblyVersion"].toString();
|
|
|
|
remoteRuntimeVersion = doc["RuntimeVersion"].toString();
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
qInfo() << "Latest Dalamud version reported: " << remoteDalamudVersion;
|
|
|
|
qInfo() << "Latest NET runtime reported: " << remoteRuntimeVersion;
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
checkIfCheckingIsDone();
|
|
|
|
});
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void AssetUpdater::beginInstall()
|
|
|
|
{
|
2022-08-15 11:14:37 -04:00
|
|
|
if (needsDalamudInstall) {
|
2023-08-18 14:52:06 -04:00
|
|
|
bool success = !JlCompress::extractDir(tempDir.path() + "/latest.zip", dalamudDir.absoluteFilePath(m_profile.dalamudChannelName())).empty();
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (!success) {
|
2022-02-25 20:06:29 -05:00
|
|
|
// TODO: handle failure here
|
2022-04-08 19:34:51 -04:00
|
|
|
qInfo() << "Failed to install Dalamud!";
|
2022-02-25 20:06:29 -05:00
|
|
|
} else {
|
|
|
|
needsDalamudInstall = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (needsRuntimeInstall) {
|
2023-08-18 14:52:06 -04:00
|
|
|
bool success = !JlCompress::extractDir(tempDir.path() + "/dotnet-core.zip", dalamudRuntimeDir.absolutePath()).empty();
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-08-18 14:52:06 -04:00
|
|
|
success |= !JlCompress::extractDir(tempDir.path() + "/dotnet-desktop.zip", dalamudRuntimeDir.absolutePath()).empty();
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
if (!success) {
|
2022-04-08 19:34:51 -04:00
|
|
|
qInfo() << "Failed to install dotnet!";
|
2022-02-25 20:06:29 -05:00
|
|
|
} else {
|
2023-08-18 14:52:06 -04:00
|
|
|
QFile file(dalamudRuntimeDir.absoluteFilePath("runtime.ver"));
|
2022-02-25 20:06:29 -05:00
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
file.write(remoteRuntimeVersion.toUtf8());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
needsRuntimeInstall = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
checkIfFinished();
|
2022-01-27 10:46:22 -05:00
|
|
|
}
|
2022-02-25 18:08:57 -05:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void AssetUpdater::checkIfDalamudAssetsDone()
|
|
|
|
{
|
2022-08-15 11:14:37 -04:00
|
|
|
if (dalamudAssetNeededFilenames.empty()) {
|
2022-02-25 18:08:57 -05:00
|
|
|
qInfo() << "Finished downloading Dalamud assets.";
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
m_profile.dalamudAssetVersion = remoteDalamudAssetVersion;
|
2022-03-13 20:07:09 -04:00
|
|
|
|
2023-08-18 14:52:06 -04:00
|
|
|
QFile file(dalamudAssetDir.absoluteFilePath("asset.ver"));
|
2022-02-25 18:08:57 -05:00
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
file.write(QString::number(remoteDalamudAssetVersion).toUtf8());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void AssetUpdater::checkIfFinished()
|
|
|
|
{
|
|
|
|
if (doneDownloadingDalamud && doneDownloadingRuntimeCore && doneDownloadingRuntimeDesktop && dalamudAssetNeededFilenames.empty()) {
|
2022-09-05 16:51:46 -04:00
|
|
|
if (needsRuntimeInstall || needsDalamudInstall) {
|
2022-02-25 18:08:57 -05:00
|
|
|
beginInstall();
|
2022-03-13 20:44:57 -04:00
|
|
|
} else {
|
2023-07-31 19:23:12 -04:00
|
|
|
Q_EMIT finishedUpdating();
|
2022-03-13 20:44:57 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 20:48:43 -04:00
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
void AssetUpdater::checkIfCheckingIsDone()
|
|
|
|
{
|
2022-09-05 16:51:46 -04:00
|
|
|
if (remoteDalamudVersion.isEmpty() || remoteRuntimeVersion.isEmpty() || remoteDalamudAssetVersion == -1) {
|
2022-03-13 20:44:57 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that we got all the information we need, let's check if anything is
|
|
|
|
// updateable
|
|
|
|
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Starting Dalamud update...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
// dalamud injector / net runtime
|
2023-07-30 08:49:34 -04:00
|
|
|
if (m_profile.runtimeVersion != remoteRuntimeVersion) {
|
2022-03-13 20:44:57 -04:00
|
|
|
needsRuntimeInstall = true;
|
|
|
|
|
|
|
|
// core
|
|
|
|
{
|
|
|
|
QNetworkRequest request(dotnetRuntimePackageURL.arg(remoteRuntimeVersion));
|
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
connect(reply, &QNetworkReply::finished, [this, reply] {
|
|
|
|
qInfo() << "Dotnet-core finished downloading!";
|
|
|
|
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Updating Dotnet-core...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
QFile file(tempDir.path() + "/dotnet-core.zip");
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
doneDownloadingRuntimeCore = true;
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// desktop
|
|
|
|
{
|
|
|
|
QNetworkRequest request(dotnetDesktopPackageURL.arg(remoteRuntimeVersion));
|
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
connect(reply, &QNetworkReply::finished, [this, reply] {
|
|
|
|
qInfo() << "Dotnet-desktop finished downloading!";
|
|
|
|
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Updating Dotnet-desktop...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
QFile file(tempDir.path() + "/dotnet-desktop.zip");
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
doneDownloadingRuntimeDesktop = true;
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
doneDownloadingRuntimeCore = true;
|
|
|
|
doneDownloadingRuntimeDesktop = true;
|
|
|
|
needsRuntimeInstall = false;
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
}
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
if (remoteDalamudVersion != m_profile.dalamudVersion) {
|
2022-03-13 20:44:57 -04:00
|
|
|
qInfo() << "Downloading Dalamud...";
|
2022-04-08 19:34:51 -04:00
|
|
|
|
2022-03-13 20:44:57 -04:00
|
|
|
needsDalamudInstall = true;
|
|
|
|
|
2023-08-18 21:36:29 -04:00
|
|
|
QNetworkRequest request(dalamudLatestPackageUrl(chosenChannel));
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
connect(reply, &QNetworkReply::finished, [this, reply] {
|
|
|
|
qInfo() << "Dalamud finished downloading!";
|
|
|
|
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Updating Dalamud...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
QFile file(tempDir.path() + "/latest.zip");
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
doneDownloadingDalamud = true;
|
|
|
|
|
2023-07-30 08:49:34 -04:00
|
|
|
m_profile.dalamudVersion = remoteDalamudVersion;
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
qInfo() << "No need to update Dalamud.";
|
|
|
|
|
|
|
|
doneDownloadingDalamud = true;
|
|
|
|
needsDalamudInstall = false;
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
}
|
|
|
|
|
|
|
|
// dalamud assets
|
2023-07-30 08:49:34 -04:00
|
|
|
if (remoteDalamudAssetVersion != m_profile.dalamudAssetVersion) {
|
2022-03-13 20:44:57 -04:00
|
|
|
qInfo() << "Dalamud assets out of date.";
|
|
|
|
|
2023-07-30 16:19:51 -04:00
|
|
|
Q_EMIT launcher.stageChanged("Updating Dalamud assets...");
|
2022-03-13 20:44:57 -04:00
|
|
|
|
|
|
|
dalamudAssetNeededFilenames.clear();
|
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
for (auto assetObject : remoteDalamudAssetArray) {
|
2022-03-13 20:44:57 -04:00
|
|
|
{
|
|
|
|
dalamudAssetNeededFilenames.append(assetObject.toObject()["FileName"].toString());
|
|
|
|
|
|
|
|
QNetworkRequest assetRequest(assetObject.toObject()["Url"].toString());
|
|
|
|
auto assetReply = launcher.mgr->get(assetRequest);
|
|
|
|
|
|
|
|
connect(assetReply, &QNetworkReply::finished, [this, assetReply, assetObject = assetObject.toObject()] {
|
|
|
|
const QString fileName = assetObject["FileName"].toString();
|
2023-08-18 14:52:06 -04:00
|
|
|
const QString dirPath = fileName.left(fileName.lastIndexOf("/"));
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-08-18 14:52:06 -04:00
|
|
|
const QString path = dalamudAssetDir.absoluteFilePath(dirPath);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-08-18 14:52:06 -04:00
|
|
|
if (!QDir().exists(path))
|
|
|
|
QDir().mkpath(path);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-08-18 14:52:06 -04:00
|
|
|
QFile file(dalamudAssetDir.absoluteFilePath(assetObject["FileName"].toString()));
|
2022-03-13 20:44:57 -04:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(assetReply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
dalamudAssetNeededFilenames.removeOne(assetObject["FileName"].toString());
|
|
|
|
checkIfDalamudAssetsDone();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
dalamudAssetNeededFilenames.clear();
|
|
|
|
|
|
|
|
qInfo() << "Dalamud assets up to date.";
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
}
|
2022-02-25 18:08:57 -05:00
|
|
|
}
|
2023-08-18 21:36:29 -04:00
|
|
|
|
|
|
|
static const QMap<Profile::DalamudChannel, QString> channelToDistribPrefix = {{Profile::DalamudChannel::Stable, "/"},
|
|
|
|
{Profile::DalamudChannel::Staging, "stg/"},
|
|
|
|
{Profile::DalamudChannel::Net5, "net5/"}};
|
|
|
|
|
|
|
|
QUrl AssetUpdater::dalamudVersionManifestUrl(const Profile::DalamudChannel channel) const
|
|
|
|
{
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme("https");
|
|
|
|
url.setHost(launcher.dalamudDistribServer());
|
|
|
|
url.setPath(QStringLiteral("/dalamud-distrib/%1version").arg(channelToDistribPrefix[channel]));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl AssetUpdater::dalamudLatestPackageUrl(Profile::DalamudChannel channel) const
|
|
|
|
{
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme("https");
|
|
|
|
url.setHost(launcher.dalamudDistribServer());
|
|
|
|
url.setPath(QStringLiteral("/dalamud-distrib/%1latest.zip").arg(channelToDistribPrefix[channel]));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl AssetUpdater::dalamudAssetManifestUrl() const
|
|
|
|
{
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme("https");
|
|
|
|
url.setHost(launcher.dalamudDistribServer());
|
|
|
|
url.setPath(QStringLiteral("/DalamudAssets/asset.json"));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|