diff --git a/api/logic/BaseInstance.h b/api/logic/BaseInstance.h index 7ea4e045e..64de4bb3d 100644 --- a/api/logic/BaseInstance.h +++ b/api/logic/BaseInstance.h @@ -147,7 +147,8 @@ public: virtual shared_qobject_ptr createUpdateTask(Net::Mode mode) = 0; /// returns a valid launcher (task container) - virtual shared_qobject_ptr createLaunchTask(AuthSessionPtr account) = 0; + virtual shared_qobject_ptr createLaunchTask( + AuthSessionPtr account, MinecraftServerTargetPtr serverToJoin) = 0; /// returns the current launch task (if any) shared_qobject_ptr getLaunchTask(); diff --git a/api/logic/CMakeLists.txt b/api/logic/CMakeLists.txt index 52821a618..c33229556 100644 --- a/api/logic/CMakeLists.txt +++ b/api/logic/CMakeLists.txt @@ -238,6 +238,7 @@ set(MINECRAFT_SOURCES minecraft/launch/ExtractNatives.h minecraft/launch/LauncherPartLaunch.cpp minecraft/launch/LauncherPartLaunch.h + minecraft/launch/MinecraftServerTarget.cpp minecraft/launch/MinecraftServerTarget.h minecraft/launch/PrintInstanceInfo.cpp minecraft/launch/PrintInstanceInfo.h diff --git a/api/logic/NullInstance.h b/api/logic/NullInstance.h index ad39c1796..94ed6c3a9 100644 --- a/api/logic/NullInstance.h +++ b/api/logic/NullInstance.h @@ -27,7 +27,7 @@ public: { return instanceRoot(); }; - shared_qobject_ptr createLaunchTask(AuthSessionPtr) override + shared_qobject_ptr createLaunchTask(AuthSessionPtr, MinecraftServerTargetPtr) override { return nullptr; } diff --git a/api/logic/minecraft/MinecraftInstance.cpp b/api/logic/minecraft/MinecraftInstance.cpp index 00ac79640..a1341e69c 100644 --- a/api/logic/minecraft/MinecraftInstance.cpp +++ b/api/logic/minecraft/MinecraftInstance.cpp @@ -822,7 +822,7 @@ shared_qobject_ptr MinecraftInstance::createUpdateTask(Net::Mode mode) return nullptr; } -shared_qobject_ptr MinecraftInstance::createLaunchTask(AuthSessionPtr session) +shared_qobject_ptr MinecraftInstance::createLaunchTask(AuthSessionPtr session, MinecraftServerTargetPtr serverToJoin) { // FIXME: get rid of shared_from_this ... auto process = LaunchTask::create(std::dynamic_pointer_cast(shared_from_this())); @@ -854,66 +854,19 @@ shared_qobject_ptr MinecraftInstance::createLaunchTask(AuthSessionPt process->appendStep(new CreateGameFolders(pptr)); } - MinecraftServerTargetPtr serverToJoin = std::make_shared(); - - if (m_settings->get("JoinServerOnLaunch").toBool()) + if (!serverToJoin && m_settings->get("JoinServerOnLaunch").toBool()) { QString fullAddress = m_settings->get("JoinServerOnLaunchAddress").toString(); - QStringList split = fullAddress.split(":"); + serverToJoin.reset(new MinecraftServerTarget(MinecraftServerTarget::parse(fullAddress))); + } - // The logic below replicates the exact logic minecraft uses for parsing server addresses. - // While the conversion is not lossless and eats errors, it ensures the same behavior - // within Minecraft and MultiMC when entering server addresses. - if (fullAddress.startsWith("[")) - { - int bracket = fullAddress.indexOf("]"); - if (bracket > 0) - { - QString ipv6 = fullAddress.mid(1, bracket - 1); - QString port = fullAddress.mid(bracket + 1).trimmed(); - - if (port.startsWith(":") && !ipv6.isEmpty()) - { - port = port.mid(1); - split = QStringList({ ipv6, port }); - } - else - { - split = QStringList({ipv6}); - } - } - } - - if (split.size() > 2) - { - split = QStringList({fullAddress}); - } - - QString realAddress = split[0]; - - quint16 realPort = 25565; - if (split.size() > 1) - { - bool ok; - realPort = split[1].toUInt(&ok); - - if (!ok) - { - realPort = 25565; - } - } - - serverToJoin->port = realPort; - serverToJoin->address = realAddress; - - if(realPort == 25565) - { - // Resolve server address to join on launch - auto *step = new LookupServerAddress(pptr); - step->setLookupAddress(realAddress); - step->setOutputAddressPtr(serverToJoin); - process->appendStep(step); - } + if(serverToJoin && serverToJoin->port == 25565) + { + // Resolve server address to join on launch + auto *step = new LookupServerAddress(pptr); + step->setLookupAddress(serverToJoin->address); + step->setOutputAddressPtr(serverToJoin); + process->appendStep(step); } // run pre-launch command if that's needed diff --git a/api/logic/minecraft/MinecraftInstance.h b/api/logic/minecraft/MinecraftInstance.h index eb1a90050..05600797c 100644 --- a/api/logic/minecraft/MinecraftInstance.h +++ b/api/logic/minecraft/MinecraftInstance.h @@ -77,7 +77,7 @@ public: ////// Launch stuff ////// shared_qobject_ptr createUpdateTask(Net::Mode mode) override; - shared_qobject_ptr createLaunchTask(AuthSessionPtr account) override; + shared_qobject_ptr createLaunchTask(AuthSessionPtr account, MinecraftServerTargetPtr serverToJoin) override; QStringList extraArguments() const override; QStringList verboseDescription(AuthSessionPtr session, MinecraftServerTargetPtr serverToJoin) override; QList getJarMods() const; diff --git a/api/logic/minecraft/launch/MinecraftServerTarget.cpp b/api/logic/minecraft/launch/MinecraftServerTarget.cpp new file mode 100644 index 000000000..569273b62 --- /dev/null +++ b/api/logic/minecraft/launch/MinecraftServerTarget.cpp @@ -0,0 +1,66 @@ +/* Copyright 2013-2021 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MinecraftServerTarget.h" + +#include + +MinecraftServerTarget MinecraftServerTarget::parse(const QString &fullAddress) { + QStringList split = fullAddress.split(":"); + + // The logic below replicates the exact logic minecraft uses for parsing server addresses. + // While the conversion is not lossless and eats errors, it ensures the same behavior + // within Minecraft and MultiMC when entering server addresses. + if (fullAddress.startsWith("[")) + { + int bracket = fullAddress.indexOf("]"); + if (bracket > 0) + { + QString ipv6 = fullAddress.mid(1, bracket - 1); + QString port = fullAddress.mid(bracket + 1).trimmed(); + + if (port.startsWith(":") && !ipv6.isEmpty()) + { + port = port.mid(1); + split = QStringList({ ipv6, port }); + } + else + { + split = QStringList({ipv6}); + } + } + } + + if (split.size() > 2) + { + split = QStringList({fullAddress}); + } + + QString realAddress = split[0]; + + quint16 realPort = 25565; + if (split.size() > 1) + { + bool ok; + realPort = split[1].toUInt(&ok); + + if (!ok) + { + realPort = 25565; + } + } + + return MinecraftServerTarget { realAddress, realPort }; +} diff --git a/api/logic/minecraft/launch/MinecraftServerTarget.h b/api/logic/minecraft/launch/MinecraftServerTarget.h index f9b407e09..3c5786f4f 100644 --- a/api/logic/minecraft/launch/MinecraftServerTarget.h +++ b/api/logic/minecraft/launch/MinecraftServerTarget.h @@ -17,9 +17,14 @@ #include +#include +#include + struct MinecraftServerTarget { QString address; quint16 port; + + static MULTIMC_LOGIC_EXPORT MinecraftServerTarget parse(const QString &fullAddress); }; typedef std::shared_ptr MinecraftServerTargetPtr; diff --git a/api/logic/minecraft/legacy/LegacyInstance.h b/api/logic/minecraft/legacy/LegacyInstance.h index 7450fddad..325bac7a5 100644 --- a/api/logic/minecraft/legacy/LegacyInstance.h +++ b/api/logic/minecraft/legacy/LegacyInstance.h @@ -111,7 +111,8 @@ public: { return false; } - shared_qobject_ptr createLaunchTask(AuthSessionPtr account) override + shared_qobject_ptr createLaunchTask( + AuthSessionPtr account, MinecraftServerTargetPtr serverToJoin) override { return nullptr; } diff --git a/application/InstancePageProvider.h b/application/InstancePageProvider.h index dde36aefb..3cb723c40 100644 --- a/application/InstancePageProvider.h +++ b/application/InstancePageProvider.h @@ -46,7 +46,7 @@ public: values.append(new TexturePackPage(onesix.get())); values.append(new NotesPage(onesix.get())); values.append(new WorldListPage(onesix.get(), onesix->worldList())); - values.append(new ServersPage(onesix.get())); + values.append(new ServersPage(onesix)); // values.append(new GameOptionsPage(onesix.get())); values.append(new ScreenshotsPage(FS::PathCombine(onesix->gameRoot(), "screenshots"))); values.append(new InstanceSettingsPage(onesix.get())); diff --git a/application/LaunchController.cpp b/application/LaunchController.cpp index bebc3db1b..3c4491a32 100644 --- a/application/LaunchController.cpp +++ b/application/LaunchController.cpp @@ -197,7 +197,7 @@ void LaunchController::launchInstance() return; } - m_launcher = m_instance->createLaunchTask(m_session); + m_launcher = m_instance->createLaunchTask(m_session, m_serverToJoin); if (!m_launcher) { emitFailed(tr("Couldn't instantiate a launcher.")); diff --git a/application/LaunchController.h b/application/LaunchController.h index 1d879028d..5f177e006 100644 --- a/application/LaunchController.h +++ b/application/LaunchController.h @@ -3,6 +3,8 @@ #include #include +#include "minecraft/launch/MinecraftServerTarget.h" + class InstanceWindow; class LaunchController: public Task { @@ -33,6 +35,10 @@ public: { m_parentWidget = widget; } + void setServerToJoin(MinecraftServerTargetPtr serverToJoin) + { + m_serverToJoin = std::move(serverToJoin); + } QString id() { return m_instance->id(); @@ -58,4 +64,5 @@ private: InstanceWindow *m_console = nullptr; AuthSessionPtr m_session; shared_qobject_ptr m_launcher; + MinecraftServerTargetPtr m_serverToJoin; }; diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index 3429a3772..8a81238e8 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -1014,8 +1014,12 @@ bool MultiMC::openJsonEditor(const QString &filename) } } -bool MultiMC::launch(InstancePtr instance, bool online, BaseProfilerFactory *profiler) -{ +bool MultiMC::launch( + InstancePtr instance, + bool online, + BaseProfilerFactory *profiler, + MinecraftServerTargetPtr serverToJoin +) { if(m_updateRunning) { qDebug() << "Cannot launch instances while an update is running. Please try again when updates are completed."; @@ -1036,6 +1040,7 @@ bool MultiMC::launch(InstancePtr instance, bool online, BaseProfilerFactory *pro controller->setInstance(instance); controller->setOnline(online); controller->setProfiler(profiler); + controller->setServerToJoin(serverToJoin); if(window) { controller->setParentWidget(window); diff --git a/application/MultiMC.h b/application/MultiMC.h index e6588a147..57a43cb90 100644 --- a/application/MultiMC.h +++ b/application/MultiMC.h @@ -11,6 +11,8 @@ #include +#include "minecraft/launch/MinecraftServerTarget.h" + class LaunchController; class LocalPeer; class InstanceWindow; @@ -150,7 +152,12 @@ signals: void globalSettingsClosed(); public slots: - bool launch(InstancePtr instance, bool online = true, BaseProfilerFactory *profiler = nullptr); + bool launch( + InstancePtr instance, + bool online = true, + BaseProfilerFactory *profiler = nullptr, + MinecraftServerTargetPtr serverToJoin = nullptr + ); bool kill(InstancePtr instance); private slots: diff --git a/application/pages/instance/InstanceSettingsPage.ui b/application/pages/instance/InstanceSettingsPage.ui index de9fa9e32..29024b654 100644 --- a/application/pages/instance/InstanceSettingsPage.ui +++ b/application/pages/instance/InstanceSettingsPage.ui @@ -39,7 +39,7 @@ QTabWidget::Rounded - 4 + 0 diff --git a/application/pages/instance/ServersPage.cpp b/application/pages/instance/ServersPage.cpp index 8b0c655c2..d63c6e700 100644 --- a/application/pages/instance/ServersPage.cpp +++ b/application/pages/instance/ServersPage.cpp @@ -556,7 +556,7 @@ private: QTimer m_saveTimer; }; -ServersPage::ServersPage(MinecraftInstance * inst, QWidget* parent) +ServersPage::ServersPage(InstancePtr inst, QWidget* parent) : QMainWindow(parent), ui(new Ui::ServersPage) { ui->setupUi(this); @@ -579,7 +579,7 @@ ServersPage::ServersPage(MinecraftInstance * inst, QWidget* parent) auto selectionModel = ui->serversView->selectionModel(); connect(selectionModel, &QItemSelectionModel::currentChanged, this, &ServersPage::currentChanged); - connect(m_inst, &MinecraftInstance::runningStatusChanged, this, &ServersPage::on_RunningState_changed); + connect(m_inst.get(), &MinecraftInstance::runningStatusChanged, this, &ServersPage::on_RunningState_changed); connect(ui->nameLine, &QLineEdit::textEdited, this, &ServersPage::nameEdited); connect(ui->addressLine, &QLineEdit::textEdited, this, &ServersPage::addressEdited); connect(ui->resourceComboBox, SIGNAL(currentIndexChanged(int)), this, SLOT(resourceIndexChanged(int))); @@ -695,6 +695,7 @@ void ServersPage::updateState() ui->actionMove_Down->setEnabled(serverEditEnabled); ui->actionMove_Up->setEnabled(serverEditEnabled); ui->actionRemove->setEnabled(serverEditEnabled); + ui->actionJoin->setEnabled(serverEditEnabled); if(server) { @@ -758,4 +759,10 @@ void ServersPage::on_actionMove_Down_triggered() } } +void ServersPage::on_actionJoin_triggered() +{ + const auto &address = m_model->at(currentServer)->m_address; + MMC->launch(m_inst, true, nullptr, std::make_shared(MinecraftServerTarget::parse(address))); +} + #include "ServersPage.moc" diff --git a/application/pages/instance/ServersPage.h b/application/pages/instance/ServersPage.h index f164da1e5..8c5b7eb88 100644 --- a/application/pages/instance/ServersPage.h +++ b/application/pages/instance/ServersPage.h @@ -35,7 +35,7 @@ class ServersPage : public QMainWindow, public BasePage Q_OBJECT public: - explicit ServersPage(MinecraftInstance *inst, QWidget *parent = 0); + explicit ServersPage(InstancePtr inst, QWidget *parent = 0); virtual ~ServersPage(); void openedImpl() override; @@ -74,6 +74,7 @@ private slots: void on_actionRemove_triggered(); void on_actionMove_Up_triggered(); void on_actionMove_Down_triggered(); + void on_actionJoin_triggered(); void on_RunningState_changed(bool running); @@ -88,6 +89,6 @@ private: // data bool m_locked = true; Ui::ServersPage *ui = nullptr; ServersModel * m_model = nullptr; - MinecraftInstance * m_inst = nullptr; + InstancePtr m_inst = nullptr; }; diff --git a/application/pages/instance/ServersPage.ui b/application/pages/instance/ServersPage.ui index e9518e359..d89b7cba6 100644 --- a/application/pages/instance/ServersPage.ui +++ b/application/pages/instance/ServersPage.ui @@ -148,6 +148,7 @@ + @@ -169,6 +170,11 @@ Move Down + + + Join + +