1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-06-08 06:57:46 +00:00

Make Dalamud version check more robust

This commit is contained in:
Joshua Goins 2022-02-23 19:05:53 -05:00
parent 2e399ecff0
commit 2e7f7c5c20

View file

@ -1,10 +1,10 @@
#include "assetupdater.h" #include "assetupdater.h"
#include <QNetworkReply>
#include <QFile> #include <QFile>
#include <QStandardPaths>
#include <QJsonDocument> #include <QJsonDocument>
#include <QJsonObject> #include <QJsonObject>
#include <QNetworkReply>
#include <QStandardPaths>
#include <quazip/JlCompress.h> #include <quazip/JlCompress.h>
@ -12,25 +12,31 @@
const QString dalamudRemotePath = "https://goatcorp.github.io/dalamud-distrib/"; const QString dalamudRemotePath = "https://goatcorp.github.io/dalamud-distrib/";
const QString dalamudVersion = "latest"; const QString dalamudVersion = "latest";
const QString dalamudVersionPath = dalamudRemotePath + "/version"; const QString dalamudVersionPath = dalamudRemotePath + "version";
const QString nativeLauncherRemotePath = "https://github.com/redstrate/nativelauncher/releases/download/"; const QString nativeLauncherRemotePath =
"https://github.com/redstrate/nativelauncher/releases/download/";
const QString nativeLauncherVersion = "v1.0.0"; const QString nativeLauncherVersion = "v1.0.0";
AssetUpdater::AssetUpdater(LauncherCore &launcher) : launcher(launcher) { AssetUpdater::AssetUpdater(LauncherCore& launcher) : launcher(launcher) {
connect(launcher.mgr, &QNetworkAccessManager::finished, this, &AssetUpdater::finishDownload); connect(launcher.mgr, &QNetworkAccessManager::finished, this,
&AssetUpdater::finishDownload);
launcher.mgr->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy); launcher.mgr->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
} }
void AssetUpdater::update(const ProfileSettings& profile) { void AssetUpdater::update(const ProfileSettings& profile) {
QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); const QString dataDir =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
const bool hasDalamud = QFile::exists(dataDir + "/NativeLauncher.exe") && QFile::exists(dataDir + "/Dalamud"); qInfo() << "Starting update sequence...";
const bool hasDalamud = QFile::exists(dataDir + "/NativeLauncher.exe") &&
QFile::exists(dataDir + "/Dalamud");
bool isDalamudUpdated = false; bool isDalamudUpdated = false;
if(hasDalamud) { if (hasDalamud) {
if(remoteDalamudVersion.isEmpty()) { if (remoteDalamudVersion.isEmpty()) {
QNetworkRequest request(dalamudVersionPath); QNetworkRequest request(dalamudVersionPath);
auto reply = launcher.mgr->get(request); auto reply = launcher.mgr->get(request);
@ -39,31 +45,44 @@ void AssetUpdater::update(const ProfileSettings& profile) {
return; return;
} else { } else {
if(QFile::exists(dataDir + "/Dalamud/Dalamud.deps.json")) { if (QFile::exists(dataDir + "/Dalamud/Dalamud.deps.json")) {
QFile depsJson(dataDir + "/Dalamud/Dalamud.deps.json"); QFile depsJson(dataDir + "/Dalamud/Dalamud.deps.json");
depsJson.open(QFile::ReadOnly); depsJson.open(QFile::ReadOnly);
QJsonDocument doc = QJsonDocument::fromJson(depsJson.readAll()); QJsonDocument doc = QJsonDocument::fromJson(depsJson.readAll());
// TODO: UGLY // TODO: UGLY
QString versionString = doc["targets"].toObject()[".NETCoreApp,Version=v5.0"].toObject().keys().filter("Dalamud")[0]; QString versionString =
doc["targets"]
.toObject()[".NETCoreApp,Version=v5.0"]
.toObject()
.keys()
.filter("Dalamud")[0];
versionString = versionString.remove("Dalamud/"); versionString = versionString.remove("Dalamud/");
if(versionString != remoteDalamudVersion) { qInfo() << "Dalamud version installed: " << versionString;
if (versionString != remoteDalamudVersion) {
isDalamudUpdated = false; isDalamudUpdated = false;
} else { } else {
qInfo() << "No need to update Dalamud.";
isDalamudUpdated = true; isDalamudUpdated = true;
} }
} }
} }
} }
// first we determine if we need dalamud // first we determine if we need dalamud
const bool needsDalamud = profile.enableDalamud && (!hasDalamud || !isDalamudUpdated); const bool needsDalamud =
if(needsDalamud) { profile.enableDalamud && (!hasDalamud || !isDalamudUpdated);
// download nativelauncher release (needed to launch the game with fixed ACLs) if (needsDalamud) {
// download nativelauncher release (needed to launch the game with fixed
// ACLs)
{ {
QNetworkRequest request(nativeLauncherRemotePath + nativeLauncherVersion + "/NativeLauncher.exe"); qInfo() << "Downloading NativeLauncher...";
QNetworkRequest request(nativeLauncherRemotePath +
nativeLauncherVersion +
"/NativeLauncher.exe");
auto reply = launcher.mgr->get(request); auto reply = launcher.mgr->get(request);
reply->setObjectName("NativeLauncher"); reply->setObjectName("NativeLauncher");
@ -71,7 +90,10 @@ void AssetUpdater::update(const ProfileSettings& profile) {
// download dalamud (... duh) // download dalamud (... duh)
{ {
QNetworkRequest request(dalamudRemotePath + dalamudVersion + ".zip"); qInfo() << "Downloading Dalamud...";
QNetworkRequest request(dalamudRemotePath + dalamudVersion +
".zip");
auto reply = launcher.mgr->get(request); auto reply = launcher.mgr->get(request);
reply->setObjectName("Dalamud"); reply->setObjectName("Dalamud");
@ -84,40 +106,62 @@ void AssetUpdater::update(const ProfileSettings& profile) {
void AssetUpdater::finishDownload(QNetworkReply* reply) { void AssetUpdater::finishDownload(QNetworkReply* reply) {
const auto checkIfFinished = [=] { const auto checkIfFinished = [=] {
if(QFile::exists(tempDir.path() + "/NativeLauncher.exe") && QFile::exists(tempDir.path() + "/latest.zip")) { if (QFile::exists(tempDir.path() + "/NativeLauncher.exe") &&
QFile::exists(tempDir.path() + "/latest.zip")) {
beginInstall(); beginInstall();
} }
}; };
if(reply->objectName() == "Dalamud") { if (reply->objectName() == "Dalamud") {
QFile file(tempDir.path() + "/latest.zip"); qInfo() << "Dalamud finished downloading!";
QFile file(tempDir.path() + "/latest.zip");
file.open(QIODevice::WriteOnly); file.open(QIODevice::WriteOnly);
file.write(reply->readAll()); file.write(reply->readAll());
file.close(); file.close();
checkIfFinished(); checkIfFinished();
} else if(reply->objectName() == "NativeLauncher") { } else if (reply->objectName() == "NativeLauncher") {
qInfo() << "NativeLauncher finished downloading!";
QFile file(tempDir.path() + "/NativeLauncher.exe"); QFile file(tempDir.path() + "/NativeLauncher.exe");
file.open(QIODevice::WriteOnly); file.open(QIODevice::WriteOnly);
file.write(reply->readAll()); file.write(reply->readAll());
file.close(); file.close();
checkIfFinished(); checkIfFinished();
} else if(reply->objectName() == "DalamudVersionCheck") { } else if (reply->objectName() == "DalamudVersionCheck") {
QJsonDocument doc = QJsonDocument::fromJson(reply->readAll()); 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());
remoteDalamudVersion = doc["AssemblyVersion"].toString(); remoteDalamudVersion = doc["AssemblyVersion"].toString();
qInfo() << "Latest Dalamud version reported: " << remoteDalamudVersion;
update(*currentSettings); update(*currentSettings);
currentSettings = nullptr; currentSettings = nullptr;
} }
} }
void AssetUpdater::beginInstall() { void AssetUpdater::beginInstall() {
QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); QString dataDir =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
bool success = !JlCompress::extractDir(tempDir.path() + "/latest.zip", dataDir + "/Dalamud").empty(); bool success = !JlCompress::extractDir(tempDir.path() + "/latest.zip",
if(success) { dataDir + "/Dalamud")
QFile::copy(tempDir.path() + "/NativeLauncher.exe", dataDir + "/NativeLauncher.exe"); .empty();
if (success) {
QFile::copy(tempDir.path() + "/NativeLauncher.exe",
dataDir + "/NativeLauncher.exe");
finishedUpdating(); finishedUpdating();
} else { } else {