2021-11-23 14:37:37 -05:00
|
|
|
#include "assetupdater.h"
|
|
|
|
|
|
|
|
#include <QFile>
|
2022-01-27 10:46:22 -05:00
|
|
|
#include <QJsonDocument>
|
|
|
|
#include <QJsonObject>
|
2022-02-23 19:05:53 -05:00
|
|
|
#include <QNetworkReply>
|
|
|
|
#include <QStandardPaths>
|
2022-02-25 18:08:57 -05:00
|
|
|
#include <QJsonArray>
|
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
|
|
|
|
2021-11-23 15:34:23 -05:00
|
|
|
#include "launchercore.h"
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
const QString baseGoatcorpDomain = "https://goatcorp.github.io";
|
|
|
|
|
|
|
|
const QString dalamudRemotePath = baseGoatcorpDomain + "/dalamud-distrib";
|
|
|
|
const QString dalamudVersion = "/latest";
|
|
|
|
const QString dalamudVersionPath = dalamudRemotePath + "/version";
|
|
|
|
|
|
|
|
const QString dalamudAssetRemotePath = baseGoatcorpDomain + "/DalamudAssets";
|
|
|
|
const QString dalamudAssetManifestPath = dalamudAssetRemotePath + "/asset.json";
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-02-23 19:05:53 -05:00
|
|
|
const QString nativeLauncherRemotePath =
|
|
|
|
"https://github.com/redstrate/nativelauncher/releases/download/";
|
2021-11-23 14:37:37 -05:00
|
|
|
const QString nativeLauncherVersion = "v1.0.0";
|
|
|
|
|
2022-02-23 19:05:53 -05:00
|
|
|
AssetUpdater::AssetUpdater(LauncherCore& launcher) : launcher(launcher) {
|
|
|
|
connect(launcher.mgr, &QNetworkAccessManager::finished, this,
|
|
|
|
&AssetUpdater::finishDownload);
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
launcher.mgr->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
|
|
|
|
}
|
|
|
|
|
2021-11-23 15:34:23 -05:00
|
|
|
void AssetUpdater::update(const ProfileSettings& profile) {
|
2022-02-25 18:08:57 -05:00
|
|
|
// non-dalamud users can bypass this process since it's not needed
|
2022-03-13 19:58:58 -04:00
|
|
|
if(!profile.dalamud.enabled) {
|
2022-02-25 18:08:57 -05:00
|
|
|
finishedUpdating();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-02-23 19:05:53 -05:00
|
|
|
const QString dataDir =
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-02-23 19:05:53 -05:00
|
|
|
qInfo() << "Starting update sequence...";
|
|
|
|
|
2022-01-27 10:46:22 -05:00
|
|
|
bool isDalamudUpdated = false;
|
2022-02-25 20:06:29 -05:00
|
|
|
if (remoteDalamudVersion.isEmpty()) {
|
|
|
|
QNetworkRequest request(dalamudVersionPath);
|
2022-01-27 10:46:22 -05:00
|
|
|
|
2022-02-25 20:06:29 -05:00
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
reply->setObjectName("DalamudVersionCheck");
|
|
|
|
currentSettings = &profile; // TODO: this is dirty, should change
|
2022-01-27 10:46:22 -05:00
|
|
|
|
2022-02-25 20:06:29 -05:00
|
|
|
return;
|
|
|
|
} else {
|
2022-02-25 22:25:21 -05:00
|
|
|
if (launcher.dalamudVersion != remoteDalamudVersion) {
|
2022-02-25 20:06:29 -05:00
|
|
|
isDalamudUpdated = false;
|
2022-01-27 10:46:22 -05:00
|
|
|
} else {
|
2022-02-25 20:06:29 -05:00
|
|
|
qInfo() << "No need to update Dalamud.";
|
|
|
|
isDalamudUpdated = true;
|
|
|
|
}
|
|
|
|
|
2022-02-25 22:25:21 -05:00
|
|
|
if(launcher.runtimeVersion != remoteRuntimeVersion) {
|
2022-02-25 20:06:29 -05:00
|
|
|
doneDownloadingRuntimeCore = false;
|
|
|
|
doneDownloadingRuntimeDesktop = false;
|
|
|
|
needsRuntimeInstall = true;
|
|
|
|
|
|
|
|
// core
|
|
|
|
{
|
|
|
|
QNetworkRequest request(
|
|
|
|
QString("https://dotnetcli.azureedge.net/dotnet/Runtime/%1/dotnet-runtime-%1-win-x64.zip")
|
|
|
|
.arg(remoteRuntimeVersion));
|
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
reply->setObjectName("Dotnet-core");
|
2022-01-27 10:46:22 -05:00
|
|
|
}
|
2022-02-25 20:06:29 -05:00
|
|
|
|
|
|
|
// desktop
|
|
|
|
{
|
|
|
|
QNetworkRequest request(
|
|
|
|
QString("https://dotnetcli.azureedge.net/dotnet/WindowsDesktop/%1/windowsdesktop-runtime-%1-win-x64.zip")
|
|
|
|
.arg(remoteRuntimeVersion));
|
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
reply->setObjectName("Dotnet-desktop");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
qInfo() << "No need to update Runtime.";
|
|
|
|
doneDownloadingRuntimeCore = true;
|
|
|
|
doneDownloadingRuntimeDesktop = true;
|
2022-01-27 10:46:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
{
|
|
|
|
qInfo() << "Checking Dalamud assets...";
|
|
|
|
|
|
|
|
// we want to prevent logging in before we actually check the version
|
|
|
|
dalamudAssetNeededFilenames.append("dummy");
|
|
|
|
|
|
|
|
// first we want to fetch the list of assets required
|
|
|
|
QNetworkRequest request(dalamudAssetManifestPath);
|
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
|
|
|
|
connect(reply, &QNetworkReply::finished, [reply, this, profile] {
|
|
|
|
// lol, they actually provide invalid json. let's fix it if it's borked
|
|
|
|
QString badJson = reply->readAll();
|
|
|
|
|
|
|
|
qInfo() << reply->errorString();
|
|
|
|
qInfo() << "Got asset manifest: " << badJson;
|
|
|
|
|
|
|
|
auto lastCommaLoc = badJson.lastIndexOf(',');
|
|
|
|
auto lastBracketLoc = badJson.lastIndexOf('{');
|
|
|
|
|
|
|
|
qInfo() << "Location of last comma: " << lastCommaLoc;
|
|
|
|
qInfo() << "Location of last bracket: " << lastBracketLoc;
|
|
|
|
|
|
|
|
// basically, if { supersedes the last ,
|
|
|
|
if (lastCommaLoc > lastBracketLoc) {
|
|
|
|
qInfo() << "Dalamud server gave bad json, attempting to fix...";
|
|
|
|
badJson.remove(lastCommaLoc, 1);
|
|
|
|
} else {
|
|
|
|
qInfo() << "Got valid json.";
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(badJson.toUtf8());
|
|
|
|
|
|
|
|
qInfo() << "Dalamud asset remote version" << doc.object()["Version"].toInt();
|
2022-02-25 22:25:21 -05:00
|
|
|
qInfo() << "Dalamud asset local version" << launcher.dalamudAssetVersion;
|
2022-02-25 18:08:57 -05:00
|
|
|
|
|
|
|
remoteDalamudAssetVersion = doc.object()["Version"].toInt();
|
|
|
|
|
2022-02-25 22:25:21 -05:00
|
|
|
if(remoteDalamudAssetVersion != launcher.dalamudAssetVersion) {
|
2022-02-25 18:08:57 -05:00
|
|
|
qInfo() << "Dalamud assets out of date.";
|
|
|
|
|
|
|
|
dalamudAssetNeededFilenames.clear();
|
|
|
|
|
|
|
|
for(auto assetObject : doc.object()["Assets"].toArray()) {
|
|
|
|
{
|
|
|
|
qInfo() << "Starting download for " << assetObject.toObject()["FileName"];
|
|
|
|
|
|
|
|
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 dataDir =
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/DalamudAssets/";
|
|
|
|
|
|
|
|
if (!QDir().exists(dataDir))
|
|
|
|
QDir().mkdir(dataDir);
|
|
|
|
|
|
|
|
const QString fileName = assetObject["FileName"].toString();
|
|
|
|
const QList<QString> dirPath = fileName.left(fileName.lastIndexOf("/")).split('/');
|
|
|
|
|
|
|
|
qInfo() << "Needed directories: " << dirPath;
|
|
|
|
|
|
|
|
QString build = dataDir;
|
|
|
|
for(auto dir : dirPath) {
|
|
|
|
if (!QDir().exists(build + dir))
|
|
|
|
QDir().mkdir(build + dir);
|
|
|
|
|
|
|
|
build += dir + "/";
|
|
|
|
}
|
|
|
|
|
|
|
|
QFile file(dataDir + assetObject["FileName"].toString());
|
|
|
|
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 22:25:21 -05:00
|
|
|
if(remoteDalamudVersion != launcher.dalamudVersion) {
|
2022-02-25 20:06:29 -05:00
|
|
|
qInfo() << "Downloading Dalamud...";
|
|
|
|
doneDownloadingDalamud = false;
|
|
|
|
needsDalamudInstall = true;
|
|
|
|
|
|
|
|
QNetworkRequest request(dalamudRemotePath + dalamudVersion +
|
|
|
|
".zip");
|
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
reply->setObjectName("Dalamud");
|
|
|
|
}
|
|
|
|
|
|
|
|
const bool hasNative = QFile::exists(dataDir + "/NativeLauncher.exe");
|
|
|
|
if (!hasNative) {
|
2022-02-23 19:05:53 -05:00
|
|
|
// download nativelauncher release (needed to launch the game with fixed
|
|
|
|
// ACLs)
|
2021-11-23 14:37:37 -05:00
|
|
|
{
|
2022-02-23 19:05:53 -05:00
|
|
|
qInfo() << "Downloading NativeLauncher...";
|
2022-02-25 18:08:57 -05:00
|
|
|
doneDownloadingNativelauncher = false;
|
2022-02-25 20:06:29 -05:00
|
|
|
needsNativeInstall = true;
|
2022-02-23 19:05:53 -05:00
|
|
|
|
|
|
|
QNetworkRequest request(nativeLauncherRemotePath +
|
|
|
|
nativeLauncherVersion +
|
|
|
|
"/NativeLauncher.exe");
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
auto reply = launcher.mgr->get(request);
|
|
|
|
reply->setObjectName("NativeLauncher");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetUpdater::finishDownload(QNetworkReply* reply) {
|
2022-02-23 19:05:53 -05:00
|
|
|
if (reply->objectName() == "Dalamud") {
|
|
|
|
qInfo() << "Dalamud finished downloading!";
|
|
|
|
|
|
|
|
QFile file(tempDir.path() + "/latest.zip");
|
2021-11-23 14:37:37 -05:00
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
doneDownloadingDalamud = true;
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-03-13 20:07:09 -04:00
|
|
|
launcher.dalamudVersion = remoteDalamudVersion;
|
|
|
|
|
2021-11-23 14:37:37 -05:00
|
|
|
checkIfFinished();
|
2022-02-23 19:05:53 -05:00
|
|
|
} else if (reply->objectName() == "NativeLauncher") {
|
|
|
|
qInfo() << "NativeLauncher finished downloading!";
|
|
|
|
|
2021-11-23 14:37:37 -05:00
|
|
|
QFile file(tempDir.path() + "/NativeLauncher.exe");
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
doneDownloadingNativelauncher = true;
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2021-11-23 14:37:37 -05:00
|
|
|
checkIfFinished();
|
2022-02-23 19:05:53 -05:00
|
|
|
} else if (reply->objectName() == "DalamudVersionCheck") {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
QJsonDocument doc = QJsonDocument::fromJson(reassmbled.toUtf8());
|
2022-01-27 10:46:22 -05:00
|
|
|
remoteDalamudVersion = doc["AssemblyVersion"].toString();
|
2022-02-25 20:06:29 -05:00
|
|
|
remoteRuntimeVersion = doc["RuntimeVersion"].toString();
|
2022-01-27 10:46:22 -05:00
|
|
|
|
2022-02-23 19:05:53 -05:00
|
|
|
qInfo() << "Latest Dalamud version reported: " << remoteDalamudVersion;
|
2022-02-25 20:06:29 -05:00
|
|
|
qInfo() << "Latest NET runtime reported: " << remoteRuntimeVersion;
|
2022-02-23 19:05:53 -05:00
|
|
|
|
2022-01-27 10:46:22 -05:00
|
|
|
update(*currentSettings);
|
|
|
|
currentSettings = nullptr;
|
2022-02-25 20:06:29 -05:00
|
|
|
} else if(reply->objectName() == "Dotnet-core") {
|
|
|
|
qInfo() << "Dotnet-core finished downloading!";
|
|
|
|
|
|
|
|
QFile file(tempDir.path() + "/dotnet-core.zip");
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
doneDownloadingRuntimeCore = true;
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
} else if(reply->objectName() == "Dotnet-desktop") {
|
|
|
|
qInfo() << "Dotnet-desktop finished downloading!";
|
|
|
|
|
|
|
|
QFile file(tempDir.path() + "/dotnet-desktop.zip");
|
|
|
|
file.open(QIODevice::WriteOnly);
|
|
|
|
file.write(reply->readAll());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
doneDownloadingRuntimeDesktop = true;
|
|
|
|
|
|
|
|
checkIfFinished();
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void AssetUpdater::beginInstall() {
|
2022-02-25 20:06:29 -05:00
|
|
|
const QString dataDir =
|
2022-02-23 19:05:53 -05:00
|
|
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
|
|
|
|
|
2022-02-25 20:06:29 -05:00
|
|
|
if(needsDalamudInstall) {
|
|
|
|
bool success = !JlCompress::extractDir(tempDir.path() + "/latest.zip",
|
|
|
|
dataDir + "/Dalamud")
|
|
|
|
.empty();
|
|
|
|
|
|
|
|
if(!success) {
|
|
|
|
// TODO: handle failure here
|
|
|
|
} else {
|
|
|
|
needsDalamudInstall = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(needsNativeInstall) {
|
2022-02-23 19:05:53 -05:00
|
|
|
QFile::copy(tempDir.path() + "/NativeLauncher.exe",
|
|
|
|
dataDir + "/NativeLauncher.exe");
|
2021-11-23 14:37:37 -05:00
|
|
|
|
2022-02-25 20:06:29 -05:00
|
|
|
needsNativeInstall = false;
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
2022-02-25 20:06:29 -05:00
|
|
|
|
|
|
|
if(needsRuntimeInstall) {
|
|
|
|
bool success = !JlCompress::extractDir(tempDir.path() + "/dotnet-core.zip",
|
|
|
|
dataDir + "/DalamudRuntime")
|
|
|
|
.empty();
|
|
|
|
|
|
|
|
success |= !JlCompress::extractDir(tempDir.path() + "/dotnet-desktop.zip",
|
|
|
|
dataDir + "/DalamudRuntime")
|
|
|
|
.empty();
|
|
|
|
|
|
|
|
if(!success) {
|
|
|
|
// TODO: handle failure here
|
|
|
|
} else {
|
|
|
|
QFile file(dataDir + "/DalamudRuntime/runtime.ver");
|
|
|
|
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
|
|
|
|
|
|
|
void AssetUpdater::checkIfDalamudAssetsDone() {
|
|
|
|
if(dalamudAssetNeededFilenames.empty()) {
|
|
|
|
qInfo() << "Finished downloading Dalamud assets.";
|
|
|
|
|
2022-03-13 20:07:09 -04:00
|
|
|
launcher.dalamudAssetVersion = remoteDalamudAssetVersion;
|
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
const QString dataDir =
|
|
|
|
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/DalamudAssets/";
|
|
|
|
|
|
|
|
QFile file(dataDir + "asset.ver");
|
|
|
|
file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
file.write(QString::number(remoteDalamudAssetVersion).toUtf8());
|
|
|
|
file.close();
|
|
|
|
|
|
|
|
checkIfFinished();
|
|
|
|
}
|
|
|
|
}
|
2022-02-25 20:06:29 -05:00
|
|
|
|
2022-02-25 18:08:57 -05:00
|
|
|
void AssetUpdater::checkIfFinished() {
|
|
|
|
if (doneDownloadingDalamud &&
|
|
|
|
doneDownloadingNativelauncher &&
|
2022-02-25 20:06:29 -05:00
|
|
|
doneDownloadingRuntimeCore &&
|
|
|
|
doneDownloadingRuntimeDesktop &&
|
2022-02-25 18:08:57 -05:00
|
|
|
dalamudAssetNeededFilenames.empty()) {
|
2022-02-25 20:06:29 -05:00
|
|
|
if(needsRuntimeInstall || needsNativeInstall || needsDalamudInstall)
|
2022-02-25 18:08:57 -05:00
|
|
|
beginInstall();
|
|
|
|
else
|
|
|
|
finishedUpdating();
|
|
|
|
}
|
|
|
|
}
|