From f873cd5b1aa17fe8119f27cc5d9b9eba10bd42bf Mon Sep 17 00:00:00 2001 From: Sefa Eyeoglu Date: Mon, 8 Aug 2022 20:49:49 +0200 Subject: [PATCH] refactor: store current capabilities Signed-off-by: Sefa Eyeoglu --- launcher/Application.cpp | 11 ++++++----- launcher/Application.h | 7 ++++++- launcher/net/Download.cpp | 2 +- launcher/net/Upload.cpp | 2 +- launcher/ui/dialogs/ModDownloadDialog.cpp | 2 +- launcher/ui/dialogs/NewInstanceDialog.cpp | 2 +- launcher/ui/pages/global/AccountListPage.cpp | 2 +- 7 files changed, 17 insertions(+), 11 deletions(-) diff --git a/launcher/Application.cpp b/launcher/Application.cpp index 97d419931..710fa38d7 100644 --- a/launcher/Application.cpp +++ b/launcher/Application.cpp @@ -919,6 +919,8 @@ Application::Application(int &argc, char **argv) : QApplication(argc, argv) { return; } + + updateCapabilities(); performMainStartupAction(); } @@ -1564,14 +1566,13 @@ shared_qobject_ptr Application::metadataIndex() return m_metadataIndex; } -Application::Capabilities Application::currentCapabilities() +void Application::updateCapabilities() { - Capabilities c; + m_capabilities = None; if (!getMSAClientID().isEmpty()) - c |= SupportsMSA; + m_capabilities |= SupportsMSA; if (!getFlameAPIKey().isEmpty()) - c |= SupportsFlame; - return c; + m_capabilities |= SupportsFlame; } QString Application::getJarPath(QString jarFile) diff --git a/launcher/Application.h b/launcher/Application.h index 019c3c3d1..05525bcf8 100644 --- a/launcher/Application.h +++ b/launcher/Application.h @@ -162,7 +162,7 @@ public: shared_qobject_ptr metadataIndex(); - Capabilities currentCapabilities(); + void updateCapabilities(); /*! * Finds and returns the full path to a jar file. @@ -180,6 +180,10 @@ public: return m_rootPath; } + const Capabilities capabilities() { + return m_capabilities; + } + /*! * Opens a json file using either a system default editor, or, if not empty, the editor * specified in the settings @@ -258,6 +262,7 @@ private: QString m_rootPath; Status m_status = Application::StartingUp; + Capabilities m_capabilities; #ifdef Q_OS_MACOS Qt::ApplicationState m_prevAppState = Qt::ApplicationInactive; diff --git a/launcher/net/Download.cpp b/launcher/net/Download.cpp index e6a6adcc1..8db11eaa2 100644 --- a/launcher/net/Download.cpp +++ b/launcher/net/Download.cpp @@ -118,7 +118,7 @@ void Download::executeTask() } request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8()); - if (APPLICATION->currentCapabilities() & Application::SupportsFlame + if (APPLICATION->capabilities() & Application::SupportsFlame && request.url().host().contains("api.curseforge.com")) { request.setRawHeader("x-api-key", APPLICATION->getFlameAPIKey().toUtf8()); }; diff --git a/launcher/net/Upload.cpp b/launcher/net/Upload.cpp index cfda4b4e0..f3b190222 100644 --- a/launcher/net/Upload.cpp +++ b/launcher/net/Upload.cpp @@ -216,7 +216,7 @@ namespace Net { } request.setHeader(QNetworkRequest::UserAgentHeader, APPLICATION->getUserAgent().toUtf8()); - if (APPLICATION->currentCapabilities() & Application::SupportsFlame + if (APPLICATION->capabilities() & Application::SupportsFlame && request.url().host().contains("api.curseforge.com")) { request.setRawHeader("x-api-key", APPLICATION->getFlameAPIKey().toUtf8()); } diff --git a/launcher/ui/dialogs/ModDownloadDialog.cpp b/launcher/ui/dialogs/ModDownloadDialog.cpp index e4fc3ecc2..bc5e5206f 100644 --- a/launcher/ui/dialogs/ModDownloadDialog.cpp +++ b/launcher/ui/dialogs/ModDownloadDialog.cpp @@ -122,7 +122,7 @@ QList ModDownloadDialog::getPages() QList pages; pages.append(new ModrinthModPage(this, m_instance)); - if (APPLICATION->currentCapabilities() & Application::SupportsFlame) + if (APPLICATION->capabilities() & Application::SupportsFlame) pages.append(new FlameModPage(this, m_instance)); return pages; diff --git a/launcher/ui/dialogs/NewInstanceDialog.cpp b/launcher/ui/dialogs/NewInstanceDialog.cpp index 35bba9be1..675f8b150 100644 --- a/launcher/ui/dialogs/NewInstanceDialog.cpp +++ b/launcher/ui/dialogs/NewInstanceDialog.cpp @@ -157,7 +157,7 @@ QList NewInstanceDialog::getPages() pages.append(new VanillaPage(this)); pages.append(importPage); pages.append(new AtlPage(this)); - if (APPLICATION->currentCapabilities() & Application::SupportsFlame) + if (APPLICATION->capabilities() & Application::SupportsFlame) pages.append(new FlamePage(this)); pages.append(new FtbPage(this)); pages.append(new LegacyFTB::Page(this)); diff --git a/launcher/ui/pages/global/AccountListPage.cpp b/launcher/ui/pages/global/AccountListPage.cpp index fcc43add2..a4f4dfb93 100644 --- a/launcher/ui/pages/global/AccountListPage.cpp +++ b/launcher/ui/pages/global/AccountListPage.cpp @@ -96,7 +96,7 @@ AccountListPage::AccountListPage(QWidget *parent) updateButtonStates(); // Xbox authentication won't work without a client identifier, so disable the button if it is missing - if (~APPLICATION->currentCapabilities() & Application::SupportsMSA) { + if (~APPLICATION->capabilities() & Application::SupportsMSA) { ui->actionAddMicrosoft->setVisible(false); ui->actionAddMicrosoft->setToolTip(tr("No Microsoft Authentication client ID was set.")); }