diff options
author | Shokara <shokara@snopyta.org> | 2021-01-04 20:05:46 -0500 |
---|---|---|
committer | Thomas Groman <tgroman@nuegia.net> | 2021-01-04 17:14:35 -0800 |
commit | 21da6d89658aef25c1def27dc5fa2c4055e6a6f9 (patch) | |
tree | 1cf5e3774d0820f30eee3265f49c282061591999 | |
parent | b57d6e6353a3048a4052201bf2a56bea00d94b0a (diff) | |
download | MultiMC-21da6d89658aef25c1def27dc5fa2c4055e6a6f9.tar MultiMC-21da6d89658aef25c1def27dc5fa2c4055e6a6f9.tar.gz MultiMC-21da6d89658aef25c1def27dc5fa2c4055e6a6f9.tar.lz MultiMC-21da6d89658aef25c1def27dc5fa2c4055e6a6f9.tar.xz MultiMC-21da6d89658aef25c1def27dc5fa2c4055e6a6f9.zip |
I added the offline patch from my MultiMC repo[1]. The only change I
made compared to my fork was adding an extra sentence in the account
adder dialog that the password needs to be blank for an offline
account.
Signed-off-by: Shokara <shokara@snopyta.org>
-rw-r--r-- | api/logic/minecraft/MinecraftInstance.cpp | 2 | ||||
-rw-r--r-- | api/logic/minecraft/auth/MojangAccount.cpp | 15 | ||||
-rw-r--r-- | api/logic/minecraft/auth/MojangAccount.h | 3 | ||||
-rw-r--r-- | application/LaunchController.cpp | 186 | ||||
-rw-r--r-- | application/dialogs/LoginDialog.cpp | 29 | ||||
-rw-r--r-- | application/pages/global/AccountListPage.cpp | 2 |
6 files changed, 136 insertions, 101 deletions
diff --git a/api/logic/minecraft/MinecraftInstance.cpp b/api/logic/minecraft/MinecraftInstance.cpp index 28073edf..4617b09f 100644 --- a/api/logic/minecraft/MinecraftInstance.cpp +++ b/api/logic/minecraft/MinecraftInstance.cpp @@ -821,7 +821,7 @@ shared_qobject_ptr<LaunchTask> MinecraftInstance::createLaunchTask(AuthSessionPt } else { - process->appendStep(new Update(pptr, Net::Mode::Offline)); + process->appendStep(new Update(pptr, Net::Mode::Online)); } // if there are any jar mods diff --git a/api/logic/minecraft/auth/MojangAccount.cpp b/api/logic/minecraft/auth/MojangAccount.cpp index 657e0009..28f31266 100644 --- a/api/logic/minecraft/auth/MojangAccount.cpp +++ b/api/logic/minecraft/auth/MojangAccount.cpp @@ -19,6 +19,7 @@ #include "flows/RefreshTask.h" #include "flows/AuthenticateTask.h" +#include <QCryptographicHash> #include <QUuid> #include <QJsonObject> #include <QJsonArray> @@ -103,6 +104,20 @@ MojangAccountPtr MojangAccount::createFromUsername(const QString &username) return account; } +MojangAccountPtr MojangAccount::createFromUsernameOffline(const QString &username) +{ + MojangAccountPtr account(new MojangAccount()); + account->m_clientToken = "ff64ff64ff64ff64ff64ff64ff64ff64"; + account->m_accessToken = "ff64ff64ff64ff64ff64ff64ff64ff64"; + account->m_username = username; + QList<AccountProfile> profiles; + QString uuid = QCryptographicHash::hash(username.toLocal8Bit(), QCryptographicHash::Md5).toHex(); + profiles.append({uuid, username, false}); + account->m_profiles = profiles; + account->setCurrentProfile(uuid); + return account; +} + QJsonObject MojangAccount::saveToJson() const { QJsonObject json; diff --git a/api/logic/minecraft/auth/MojangAccount.h b/api/logic/minecraft/auth/MojangAccount.h index 7006435e..92679076 100644 --- a/api/logic/minecraft/auth/MojangAccount.h +++ b/api/logic/minecraft/auth/MojangAccount.h @@ -77,6 +77,9 @@ public: /* construction */ //! Creates an empty account for the specified user name. static MojangAccountPtr createFromUsername(const QString &username); + //! Creates an offline account for the specified user name. + static MojangAccountPtr createFromUsernameOffline(const QString &username); + //! Loads a MojangAccount from the given JSON object. static MojangAccountPtr loadFromJson(const QJsonObject &json); diff --git a/application/LaunchController.cpp b/application/LaunchController.cpp index e39048f1..b6916027 100644 --- a/application/LaunchController.cpp +++ b/application/LaunchController.cpp @@ -78,112 +78,122 @@ void LaunchController::login() return; } - // we try empty password first :) - QString password; - // we loop until the user succeeds in logging in or gives up - bool tryagain = true; - // the failure. the default failure. - const QString needLoginAgain = tr("Your account is currently not logged in. Please enter " - "your password to log in again."); - QString failReason = needLoginAgain; - - while (tryagain) - { - m_session = std::make_shared<AuthSession>(); - m_session->wants_online = m_online; - auto task = account->login(m_session, password); - if (task) + if(account->clientToken() != "ff64ff64ff64ff64ff64ff64ff64ff64") { + // Online + // we try empty password first :) + QString password; + // we loop until the user succeeds in logging in or gives up + bool tryagain = true; + // the failure. the default failure. + const QString needLoginAgain = tr("Your account is currently not logged in. Please enter your password to log in again. <br /> <br /> This could be caused by a password change."); + QString failReason = needLoginAgain; + while (tryagain) { - // We'll need to validate the access token to make sure the account - // is still logged in. - ProgressDialog progDialog(m_parentWidget); - if (m_online) - { - progDialog.setSkipButton(true, tr("Play Offline")); - } - progDialog.execWithTask(task.get()); - if (!task->wasSuccessful()) + m_session = std::make_shared<AuthSession>(); + m_session->wants_online = m_online; + auto task = account->login(m_session, password); + if (task) { - auto failReasonNew = task->failReason(); - if(failReasonNew == "Invalid token.") + // We'll need to validate the access token to make sure the account + // is still logged in. + ProgressDialog progDialog(m_parentWidget); + if (m_online) { - account->invalidateClientToken(); - failReason = needLoginAgain; + progDialog.setSkipButton(true, tr("Play Offline")); } - else failReason = failReasonNew; - } - } - switch (m_session->status) - { - case AuthSession::Undetermined: - { - qCritical() << "Received undetermined session status during login. Bye."; - tryagain = false; - emitFailed(tr("Received undetermined session status during login.")); - break; - } - case AuthSession::RequiresPassword: - { - EditAccountDialog passDialog(failReason, m_parentWidget, EditAccountDialog::PasswordField); - auto username = m_session->username; - auto chopN = [](QString toChop, int N) -> QString - { - if(toChop.size() > N) + progDialog.execWithTask(task.get()); + if (!task->wasSuccessful()) { - auto left = toChop.left(N); - left += QString("\u25CF").repeated(toChop.size() - N); - return left; + auto failReasonNew = task->failReason(); + if(failReasonNew == "Invalid token.") + { + account->invalidateClientToken(); + failReason = needLoginAgain; + } + else failReason = failReasonNew; } - return toChop; - }; - - if(username.contains('@')) - { - auto parts = username.split('@'); - auto mailbox = chopN(parts[0],3); - QString domain = chopN(parts[1], 3); - username = mailbox + '@' + domain; } - passDialog.setUsername(username); - if (passDialog.exec() == QDialog::Accepted) + switch (m_session->status) { - password = passDialog.password(); - } - else + case AuthSession::Undetermined: { + qCritical() << "Received undetermined session status during login. Bye."; tryagain = false; + emitFailed(tr("Received undetermined session status during login.")); + break; } - break; - } - case AuthSession::PlayableOffline: - { - // we ask the user for a player name - bool ok = false; - QString usedname = m_session->player_name; - QString name = QInputDialog::getText(m_parentWidget, tr("Player name"), - tr("Choose your offline mode player name."), - QLineEdit::Normal, m_session->player_name, &ok); - if (!ok) + case AuthSession::RequiresPassword: { - tryagain = false; + EditAccountDialog passDialog(failReason, m_parentWidget, EditAccountDialog::PasswordField); + auto username = m_session->username; + auto chopN = [](QString toChop, int N) -> QString + { + if(toChop.size() > N) + { + auto left = toChop.left(N); + left += QString("\u25CF").repeated(toChop.size() - N); + return left; + } + return toChop; + }; + + if(username.contains('@')) + { + auto parts = username.split('@'); + auto mailbox = chopN(parts[0],3); + QString domain = chopN(parts[1], 3); + username = mailbox + '@' + domain; + } + passDialog.setUsername(username); + if (passDialog.exec() == QDialog::Accepted) + { + password = passDialog.password(); + } + else + { + tryagain = false; + } break; } - if (name.length()) + case AuthSession::PlayableOffline: { - usedname = name; + // we ask the user for a player name + bool ok = false; + QString usedname = m_session->player_name; + QString name = QInputDialog::getText(m_parentWidget, tr("Player name"), + tr("Choose your offline mode player name."), + QLineEdit::Normal, m_session->player_name, &ok); + if (!ok) + { + tryagain = false; + break; + } + if (name.length()) + { + usedname = name; + } + m_session->MakeOffline(usedname); + // offline flavored game from here :3 + } + case AuthSession::PlayableOnline: + { + launchInstance(); + tryagain = false; + return; + } } - m_session->MakeOffline(usedname); - // offline flavored game from here :3 - } - case AuthSession::PlayableOnline: - { - launchInstance(); - tryagain = false; - return; - } } + emitFailed(tr("Failed to launch.")); + }else{ + // Offline + m_session = std::make_shared<AuthSession>(); + m_session->client_token = account->clientToken(); + m_session->access_token = account->accessToken(); + m_session->uuid = account->currentProfile()->id; + m_session->status = AuthSession::PlayableOffline; + m_session->MakeOffline(account->currentProfile()->name); + launchInstance(); } - emitFailed(tr("Failed to launch.")); } void LaunchController::launchInstance() diff --git a/application/dialogs/LoginDialog.cpp b/application/dialogs/LoginDialog.cpp index 484cb8e2..a74b898d 100644 --- a/application/dialogs/LoginDialog.cpp +++ b/application/dialogs/LoginDialog.cpp @@ -41,15 +41,22 @@ void LoginDialog::accept() setUserInputsEnabled(false); ui->progressBar->setVisible(true); - // Setup the login task and start it - m_account = MojangAccount::createFromUsername(ui->userTextBox->text()); - m_loginTask = m_account->login(nullptr, ui->passTextBox->text()); - connect(m_loginTask.get(), &Task::failed, this, &LoginDialog::onTaskFailed); - connect(m_loginTask.get(), &Task::succeeded, this, - &LoginDialog::onTaskSucceeded); - connect(m_loginTask.get(), &Task::status, this, &LoginDialog::onTaskStatus); - connect(m_loginTask.get(), &Task::progress, this, &LoginDialog::onTaskProgress); - m_loginTask->start(); + if(!ui->passTextBox->text().isEmpty()){ + // Online mode + // Setup the login task and start it + m_account = MojangAccount::createFromUsername(ui->userTextBox->text()); + m_loginTask = m_account->login(nullptr, ui->passTextBox->text()); + connect(m_loginTask.get(), &Task::failed, this, &LoginDialog::onTaskFailed); + connect(m_loginTask.get(), &Task::succeeded, this, + &LoginDialog::onTaskSucceeded); + connect(m_loginTask.get(), &Task::status, this, &LoginDialog::onTaskStatus); + connect(m_loginTask.get(), &Task::progress, this, &LoginDialog::onTaskProgress); + m_loginTask->start(); + }else{ + // Offline mode + m_account = MojangAccount::createFromUsernameOffline(ui->userTextBox->text()); + QDialog::accept(); + } } void LoginDialog::setUserInputsEnabled(bool enable) @@ -63,12 +70,12 @@ void LoginDialog::setUserInputsEnabled(bool enable) void LoginDialog::on_userTextBox_textEdited(const QString &newText) { ui->buttonBox->button(QDialogButtonBox::Ok) - ->setEnabled(!newText.isEmpty() && !ui->passTextBox->text().isEmpty()); + ->setEnabled(!newText.isEmpty()); } void LoginDialog::on_passTextBox_textEdited(const QString &newText) { ui->buttonBox->button(QDialogButtonBox::Ok) - ->setEnabled(!newText.isEmpty() && !ui->userTextBox->text().isEmpty()); + ->setEnabled(!ui->userTextBox->text().isEmpty()); } void LoginDialog::onTaskFailed(const QString &reason) diff --git a/application/pages/global/AccountListPage.cpp b/application/pages/global/AccountListPage.cpp index c14134f3..240fa33a 100644 --- a/application/pages/global/AccountListPage.cpp +++ b/application/pages/global/AccountListPage.cpp @@ -104,7 +104,7 @@ void AccountListPage::listChanged() void AccountListPage::on_actionAdd_triggered() { addAccount(tr("Please enter your Mojang or Minecraft account username and password to add " - "your account.")); + "your account.\nLeave password blank for an offline account.")); } void AccountListPage::on_actionRemove_triggered() |