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

462 lines
14 KiB
C++
Raw Normal View History

// SPDX-FileCopyrightText: 2023 Joshua Goins <josh@redstrate.com>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "gameinstaller.h"
2023-09-17 08:31:24 -04:00
#include <KLocalizedString>
#include <QDir>
#include <QImage>
#include <QNetworkAccessManager>
#include <QStandardPaths>
#include <algorithm>
2023-09-16 18:37:42 -04:00
#include <qcoronetworkreply.h>
#include "account.h"
#include "assetupdater.h"
#include "astra_log.h"
#include "benchmarkinstaller.h"
#include "compatibilitytoolinstaller.h"
#include "gamerunner.h"
#include "launchercore.h"
#include "sapphirelogin.h"
#include "squareenixlogin.h"
#include "utility.h"
using namespace Qt::StringLiterals;
2023-10-11 14:49:24 -04:00
LauncherCore::LauncherCore()
: QObject()
{
2023-10-11 14:49:24 -04:00
m_settings = new LauncherSettings(this);
m_mgr = new QNetworkAccessManager(this);
m_sapphireLogin = new SapphireLogin(*this, this);
m_squareEnixLogin = new SquareEnixLogin(*this, this);
m_profileManager = new ProfileManager(*this, this);
m_accountManager = new AccountManager(*this, this);
m_runner = new GameRunner(*this, this);
m_profileManager->load();
m_accountManager->load();
// restore profile -> account connections
for (auto profile : m_profileManager->profiles()) {
if (auto account = m_accountManager->getByUuid(profile->accountUuid())) {
profile->setAccount(account);
}
}
// set default profile, if found
if (auto profile = m_profileManager->getProfileByUUID(m_settings->currentProfile())) {
setCurrentProfile(profile);
}
m_loadingFinished = true;
Q_EMIT loadingFinished();
}
2023-10-11 14:49:24 -04:00
void LauncherCore::initializeSteam()
{
2023-10-11 14:49:24 -04:00
m_steamApi = new SteamAPI(this);
m_steamApi->setLauncherMode(true);
}
void LauncherCore::login(Profile *profile, const QString &username, const QString &password, const QString &oneTimePassword)
{
Q_ASSERT(profile != nullptr);
auto loginInformation = new LoginInformation(this);
loginInformation->profile = profile;
// Benchmark never has to login, of course
if (!profile->isBenchmark()) {
loginInformation->username = username;
loginInformation->password = password;
loginInformation->oneTimePassword = oneTimePassword;
if (profile->account()->rememberPassword()) {
profile->account()->setPassword(password);
}
}
beginLogin(*loginInformation);
}
2023-10-08 13:21:13 -04:00
bool LauncherCore::autoLogin(Profile *profile)
{
2023-10-08 17:52:44 -04:00
Q_ASSERT(profile != nullptr);
2023-10-06 18:09:50 -04:00
QString otp;
if (profile->account()->useOTP()) {
if (!profile->account()->rememberOTP()) {
Q_EMIT loginError(i18n("This account does not have an OTP secret set, but requires it for login."));
2023-10-08 13:21:13 -04:00
return false;
2023-10-06 18:09:50 -04:00
}
otp = profile->account()->getOTP();
if (otp.isEmpty()) {
Q_EMIT loginError(i18n("Failed to generate OTP, review the stored secret."));
2023-10-08 13:21:13 -04:00
return false;
2023-10-06 18:09:50 -04:00
}
}
login(profile, profile->account()->name(), profile->account()->getPassword(), otp);
2023-10-08 13:21:13 -04:00
return true;
}
void LauncherCore::immediatelyLaunch(Profile *profile)
{
Q_ASSERT(profile != nullptr);
m_runner->beginGameExecutable(*profile, std::nullopt);
}
GameInstaller *LauncherCore::createInstaller(Profile *profile)
{
Q_ASSERT(profile != nullptr);
2023-10-08 17:52:44 -04:00
return new GameInstaller(*this, *profile, this);
}
GameInstaller *LauncherCore::createInstallerFromExisting(Profile *profile, const QString &filePath)
{
Q_ASSERT(profile != nullptr);
return new GameInstaller(*this, *profile, filePath, this);
}
CompatibilityToolInstaller *LauncherCore::createCompatInstaller()
{
return new CompatibilityToolInstaller(*this, this);
}
BenchmarkInstaller *LauncherCore::createBenchmarkInstaller(Profile *profile)
{
Q_ASSERT(profile != nullptr);
return new BenchmarkInstaller(*this, *profile, this);
}
BenchmarkInstaller *LauncherCore::createBenchmarkInstallerFromExisting(Profile *profile, const QString &filePath)
{
Q_ASSERT(profile != nullptr);
return new BenchmarkInstaller(*this, *profile, filePath, this);
}
2023-10-11 14:49:24 -04:00
void LauncherCore::clearAvatarCache()
{
const QString cacheLocation = QStandardPaths::standardLocations(QStandardPaths::CacheLocation)[0] + QStringLiteral("/avatars");
2023-10-11 14:49:24 -04:00
if (QDir(cacheLocation).exists()) {
QDir(cacheLocation).removeRecursively();
}
}
2023-10-11 14:49:24 -04:00
void LauncherCore::refreshNews()
{
2023-10-11 14:49:24 -04:00
fetchNews();
}
void LauncherCore::refreshLogoImage()
{
const QDir cacheDir = QStandardPaths::standardLocations(QStandardPaths::StandardLocation::CacheLocation).last();
const QDir logoDir = cacheDir.absoluteFilePath(QStringLiteral("logos"));
if (!logoDir.exists()) {
QDir().mkpath(logoDir.absolutePath());
}
const auto saveTexture = [](GameData *data, const QString &path, const QString &name) {
if (QFile::exists(name)) {
return;
}
auto file = physis_gamedata_extract_file(data, path.toStdString().c_str());
if (file.data != nullptr) {
auto tex = physis_texture_parse(file);
QImage image(tex.rgba, tex.width, tex.height, QImage::Format_RGBA8888);
image.save(name);
}
};
// TODO: this finds the first profile that has a valid image, but this could probably be cached per-profile
for (int i = 0; i < m_profileManager->numProfiles(); i++) {
auto profile = m_profileManager->getProfile(i);
if (profile->isGameInstalled() && profile->gameData()) {
// A Realm Reborn
saveTexture(profile->gameData(), QStringLiteral("ui/uld/Title_Logo.tex"), logoDir.absoluteFilePath(QStringLiteral("ffxiv.png")));
for (int j = 0; j < profile->numInstalledExpansions(); j++) {
const int expansionNumber = 100 * (j + 3); // logo number starts at 300 for ex1
saveTexture(profile->gameData(),
QStringLiteral("ui/uld/Title_Logo%1_hr1.tex").arg(expansionNumber),
logoDir.absoluteFilePath(QStringLiteral("ex%1.png").arg(j + 1)));
}
}
}
QList<QString> imageFiles;
// TODO: sort
QDirIterator it(logoDir.absolutePath());
while (it.hasNext()) {
const QFileInfo logoFile(it.next());
if (logoFile.completeSuffix() != QStringLiteral("png")) {
continue;
}
imageFiles.push_back(logoFile.absoluteFilePath());
}
if (!imageFiles.isEmpty()) {
m_cachedLogoImage = imageFiles.last();
Q_EMIT cachedLogoImageChanged();
}
}
2023-10-11 14:49:24 -04:00
Profile *LauncherCore::currentProfile() const
{
2023-10-11 14:49:24 -04:00
return m_profileManager->getProfile(m_currentProfileIndex);
}
void LauncherCore::setCurrentProfile(Profile *profile)
{
Q_ASSERT(profile != nullptr);
const int newIndex = m_profileManager->getProfileIndex(profile->uuid());
if (newIndex != m_currentProfileIndex) {
m_currentProfileIndex = newIndex;
m_settings->setCurrentProfile(profile->uuid());
2023-10-11 14:49:24 -04:00
m_settings->config()->save();
Q_EMIT currentProfileChanged();
}
}
2023-09-17 19:20:41 -04:00
[[nodiscard]] QString LauncherCore::autoLoginProfileName() const
{
return m_settings->config()->autoLoginProfile();
2023-09-17 19:20:41 -04:00
}
[[nodiscard]] Profile *LauncherCore::autoLoginProfile() const
{
if (m_settings->config()->autoLoginProfile().isEmpty()) {
2023-09-17 19:20:41 -04:00
return nullptr;
}
return m_profileManager->getProfileByUUID(m_settings->config()->autoLoginProfile());
2023-09-17 19:20:41 -04:00
}
void LauncherCore::setAutoLoginProfile(Profile *profile)
{
if (profile != nullptr) {
auto uuid = profile->uuid();
if (uuid != m_settings->config()->autoLoginProfile()) {
m_settings->config()->setAutoLoginProfile(uuid);
}
} else {
m_settings->config()->setAutoLoginProfile({});
2023-09-17 19:20:41 -04:00
}
m_settings->config()->save();
Q_EMIT autoLoginProfileChanged();
2023-09-17 19:20:41 -04:00
}
2023-10-11 14:49:24 -04:00
void LauncherCore::buildRequest(const Profile &settings, QNetworkRequest &request)
2023-09-16 18:37:42 -04:00
{
2023-12-17 13:05:37 -05:00
Utility::setSSL(request);
2023-10-11 14:49:24 -04:00
if (settings.account()->license() == Account::GameLicense::macOS) {
request.setHeader(QNetworkRequest::UserAgentHeader, QByteArrayLiteral("macSQEXAuthor/2.0.0(MacOSX; ja-jp)"));
} else {
request.setHeader(QNetworkRequest::UserAgentHeader,
QStringLiteral("SQEXAuthor/2.0.0(Windows 6.2; ja-jp; %1)").arg(QString::fromUtf8(QSysInfo::bootUniqueId())));
2023-10-11 14:49:24 -04:00
}
request.setRawHeader(QByteArrayLiteral("Accept"),
QByteArrayLiteral("image/gif, image/jpeg, image/pjpeg, application/x-ms-application, application/xaml+xml, "
"application/x-ms-xbap, */*"));
request.setRawHeader(QByteArrayLiteral("Accept-Encoding"), QByteArrayLiteral("gzip, deflate"));
request.setRawHeader(QByteArrayLiteral("Accept-Language"), QByteArrayLiteral("en-us"));
}
void LauncherCore::setupIgnoreSSL(QNetworkReply *reply)
{
Q_ASSERT(reply != nullptr);
if (m_settings->preferredProtocol() == QStringLiteral("http")) {
connect(reply, &QNetworkReply::sslErrors, this, [reply](const QList<QSslError> &errors) {
reply->ignoreSslErrors(errors);
});
}
}
bool LauncherCore::isLoadingFinished() const
{
return m_loadingFinished;
}
bool LauncherCore::isSteam() const
{
return m_steamApi != nullptr;
}
bool LauncherCore::isSteamDeck() const
{
return qEnvironmentVariable("SteamDeck") == QStringLiteral("1");
2023-10-11 14:49:24 -04:00
}
bool LauncherCore::isWindows()
{
#if defined(Q_OS_WIN)
return true;
#else
return false;
#endif
}
bool LauncherCore::needsCompatibilityTool()
{
return !isWindows();
}
bool LauncherCore::isPatching() const
{
return m_isPatching;
}
2023-10-11 14:49:24 -04:00
QNetworkAccessManager *LauncherCore::mgr()
{
return m_mgr;
}
LauncherSettings *LauncherCore::settings()
{
return m_settings;
}
ProfileManager *LauncherCore::profileManager()
{
return m_profileManager;
}
AccountManager *LauncherCore::accountManager()
{
return m_accountManager;
}
Headline *LauncherCore::headline() const
{
return m_headline;
}
QString LauncherCore::cachedLogoImage() const
{
return m_cachedLogoImage;
}
2023-10-11 14:49:24 -04:00
QCoro::Task<> LauncherCore::beginLogin(LoginInformation &info)
{
// Hmm, I don't think we're set up for this yet?
if (!info.profile->isBenchmark()) {
info.profile->account()->updateConfig();
}
2023-10-11 14:49:24 -04:00
std::optional<LoginAuth> auth;
if (!info.profile->isBenchmark()) {
if (info.profile->account()->isSapphire()) {
auth = co_await m_sapphireLogin->login(info.profile->account()->lobbyUrl(), info);
} else {
auth = co_await m_squareEnixLogin->login(&info);
2023-10-11 14:49:24 -04:00
}
}
2023-10-11 14:49:24 -04:00
auto assetUpdater = new AssetUpdater(*info.profile, *this, this);
if (co_await assetUpdater->update()) {
// If we expect an auth ticket, don't continue if missing
if (!info.profile->isBenchmark() && auth == std::nullopt) {
co_return;
}
2023-10-11 14:49:24 -04:00
Q_EMIT stageChanged(i18n("Launching game..."));
2023-10-11 14:49:24 -04:00
if (isSteam()) {
m_steamApi->setLauncherMode(false);
2023-10-11 14:49:24 -04:00
}
m_runner->beginGameExecutable(*info.profile, auth);
2023-10-11 14:49:24 -04:00
}
assetUpdater->deleteLater();
2023-09-16 18:37:42 -04:00
}
QCoro::Task<> LauncherCore::fetchNews()
{
qInfo(ASTRA_LOG) << "Fetching news...";
QUrlQuery query;
2023-09-17 08:31:24 -04:00
query.addQueryItem(QStringLiteral("lang"), QStringLiteral("en-us"));
query.addQueryItem(QStringLiteral("media"), QStringLiteral("pcapp"));
QUrl url;
url.setScheme(m_settings->preferredProtocol());
url.setHost(QStringLiteral("frontier.%1").arg(m_settings->squareEnixServer()));
2023-09-17 08:31:24 -04:00
url.setPath(QStringLiteral("/news/headline.json"));
url.setQuery(query);
QNetworkRequest request(QUrl(QStringLiteral("%1&%2").arg(url.toString(), QString::number(QDateTime::currentMSecsSinceEpoch()))));
2023-09-17 08:31:24 -04:00
request.setRawHeader(QByteArrayLiteral("Accept"), QByteArrayLiteral("application/json, text/plain, */*"));
request.setRawHeader(QByteArrayLiteral("Origin"), QByteArrayLiteral("https://launcher.finalfantasyxiv.com"));
request.setRawHeader(QByteArrayLiteral("Referer"),
QStringLiteral("https://launcher.finalfantasyxiv.com/v600/index.html?rc_lang=%1&time=%2")
.arg(QStringLiteral("en-us"), QDateTime::currentDateTimeUtc().toString(QStringLiteral("yyyy-MM-dd-HH")))
.toUtf8());
Utility::printRequest(QStringLiteral("GET"), request);
auto reply = mgr()->get(request);
2023-09-16 18:37:42 -04:00
co_await reply;
2023-09-16 18:37:42 -04:00
auto document = QJsonDocument::fromJson(reply->readAll());
2023-09-16 18:37:42 -04:00
auto headline = new Headline(this);
if (document.isEmpty()) {
headline->failedToLoad = true;
}
2023-09-16 18:37:42 -04:00
const auto parseNews = [](QJsonObject object) -> News {
News news;
news.date = QDateTime::fromString(object["date"_L1].toString(), Qt::DateFormat::ISODate);
news.id = object["id"_L1].toString();
news.tag = object["tag"_L1].toString();
news.title = object["title"_L1].toString();
if (object["url"_L1].toString().isEmpty()) {
2023-09-17 08:31:24 -04:00
news.url = QUrl(QStringLiteral("https://na.finalfantasyxiv.com/lodestone/news/detail/%1").arg(news.id));
2023-09-16 18:37:42 -04:00
} else {
news.url = QUrl(object["url"_L1].toString());
}
2023-09-16 18:37:42 -04:00
return news;
};
for (const auto bannerObject : document.object()["banner"_L1].toArray()) {
headline->banners.push_back(
{.link = QUrl(bannerObject.toObject()["link"_L1].toString()), .bannerImage = QUrl(bannerObject.toObject()["lsb_banner"_L1].toString())});
2023-09-16 18:37:42 -04:00
}
for (const auto newsObject : document.object()["news"_L1].toArray()) {
headline->news.push_back(parseNews(newsObject.toObject()));
2023-09-16 18:37:42 -04:00
}
for (const auto pinnedObject : document.object()["pinned"_L1].toArray()) {
headline->pinned.push_back(parseNews(pinnedObject.toObject()));
2023-09-16 18:37:42 -04:00
}
for (const auto pinnedObject : document.object()["topics"_L1].toArray()) {
headline->topics.push_back(parseNews(pinnedObject.toObject()));
2023-09-16 18:37:42 -04:00
}
m_headline = headline;
Q_EMIT newsChanged();
}
#include "moc_launchercore.cpp"