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"
|
2023-10-08 18:02:02 -04:00
|
|
|
#include "utility.h"
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
#include <KLocalizedString>
|
2021-11-23 14:37:37 -05:00
|
|
|
#include <QFile>
|
2022-01-27 10:46:22 -05:00
|
|
|
#include <QJsonDocument>
|
2022-02-23 19:05:53 -05:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QStandardPaths>
|
2023-09-17 18:43:58 -04:00
|
|
|
#include <qcorofuture.h>
|
|
|
|
#include <qcoronetworkreply.h>
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-03-01 16:43:49 -05:00
|
|
|
#include <JlCompress.h>
|
2023-09-17 18:43:58 -04:00
|
|
|
#include <QtConcurrentRun>
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const QString dotnetRuntimePackageURL = QStringLiteral("https://dotnetcli.azureedge.net/dotnet/Runtime/%1/dotnet-runtime-%1-win-x64.zip");
|
|
|
|
const QString dotnetDesktopPackageURL = QStringLiteral("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)
|
2023-09-17 09:02:11 -04:00
|
|
|
, chosenChannel(profile.dalamudChannel())
|
2023-07-30 08:49:34 -04:00
|
|
|
, m_profile(profile)
|
|
|
|
{
|
2021-11-23 14:37:37 -05:00
|
|
|
launcher.mgr->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
|
2023-09-17 18:43:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
QCoro::Task<> AssetUpdater::update()
|
|
|
|
{
|
|
|
|
if (!m_profile.dalamudEnabled()) {
|
|
|
|
co_return;
|
|
|
|
}
|
|
|
|
|
|
|
|
qInfo() << "Starting asset update sequence...";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2022-08-15 11:14:37 -04:00
|
|
|
dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
2023-09-17 18:43:58 -04:00
|
|
|
dalamudDir = dataDir.absoluteFilePath(QStringLiteral("dalamud"));
|
|
|
|
dalamudAssetDir = dalamudDir.absoluteFilePath(QStringLiteral("assets"));
|
|
|
|
dalamudRuntimeDir = dalamudDir.absoluteFilePath(QStringLiteral("runtime"));
|
2023-08-18 14:52:06 -04:00
|
|
|
|
|
|
|
const auto createIfNeeded = [](const QDir &dir) {
|
|
|
|
if (!QDir().exists(dir.absolutePath()))
|
2023-09-17 18:43:58 -04:00
|
|
|
QDir().mkpath(dir.absolutePath());
|
2023-08-18 14:52:06 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
createIfNeeded(dalamudDir);
|
|
|
|
createIfNeeded(dalamudAssetDir);
|
|
|
|
createIfNeeded(dalamudRuntimeDir);
|
2023-09-17 18:43:58 -04:00
|
|
|
|
|
|
|
co_await checkRemoteDalamudAssetVersion();
|
|
|
|
co_await checkRemoteDalamudVersion();
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QCoro::Task<> AssetUpdater::checkRemoteDalamudAssetVersion()
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2023-09-17 18:43:58 -04:00
|
|
|
// first we want to fetch the list of assets required
|
|
|
|
const QNetworkRequest request(dalamudAssetManifestUrl());
|
2023-10-08 18:02:02 -04:00
|
|
|
Utility::printRequest(QStringLiteral("GET"), request);
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto reply = launcher.mgr->get(request);
|
|
|
|
co_await reply;
|
2022-02-23 19:05:53 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
// TODO: handle asset failure
|
|
|
|
const QJsonDocument doc = QJsonDocument::fromJson(reply->readAll());
|
2022-02-25 18:08:57 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
remoteDalamudAssetVersion = doc.object()[QLatin1String("Version")].toInt();
|
|
|
|
remoteDalamudAssetArray = doc.object()[QLatin1String("Assets")].toArray();
|
2022-02-25 18:08:57 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Dalamud asset remote version" << remoteDalamudAssetVersion;
|
|
|
|
qInfo() << "Dalamud asset local version" << m_profile.dalamudAssetVersion();
|
2022-02-25 18:08:57 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
// dalamud assets
|
|
|
|
if (remoteDalamudAssetVersion != m_profile.dalamudAssetVersion()) {
|
|
|
|
qInfo() << "Dalamud assets out of date.";
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
co_await installDalamudAssets();
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QCoro::Task<> AssetUpdater::checkRemoteDalamudVersion()
|
2023-07-30 08:49:34 -04:00
|
|
|
{
|
2023-09-17 18:43:58 -04:00
|
|
|
const QNetworkRequest request(dalamudVersionManifestUrl(m_profile.dalamudChannel()));
|
2023-10-08 18:02:02 -04:00
|
|
|
Utility::printRequest(QStringLiteral("GET"), request);
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
remoteDalamudVersion.clear();
|
|
|
|
remoteRuntimeVersion.clear();
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto reply = launcher.mgr->get(request);
|
|
|
|
co_await reply;
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
if (reply->error() != QNetworkReply::NetworkError::NoError) {
|
|
|
|
Q_EMIT launcher.loginError(i18n("Could not check for Dalamud updates.\n\n%1", reply->errorString()));
|
|
|
|
co_return;
|
2022-02-25 20:06:29 -05:00
|
|
|
}
|
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const QByteArray str = reply->readAll();
|
2022-02-25 18:08:57 -05:00
|
|
|
|
2023-10-08 13:19:23 -04:00
|
|
|
// for some godforsaken reason, the version string comes back as raw
|
2023-09-17 18:43:58 -04:00
|
|
|
// 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 = static_cast<int>(str.indexOf('{')); i < str.size(); i++) {
|
|
|
|
char t = str[i];
|
|
|
|
if (QChar(t).isPrint())
|
|
|
|
reassmbled += t;
|
2022-02-25 18:08:57 -05:00
|
|
|
}
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const QJsonDocument doc = QJsonDocument::fromJson(reassmbled.toUtf8());
|
|
|
|
remoteDalamudVersion = doc[QLatin1String("AssemblyVersion")].toString();
|
|
|
|
remoteRuntimeVersion = doc[QLatin1String("RuntimeVersion")].toString();
|
2022-03-13 20:48:43 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Latest Dalamud version reported:" << remoteDalamudVersion << "local:" << m_profile.dalamudVersion();
|
|
|
|
qInfo() << "Latest NET runtime reported:" << remoteRuntimeVersion;
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
if (remoteDalamudVersion != m_profile.dalamudVersion()) {
|
|
|
|
qInfo() << "Downloading Dalamud...";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
co_await installDalamud();
|
|
|
|
}
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 08:51:26 -04:00
|
|
|
if (m_profile.runtimeVersion() != remoteRuntimeVersion) {
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Downloading Runtime...";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
co_await installRuntime();
|
|
|
|
}
|
|
|
|
}
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QCoro::Task<> AssetUpdater::installDalamudAssets()
|
|
|
|
{
|
|
|
|
Q_EMIT launcher.stageChanged(i18n("Updating Dalamud assets..."));
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QFutureSynchronizer<void> synchronizer;
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
for (const auto &assetObject : remoteDalamudAssetArray) {
|
|
|
|
const QNetworkRequest assetRequest(assetObject.toObject()[QLatin1String("Url")].toString());
|
2023-10-08 18:02:02 -04:00
|
|
|
Utility::printRequest(QStringLiteral("GET"), assetRequest);
|
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto assetReply = launcher.mgr->get(assetRequest);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto future = QtFuture::connect(assetReply, &QNetworkReply::finished).then([this, assetReply, assetObject] {
|
|
|
|
const QString fileName = assetObject.toObject()[QLatin1String("FileName")].toString();
|
|
|
|
const QString dirPath = fileName.left(fileName.lastIndexOf(QLatin1Char('/')));
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const QString path = dalamudAssetDir.absoluteFilePath(dirPath);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
if (!QDir().exists(path))
|
|
|
|
QDir().mkpath(path);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QFile file(dalamudAssetDir.absoluteFilePath(assetObject.toObject()[QLatin1String("FileName")].toString()));
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(assetReply->readAll());
|
|
|
|
file.close();
|
|
|
|
});
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
synchronizer.addFuture(future);
|
2022-03-13 20:44:57 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
co_await QtConcurrent::run([&synchronizer] {
|
|
|
|
synchronizer.waitForFinished();
|
|
|
|
});
|
2022-04-08 19:34:51 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Finished downloading Dalamud assets.";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
m_profile.setDalamudAssetVersion(remoteDalamudAssetVersion);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QFile file(dalamudAssetDir.absoluteFilePath(QStringLiteral("asset.ver")));
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
file.write(QString::number(remoteDalamudAssetVersion).toUtf8());
|
|
|
|
file.close();
|
|
|
|
}
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QCoro::Task<> AssetUpdater::installDalamud()
|
|
|
|
{
|
|
|
|
Q_EMIT launcher.stageChanged(i18n("Updating Dalamud..."));
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const QNetworkRequest request(dalamudLatestPackageUrl(chosenChannel));
|
2023-10-08 18:02:02 -04:00
|
|
|
Utility::printRequest(QStringLiteral("GET"), request);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto reply = launcher.mgr->get(request);
|
|
|
|
co_await reply;
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Dalamud finished downloading!";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QFile file(tempDir.path() + QStringLiteral("/latest.zip"));
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const bool success =
|
|
|
|
!JlCompress::extractDir(tempDir.path() + QLatin1String("/latest.zip"), dalamudDir.absoluteFilePath(m_profile.dalamudChannelName())).empty();
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
if (!success) {
|
|
|
|
// TODO: handle failure here
|
|
|
|
qInfo() << "Failed to install Dalamud!";
|
2022-03-13 20:44:57 -04:00
|
|
|
}
|
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
m_profile.setDalamudVersion(remoteDalamudVersion);
|
|
|
|
}
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QCoro::Task<> AssetUpdater::installRuntime()
|
|
|
|
{
|
|
|
|
Q_EMIT launcher.stageChanged(i18n("Updating .NET Runtime..."));
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
// core
|
|
|
|
{
|
|
|
|
const QNetworkRequest request(dotnetRuntimePackageURL.arg(remoteRuntimeVersion));
|
2023-10-08 18:02:02 -04:00
|
|
|
Utility::printRequest(QStringLiteral("GET"), request);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto reply = launcher.mgr->get(request);
|
|
|
|
co_await reply;
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Dotnet-core finished downloading!";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QFile file(tempDir.path() + QStringLiteral("/dotnet-core.zip"));
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
}
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
// desktop
|
|
|
|
{
|
|
|
|
const QNetworkRequest request(dotnetDesktopPackageURL.arg(remoteRuntimeVersion));
|
2023-10-08 18:02:02 -04:00
|
|
|
Utility::printRequest(QStringLiteral("GET"), request);
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
const auto reply = launcher.mgr->get(request);
|
|
|
|
co_await reply;
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
qInfo() << "Dotnet-desktop finished downloading!";
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
QFile file(tempDir.path() + QStringLiteral("/dotnet-desktop.zip"));
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
}
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
bool success = !JlCompress::extractDir(tempDir.path() + QStringLiteral("/dotnet-core.zip"), dalamudRuntimeDir.absolutePath()).empty();
|
|
|
|
success |= !JlCompress::extractDir(tempDir.path() + QStringLiteral("/dotnet-desktop.zip"), dalamudRuntimeDir.absolutePath()).empty();
|
2022-03-13 20:44:57 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
if (!success) {
|
|
|
|
qInfo() << "Failed to install dotnet!";
|
|
|
|
} else {
|
|
|
|
QFile file(dalamudRuntimeDir.absoluteFilePath(QStringLiteral("runtime.ver")));
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
file.write(remoteRuntimeVersion.toUtf8());
|
|
|
|
file.close();
|
2022-03-13 20:44:57 -04:00
|
|
|
}
|
2022-02-25 18:08:57 -05:00
|
|
|
}
|
2023-08-18 21:36:29 -04:00
|
|
|
|
2023-09-17 18:43:58 -04:00
|
|
|
static const QMap<Profile::DalamudChannel, QString> channelToDistribPrefix = {{Profile::DalamudChannel::Stable, QStringLiteral("/")},
|
|
|
|
{Profile::DalamudChannel::Staging, QStringLiteral("stg/")},
|
|
|
|
{Profile::DalamudChannel::Net5, QStringLiteral("net5/")}};
|
2023-08-18 21:36:29 -04:00
|
|
|
|
|
|
|
QUrl AssetUpdater::dalamudVersionManifestUrl(const Profile::DalamudChannel channel) const
|
|
|
|
{
|
|
|
|
QUrl url;
|
2023-10-06 18:14:32 -04:00
|
|
|
url.setScheme(launcher.preferredProtocol());
|
2023-08-18 21:36:29 -04:00
|
|
|
url.setHost(launcher.dalamudDistribServer());
|
|
|
|
url.setPath(QStringLiteral("/dalamud-distrib/%1version").arg(channelToDistribPrefix[channel]));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl AssetUpdater::dalamudLatestPackageUrl(Profile::DalamudChannel channel) const
|
|
|
|
{
|
|
|
|
QUrl url;
|
2023-10-06 18:14:32 -04:00
|
|
|
url.setScheme(launcher.preferredProtocol());
|
2023-08-18 21:36:29 -04:00
|
|
|
url.setHost(launcher.dalamudDistribServer());
|
|
|
|
url.setPath(QStringLiteral("/dalamud-distrib/%1latest.zip").arg(channelToDistribPrefix[channel]));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|
|
|
|
|
|
|
|
QUrl AssetUpdater::dalamudAssetManifestUrl() const
|
|
|
|
{
|
|
|
|
QUrl url;
|
2023-10-06 18:14:32 -04:00
|
|
|
url.setScheme(launcher.preferredProtocol());
|
2023-08-18 21:36:29 -04:00
|
|
|
url.setHost(launcher.dalamudDistribServer());
|
|
|
|
url.setPath(QStringLiteral("/DalamudAssets/asset.json"));
|
|
|
|
|
|
|
|
return url;
|
|
|
|
}
|