From 7f1320390c55e408a4b0081b2606eab3892a8148 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Sun, 5 Jul 2015 01:54:30 +0200 Subject: GH-1053 move launch process UI to a separate class --- application/CMakeLists.txt | 4 + application/ConsoleWindow.cpp | 27 ++--- application/ConsoleWindow.h | 4 +- application/LaunchController.cpp | 231 +++++++++++++++++++++++++++++++++++++++ application/LaunchController.h | 49 +++++++++ application/MainWindow.cpp | 220 +++---------------------------------- application/MainWindow.h | 21 +--- 7 files changed, 312 insertions(+), 244 deletions(-) create mode 100644 application/LaunchController.cpp create mode 100644 application/LaunchController.h (limited to 'application') diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt index a9ee1da6..08099cae 100644 --- a/application/CMakeLists.txt +++ b/application/CMakeLists.txt @@ -136,6 +136,10 @@ SET(MULTIMC_SOURCES ConsoleWindow.h ConsoleWindow.cpp + # Processes + LaunchController.h + LaunchController.cpp + # page provider for instances InstancePageProvider.h InstancePageProvider.cpp diff --git a/application/ConsoleWindow.cpp b/application/ConsoleWindow.cpp index 98492414..84140516 100644 --- a/application/ConsoleWindow.cpp +++ b/application/ConsoleWindow.cpp @@ -129,12 +129,8 @@ ConsoleWindow::ConsoleWindow(std::shared_ptr process, QWidget *par } // Set up signal connections - connect(m_proc.get(), SIGNAL(ended(InstancePtr, int, QProcess::ExitStatus)), this, - SLOT(onEnded(InstancePtr, int, QProcess::ExitStatus))); - connect(m_proc.get(), SIGNAL(prelaunch_failed(InstancePtr, int, QProcess::ExitStatus)), this, - SLOT(onEnded(InstancePtr, int, QProcess::ExitStatus))); - connect(m_proc.get(), SIGNAL(launch_failed(InstancePtr)), this, - SLOT(onLaunchFailed(InstancePtr))); + connect(m_proc.get(), &BaseLauncher::succeeded, this, &ConsoleWindow::onSucceeded); + connect(m_proc.get(), &BaseLauncher::failed, this, &ConsoleWindow::onFailed); setMayClose(false); @@ -220,18 +216,14 @@ void ConsoleWindow::on_btnKillMinecraft_clicked() m_killButton->setEnabled(true); } -void ConsoleWindow::onEnded(InstancePtr instance, int code, QProcess::ExitStatus status) +void ConsoleWindow::onSucceeded() { - bool peacefulExit = code == 0 && status != QProcess::CrashExit; m_killButton->setEnabled(false); setMayClose(true); - if (instance->settings()->get("AutoCloseConsole").toBool()) + if (m_proc->instance()->settings()->get("AutoCloseConsole").toBool()) { - if (peacefulExit) - { - this->close(); - return; - } + this->close(); + return; } if (!isVisible()) { @@ -245,15 +237,16 @@ void ConsoleWindow::onEnded(InstancePtr instance, int code, QProcess::ExitStatus } } -void ConsoleWindow::onLaunchFailed(InstancePtr instance) +void ConsoleWindow::onFailed(QString reason) { m_killButton->setEnabled(false); - setMayClose(true); - if (!isVisible()) + { show(); + } } + ConsoleWindow::~ConsoleWindow() { diff --git a/application/ConsoleWindow.h b/application/ConsoleWindow.h index 33ae67c7..228b58e9 100644 --- a/application/ConsoleWindow.h +++ b/application/ConsoleWindow.h @@ -44,8 +44,8 @@ slots: void on_closeButton_clicked(); void on_btnKillMinecraft_clicked(); - void onEnded(InstancePtr instance, int code, QProcess::ExitStatus status); - void onLaunchFailed(InstancePtr instance); + void onSucceeded(); + void onFailed(QString reason); // FIXME: add handlers for the other MinecraftLauncher signals (pre/post launch command // failures) diff --git a/application/LaunchController.cpp b/application/LaunchController.cpp new file mode 100644 index 00000000..8680e557 --- /dev/null +++ b/application/LaunchController.cpp @@ -0,0 +1,231 @@ +#include "LaunchController.h" +#include +#include "MultiMC.h" +#include "dialogs/CustomMessageBox.h" +#include "dialogs/AccountSelectDialog.h" +#include "dialogs/ProgressDialog.h" +#include "dialogs/EditAccountDialog.h" +#include "ConsoleWindow.h" +#include "BuildConfig.h" +#include "JavaCommon.h" +#include +#include +#include +#include + +LaunchController::LaunchController(QObject *parent) : QObject(parent) +{ +} + +void LaunchController::launch() +{ + login(); +} + +void LaunchController::login() +{ + if (!m_instance) + return; + + JavaCommon::checkJVMArgs(m_instance->settings()->get("JvmArgs").toString(), m_parentWidget); + + // Find an account to use. + std::shared_ptr accounts = MMC->accounts(); + MojangAccountPtr account = accounts->activeAccount(); + 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"), + tr("In order to play Minecraft, you must have at least one Mojang or Minecraft " + "account logged in to MultiMC." + "Would you like to open the account manager to add an account now?"), + QMessageBox::Information, QMessageBox::Yes | QMessageBox::No)->exec(); + + if (reply == QMessageBox::Yes) + { + // Open the account manager. + //on_actionManageAccounts_triggered(); + } + } + else if (account.get() == nullptr) + { + // If no default account is set, ask the user which one to use. + AccountSelectDialog selectDialog(tr("Which account would you like to use?"), + AccountSelectDialog::GlobalDefaultCheckbox, m_parentWidget); + + selectDialog.exec(); + + // Launch the instance with the selected account. + account = selectDialog.selectedAccount(); + + // If the user said to use the account as default, do that. + if (selectDialog.useAsGlobalDefault() && account.get() != nullptr) + accounts->setActiveAccount(account->username()); + } + + // if no account is selected, we bail + if (!account.get()) + 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. + QString failReason = tr("Your account is currently not logged in. Please enter " + "your password to log in again."); + + while (tryagain) + { + m_session = std::make_shared(); + m_session->wants_online = m_online; + auto task = account->login(m_session, password); + if (task) + { + // 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.exec(task.get()); + if (!task->successful()) + { + failReason = task->failReason(); + } + } + switch (m_session->status) + { + case AuthSession::Undetermined: + { + qCritical() << "Received undetermined session status during login. Bye."; + tryagain = false; + break; + } + case AuthSession::RequiresPassword: + { + EditAccountDialog passDialog(failReason, m_parentWidget, EditAccountDialog::PasswordField); + if (passDialog.exec() == QDialog::Accepted) + { + password = passDialog.password(); + } + else + { + tryagain = false; + } + 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) + { + tryagain = false; + break; + } + if (name.length()) + { + usedname = name; + } + m_session->MakeOffline(usedname); + // offline flavored game from here :3 + } + case AuthSession::PlayableOnline: + { + launchInstance(); + tryagain = false; + } + } + } +} + +void LaunchController::launchInstance() +{ + Q_ASSERT_X(m_instance != NULL, "launchInstance", "instance is NULL"); + Q_ASSERT_X(m_session.get() != nullptr, "launchInstance", "session is NULL"); + + if(!m_instance->reload()) + { + QMessageBox::critical(m_parentWidget, tr("Error"), tr("Couldn't load the instance profile.")); + return; + } + + m_launcher = m_instance->prepareForLaunch(m_session); + if (!m_launcher) + { + return; + } + + m_launcher->setProfiler(m_profiler); + + if(m_parentWidget) + { + m_parentWidget->hide(); + } + + m_console = new ConsoleWindow(m_launcher); + connect(m_console, &ConsoleWindow::isClosing, this, &LaunchController::instanceEnded); + connect(m_launcher.get(), &BaseLauncher::readyForLaunch, this, &LaunchController::readyForLaunch); + + m_launcher->setHeader("MultiMC version: " + BuildConfig.printableVersionString() + "\n\n"); + m_launcher->start(); +} + +void LaunchController::readyForLaunch() +{ + auto profiler = m_launcher->getProfiler(); + + if (!profiler) + { + m_launcher->launch(); + return; + } + + QString error; + if (!profiler->check(&error)) + { + m_launcher->abort(); + QMessageBox::critical(m_parentWidget, tr("Error"), tr("Couldn't start profiler: %1").arg(error)); + return; + } + BaseProfiler *profilerInstance = profiler->createProfiler(m_launcher->instance(), this); + + 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")); + msg.setIcon(QMessageBox::Information); + msg.addButton(tr("Launch"), QMessageBox::AcceptRole); + msg.setModal(true); + msg.exec(); + m_launcher->launch(); + }); + 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(); + }); + profilerInstance->beginProfiling(m_launcher); +} + +void LaunchController::instanceEnded() +{ + if(m_parentWidget) + { + m_parentWidget->show(); + } +} diff --git a/application/LaunchController.h b/application/LaunchController.h new file mode 100644 index 00000000..f62d5124 --- /dev/null +++ b/application/LaunchController.h @@ -0,0 +1,49 @@ +#pragma once +#include +#include +#include + +class ConsoleWindow; +class LaunchController: public QObject +{ + Q_OBJECT +public: + LaunchController(QObject * parent = nullptr); + virtual ~LaunchController(){}; + + void setInstance(InstancePtr instance) + { + m_instance = instance; + } + void setOnline(bool online) + { + m_online = online; + } + void setProfiler(BaseProfilerFactory *profiler) + { + m_profiler = profiler; + } + void setParentWidget(QWidget * widget) + { + m_parentWidget = widget; + } + + void launch(); + +private: + void login(); + void launchInstance(); + +private slots: + void readyForLaunch(); + void instanceEnded(); + +private: + BaseProfilerFactory *m_profiler = nullptr; + bool m_online = true; + InstancePtr m_instance; + QWidget * m_parentWidget = nullptr; + ConsoleWindow *m_console = nullptr; + AuthSessionPtr m_session; + std::shared_ptr m_launcher; +}; diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index eb258d20..e5961693 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -357,7 +357,6 @@ namespace Ui { #include "pages/global/JavaPage.h" #include "pages/global/MinecraftPage.h" -#include "ConsoleWindow.h" #include "pagedialog/PageDialog.h" #include "InstanceList.h" @@ -382,6 +381,7 @@ namespace Ui { #include "java/JavaUtils.h" #include "JavaCommon.h" #include "InstancePageProvider.h" +#include "LaunchController.h" #include "minecraft/SkinUtils.h" #include "resources/Resource.h" @@ -719,7 +719,7 @@ void MainWindow::updateToolsMenu() QAction *normalLaunch = launchMenu->addAction(tr("Launch")); connect(normalLaunch, &QAction::triggered, [this]() - { doLaunch(); }); + { launch(m_selectedInstance); }); launchMenu->addSeparator()->setText(tr("Profilers")); for (auto profiler : MMC->profilers().values()) { @@ -734,7 +734,7 @@ void MainWindow::updateToolsMenu() else { connect(profilerAction, &QAction::triggered, [this, profiler]() - { doLaunch(true, profiler.get()); }); + { launch(m_selectedInstance, true, profiler.get()); }); } } launchMenu->addSeparator()->setText(tr("Tools")); @@ -1563,17 +1563,14 @@ void MainWindow::instanceActivated(QModelIndex index) if (!inst) return; - JavaCommon::checkJVMArgs(inst->settings()->get("JvmArgs").toString(), this); - - doLaunch(); + launch(inst); } void MainWindow::on_actionLaunchInstance_triggered() { if (m_selectedInstance) { - JavaCommon::checkJVMArgs(m_selectedInstance->settings()->get("JvmArgs").toString(), this); - doLaunch(); + launch(m_selectedInstance); } } @@ -1581,207 +1578,21 @@ void MainWindow::on_actionLaunchInstanceOffline_triggered() { if (m_selectedInstance) { - JavaCommon::checkJVMArgs(m_selectedInstance->settings()->get("JvmArgs").toString(), this); - doLaunch(false); + launch(m_selectedInstance, false); } } -void MainWindow::doLaunch(bool online, BaseProfilerFactory *profiler) +void MainWindow::launch(InstancePtr instance, bool online, BaseProfilerFactory* profiler) { - if (!m_selectedInstance) - return; - - // Find an account to use. - std::shared_ptr accounts = MMC->accounts(); - MojangAccountPtr account = accounts->activeAccount(); - if (accounts->count() <= 0) - { - // Tell the user they need to log in at least one account in order to play. - auto reply = CustomMessageBox::selectable( - this, tr("No Accounts"), - tr("In order to play Minecraft, you must have at least one Mojang or Minecraft " - "account logged in to MultiMC." - "Would you like to open the account manager to add an account now?"), - QMessageBox::Information, QMessageBox::Yes | QMessageBox::No)->exec(); - - if (reply == QMessageBox::Yes) - { - // Open the account manager. - on_actionManageAccounts_triggered(); - } - } - else if (account.get() == nullptr) - { - // If no default account is set, ask the user which one to use. - AccountSelectDialog selectDialog(tr("Which account would you like to use?"), - AccountSelectDialog::GlobalDefaultCheckbox, this); - - selectDialog.exec(); - - // Launch the instance with the selected account. - account = selectDialog.selectedAccount(); - - // If the user said to use the account as default, do that. - if (selectDialog.useAsGlobalDefault() && account.get() != nullptr) - accounts->setActiveAccount(account->username()); - } - - // if no account is selected, we bail - if (!account.get()) - 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. - QString failReason = tr("Your account is currently not logged in. Please enter " - "your password to log in again."); - - while (tryagain) - { - AuthSessionPtr session(new AuthSession()); - session->wants_online = online; - auto task = account->login(session, password); - if (task) - { - // We'll need to validate the access token to make sure the account - // is still logged in. - ProgressDialog progDialog(this); - if (online) - progDialog.setSkipButton(true, tr("Play Offline")); - progDialog.exec(task.get()); - if (!task->successful()) - { - failReason = task->failReason(); - } - } - switch (session->status) - { - case AuthSession::Undetermined: - { - qCritical() << "Received undetermined session status during login. Bye."; - tryagain = false; - break; - } - case AuthSession::RequiresPassword: - { - EditAccountDialog passDialog(failReason, this, EditAccountDialog::PasswordField); - if (passDialog.exec() == QDialog::Accepted) - { - password = passDialog.password(); - } - else - { - tryagain = false; - } - break; - } - case AuthSession::PlayableOffline: - { - // we ask the user for a player name - bool ok = false; - QString usedname = session->player_name; - QString name = QInputDialog::getText(this, tr("Player name"), - tr("Choose your offline mode player name."), - QLineEdit::Normal, session->player_name, &ok); - if (!ok) - { - tryagain = false; - break; - } - if (name.length()) - { - usedname = name; - } - session->MakeOffline(usedname); - // offline flavored game from here :3 - } - case AuthSession::PlayableOnline: - { - launchInstance(m_selectedInstance, session, profiler); - tryagain = false; - } - } - } -} - -void MainWindow::launchInstance(InstancePtr instance, AuthSessionPtr session, BaseProfilerFactory *profiler) -{ - Q_ASSERT_X(instance != NULL, "launchInstance", "instance is NULL"); - Q_ASSERT_X(session.get() != nullptr, "launchInstance", "session is NULL"); - - QString launchScript; - - if(!instance->reload()) - { - QMessageBox::critical(this, tr("Error"), tr("Couldn't load the instance profile.")); - return; - } - - auto proc = instance->prepareForLaunch(session); - if (!proc) - return; - - proc->setProfiler(profiler); - - this->hide(); - - console = new ConsoleWindow(proc); - connect(console, &ConsoleWindow::isClosing, this, &MainWindow::instanceEnded); - connect(proc.get(), &BaseLauncher::readyForLaunch, this, &MainWindow::readyForLaunch); - - proc->setHeader("MultiMC version: " + BuildConfig.printableVersionString() + "\n\n"); - proc->start(); + m_launchController = std::make_shared(); + m_launchController->setInstance(instance); + m_launchController->setOnline(online); + m_launchController->setParentWidget(this); + m_launchController->setProfiler(profiler); + m_launchController->launch(); } -void MainWindow::readyForLaunch(std::shared_ptr launcher) -{ - auto profiler = launcher->getProfiler(); - - if (!profiler) - { - launcher->launch(); - return; - } - QString error; - if (!profiler->check(&error)) - { - launcher->abort(); - QMessageBox::critical(this, tr("Error"), tr("Couldn't start profiler: %1").arg(error)); - return; - } - BaseProfiler *profilerInstance = profiler->createProfiler(launcher->instance(), this); - - connect(profilerInstance, &BaseProfiler::readyToLaunch, - [this, launcher](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")); - msg.setIcon(QMessageBox::Information); - msg.addButton(tr("Launch"), QMessageBox::AcceptRole); - msg.setModal(true); - msg.exec(); - launcher->launch(); - }); - connect(profilerInstance, &BaseProfiler::abortLaunch, - [this, launcher](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(); - launcher->abort(); - }); - profilerInstance->beginProfiling(launcher); -} /* void MainWindow::onGameUpdateError(QString error) { @@ -1871,11 +1682,6 @@ void MainWindow::selectionBad() setSelectedInstanceById(MMC->settings()->get("SelectedInstance").toString()); } -void MainWindow::instanceEnded() -{ - this->show(); -} - void MainWindow::checkSetDefaultJava() { const QString javaHack = "IntelHack"; diff --git a/application/MainWindow.h b/application/MainWindow.h index 292a8d28..505e5f13 100644 --- a/application/MainWindow.h +++ b/application/MainWindow.h @@ -24,6 +24,7 @@ #include "net/NetJob.h" #include "updater/GoUpdate.h" +class LaunchController; class NewsChecker; class NotificationChecker; class QToolButton; @@ -31,7 +32,6 @@ class InstanceProxyModel; class LabeledToolButton; class QLabel; class MinecraftLauncher; -class ConsoleWindow; class BaseProfilerFactory; class GenericPageProvider; @@ -116,25 +116,9 @@ slots: void on_actionScreenshots_triggered(); - /*! - * Launches the currently selected instance with the default account. - * If no default account is selected, prompts the user to pick an account. - */ - void doLaunch(bool online = true, BaseProfilerFactory *profiler = 0); - - /*! - * Launches the given instance with the given account. - * This function assumes that the given account has a valid, usable access token. - */ - void launchInstance(InstancePtr instance, AuthSessionPtr session, BaseProfilerFactory *profiler = 0); - - void readyForLaunch(std::shared_ptr); - void taskStart(); void taskEnd(); - void instanceEnded(); - // called when an icon is changed in the icon model. void iconUpdated(QString); @@ -185,13 +169,13 @@ protected: void instanceFromVersion(QString instName, QString instGroup, QString instIcon, BaseVersionPtr version); void instanceFromZipPack(QString instName, QString instGroup, QString instIcon, QUrl url); void finalizeInstance(InstancePtr inst); + void launch(InstancePtr instance, bool online = true, BaseProfilerFactory *profiler = nullptr); private: Ui::MainWindow *ui; class GroupView *view; InstanceProxyModel *proxymodel; NetJobPtr skin_download_job; - ConsoleWindow *console; LabeledToolButton *renameButton; QToolButton *changeIconButton; QToolButton *newsLabel; @@ -199,6 +183,7 @@ private: std::shared_ptr m_globalSettingsProvider; std::shared_ptr m_newsChecker; std::shared_ptr m_notificationChecker; + std::shared_ptr m_launchController; InstancePtr m_selectedInstance; QString m_currentInstIcon; -- cgit v1.2.3