From 5dd48e89f5a7facf8355641d0caf8deaec2a03ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Fri, 10 Jul 2015 00:06:05 +0200 Subject: GH-1034 do jar modding separate from update --- application/CMakeLists.txt | 4 +- application/LaunchController.cpp | 228 ------------------------------------ application/LaunchController.h | 49 -------- application/LaunchInteraction.cpp | 229 +++++++++++++++++++++++++++++++++++++ application/LaunchInteraction.h | 49 ++++++++ application/MainWindow.cpp | 4 +- application/pages/VersionPage.cpp | 2 +- logic/BaseInstance.h | 18 ++- logic/BaseLauncher.cpp | 26 ++++- logic/BaseLauncher.h | 6 + logic/NullInstance.h | 8 +- logic/ftb/OneSixFTBInstance.cpp | 4 +- logic/ftb/OneSixFTBInstance.h | 2 +- logic/minecraft/LegacyInstance.cpp | 86 +++++++++++++- logic/minecraft/LegacyInstance.h | 7 +- logic/minecraft/LegacyUpdate.cpp | 74 +----------- logic/minecraft/LegacyUpdate.h | 2 - logic/minecraft/OneSixInstance.cpp | 62 +++++++++- logic/minecraft/OneSixInstance.h | 5 +- logic/minecraft/OneSixUpdate.cpp | 40 ------- 20 files changed, 489 insertions(+), 416 deletions(-) delete mode 100644 application/LaunchController.cpp delete mode 100644 application/LaunchController.h create mode 100644 application/LaunchInteraction.cpp create mode 100644 application/LaunchInteraction.h diff --git a/application/CMakeLists.txt b/application/CMakeLists.txt index 281f402a..ae4c0a88 100644 --- a/application/CMakeLists.txt +++ b/application/CMakeLists.txt @@ -141,8 +141,8 @@ SET(MULTIMC_SOURCES SettingsUI.cpp # Processes - LaunchController.h - LaunchController.cpp + LaunchInteraction.h + LaunchInteraction.cpp # page provider for instances InstancePageProvider.h diff --git a/application/LaunchController.cpp b/application/LaunchController.cpp deleted file mode 100644 index b1116e07..00000000 --- a/application/LaunchController.cpp +++ /dev/null @@ -1,228 +0,0 @@ -#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 "SettingsUI.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. - SettingsUI::ShowPageDialog(MMC->globalSettingsPages(), m_parentWidget, "accounts"); - } - } - 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; - } - - 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() -{ - if (!m_profiler) - { - m_launcher->launch(); - return; - } - - QString error; - if (!m_profiler->check(&error)) - { - m_launcher->abort(); - QMessageBox::critical(m_parentWidget, tr("Error"), tr("Couldn't start profiler: %1").arg(error)); - return; - } - BaseProfiler *profilerInstance = m_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 deleted file mode 100644 index f62d5124..00000000 --- a/application/LaunchController.h +++ /dev/null @@ -1,49 +0,0 @@ -#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/LaunchInteraction.cpp b/application/LaunchInteraction.cpp new file mode 100644 index 00000000..ea966129 --- /dev/null +++ b/application/LaunchInteraction.cpp @@ -0,0 +1,229 @@ +#include "LaunchInteraction.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 "SettingsUI.h" +#include +#include +#include +#include + +LaunchController::LaunchController(QObject *parent) : QObject(parent) +{ +} + +void LaunchController::launch() +{ + login(); +} + +// FIXME: minecraft specific +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. + SettingsUI::ShowPageDialog(MMC->globalSettingsPages(), m_parentWidget, "accounts"); + } + } + 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->createLaunchTask(m_session); + if (!m_launcher) + { + return; + } + + 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() +{ + if (!m_profiler) + { + m_launcher->launch(); + return; + } + + QString error; + if (!m_profiler->check(&error)) + { + m_launcher->abort(); + QMessageBox::critical(m_parentWidget, tr("Error"), tr("Couldn't start profiler: %1").arg(error)); + return; + } + BaseProfiler *profilerInstance = m_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/LaunchInteraction.h b/application/LaunchInteraction.h new file mode 100644 index 00000000..f62d5124 --- /dev/null +++ b/application/LaunchInteraction.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 4be80492..bd50e06d 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -372,7 +372,7 @@ namespace Ui { #include "java/JavaUtils.h" #include "JavaCommon.h" #include "InstancePageProvider.h" -#include "LaunchController.h" +#include "LaunchInteraction.h" #include "SettingsUI.h" #include "minecraft/SkinUtils.h" #include "resources/Resource.h" @@ -1184,7 +1184,7 @@ void MainWindow::finalizeInstance(InstancePtr inst) if (MMC->accounts()->anyAccountIsValid()) { ProgressDialog loadDialog(this); - auto update = inst->doUpdate(); + auto update = inst->createUpdateTask(); connect(update.get(), &Task::failed, [this](QString reason) { QString error = QString("Instance load failed: %1").arg(reason); diff --git a/application/pages/VersionPage.cpp b/application/pages/VersionPage.cpp index efc0b446..81da0180 100644 --- a/application/pages/VersionPage.cpp +++ b/application/pages/VersionPage.cpp @@ -269,7 +269,7 @@ void VersionPage::on_changeVersionBtn_clicked() int VersionPage::doUpdate() { - auto updateTask = m_inst->doUpdate(); + auto updateTask = m_inst->createUpdateTask(); if (!updateTask) { return 1; diff --git a/logic/BaseInstance.h b/logic/BaseInstance.h index 497431d0..720385c8 100644 --- a/logic/BaseInstance.h +++ b/logic/BaseInstance.h @@ -135,13 +135,21 @@ public: virtual SettingsObjectPtr settings() const; /// returns a valid update task - virtual std::shared_ptr doUpdate() = 0; + virtual std::shared_ptr createUpdateTask() = 0; - /// returns a valid process, ready for launch with the given account. - virtual std::shared_ptr prepareForLaunch(AuthSessionPtr account) = 0; + /// returns a valid launcher (task container) + virtual std::shared_ptr createLaunchTask(AuthSessionPtr account) = 0; - /// do any necessary cleanups after the instance finishes. also runs before - /// 'prepareForLaunch' + /*! + * Returns a task that should be done right before launch + * This task should do any extra preparations needed + */ + virtual std::shared_ptr createJarModdingTask() = 0; + + /*! + * does any necessary cleanups after the instance finishes. also runs before\ + * TODO: turn into a task that can run asynchronously + */ virtual void cleanupAfterRun() = 0; virtual QString getStatusbarDescription() = 0; diff --git a/logic/BaseLauncher.cpp b/logic/BaseLauncher.cpp index 8dfb5f6d..4ab0c41a 100644 --- a/logic/BaseLauncher.cpp +++ b/logic/BaseLauncher.cpp @@ -421,7 +421,7 @@ void BaseLauncher::on_pre_state(LoggedProcess::State state) void BaseLauncher::updateInstance() { - m_updateTask = m_instance->doUpdate(); + m_updateTask = m_instance->createUpdateTask(); if(m_updateTask) { connect(m_updateTask.get(), SIGNAL(finished()), this, SLOT(updateFinished())); @@ -435,7 +435,7 @@ void BaseLauncher::updateFinished() { if(m_updateTask->successful()) { - makeReady(); + doJarModding(); } else { @@ -445,6 +445,28 @@ void BaseLauncher::updateFinished() } } +void BaseLauncher::doJarModding() +{ + m_jarModTask = m_instance->createJarModdingTask(); + if(!m_jarModTask) + { + jarModdingSucceeded(); + } + connect(m_jarModTask.get(), SIGNAL(succeeded()), this, SLOT(jarModdingSucceeded())); + connect(m_jarModTask.get(), SIGNAL(failed(QString)), this, SLOT(jarModdingFailed(QString))); + m_jarModTask->start(); +} + +void BaseLauncher::jarModdingSucceeded() +{ + makeReady(); +} + +void BaseLauncher::jarModdingFailed(QString reason) +{ + emitFailed(reason); +} + void BaseLauncher::makeReady() { QStringList args = javaArguments(); diff --git a/logic/BaseLauncher.h b/logic/BaseLauncher.h index d8877b3d..f7b52c89 100644 --- a/logic/BaseLauncher.h +++ b/logic/BaseLauncher.h @@ -95,6 +95,7 @@ public: /* HACK: MINECRAFT: split! */ protected: /* methods */ void preLaunch(); void updateInstance(); + void doJarModding(); void makeReady(); void postLaunch(); virtual void emitFailed(QString reason); @@ -108,6 +109,10 @@ protected: /* methods */ virtual QString censorPrivateInfo(QString in); virtual MessageLevel::Enum guessLevel(const QString &message, MessageLevel::Enum defaultLevel); +protected slots: + void jarModdingSucceeded(); + void jarModdingFailed(QString reason); + signals: /** * @brief emitted when the launch preparations are done @@ -161,6 +166,7 @@ protected: /* HACK: MINECRAFT: split! */ QString launchScript; QString m_nativeFolder; std::shared_ptr m_updateTask; + std::shared_ptr m_jarModTask; protected: /* HACK: MINECRAFT: split! */ void checkJava(); diff --git a/logic/NullInstance.h b/logic/NullInstance.h index 6f737e86..0927322e 100644 --- a/logic/NullInstance.h +++ b/logic/NullInstance.h @@ -43,11 +43,15 @@ public: { return instanceRoot(); }; - virtual std::shared_ptr prepareForLaunch(AuthSessionPtr) + virtual std::shared_ptr createLaunchTask(AuthSessionPtr) { return nullptr; } - virtual std::shared_ptr< Task > doUpdate() + virtual std::shared_ptr< Task > createUpdateTask() + { + return nullptr; + } + virtual std::shared_ptr createJarModdingTask() { return nullptr; } diff --git a/logic/ftb/OneSixFTBInstance.cpp b/logic/ftb/OneSixFTBInstance.cpp index 88c885b9..7cce1f48 100644 --- a/logic/ftb/OneSixFTBInstance.cpp +++ b/logic/ftb/OneSixFTBInstance.cpp @@ -134,9 +134,9 @@ QString OneSixFTBInstance::getStatusbarDescription() return "OneSix FTB: " + intendedVersionId(); } -std::shared_ptr OneSixFTBInstance::doUpdate() +std::shared_ptr OneSixFTBInstance::createUpdateTask() { - return OneSixInstance::doUpdate(); + return OneSixInstance::createUpdateTask(); } #include "OneSixFTBInstance.moc" diff --git a/logic/ftb/OneSixFTBInstance.h b/logic/ftb/OneSixFTBInstance.h index dd652fd1..564f275d 100644 --- a/logic/ftb/OneSixFTBInstance.h +++ b/logic/ftb/OneSixFTBInstance.h @@ -17,7 +17,7 @@ public: virtual QString getStatusbarDescription(); - virtual std::shared_ptr doUpdate() override; + virtual std::shared_ptr createUpdateTask() override; virtual QString id() const; diff --git a/logic/minecraft/LegacyInstance.cpp b/logic/minecraft/LegacyInstance.cpp index 3fe51a4a..8431c35b 100644 --- a/logic/minecraft/LegacyInstance.cpp +++ b/logic/minecraft/LegacyInstance.cpp @@ -26,6 +26,7 @@ #include "icons/IconList.h" #include "BaseLauncher.h" #include "minecraft/ModList.h" +#include LegacyInstance::LegacyInstance(SettingsObjectPtr globalSettings, SettingsObjectPtr settings, const QString &rootDir) : MinecraftInstance(globalSettings, settings, rootDir) @@ -87,7 +88,7 @@ bool LegacyInstance::shouldUseCustomBaseJar() const } -std::shared_ptr LegacyInstance::doUpdate() +std::shared_ptr LegacyInstance::createUpdateTask() { // make sure the jar mods list is initialized by asking for it. auto list = jarModList(); @@ -95,7 +96,7 @@ std::shared_ptr LegacyInstance::doUpdate() return std::shared_ptr(new LegacyUpdate(this, this)); } -std::shared_ptr LegacyInstance::prepareForLaunch(AuthSessionPtr account) +std::shared_ptr LegacyInstance::createLaunchTask(AuthSessionPtr account) { QString launchScript; QIcon icon = ENV.icons()->getIcon(iconKey()); @@ -129,6 +130,87 @@ std::shared_ptr LegacyInstance::prepareForLaunch(AuthSessionPtr ac return process; } +std::shared_ptr LegacyInstance::createJarModdingTask() +{ + class JarModTask : public Task + { + public: + explicit JarModTask(std::shared_ptr inst) : m_inst(inst), Task(nullptr) + { + } + virtual void executeTask() + { + if (!m_inst->shouldRebuild()) + { + emitSucceeded(); + return; + } + + // Get the mod list + auto modList = m_inst->getJarMods(); + + QFileInfo runnableJar(m_inst->runnableJar()); + QFileInfo baseJar(m_inst->baseJar()); + bool base_is_custom = m_inst->shouldUseCustomBaseJar(); + + // Nothing to do if there are no jar mods to install, no backup and just the mc jar + if (base_is_custom) + { + // yes, this can happen if the instance only has the runnable jar and not the base jar + // it *could* be assumed that such an instance is vanilla, but that wouldn't be safe + // because that's not something mmc4 guarantees + if (runnableJar.isFile() && !baseJar.exists() && modList.empty()) + { + m_inst->setShouldRebuild(false); + emitSucceeded(); + return; + } + + setStatus(tr("Installing mods: Backing up minecraft.jar ...")); + if (!baseJar.exists() && !QFile::copy(runnableJar.filePath(), baseJar.filePath())) + { + emitFailed("It seems both the active and base jar are gone. A fresh base jar will " + "be used on next run."); + m_inst->setShouldRebuild(true); + m_inst->setShouldUpdate(true); + m_inst->setShouldUseCustomBaseJar(false); + return; + } + } + + if (!baseJar.exists()) + { + emitFailed("The base jar " + baseJar.filePath() + " does not exist"); + return; + } + + if (runnableJar.exists() && !QFile::remove(runnableJar.filePath())) + { + emitFailed("Failed to delete old minecraft.jar"); + return; + } + + setStatus(tr("Installing mods: Opening minecraft.jar ...")); + + QString outputJarPath = runnableJar.filePath(); + QString inputJarPath = baseJar.filePath(); + + if(!MMCZip::createModdedJar(inputJarPath, outputJarPath, modList)) + { + emitFailed(tr("Failed to create the custom Minecraft jar file.")); + return; + } + m_inst->setShouldRebuild(false); + // inst->UpdateVersion(true); + emitSucceeded(); + return; + + } + std::shared_ptr m_inst; + }; + return std::make_shared(std::dynamic_pointer_cast(shared_from_this())); +} + void LegacyInstance::cleanupAfterRun() { // FIXME: delete the launcher and icons and whatnot. diff --git a/logic/minecraft/LegacyInstance.h b/logic/minecraft/LegacyInstance.h index a8363e58..51871876 100644 --- a/logic/minecraft/LegacyInstance.h +++ b/logic/minecraft/LegacyInstance.h @@ -109,9 +109,12 @@ public: virtual bool shouldUpdate() const override; virtual void setShouldUpdate(bool val) override; - virtual std::shared_ptr doUpdate() override; + virtual std::shared_ptr createUpdateTask() override; + + virtual std::shared_ptr createLaunchTask(AuthSessionPtr account) override; + + virtual std::shared_ptr createJarModdingTask() override; - virtual std::shared_ptr prepareForLaunch(AuthSessionPtr account) override; virtual void cleanupAfterRun() override; virtual QString getStatusbarDescription() override; diff --git a/logic/minecraft/LegacyUpdate.cpp b/logic/minecraft/LegacyUpdate.cpp index 78f2f5b7..7ea20ad8 100644 --- a/logic/minecraft/LegacyUpdate.cpp +++ b/logic/minecraft/LegacyUpdate.cpp @@ -349,7 +349,7 @@ void LegacyUpdate::jarStart() LegacyInstance *inst = (LegacyInstance *)m_inst; if (!inst->shouldUpdate() || inst->shouldUseCustomBaseJar()) { - ModTheJar(); + emitSucceeded(); return; } @@ -384,7 +384,7 @@ void LegacyUpdate::jarStart() void LegacyUpdate::jarFinished() { // process the jar - ModTheJar(); + emitSucceeded(); } void LegacyUpdate::jarFailed(QString reason) @@ -392,73 +392,3 @@ void LegacyUpdate::jarFailed(QString reason) // bad, bad emitFailed(tr("Failed to download the minecraft jar: %1.").arg(reason)); } - -void LegacyUpdate::ModTheJar() -{ - LegacyInstance *inst = (LegacyInstance *)m_inst; - - if (!inst->shouldRebuild()) - { - emitSucceeded(); - return; - } - - // Get the mod list - auto modList = inst->getJarMods(); - - QFileInfo runnableJar(inst->runnableJar()); - QFileInfo baseJar(inst->baseJar()); - bool base_is_custom = inst->shouldUseCustomBaseJar(); - - // Nothing to do if there are no jar mods to install, no backup and just the mc jar - if (base_is_custom) - { - // yes, this can happen if the instance only has the runnable jar and not the base jar - // it *could* be assumed that such an instance is vanilla, but that wouldn't be safe - // because that's not something mmc4 guarantees - if (runnableJar.isFile() && !baseJar.exists() && modList.empty()) - { - inst->setShouldRebuild(false); - emitSucceeded(); - return; - } - - setStatus(tr("Installing mods: Backing up minecraft.jar ...")); - if (!baseJar.exists() && !QFile::copy(runnableJar.filePath(), baseJar.filePath())) - { - emitFailed("It seems both the active and base jar are gone. A fresh base jar will " - "be used on next run."); - inst->setShouldRebuild(true); - inst->setShouldUpdate(true); - inst->setShouldUseCustomBaseJar(false); - return; - } - } - - if (!baseJar.exists()) - { - emitFailed("The base jar " + baseJar.filePath() + " does not exist"); - return; - } - - if (runnableJar.exists() && !QFile::remove(runnableJar.filePath())) - { - emitFailed("Failed to delete old minecraft.jar"); - return; - } - - setStatus(tr("Installing mods: Opening minecraft.jar ...")); - - QString outputJarPath = runnableJar.filePath(); - QString inputJarPath = baseJar.filePath(); - - if(!MMCZip::createModdedJar(inputJarPath, outputJarPath, modList)) - { - emitFailed(tr("Failed to create the custom Minecraft jar file.")); - return; - } - inst->setShouldRebuild(false); - // inst->UpdateVersion(true); - emitSucceeded(); - return; -} diff --git a/logic/minecraft/LegacyUpdate.h b/logic/minecraft/LegacyUpdate.h index fc389ae1..c52bf934 100644 --- a/logic/minecraft/LegacyUpdate.h +++ b/logic/minecraft/LegacyUpdate.h @@ -51,8 +51,6 @@ slots: void extractLwjgl(); - void ModTheJar(); - private: std::shared_ptr m_reply; diff --git a/logic/minecraft/OneSixInstance.cpp b/logic/minecraft/OneSixInstance.cpp index 63bc071f..4af56b54 100644 --- a/logic/minecraft/OneSixInstance.cpp +++ b/logic/minecraft/OneSixInstance.cpp @@ -56,7 +56,7 @@ QSet OneSixInstance::traits() return version->traits; } -std::shared_ptr OneSixInstance::doUpdate() +std::shared_ptr OneSixInstance::createUpdateTask() { return std::shared_ptr(new OneSixUpdate(this)); } @@ -123,7 +123,7 @@ QStringList OneSixInstance::processMinecraftArgs(AuthSessionPtr session) return parts; } -std::shared_ptr OneSixInstance::prepareForLaunch(AuthSessionPtr session) +std::shared_ptr OneSixInstance::createLaunchTask(AuthSessionPtr session) { QString launchScript; QIcon icon = ENV.icons()->getIcon(iconKey()); @@ -237,6 +237,64 @@ std::shared_ptr OneSixInstance::prepareForLaunch(AuthSessionPtr se return process; } +std::shared_ptr OneSixInstance::createJarModdingTask() +{ + class JarModTask : public Task + { + public: + explicit JarModTask(std::shared_ptr inst) : m_inst(inst), Task(nullptr) + { + } + virtual void executeTask() + { + std::shared_ptr version = m_inst->getMinecraftProfile(); + // nuke obsolete stripped jar(s) if needed + QString version_id = version->id; + QString strippedPath = version_id + "/" + version_id + "-stripped.jar"; + QFile strippedJar(strippedPath); + if(strippedJar.exists()) + { + strippedJar.remove(); + } + auto tempJarPath = QDir(m_inst->instanceRoot()).absoluteFilePath("temp.jar"); + QFile tempJar(tempJarPath); + if(tempJar.exists()) + { + tempJar.remove(); + } + auto finalJarPath = QDir(m_inst->instanceRoot()).absoluteFilePath("minecraft.jar"); + QFile finalJar(finalJarPath); + if(finalJar.exists()) + { + if(!finalJar.remove()) + { + emitFailed(tr("Couldn't remove stale jar file: %1").arg(finalJarPath)); + return; + } + } + + // create temporary modded jar, if needed + auto jarMods = m_inst->getJarMods(); + if(jarMods.size()) + { + auto sourceJarPath = m_inst->versionsPath().absoluteFilePath(version->id + "/" + version->id + ".jar"); + QString localPath = version_id + "/" + version_id + ".jar"; + auto metacache = ENV.metacache(); + auto entry = metacache->resolveEntry("versions", localPath); + QString fullJarPath = entry->getFullPath(); + if(!MMCZip::createModdedJar(sourceJarPath, finalJarPath, jarMods)) + { + emitFailed(tr("Failed to create the custom Minecraft jar file.")); + return; + } + } + emitSucceeded(); + } + std::shared_ptr m_inst; + }; + return std::make_shared(std::dynamic_pointer_cast(shared_from_this())); +} + void OneSixInstance::cleanupAfterRun() { QString target_dir = PathCombine(instanceRoot(), "natives/"); diff --git a/logic/minecraft/OneSixInstance.h b/logic/minecraft/OneSixInstance.h index 3825ba6a..46545462 100644 --- a/logic/minecraft/OneSixInstance.h +++ b/logic/minecraft/OneSixInstance.h @@ -48,8 +48,9 @@ public: QString libDir() const; virtual QString instanceConfigFolder() const override; - virtual std::shared_ptr doUpdate() override; - virtual std::shared_ptr prepareForLaunch(AuthSessionPtr account) override; + virtual std::shared_ptr createUpdateTask() override; + virtual std::shared_ptr createLaunchTask(AuthSessionPtr account) override; + virtual std::shared_ptr createJarModdingTask() override; virtual void cleanupAfterRun() override; diff --git a/logic/minecraft/OneSixUpdate.cpp b/logic/minecraft/OneSixUpdate.cpp index 8463ead6..34bd7de9 100644 --- a/logic/minecraft/OneSixUpdate.cpp +++ b/logic/minecraft/OneSixUpdate.cpp @@ -296,46 +296,6 @@ void OneSixUpdate::jarlibFinished() OneSixInstance *inst = (OneSixInstance *)m_inst; std::shared_ptr version = inst->getMinecraftProfile(); - // nuke obsolete stripped jar(s) if needed - QString version_id = version->id; - QString strippedPath = version_id + "/" + version_id + "-stripped.jar"; - QFile strippedJar(strippedPath); - if(strippedJar.exists()) - { - strippedJar.remove(); - } - auto tempJarPath = QDir(m_inst->instanceRoot()).absoluteFilePath("temp.jar"); - QFile tempJar(tempJarPath); - if(tempJar.exists()) - { - tempJar.remove(); - } - auto finalJarPath = QDir(m_inst->instanceRoot()).absoluteFilePath("minecraft.jar"); - QFile finalJar(finalJarPath); - if(finalJar.exists()) - { - if(!finalJar.remove()) - { - emitFailed(tr("Couldn't remove stale jar file: %1").arg(finalJarPath)); - return; - } - } - - // create temporary modded jar, if needed - auto jarMods = inst->getJarMods(); - if(jarMods.size()) - { - auto sourceJarPath = m_inst->versionsPath().absoluteFilePath(version->id + "/" + version->id + ".jar"); - QString localPath = version_id + "/" + version_id + ".jar"; - auto metacache = ENV.metacache(); - auto entry = metacache->resolveEntry("versions", localPath); - QString fullJarPath = entry->getFullPath(); - if(!MMCZip::createModdedJar(sourceJarPath, finalJarPath, jarMods)) - { - emitFailed(tr("Failed to create the custom Minecraft jar file.")); - return; - } - } if (version->traits.contains("legacyFML")) { fmllibsStart(); -- cgit v1.2.3