From 7e2d78bab555ac17ff79711d41d59b14b226998f Mon Sep 17 00:00:00 2001 From: Aaron <10217842+byteduck@users.noreply.github.com> Date: Tue, 27 Dec 2022 15:00:15 -0700 Subject: [PATCH 1/9] Allow selecting a default account to use with an instance Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/LaunchController.cpp | 14 +++- launcher/minecraft/MinecraftInstance.cpp | 4 + .../pages/instance/InstanceSettingsPage.cpp | 83 ++++++++++++++++++- .../ui/pages/instance/InstanceSettingsPage.h | 13 ++- .../ui/pages/instance/InstanceSettingsPage.ui | 42 ++++++++++ 5 files changed, 150 insertions(+), 6 deletions(-) diff --git a/launcher/LaunchController.cpp b/launcher/LaunchController.cpp index 11e3de152..5dd551eea 100644 --- a/launcher/LaunchController.cpp +++ b/launcher/LaunchController.cpp @@ -112,7 +112,19 @@ void LaunchController::decideAccount() } } - m_accountToUse = accounts->defaultAccount(); + // Select the account to use. If the instance has a specific account set, that will be used. Otherwise, the default account will be used + auto instanceAccountId = m_instance->settings()->get("InstanceAccountId").toString(); + auto instanceAccountIndex = accounts->findAccountByProfileId(instanceAccountId); + if (instanceAccountIndex == -1) + { + m_accountToUse = accounts->defaultAccount(); + } + else + { + m_accountToUse = accounts->at(instanceAccountIndex); + } + + if (!m_accountToUse) { // If no default account is set, ask the user which one to use. diff --git a/launcher/minecraft/MinecraftInstance.cpp b/launcher/minecraft/MinecraftInstance.cpp index 1d37224aa..d0a5ed314 100644 --- a/launcher/minecraft/MinecraftInstance.cpp +++ b/launcher/minecraft/MinecraftInstance.cpp @@ -192,6 +192,10 @@ void MinecraftInstance::loadSpecificSettings() m_settings->registerSetting("JoinServerOnLaunch", false); m_settings->registerSetting("JoinServerOnLaunchAddress", ""); + // Use account for instance, this does not have a global override + m_settings->registerSetting("UseAccountForInstance", false); + m_settings->registerSetting("InstanceAccountId", ""); + qDebug() << "Instance-type specific settings were loaded!"; setSpecificSettingsLoaded(true); diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index af2ba7c80..18f5f2ac6 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -48,18 +48,23 @@ #include "JavaCommon.h" #include "Application.h" +#include "minecraft/auth/AccountList.h" #include "java/JavaInstallList.h" #include "java/JavaUtils.h" #include "FileSystem.h" - InstanceSettingsPage::InstanceSettingsPage(BaseInstance *inst, QWidget *parent) : QWidget(parent), ui(new Ui::InstanceSettingsPage), m_instance(inst) { m_settings = inst->settings(); ui->setupUi(this); + accountMenu = new QMenu(this); + // Use undocumented property... https://stackoverflow.com/questions/7121718/create-a-scrollbar-in-a-submenu-qt + accountMenu->setStyleSheet("QMenu { menu-scrollable: 1; }"); + ui->instanceAccountSelector->setMenu(accountMenu); + connect(ui->openGlobalJavaSettingsButton, &QCommandLinkButton::clicked, this, &InstanceSettingsPage::globalSettingsButtonClicked); connect(APPLICATION, &Application::globalSettingsAboutToOpen, this, &InstanceSettingsPage::applySettings); connect(APPLICATION, &Application::globalSettingsClosed, this, &InstanceSettingsPage::loadSettings); @@ -75,6 +80,7 @@ bool InstanceSettingsPage::shouldDisplay() const InstanceSettingsPage::~InstanceSettingsPage() { delete ui; + delete accountMenu; } void InstanceSettingsPage::globalSettingsButtonClicked(bool) @@ -275,6 +281,14 @@ void InstanceSettingsPage::applySettings() m_settings->reset("JoinServerOnLaunchAddress"); } + // Use an account for this instance + bool useAccountForInstance = ui->instanceAccountGroupBox->isChecked(); + m_settings->set("UseAccountForInstance", useAccountForInstance); + if (!useAccountForInstance) + { + m_settings->reset("InstanceAccountId"); + } + // FIXME: This should probably be called by a signal instead m_instance->updateRuntimeContext(); } @@ -372,6 +386,9 @@ void InstanceSettingsPage::loadSettings() ui->serverJoinGroupBox->setChecked(m_settings->get("JoinServerOnLaunch").toBool()); ui->serverJoinAddress->setText(m_settings->get("JoinServerOnLaunchAddress").toString()); + + ui->instanceAccountGroupBox->setChecked(m_settings->get("UseAccountForInstance").toBool()); + updateAccountsMenu(); } void InstanceSettingsPage::on_javaDetectBtn_clicked() @@ -437,6 +454,70 @@ void InstanceSettingsPage::on_javaTestBtn_clicked() checker->run(); } +void InstanceSettingsPage::updateAccountsMenu() +{ + accountMenu->clear(); + + auto accounts = APPLICATION->accounts(); + int accountIndex = accounts->findAccountByProfileId(m_settings->get("InstanceAccountId").toString()); + + if (accountIndex != -1) + { + auto account = accounts->at(accountIndex); + ui->instanceAccountSelector->setText(account->profileName()); + ui->instanceAccountSelector->setIcon(account->getFace()); + } else { + ui->instanceAccountSelector->setText(tr("No default account")); + ui->instanceAccountSelector->setIcon(APPLICATION->getThemedIcon("noaccount")); + } + + for (int i = 0; i < accounts->count(); i++) + { + MinecraftAccountPtr account = accounts->at(i); + QAction *action = new QAction(account->profileName(), this); + action->setData(i); + action->setCheckable(true); + if (accountIndex == i) + { + action->setChecked(true); + } + + auto face = account->getFace(); + if(!face.isNull()) { + action->setIcon(face); + } + else { + action->setIcon(APPLICATION->getThemedIcon("noaccount")); + } + + accountMenu->addAction(action); + connect(action, SIGNAL(triggered(bool)), SLOT(changeInstanceAccount())); + } +} + +void InstanceSettingsPage::changeInstanceAccount() +{ + QAction *sAction = (QAction *)sender(); + + // Profile's associated Mojang username + if (sAction->data().type() != QVariant::Type::Int) + return; + + QVariant data = sAction->data(); + bool valid = false; + int index = data.toInt(&valid); + if(!valid) { + index = -1; + } + auto accounts = APPLICATION->accounts(); + auto account = accounts->at(index); + + m_settings->set("InstanceAccountId", account->profileId()); + + ui->instanceAccountSelector->setText(account->profileName()); + ui->instanceAccountSelector->setIcon(account->getFace()); +} + void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i) { updateThresholds(); diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.h b/launcher/ui/pages/instance/InstanceSettingsPage.h index 7450188d5..b80db99a8 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.h +++ b/launcher/ui/pages/instance/InstanceSettingsPage.h @@ -37,12 +37,13 @@ #include -#include "java/JavaChecker.h" -#include "BaseInstance.h" #include -#include "ui/pages/BasePage.h" -#include "JavaCommon.h" +#include #include "Application.h" +#include "BaseInstance.h" +#include "JavaCommon.h" +#include "java/JavaChecker.h" +#include "ui/pages/BasePage.h" class JavaChecker; namespace Ui @@ -92,9 +93,13 @@ private slots: void globalSettingsButtonClicked(bool checked); + void updateAccountsMenu(); + void changeInstanceAccount(); + private: Ui::InstanceSettingsPage *ui; BaseInstance *m_instance; SettingsObjectPtr m_settings; unique_qobject_ptr checker; + QMenu *accountMenu = nullptr; }; diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.ui b/launcher/ui/pages/instance/InstanceSettingsPage.ui index b064367d1..ef86ed7e1 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.ui +++ b/launcher/ui/pages/instance/InstanceSettingsPage.ui @@ -608,6 +608,48 @@ + + + + Set a default account to use with this instance + + + true + + + false + + + + + + + + + 0 + 0 + + + + Account: + + + + + + + QToolButton::InstantPopup + + + Qt::ToolButtonTextBesideIcon + + + + + + + + From cba3d68063bea28acb1eae870aee7e0b2a57b2be Mon Sep 17 00:00:00 2001 From: Aaron <10217842+byteduck@users.noreply.github.com> Date: Tue, 27 Dec 2022 15:39:35 -0700 Subject: [PATCH 2/9] Fix conflicting layout name in InstanceSettingsPage Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/ui/pages/instance/InstanceSettingsPage.ui | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.ui b/launcher/ui/pages/instance/InstanceSettingsPage.ui index ef86ed7e1..a88fdb542 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.ui +++ b/launcher/ui/pages/instance/InstanceSettingsPage.ui @@ -619,7 +619,7 @@ false - + From 021e6c02d781706da82ca8ee5c77f716b5c210b9 Mon Sep 17 00:00:00 2001 From: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:49:16 -0700 Subject: [PATCH 3/9] Replace unecessary type check with assertion in InstanceSettingsPage Co-authored-by: flow Signed-off-by: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/ui/pages/instance/InstanceSettingsPage.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index 18f5f2ac6..1c3989f61 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -500,8 +500,7 @@ void InstanceSettingsPage::changeInstanceAccount() QAction *sAction = (QAction *)sender(); // Profile's associated Mojang username - if (sAction->data().type() != QVariant::Type::Int) - return; + Q_ASSERT(sAction->data().type() == QVariant::Type::Int); QVariant data = sAction->data(); bool valid = false; From e18652387835e61d6b006d88fe856c63e24a098f Mon Sep 17 00:00:00 2001 From: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:50:24 -0700 Subject: [PATCH 4/9] Add null check for face in instance account settings selector Co-authored-by: flow Signed-off-by: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/ui/pages/instance/InstanceSettingsPage.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index 1c3989f61..a870c01b8 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -514,7 +514,11 @@ void InstanceSettingsPage::changeInstanceAccount() m_settings->set("InstanceAccountId", account->profileId()); ui->instanceAccountSelector->setText(account->profileName()); - ui->instanceAccountSelector->setIcon(account->getFace()); + if (auto face = account->getFace(); !face.isNull()) { + ui->instanceAccountSelector->setIcon(face); + } else { + ui->instanceAccountSelector->setIcon(APPLICATION->getThemedIcon("noaccount")); + } } void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i) From 9b8add196123f13b18b6a8c878da95921103b6f7 Mon Sep 17 00:00:00 2001 From: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:50:59 -0700 Subject: [PATCH 5/9] Properly connect signal in instance settings for account selector Co-authored-by: flow Signed-off-by: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/ui/pages/instance/InstanceSettingsPage.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index a870c01b8..6a823d5f4 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -491,7 +491,7 @@ void InstanceSettingsPage::updateAccountsMenu() } accountMenu->addAction(action); - connect(action, SIGNAL(triggered(bool)), SLOT(changeInstanceAccount())); + connect(action, SIGNAL(triggered(bool)), this, SLOT(changeInstanceAccount())); } } From eefb259ddff3de641457b6312bc125796e7661c9 Mon Sep 17 00:00:00 2001 From: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Date: Mon, 2 Jan 2023 10:51:17 -0700 Subject: [PATCH 6/9] Remove unecessary delete in InstanceSettingsPage destructor Co-authored-by: flow Signed-off-by: Aaron Sonin <10217842+byteduck@users.noreply.github.com> Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/ui/pages/instance/InstanceSettingsPage.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index 6a823d5f4..4b7c0f839 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -80,7 +80,6 @@ bool InstanceSettingsPage::shouldDisplay() const InstanceSettingsPage::~InstanceSettingsPage() { delete ui; - delete accountMenu; } void InstanceSettingsPage::globalSettingsButtonClicked(bool) From ba81ad1ac3cff48b973ee167802a5d6398eac990 Mon Sep 17 00:00:00 2001 From: Aaron <10217842+byteduck@users.noreply.github.com> Date: Mon, 2 Jan 2023 11:16:09 -0700 Subject: [PATCH 7/9] Reword instance-specific account settings, apply clang-format Signed-off-by: Aaron <10217842+byteduck@users.noreply.github.com> --- launcher/LaunchController.cpp | 8 +--- .../pages/instance/InstanceSettingsPage.cpp | 39 ++++++++----------- .../ui/pages/instance/InstanceSettingsPage.ui | 2 +- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/launcher/LaunchController.cpp b/launcher/LaunchController.cpp index 5dd551eea..9741fd95a 100644 --- a/launcher/LaunchController.cpp +++ b/launcher/LaunchController.cpp @@ -115,16 +115,12 @@ void LaunchController::decideAccount() // Select the account to use. If the instance has a specific account set, that will be used. Otherwise, the default account will be used auto instanceAccountId = m_instance->settings()->get("InstanceAccountId").toString(); auto instanceAccountIndex = accounts->findAccountByProfileId(instanceAccountId); - if (instanceAccountIndex == -1) - { + if (instanceAccountIndex == -1) { m_accountToUse = accounts->defaultAccount(); - } - else - { + } else { m_accountToUse = accounts->at(instanceAccountIndex); } - if (!m_accountToUse) { // If no default account is set, ask the user which one to use. diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index 4b7c0f839..24b261ba7 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -283,8 +283,7 @@ void InstanceSettingsPage::applySettings() // Use an account for this instance bool useAccountForInstance = ui->instanceAccountGroupBox->isChecked(); m_settings->set("UseAccountForInstance", useAccountForInstance); - if (!useAccountForInstance) - { + if (!useAccountForInstance) { m_settings->reset("InstanceAccountId"); } @@ -459,33 +458,33 @@ void InstanceSettingsPage::updateAccountsMenu() auto accounts = APPLICATION->accounts(); int accountIndex = accounts->findAccountByProfileId(m_settings->get("InstanceAccountId").toString()); + MinecraftAccountPtr defaultAccount = accounts->defaultAccount(); - if (accountIndex != -1) - { - auto account = accounts->at(accountIndex); - ui->instanceAccountSelector->setText(account->profileName()); - ui->instanceAccountSelector->setIcon(account->getFace()); + if (accountIndex != -1 && accounts->at(accountIndex)) { + defaultAccount = accounts->at(accountIndex); + } + + if (defaultAccount) { + ui->instanceAccountSelector->setText(defaultAccount->profileName()); + ui->instanceAccountSelector->setIcon(defaultAccount->getFace()); } else { ui->instanceAccountSelector->setText(tr("No default account")); ui->instanceAccountSelector->setIcon(APPLICATION->getThemedIcon("noaccount")); } - for (int i = 0; i < accounts->count(); i++) - { + for (int i = 0; i < accounts->count(); i++) { MinecraftAccountPtr account = accounts->at(i); - QAction *action = new QAction(account->profileName(), this); + QAction* action = new QAction(account->profileName(), this); action->setData(i); action->setCheckable(true); - if (accountIndex == i) - { + if (accountIndex == i) { action->setChecked(true); } auto face = account->getFace(); - if(!face.isNull()) { + if (!face.isNull()) { action->setIcon(face); - } - else { + } else { action->setIcon(APPLICATION->getThemedIcon("noaccount")); } @@ -496,20 +495,14 @@ void InstanceSettingsPage::updateAccountsMenu() void InstanceSettingsPage::changeInstanceAccount() { - QAction *sAction = (QAction *)sender(); + QAction* sAction = (QAction*)sender(); - // Profile's associated Mojang username Q_ASSERT(sAction->data().type() == QVariant::Type::Int); QVariant data = sAction->data(); - bool valid = false; - int index = data.toInt(&valid); - if(!valid) { - index = -1; - } + int index = data.toInt(); auto accounts = APPLICATION->accounts(); auto account = accounts->at(index); - m_settings->set("InstanceAccountId", account->profileId()); ui->instanceAccountSelector->setText(account->profileName()); diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.ui b/launcher/ui/pages/instance/InstanceSettingsPage.ui index a88fdb542..1b9861842 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.ui +++ b/launcher/ui/pages/instance/InstanceSettingsPage.ui @@ -611,7 +611,7 @@ - Set a default account to use with this instance + Override default account true From 160dd09fc2788fea17c8e9e332c2877586640971 Mon Sep 17 00:00:00 2001 From: Aaron <10217842+byteduck@users.noreply.github.com> Date: Thu, 12 Jan 2023 20:03:31 -0800 Subject: [PATCH 8/9] Fix instance account selector face for offline accounts --- .../pages/instance/InstanceSettingsPage.cpp | 26 +++++++++---------- .../ui/pages/instance/InstanceSettingsPage.h | 1 + 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.cpp b/launcher/ui/pages/instance/InstanceSettingsPage.cpp index 24b261ba7..4b4c73dc6 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.cpp +++ b/launcher/ui/pages/instance/InstanceSettingsPage.cpp @@ -466,7 +466,7 @@ void InstanceSettingsPage::updateAccountsMenu() if (defaultAccount) { ui->instanceAccountSelector->setText(defaultAccount->profileName()); - ui->instanceAccountSelector->setIcon(defaultAccount->getFace()); + ui->instanceAccountSelector->setIcon(getFaceForAccount(defaultAccount)); } else { ui->instanceAccountSelector->setText(tr("No default account")); ui->instanceAccountSelector->setIcon(APPLICATION->getThemedIcon("noaccount")); @@ -480,19 +480,21 @@ void InstanceSettingsPage::updateAccountsMenu() if (accountIndex == i) { action->setChecked(true); } - - auto face = account->getFace(); - if (!face.isNull()) { - action->setIcon(face); - } else { - action->setIcon(APPLICATION->getThemedIcon("noaccount")); - } - + action->setIcon(getFaceForAccount(account)); accountMenu->addAction(action); connect(action, SIGNAL(triggered(bool)), this, SLOT(changeInstanceAccount())); } } +QIcon InstanceSettingsPage::getFaceForAccount(MinecraftAccountPtr account) +{ + if (auto face = account->getFace(); !face.isNull()) { + return face; + } + + return APPLICATION->getThemedIcon("noaccount"); +} + void InstanceSettingsPage::changeInstanceAccount() { QAction* sAction = (QAction*)sender(); @@ -506,11 +508,7 @@ void InstanceSettingsPage::changeInstanceAccount() m_settings->set("InstanceAccountId", account->profileId()); ui->instanceAccountSelector->setText(account->profileName()); - if (auto face = account->getFace(); !face.isNull()) { - ui->instanceAccountSelector->setIcon(face); - } else { - ui->instanceAccountSelector->setIcon(APPLICATION->getThemedIcon("noaccount")); - } + ui->instanceAccountSelector->setIcon(getFaceForAccount(account)); } void InstanceSettingsPage::on_maxMemSpinBox_valueChanged(int i) diff --git a/launcher/ui/pages/instance/InstanceSettingsPage.h b/launcher/ui/pages/instance/InstanceSettingsPage.h index b80db99a8..cb6fbae0c 100644 --- a/launcher/ui/pages/instance/InstanceSettingsPage.h +++ b/launcher/ui/pages/instance/InstanceSettingsPage.h @@ -94,6 +94,7 @@ private slots: void globalSettingsButtonClicked(bool checked); void updateAccountsMenu(); + QIcon getFaceForAccount(MinecraftAccountPtr account); void changeInstanceAccount(); private: