From 756f3194573e93d0c228561f10e9c093c80687fc Mon Sep 17 00:00:00 2001 From: Joshua Goins Date: Sat, 16 Sep 2023 19:02:59 -0400 Subject: [PATCH] Improve const correctness, use string literals in square enix login --- launcher/src/squareboot.cpp | 30 ++++---- launcher/src/squarelauncher.cpp | 118 ++++++++++++++++---------------- 2 files changed, 72 insertions(+), 76 deletions(-) diff --git a/launcher/src/squareboot.cpp b/launcher/src/squareboot.cpp index 9a76963..cc3930e 100644 --- a/launcher/src/squareboot.cpp +++ b/launcher/src/squareboot.cpp @@ -27,37 +27,34 @@ QCoro::Task<> SquareBoot::bootCheck(const LoginInformation &info) Q_EMIT window.stageChanged(i18n("Checking for launcher updates...")); qDebug() << "Performing boot check..."; - patcher = new Patcher(window, info.profile->gamePath() + "/boot", info.profile->bootData, this); + patcher = new Patcher(window, info.profile->gamePath() + QStringLiteral("/boot"), info.profile->bootData, this); connect(patcher, &Patcher::done, [this, &info] { info.profile->readGameVersion(); launcher.login(info); }); - QUrlQuery query; - query.addQueryItem("time", QDateTime::currentDateTimeUtc().toString("yyyy-MM-dd-HH-mm")); + const QUrlQuery query{{QStringLiteral("time"), QDateTime::currentDateTimeUtc().toString(QStringLiteral("yyyy-MM-dd-HH-mm"))}}; QUrl url; - url.setScheme("http"); + url.setScheme(QStringLiteral("http")); url.setHost(QStringLiteral("patch-bootver.%1").arg(window.squareEnixServer())); - url.setPath(QString("/http/win32/ffxivneo_release_boot/%1").arg(info.profile->bootVersion)); + url.setPath(QStringLiteral("/http/win32/ffxivneo_release_boot/%1").arg(info.profile->bootVersion)); url.setQuery(query); auto request = QNetworkRequest(url); if (info.profile->account()->license() == Account::GameLicense::macOS) { - request.setRawHeader("User-Agent", "FFXIV-MAC PATCH CLIENT"); + request.setRawHeader(QByteArrayLiteral("User-Agent"), QByteArrayLiteral("FFXIV-MAC PATCH CLIENT")); } else { - request.setRawHeader("User-Agent", "FFXIV PATCH CLIENT"); + request.setRawHeader(QByteArrayLiteral("User-Agent"), QByteArrayLiteral("FFXIV PATCH CLIENT")); } - request.setRawHeader("Host", QStringLiteral("patch-bootver.%1").arg(window.squareEnixServer()).toUtf8()); + request.setRawHeader(QByteArrayLiteral("Host"), QStringLiteral("patch-bootver.%1").arg(window.squareEnixServer()).toUtf8()); - auto reply = window.mgr->get(request); + const auto reply = window.mgr->get(request); co_await reply; - const QString response = reply->readAll(); - - patcher->processPatchList(*window.mgr, response); + patcher->processPatchList(*window.mgr, reply->readAll()); } QCoro::Task<> SquareBoot::checkGateStatus(LoginInformation *info) @@ -66,9 +63,9 @@ QCoro::Task<> SquareBoot::checkGateStatus(LoginInformation *info) qDebug() << "Checking gate..."; QUrl url; - url.setScheme("https"); + url.setScheme(QStringLiteral("https")); url.setHost(QStringLiteral("frontier.%1").arg(window.squareEnixServer())); - url.setPath("/worldStatus/gate_status.json"); + url.setPath(QStringLiteral("/worldStatus/gate_status.json")); url.setQuery(QString::number(QDateTime::currentMSecsSinceEpoch())); QNetworkRequest request(url); @@ -76,12 +73,11 @@ QCoro::Task<> SquareBoot::checkGateStatus(LoginInformation *info) // TODO: really? window.buildRequest(*info->profile, request); - auto reply = window.mgr->get(request); + const auto reply = window.mgr->get(request); co_await reply; const QJsonDocument document = QJsonDocument::fromJson(reply->readAll()); - - const bool isGateOpen = !document.isEmpty() && document.object()["status"].toInt() != 0; + const bool isGateOpen = !document.isEmpty() && document.object()[QLatin1String("status")].toInt() != 0; if (isGateOpen) { bootCheck(*info); diff --git a/launcher/src/squarelauncher.cpp b/launcher/src/squarelauncher.cpp index 404bca1..f5aa571 100644 --- a/launcher/src/squarelauncher.cpp +++ b/launcher/src/squarelauncher.cpp @@ -24,12 +24,12 @@ QString getFileHash(const QString &file) { auto f = QFile(file); if (!f.open(QIODevice::ReadOnly)) - return ""; + return {}; QCryptographicHash hash(QCryptographicHash::Sha1); hash.addData(&f); - return QString("%1/%2").arg(QString::number(f.size()), hash.result().toHex()); + return QStringLiteral("%1/%2").arg(QString::number(f.size()), hash.result().toHex()); } QCoro::Task> SquareLauncher::getStored(const LoginInformation &info) @@ -38,40 +38,40 @@ QCoro::Task> SquareLauncher::getStored QUrlQuery query; // en is always used to the top url - query.addQueryItem("lng", "en"); + query.addQueryItem(QStringLiteral("lng"), QStringLiteral("en")); // for some reason, we always use region 3. the actual region is acquired later - query.addQueryItem("rgn", "3"); - query.addQueryItem("isft", info.profile->account()->isFreeTrial() ? "1" : "0"); - query.addQueryItem("cssmode", "1"); - query.addQueryItem("isnew", "1"); - query.addQueryItem("launchver", "3"); + query.addQueryItem(QStringLiteral("rgn"), QStringLiteral("3")); + query.addQueryItem(QStringLiteral("isft"), info.profile->account()->isFreeTrial() ? QStringLiteral("1") : QStringLiteral("0")); + query.addQueryItem(QStringLiteral("cssmode"), QStringLiteral("1")); + query.addQueryItem(QStringLiteral("isnew"), QStringLiteral("1")); + query.addQueryItem(QStringLiteral("launchver"), QStringLiteral("3")); if (info.profile->account()->license() == Account::GameLicense::WindowsSteam) { - query.addQueryItem("issteam", "1"); + query.addQueryItem(QStringLiteral("issteam"), QStringLiteral("1")); // TODO: get steam ticket information from steam api - query.addQueryItem("session_ticket", "1"); - query.addQueryItem("ticket_size", "1"); + query.addQueryItem(QStringLiteral("session_ticket"), QStringLiteral("1")); + query.addQueryItem(QStringLiteral("ticket_size"), QStringLiteral("1")); } QUrl url; - url.setScheme("https"); + url.setScheme(QStringLiteral("https")); url.setHost(QStringLiteral("ffxiv-login.%1").arg(window.squareEnixLoginServer())); - url.setPath("/oauth/ffxivarr/login/top"); + url.setPath(QStringLiteral("/oauth/ffxivarr/login/top")); url.setQuery(query); auto request = QNetworkRequest(url); window.buildRequest(*info.profile, request); - auto reply = window.mgr->get(request); + const auto reply = window.mgr->get(request); co_await reply; - const auto str = QString(reply->readAll()); + const QString str = reply->readAll(); // fetches Steam username if (info.profile->account()->license() == Account::GameLicense::WindowsSteam) { - QRegularExpression re(R"lit(.*)""\/>)lit"); - QRegularExpressionMatch match = re.match(str); + const QRegularExpression re(QStringLiteral(R"lit(.*)""\/>)lit")); + const QRegularExpressionMatch match = re.match(str); if (match.hasMatch()) { username = match.captured(1); @@ -82,8 +82,8 @@ QCoro::Task> SquareLauncher::getStored username = info.username; } - QRegularExpression re(R"lit(\t<\s*input .* name="_STORED_" value="(?.*)">)lit"); - QRegularExpressionMatch match = re.match(str); + const QRegularExpression re(QStringLiteral(R"lit(\t<\s*input .* name="_STORED_" value="(?.*)">)lit")); + const QRegularExpressionMatch match = re.match(str); if (match.hasMatch()) { co_return StoredInfo{match.captured(1), url}; } else { @@ -102,34 +102,34 @@ QCoro::Task<> SquareLauncher::login(const LoginInformation &info) const auto [stored, referer] = *storedResult; QUrlQuery postData; - postData.addQueryItem("_STORED_", stored); - postData.addQueryItem("sqexid", info.username); - postData.addQueryItem("password", info.password); - postData.addQueryItem("otppw", info.oneTimePassword); + postData.addQueryItem(QStringLiteral("_STORED_"), stored); + postData.addQueryItem(QStringLiteral("sqexid"), info.username); + postData.addQueryItem(QStringLiteral("password"), info.password); + postData.addQueryItem(QStringLiteral("otppw"), info.oneTimePassword); QUrl url; - url.setScheme("https"); + url.setScheme(QStringLiteral("https")); url.setHost(QStringLiteral("ffxiv-login.%1").arg(window.squareEnixLoginServer())); - url.setPath("/oauth/ffxivarr/login/login.send"); + url.setPath(QStringLiteral("/oauth/ffxivarr/login/login.send")); QNetworkRequest request(url); window.buildRequest(*info.profile, request); - request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); - request.setRawHeader("Referer", referer.toEncoded()); - request.setRawHeader("Cache-Control", "no-cache"); + request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArrayLiteral("application/x-www-form-urlencoded")); + request.setRawHeader(QByteArrayLiteral("Referer"), referer.toEncoded()); + request.setRawHeader(QByteArrayLiteral("Cache-Control"), QByteArrayLiteral("no-cache")); - auto reply = window.mgr->post(request, postData.toString(QUrl::FullyEncoded).toUtf8()); + const auto reply = window.mgr->post(request, postData.toString(QUrl::FullyEncoded).toUtf8()); co_await reply; - auto str = QString(reply->readAll()); + const QString str = reply->readAll(); - QRegularExpression re(R"lit(window.external.user\("login=auth,ok,(?.*)\);)lit"); - QRegularExpressionMatch match = re.match(str); + const QRegularExpression re(QStringLiteral(R"lit(window.external.user\("login=auth,ok,(?.*)\);)lit")); + const QRegularExpressionMatch match = re.match(str); if (match.hasMatch()) { - const auto parts = match.captured(1).split(','); + const auto parts = match.captured(1).split(QLatin1Char(',')); - const bool terms = parts[3] == "1"; - const bool playable = parts[9] == "1"; + const bool terms = parts[3] == QLatin1String("1"); + const bool playable = parts[9] == QLatin1String("1"); if (!playable) { Q_EMIT window.loginError(i18n("Your account is unplayable. Check that you have the correct license, and a valid subscription.")); @@ -147,53 +147,48 @@ QCoro::Task<> SquareLauncher::login(const LoginInformation &info) registerSession(info); } else { - QRegularExpression re(R"lit(window.external.user\("login=auth,ng,err,(?.*)\);)lit"); - QRegularExpressionMatch match = re.match(str); - - const auto parts = match.captured(1).split(','); + const QRegularExpression re(QStringLiteral(R"lit(window.external.user\("login=auth,ng,err,(?.*)\);)lit")); + const QRegularExpressionMatch match = re.match(str); // there's a stray quote at the end of the error string, so let's remove that - QString errorStr = match.captured(1).chopped(1); - - Q_EMIT window.loginError(errorStr); + Q_EMIT window.loginError(match.captured(1).chopped(1)); } } QCoro::Task<> SquareLauncher::registerSession(const LoginInformation &info) { QUrl url; - url.setScheme("https"); + url.setScheme(QStringLiteral("https")); url.setHost(QStringLiteral("patch-gamever.%1").arg(window.squareEnixServer())); - url.setPath(QString("/http/win32/ffxivneo_release_game/%1/%2").arg(info.profile->repositories.repositories[0].version, SID)); + url.setPath(QStringLiteral("/http/win32/ffxivneo_release_game/%1/%2").arg(info.profile->repositories.repositories[0].version, SID)); auto request = QNetworkRequest(url); window.setSSL(request); - request.setRawHeader("X-Hash-Check", "enabled"); - request.setRawHeader("User-Agent", "FFXIV PATCH CLIENT"); - request.setHeader(QNetworkRequest::ContentTypeHeader, "application/x-www-form-urlencoded"); - - QString report = QString("%1=%2").arg(info.profile->bootVersion, getBootHash(info)); + request.setRawHeader(QByteArrayLiteral("X-Hash-Check"), QByteArrayLiteral("enabled")); + request.setRawHeader(QByteArrayLiteral("User-Agent"), QByteArrayLiteral("FFXIV PATCH CLIENT")); + request.setHeader(QNetworkRequest::ContentTypeHeader, QByteArrayLiteral("application/x-www-form-urlencoded")); + QString report = QStringLiteral("%1=%2").arg(info.profile->bootVersion, getBootHash(info)); for (int i = 1; i < auth.maxExpansion + 1; i++) { if (i <= static_cast(info.profile->repositories.repositories_count)) { - report += QString("\nex%1\t%2").arg(QString::number(i), info.profile->repositories.repositories[i].version); + report += QStringLiteral("\nex%1\t%2").arg(QString::number(i), info.profile->repositories.repositories[i].version); } else { - report += QString("\nex%1\t2012.01.01.0000.0000").arg(QString::number(i)); + report += QStringLiteral("\nex%1\t2012.01.01.0000.0000").arg(QString::number(i)); } } - auto reply = window.mgr->post(request, report.toUtf8()); + const auto reply = window.mgr->post(request, report.toUtf8()); co_await reply; if (reply->error() == QNetworkReply::NoError) { - if (reply->rawHeaderList().contains("X-Patch-Unique-Id")) { - QString body = reply->readAll(); + if (reply->rawHeaderList().contains(QByteArrayLiteral("X-Patch-Unique-Id"))) { + const QString body = reply->readAll(); - patcher = new Patcher(window, info.profile->gamePath() + "/game", info.profile->gameData, this); + patcher = new Patcher(window, info.profile->gamePath() + QStringLiteral("/game"), info.profile->gameData, this); connect(patcher, &Patcher::done, [this, &info, reply] { info.profile->readGameVersion(); - auth.SID = reply->rawHeader("X-Patch-Unique-Id"); + auth.SID = reply->rawHeader(QByteArrayLiteral("X-Patch-Unique-Id")); window.launchGame(*info.profile, auth); }); @@ -216,14 +211,19 @@ QCoro::Task<> SquareLauncher::registerSession(const LoginInformation &info) QString SquareLauncher::getBootHash(const LoginInformation &info) { - const QList fileList = {"ffxivboot.exe", "ffxivboot64.exe", "ffxivlauncher.exe", "ffxivlauncher64.exe", "ffxivupdater.exe", "ffxivupdater64.exe"}; + const QList fileList = {QStringLiteral("ffxivboot.exe"), + QStringLiteral("ffxivboot64.exe"), + QStringLiteral("ffxivlauncher.exe"), + QStringLiteral("ffxivlauncher64.exe"), + QStringLiteral("ffxivupdater.exe"), + QStringLiteral("ffxivupdater64.exe")}; QString result; for (int i = 0; i < fileList.count(); i++) { - result += fileList[i] + "/" + getFileHash(info.profile->gamePath() + "/boot/" + fileList[i]); + result += fileList[i] + QStringLiteral("/") + getFileHash(info.profile->gamePath() + QStringLiteral("/boot/") + fileList[i]); if (i != fileList.length() - 1) - result += ","; + result += QStringLiteral(","); } return result;