Skip to content

Commit

Permalink
Merge pull request #1493 from urFate/develop
Browse files Browse the repository at this point in the history
Implement account per instance feature
  • Loading branch information
LennyMcLennington committed Nov 17, 2022
2 parents 51de23d + 6ff87f7 commit 211c423
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 1 deletion.
11 changes: 11 additions & 0 deletions launcher/LaunchController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,18 @@ void LaunchController::decideAccount()
}
}

bool overrideAccount = m_instance->settings()->get("OverrideAccount").toBool();
QString overrideAccountProfileId = m_instance->settings()->get("OverrideAccountProfileId").toString();

m_accountToUse = accounts->defaultAccount();

if (overrideAccount) {
int overrideIndex = accounts->findAccountByProfileId(overrideAccountProfileId);
if (overrideIndex != -1) {
m_accountToUse = accounts->at(overrideIndex);
}
}

if (!m_accountToUse)
{
// If no default account is set, ask the user which one to use.
Expand Down
4 changes: 4 additions & 0 deletions launcher/minecraft/MinecraftInstance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ void MinecraftInstance::loadSpecificSettings()
m_settings->registerSetting("JoinServerOnLaunch", false);
m_settings->registerSetting("JoinServerOnLaunchAddress", "");

// Account override
m_settings->registerSetting("OverrideAccount", false);
m_settings->registerSetting("OverrideAccountProfileId", "");

qDebug() << "Instance-type specific settings were loaded!";

setSpecificSettingsLoaded(true);
Expand Down
59 changes: 59 additions & 0 deletions launcher/ui/pages/instance/InstanceSettingsPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,20 @@
#include "java/JavaUtils.h"
#include "FileSystem.h"

#include "minecraft/auth/AccountList.h"


InstanceSettingsPage::InstanceSettingsPage(BaseInstance *inst, QWidget *parent)
: QWidget(parent), ui(new Ui::InstanceSettingsPage), m_instance(inst)
{
m_settings = inst->settings();
m_accounts = APPLICATION->accounts();
ui->setupUi(this);
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
// This would just be set in the .ui file, if only Qt's uic would just ignore propreties that don't exist. SAD!
ui->accountComboBox->setPlaceholderText(tr("No account selected"));
#endif

auto sysMB = Sys::getSystemRam() / Sys::mebibyte;
ui->maxMemSpinBox->setMaximum(sysMB);
connect(ui->openGlobalJavaSettingsButton, &QCommandLinkButton::clicked, this, &InstanceSettingsPage::globalSettingsButtonClicked);
Expand Down Expand Up @@ -275,6 +283,18 @@ void InstanceSettingsPage::applySettings()
m_settings->reset("JoinServerOnLaunchAddress");
}

// Account override settings
bool accountOverride = ui->accountGroupBox->isChecked();
m_settings->set("OverrideAccount", accountOverride);
if (accountOverride && ui->accountComboBox->currentData().isValid())
{
m_settings->set("OverrideAccountProfileId", ui->accountComboBox->currentData().toString());
}
else
{
m_settings->reset("OverrideAccountProfileId");
}

// FIXME: This should probably be called by a signal instead
m_instance->updateRuntimeContext();
}
Expand Down Expand Up @@ -372,6 +392,45 @@ void InstanceSettingsPage::loadSettings()

ui->serverJoinGroupBox->setChecked(m_settings->get("JoinServerOnLaunch").toBool());
ui->serverJoinAddress->setText(m_settings->get("JoinServerOnLaunchAddress").toString());

ui->accountGroupBox->setChecked(m_settings->get("OverrideAccount").toBool());
if (m_accounts->count() > 0) {
for (int i = 0; i < m_accounts->count(); i++) {
MinecraftAccountPtr account = m_accounts->at(i);

QString profileLabel = account->profileName();
QString profileId = account->profileId();
QString profileType = account->typeString();
profileType[0] = profileType[0].toUpper();
QPixmap profileFace = account->getFace();
QIcon profileIcon = !profileFace.isNull() ? QIcon(profileFace) : APPLICATION->getThemedIcon("noaccount");

ui->accountComboBox->addItem(profileIcon, QString("%1 (%2)").arg(profileLabel, profileType), profileId);
}

QString accountProfileId = m_settings->get("OverrideAccountProfileId").toString();
int index = -1;
if (ui->accountGroupBox->isChecked()) {
// Try find the account by the override profile id setting
// It'll be -1 if the account doesn't exist, which will just
// Set the combobox to the "no option selected" state
index = ui->accountComboBox->findData(accountProfileId);
}
ui->accountComboBox->setCurrentIndex(index);
} else {
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
ui->accountComboBox->setPlaceholderText(tr("No accounts available"));
ui->accountComboBox->setCurrentIndex(-1);
#else
ui->accountComboBox->addItem(tr("No accounts available"));
#endif
ui->accountComboBox->setDisabled(true);
}

// If there isn't a currently selected account, the override should show as disabled
if (ui->accountComboBox->currentIndex() == -1) {
ui->accountGroupBox->setChecked(false);
}
}

void InstanceSettingsPage::on_javaDetectBtn_clicked()
Expand Down
1 change: 1 addition & 0 deletions launcher/ui/pages/instance/InstanceSettingsPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,6 @@ private slots:
Ui::InstanceSettingsPage *ui;
BaseInstance *m_instance;
SettingsObjectPtr m_settings;
shared_qobject_ptr<AccountList> m_accounts;
unique_qobject_ptr<JavaCommon::TestCheck> checker;
};
44 changes: 43 additions & 1 deletion launcher/ui/pages/instance/InstanceSettingsPage.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>691</width>
<height>581</height>
<height>629</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
Expand Down Expand Up @@ -595,6 +595,48 @@
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="accountGroupBox">
<property name="title">
<string>Set an account to use</string>
</property>
<property name="checkable">
<bool>true</bool>
</property>
<property name="checked">
<bool>false</bool>
</property>
<layout class="QVBoxLayout" name="verticalLayout_15">
<item>
<layout class="QGridLayout" name="accountLayout">
<item row="0" column="0">
<widget class="QLabel" name="accountLabel">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Game account:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="accountComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<spacer name="verticalSpacerMiscellaneous">
<property name="orientation">
Expand Down

0 comments on commit 211c423

Please sign in to comment.