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>
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
#include <quazip/JlCompress.h>
|
|
|
|
|
2021-11-23 15:34:23 -05:00
|
|
|
#include "launchercore.h"
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
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";
|
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-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...";
|
|
|
|
|
|
|
|
const bool hasDalamud = QFile::exists(dataDir + "/NativeLauncher.exe") &&
|
|
|
|
QFile::exists(dataDir + "/Dalamud");
|
2021-11-23 14:37:37 -05:00
|
|
|
|
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 21:28:56 -05:00
|
|
|
if (profile.dalamudVersion != remoteDalamudVersion) {
|
|
|
|
isDalamudUpdated = false;
|
|
|
|
} else {
|
|
|
|
qInfo() << "No need to update Dalamud.";
|
|
|
|
isDalamudUpdated = true;
|
2022-01-27 10:46:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-23 14:37:37 -05:00
|
|
|
// 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)
|
2021-11-23 14:37:37 -05:00
|
|
|
{
|
2022-02-23 19:05:53 -05:00
|
|
|
qInfo() << "Downloading NativeLauncher...";
|
|
|
|
|
|
|
|
QNetworkRequest request(nativeLauncherRemotePath +
|
|
|
|
nativeLauncherVersion +
|
|
|
|
"/NativeLauncher.exe");
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
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");
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
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")) {
|
2021-11-23 14:37:37 -05:00
|
|
|
beginInstall();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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();
|
|
|
|
|
|
|
|
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;
|
2021-11-23 14:37:37 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
2021-11-23 14:37:37 -05:00
|
|
|
|
|
|
|
finishedUpdating();
|
|
|
|
} else {
|
|
|
|
// STUB: install failure
|
|
|
|
}
|
2022-01-27 10:46:22 -05:00
|
|
|
}
|