PrismLauncher/launcher/LaunchController.cpp

416 lines
15 KiB
C++
Raw Normal View History

#include "LaunchController.h"
#include "minecraft/auth/AccountList.h"
2021-11-20 15:22:22 +00:00
#include "Application.h"
#include "ui/MainWindow.h"
#include "ui/InstanceWindow.h"
#include "ui/dialogs/CustomMessageBox.h"
#include "ui/dialogs/ProfileSelectDialog.h"
#include "ui/dialogs/ProgressDialog.h"
#include "ui/dialogs/EditAccountDialog.h"
#include "ui/dialogs/ProfileSetupDialog.h"
#include <QLineEdit>
#include <QInputDialog>
2015-08-14 01:27:01 +01:00
#include <QStringList>
#include <QHostInfo>
#include <QList>
#include <QHostAddress>
#include <QPushButton>
#include "BuildConfig.h"
#include "JavaCommon.h"
#include "tasks/Task.h"
#include "minecraft/auth/AccountTask.h"
#include "launch/steps/TextPrint.h"
LaunchController::LaunchController(QObject *parent) : Task(parent)
{
}
void LaunchController::executeTask()
{
2018-07-15 13:51:05 +01:00
if (!m_instance)
{
emitFailed(tr("No instance specified!"));
2018-07-15 13:51:05 +01:00
return;
}
JavaCommon::checkJVMArgs(m_instance->settings()->get("JvmArgs").toString(), m_parentWidget);
2018-07-15 13:51:05 +01:00
login();
}
void LaunchController::decideAccount()
{
if(m_accountToUse) {
return;
}
2018-07-15 13:51:05 +01:00
// Find an account to use.
2021-11-20 15:22:22 +00:00
auto accounts = APPLICATION->accounts();
2018-07-15 13:51:05 +01:00
if (accounts->count() <= 0)
{
// Tell the user they need to log in at least one account in order to play.
auto reply = CustomMessageBox::selectable(
m_parentWidget,
tr("No Accounts"),
2018-07-15 13:51:05 +01:00
tr("In order to play Minecraft, you must have at least one Mojang or Minecraft "
"account logged in. "
2018-07-15 13:51:05 +01:00
"Would you like to open the account manager to add an account now?"),
QMessageBox::Information,
QMessageBox::Yes | QMessageBox::No
)->exec();
2018-07-15 13:51:05 +01:00
if (reply == QMessageBox::Yes)
{
// Open the account manager.
2021-11-20 15:22:22 +00:00
APPLICATION->ShowGlobalSettings(m_parentWidget, "accounts");
2018-07-15 13:51:05 +01:00
}
}
2021-11-20 15:22:22 +00:00
m_accountToUse = accounts->defaultAccount();
if (!m_accountToUse)
2018-07-15 13:51:05 +01:00
{
// If no default account is set, ask the user which one to use.
ProfileSelectDialog selectDialog(
tr("Which account would you like to use?"),
ProfileSelectDialog::GlobalDefaultCheckbox,
m_parentWidget
);
2018-07-15 13:51:05 +01:00
selectDialog.exec();
2018-07-15 13:51:05 +01:00
// Launch the instance with the selected account.
m_accountToUse = selectDialog.selectedAccount();
2018-07-15 13:51:05 +01:00
// If the user said to use the account as default, do that.
if (selectDialog.useAsGlobalDefault() && m_accountToUse) {
2021-11-20 15:22:22 +00:00
accounts->setDefaultAccount(m_accountToUse);
}
2018-07-15 13:51:05 +01:00
}
}
void LaunchController::login() {
decideAccount();
2018-07-15 13:51:05 +01:00
// if no account is selected, we bail
if (!m_accountToUse)
2018-07-15 13:51:05 +01:00
{
emitFailed(tr("No account selected for launch."));
2018-07-15 13:51:05 +01:00
return;
}
2018-07-15 13:51:05 +01:00
// 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.");
2018-07-15 13:51:05 +01:00
QString failReason = needLoginAgain;
2018-07-15 13:51:05 +01:00
while (tryagain)
{
m_session = std::make_shared<AuthSession>();
m_session->wants_online = m_online;
m_accountToUse->fillSession(m_session);
2022-01-17 11:08:10 +00:00
// Launch immediately in true offline mode
if(m_accountToUse->isOffline()) {
launchInstance();
return;
}
switch(m_accountToUse->accountState()) {
case AccountState::Offline: {
m_session->wants_online = false;
// NOTE: fallthrough is intentional
2018-07-15 13:51:05 +01:00
}
case AccountState::Online: {
if(!m_session->wants_online) {
// 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
}
if(m_accountToUse->ownsMinecraft()) {
if(!m_accountToUse->hasProfile()) {
// Now handle setting up a profile name here...
ProfileSetupDialog dialog(m_accountToUse, m_parentWidget);
if (dialog.exec() == QDialog::Accepted)
{
tryagain = true;
continue;
}
else
{
emitFailed(tr("Received undetermined session status during login."));
return;
}
}
// we own Minecraft, there is a profile, it's all ready to go!
launchInstance();
return;
}
else {
// play demo ?
QMessageBox box(m_parentWidget);
box.setWindowTitle(tr("Play demo?"));
box.setText(tr("This account does not own Minecraft.\nYou need to purchase the game first to play it.\n\nDo you want to play the demo?"));
box.setIcon(QMessageBox::Warning);
auto demoButton = box.addButton(tr("Play Demo"), QMessageBox::ButtonRole::YesRole);
auto cancelButton = box.addButton(tr("Cancel"), QMessageBox::ButtonRole::NoRole);
box.setDefaultButton(cancelButton);
box.exec();
if(box.clickedButton() == demoButton) {
// play demo here
m_session->MakeDemo();
launchInstance();
}
else {
emitFailed(tr("Launch cancelled - account does not own Minecraft."));
}
}
return;
}
case AccountState::Errored:
// This means some sort of soft error that we can fix with a refresh ... so let's refresh.
case AccountState::Unchecked: {
m_accountToUse->refresh();
// NOTE: fallthrough intentional
}
case AccountState::Working: {
// refresh is in progress, we need to wait for it to finish to proceed.
ProgressDialog progDialog(m_parentWidget);
if (m_online)
{
progDialog.setSkipButton(true, tr("Play Offline"));
}
auto task = m_accountToUse->currentTask();
progDialog.execWithTask(task.get());
continue;
}
// FIXME: this is missing - the meaning is that the account is queued for refresh and we should wait for that
/*
case AccountState::Queued: {
return;
}
*/
case AccountState::Expired: {
auto errorString = tr("The account has expired and needs to be logged into manually again.");
QMessageBox::warning(
m_parentWidget,
tr("Account refresh failed"),
errorString,
QMessageBox::StandardButton::Ok,
QMessageBox::StandardButton::Ok
);
emitFailed(errorString);
return;
2018-07-15 13:51:05 +01:00
}
case AccountState::Disabled: {
auto errorString = tr("The launcher's client identification has changed. Please remove this account and add it again.");
QMessageBox::warning(
m_parentWidget,
tr("Client identification changed"),
errorString,
QMessageBox::StandardButton::Ok,
QMessageBox::StandardButton::Ok
);
emitFailed(errorString);
return;
}
case AccountState::Gone: {
auto errorString = tr("The account no longer exists on the servers. It may have been migrated, in which case please add the new account you migrated this one to.");
QMessageBox::warning(
m_parentWidget,
tr("Account gone"),
errorString,
QMessageBox::StandardButton::Ok,
QMessageBox::StandardButton::Ok
);
emitFailed(errorString);
return;
}
2018-07-15 13:51:05 +01:00
}
}
emitFailed(tr("Failed to launch."));
}
void LaunchController::launchInstance()
{
2018-07-15 13:51:05 +01:00
Q_ASSERT_X(m_instance != NULL, "launchInstance", "instance is NULL");
Q_ASSERT_X(m_session.get() != nullptr, "launchInstance", "session is NULL");
2018-07-15 13:51:05 +01:00
if(!m_instance->reloadSettings())
{
QMessageBox::critical(m_parentWidget, tr("Error!"), tr("Couldn't load the instance profile."));
2018-07-15 13:51:05 +01:00
emitFailed(tr("Couldn't load the instance profile."));
return;
}
m_launcher = m_instance->createLaunchTask(m_session, m_serverToJoin);
2018-07-15 13:51:05 +01:00
if (!m_launcher)
{
emitFailed(tr("Couldn't instantiate a launcher."));
return;
}
2018-07-15 13:51:05 +01:00
auto console = qobject_cast<InstanceWindow *>(m_parentWidget);
auto showConsole = m_instance->settings()->get("ShowConsole").toBool();
if(!console && showConsole)
{
2021-11-20 15:22:22 +00:00
APPLICATION->showInstanceWindow(m_instance);
2018-07-15 13:51:05 +01:00
}
connect(m_launcher.get(), &LaunchTask::readyForLaunch, this, &LaunchController::readyForLaunch);
connect(m_launcher.get(), &LaunchTask::succeeded, this, &LaunchController::onSucceeded);
connect(m_launcher.get(), &LaunchTask::failed, this, &LaunchController::onFailed);
connect(m_launcher.get(), &LaunchTask::requestProgress, this, &LaunchController::onProgressRequested);
// Prepend Online and Auth Status
QString online_mode;
if(m_session->wants_online) {
online_mode = "online";
// Prepend Server Status
QStringList servers = {"authserver.mojang.com", "session.minecraft.net", "textures.minecraft.net", "api.mojang.com"};
QString resolved_servers = "";
QHostInfo host_info;
for(QString server : servers) {
host_info = QHostInfo::fromName(server);
resolved_servers = resolved_servers + server + " resolves to:\n [";
if(!host_info.addresses().isEmpty()) {
for(QHostAddress address : host_info.addresses()) {
resolved_servers = resolved_servers + address.toString();
if(!host_info.addresses().endsWith(address)) {
resolved_servers = resolved_servers + ", ";
}
}
} else {
resolved_servers = resolved_servers + "N/A";
}
resolved_servers = resolved_servers + "]\n\n";
}
2021-10-17 23:47:02 +01:00
m_launcher->prependStep(new TextPrint(m_launcher.get(), resolved_servers, MessageLevel::Launcher));
} else {
online_mode = "offline";
}
m_launcher->prependStep(new TextPrint(m_launcher.get(), "Launched instance in " + online_mode + " mode\n", MessageLevel::Launcher));
// Prepend Version
2021-10-17 23:47:02 +01:00
m_launcher->prependStep(new TextPrint(m_launcher.get(), BuildConfig.LAUNCHER_NAME + " version: " + BuildConfig.printableVersionString() + "\n\n", MessageLevel::Launcher));
2018-07-15 13:51:05 +01:00
m_launcher->start();
}
void LaunchController::readyForLaunch()
{
2018-07-15 13:51:05 +01:00
if (!m_profiler)
{
m_launcher->proceed();
return;
}
2018-07-15 13:51:05 +01:00
QString error;
if (!m_profiler->check(&error))
{
m_launcher->abort();
QMessageBox::critical(m_parentWidget, tr("Error!"), tr("Couldn't start profiler: %1").arg(error));
emitFailed("Profiler startup failed!");
2018-07-15 13:51:05 +01:00
return;
}
BaseProfiler *profilerInstance = m_profiler->createProfiler(m_launcher->instance(), this);
2018-07-15 13:51:05 +01:00
connect(profilerInstance, &BaseProfiler::readyToLaunch, [this](const QString & message)
{
QMessageBox msg;
msg.setText(tr("The game launch is delayed until you press the "
"button. This is the right time to setup the profiler, as the "
"profiler server is running now.\n\n%1").arg(message));
msg.setWindowTitle(tr("Waiting."));
2018-07-15 13:51:05 +01:00
msg.setIcon(QMessageBox::Information);
msg.addButton(tr("Launch"), QMessageBox::AcceptRole);
msg.setModal(true);
msg.exec();
m_launcher->proceed();
});
connect(profilerInstance, &BaseProfiler::abortLaunch, [this](const QString & message)
{
QMessageBox msg;
msg.setText(tr("Couldn't start the profiler: %1").arg(message));
msg.setWindowTitle(tr("Error"));
msg.setIcon(QMessageBox::Critical);
msg.addButton(QMessageBox::Ok);
msg.setModal(true);
msg.exec();
m_launcher->abort();
emitFailed("Profiler startup failed!");
2018-07-15 13:51:05 +01:00
});
profilerInstance->beginProfiling(m_launcher);
}
void LaunchController::onSucceeded()
{
2018-07-15 13:51:05 +01:00
emitSucceeded();
}
void LaunchController::onFailed(QString reason)
{
2018-07-15 13:51:05 +01:00
if(m_instance->settings()->get("ShowConsoleOnError").toBool())
{
2021-11-20 15:22:22 +00:00
APPLICATION->showInstanceWindow(m_instance, "console");
2018-07-15 13:51:05 +01:00
}
emitFailed(reason);
}
void LaunchController::onProgressRequested(Task* task)
{
2018-07-15 13:51:05 +01:00
ProgressDialog progDialog(m_parentWidget);
progDialog.setSkipButton(true, tr("Abort"));
m_launcher->proceed();
progDialog.execWithTask(task);
}
bool LaunchController::abort()
{
2018-07-15 13:51:05 +01:00
if(!m_launcher)
{
return true;
}
if(!m_launcher->canAbort())
{
return false;
}
auto response = CustomMessageBox::selectable(
m_parentWidget, tr("Kill Minecraft?"),
tr("This can cause the instance to get corrupted and should only be used if Minecraft "
"is frozen for some reason"),
QMessageBox::Question, QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes)->exec();
if (response == QMessageBox::Yes)
{
return m_launcher->abort();
}
return false;
}