1
Fork 0
mirror of https://github.com/redstrate/Astra.git synced 2025-04-20 11:47:46 +00:00
astra/src/assetupdater.cpp

171 lines
5.7 KiB
C++
Raw Normal View History

#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>
#include <quazip/JlCompress.h>
#include "launchercore.h"
const QString dalamudRemotePath = "https://goatcorp.github.io/dalamud-distrib/";
const QString dalamudVersion = "latest";
2022-02-23 19:05:53 -05:00
const QString dalamudVersionPath = dalamudRemotePath + "version";
2022-02-23 19:05:53 -05:00
const QString nativeLauncherRemotePath =
"https://github.com/redstrate/nativelauncher/releases/download/";
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);
launcher.mgr->setRedirectPolicy(QNetworkRequest::NoLessSafeRedirectPolicy);
}
void AssetUpdater::update(const ProfileSettings& profile) {
2022-02-23 19:05:53 -05:00
const QString dataDir =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
2022-02-23 19:05:53 -05:00
qInfo() << "Starting update sequence...";
const bool hasDalamud = QFile::exists(dataDir + "/NativeLauncher.exe") &&
QFile::exists(dataDir + "/Dalamud");
2022-01-27 10:46:22 -05:00
bool isDalamudUpdated = false;
2022-02-23 19:05:53 -05:00
if (hasDalamud) {
if (remoteDalamudVersion.isEmpty()) {
2022-01-27 10:46:22 -05:00
QNetworkRequest request(dalamudVersionPath);
auto reply = launcher.mgr->get(request);
reply->setObjectName("DalamudVersionCheck");
currentSettings = &profile; // TODO: this is dirty, should change
return;
} else {
2022-02-23 19:05:53 -05:00
if (QFile::exists(dataDir + "/Dalamud/Dalamud.deps.json")) {
2022-01-27 10:46:22 -05:00
QFile depsJson(dataDir + "/Dalamud/Dalamud.deps.json");
depsJson.open(QFile::ReadOnly);
QJsonDocument doc = QJsonDocument::fromJson(depsJson.readAll());
// TODO: UGLY
2022-02-23 19:05:53 -05:00
QString versionString =
doc["targets"]
.toObject()[".NETCoreApp,Version=v5.0"]
.toObject()
.keys()
.filter("Dalamud")[0];
2022-01-27 10:46:22 -05:00
versionString = versionString.remove("Dalamud/");
2022-02-23 19:05:53 -05:00
qInfo() << "Dalamud version installed: " << versionString;
if (versionString != remoteDalamudVersion) {
2022-01-27 10:46:22 -05:00
isDalamudUpdated = false;
} else {
2022-02-23 19:05:53 -05:00
qInfo() << "No need to update Dalamud.";
2022-01-27 10:46:22 -05:00
isDalamudUpdated = true;
}
}
}
}
// first we determine if we need dalamud
2022-02-23 19:05:53 -05:00
const bool needsDalamud =
profile.enableDalamud && (!hasDalamud || !isDalamudUpdated);
if (needsDalamud) {
// download nativelauncher release (needed to launch the game with fixed
// ACLs)
{
2022-02-23 19:05:53 -05:00
qInfo() << "Downloading NativeLauncher...";
QNetworkRequest request(nativeLauncherRemotePath +
nativeLauncherVersion +
"/NativeLauncher.exe");
auto reply = launcher.mgr->get(request);
reply->setObjectName("NativeLauncher");
}
// download dalamud (... duh)
{
2022-02-23 19:05:53 -05:00
qInfo() << "Downloading Dalamud...";
QNetworkRequest request(dalamudRemotePath + dalamudVersion +
".zip");
auto reply = launcher.mgr->get(request);
reply->setObjectName("Dalamud");
}
} else {
// non-dalamud users can bypass this process since it's not needed
finishedUpdating();
}
}
void AssetUpdater::finishDownload(QNetworkReply* reply) {
const auto checkIfFinished = [=] {
2022-02-23 19:05:53 -05:00
if (QFile::exists(tempDir.path() + "/NativeLauncher.exe") &&
QFile::exists(tempDir.path() + "/latest.zip")) {
beginInstall();
}
};
2022-02-23 19:05:53 -05:00
if (reply->objectName() == "Dalamud") {
qInfo() << "Dalamud finished downloading!";
QFile file(tempDir.path() + "/latest.zip");
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
file.close();
checkIfFinished();
2022-02-23 19:05:53 -05:00
} else if (reply->objectName() == "NativeLauncher") {
qInfo() << "NativeLauncher finished downloading!";
QFile file(tempDir.path() + "/NativeLauncher.exe");
file.open(QIODevice::WriteOnly);
file.write(reply->readAll());
file.close();
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-23 19:05:53 -05:00
qInfo() << "Latest Dalamud version reported: " << remoteDalamudVersion;
2022-01-27 10:46:22 -05:00
update(*currentSettings);
currentSettings = nullptr;
}
}
void AssetUpdater::beginInstall() {
2022-02-23 19:05:53 -05:00
QString dataDir =
QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
bool success = !JlCompress::extractDir(tempDir.path() + "/latest.zip",
dataDir + "/Dalamud")
.empty();
if (success) {
QFile::copy(tempDir.path() + "/NativeLauncher.exe",
dataDir + "/NativeLauncher.exe");
finishedUpdating();
} else {
// STUB: install failure
}
2022-01-27 10:46:22 -05:00
}