From bc04d89c32142094eec161709cf3932b7213b058 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Wed, 5 Feb 2020 00:29:23 +0100 Subject: [PATCH 1/4] feat: add --import command-line option When specified, opens the "Import from zip" dialog as soon as the main window is shown, with the URL field prefilled with the argument given to the option. Closes #2998 --- application/MainWindow.h | 4 ++-- application/MultiMC.cpp | 12 ++++++++++++ application/MultiMC.h | 2 ++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/application/MainWindow.h b/application/MainWindow.h index a415b5e85..00b8e043e 100644 --- a/application/MainWindow.h +++ b/application/MainWindow.h @@ -57,6 +57,8 @@ public: void checkInstancePathForProblems(); void updatesAllowedChanged(bool allowed); + + void droppedURLs(QList urls); signals: void isClosing(); @@ -180,8 +182,6 @@ private slots: */ void downloadUpdates(GoUpdate::Status status); - void droppedURLs(QList urls); - void konamiTriggered(); void globalSettingsClosed(); diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index c4e4392de..039fb11d6 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -174,6 +175,10 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) parser.addSwitch("alive"); parser.addDocumentation("alive", "Write a small '" + liveCheckFile + "' file after MultiMC starts"); + parser.addOption("import"); + parser.addShortOpt("import", 'I'); + parser.addDocumentation("import", "Import instance from specified zip (local path or URL)"); + // parse the arguments try { @@ -207,6 +212,7 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) } m_instanceIdToLaunch = args["launch"].toString(); m_liveCheck = args["alive"].toBool(); + m_zipToImport = args["import"].toUrl(); QString origcwdPath = QDir::currentPath(); QString binPath = applicationDirPath(); @@ -812,6 +818,12 @@ void MultiMC::performMainStartupAction() showMainWindow(false); qDebug() << "<> Main window shown."; } + + if (!m_zipToImport.isEmpty()) { + qDebug() << "<> Importing instance from zip:" << m_zipToImport.toString(); + QList urls = { m_zipToImport }; + m_mainWindow->droppedURLs(urls); + } } void MultiMC::showFatalErrorMessage(const QString& title, const QString& content) diff --git a/application/MultiMC.h b/application/MultiMC.h index d7c727e02..e6588a147 100644 --- a/application/MultiMC.h +++ b/application/MultiMC.h @@ -6,6 +6,7 @@ #include #include #include +#include #include #include @@ -221,5 +222,6 @@ private: public: QString m_instanceIdToLaunch; bool m_liveCheck = false; + QUrl m_zipToImport; std::unique_ptr logFile; }; From 060d6955e1271bc05e35e5e24b71e1b7b0e8eb17 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Sun, 9 Feb 2020 15:45:31 +0100 Subject: [PATCH 2/4] style: follow upstream code style --- application/MultiMC.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index 039fb11d6..45df59374 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -174,7 +174,7 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) // --alive parser.addSwitch("alive"); parser.addDocumentation("alive", "Write a small '" + liveCheckFile + "' file after MultiMC starts"); - + // --import parser.addOption("import"); parser.addShortOpt("import", 'I'); parser.addDocumentation("import", "Import instance from specified zip (local path or URL)"); @@ -818,8 +818,8 @@ void MultiMC::performMainStartupAction() showMainWindow(false); qDebug() << "<> Main window shown."; } - - if (!m_zipToImport.isEmpty()) { + if(!m_zipToImport.isEmpty()) + { qDebug() << "<> Importing instance from zip:" << m_zipToImport.toString(); QList urls = { m_zipToImport }; m_mainWindow->droppedURLs(urls); From 381c12547fe91a7392f42337674da390841b39ed Mon Sep 17 00:00:00 2001 From: OverMighty Date: Sun, 9 Feb 2020 16:17:41 +0100 Subject: [PATCH 3/4] feat: allow telling the main process to import a zip This commit enhances the IPC message system and adds a new IPC command in order to allow secondary MultiMC processes that were started by executing MultiMC with the `--import` command-line option to tell the main MutliMC process to open the "Import from zip" dialog window and prefill the URL field with the specified URL. --- application/MultiMC.cpp | 44 +++++++++++++++++++++++++++++++++-------- 1 file changed, 36 insertions(+), 8 deletions(-) diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index 45df59374..393ea0467 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -284,13 +284,20 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv) connect(m_peerInstance, &LocalPeer::messageReceived, this, &MultiMC::messageReceived); if(m_peerInstance->isClient()) { + int timeout = 2000; + if(m_instanceIdToLaunch.isEmpty()) { - m_peerInstance->sendMessage("activate", 2000); + m_peerInstance->sendMessage("activate", timeout); + + if(!m_zipToImport.isEmpty()) + { + m_peerInstance->sendMessage("import " + m_zipToImport.toString(), timeout); + } } else { - m_peerInstance->sendMessage(m_instanceIdToLaunch, 2000); + m_peerInstance->sendMessage("launch " + m_instanceIdToLaunch, timeout); } m_status = MultiMC::Succeeded; return; @@ -820,9 +827,8 @@ void MultiMC::performMainStartupAction() } if(!m_zipToImport.isEmpty()) { - qDebug() << "<> Importing instance from zip:" << m_zipToImport.toString(); - QList urls = { m_zipToImport }; - m_mainWindow->droppedURLs(urls); + qDebug() << "<> Importing instance from zip:" << m_zipToImport; + m_mainWindow->droppedURLs({ m_zipToImport }); } } @@ -860,18 +866,40 @@ void MultiMC::messageReceived(const QString& message) qDebug() << "Received message" << message << "while still initializing. It will be ignored."; return; } - if(message == "activate") + + QStringList args = message.split(' '); + QString command = args.takeFirst(); + + if(command == "activate") { showMainWindow(); } - else + else if(command == "import") { - auto inst = instances()->getInstanceById(message); + if(args.isEmpty()) + { + qWarning() << "Received" << command << "message without a zip path/URL."; + return; + } + m_mainWindow->droppedURLs({ QUrl(args.takeFirst()) }); + } + else if(command == "launch") + { + if(args.isEmpty()) + { + qWarning() << "Received" << command << "message without an instance ID."; + return; + } + auto inst = instances()->getInstanceById(args.takeFirst()); if(inst) { launch(inst, true, nullptr); } } + else + { + qWarning() << "Received invalid message" << message; + } } void MultiMC::analyticsSettingChanged(const Setting&, QVariant value) From bbcacec6eca70fefcb6c7a3249586a8c7b2fb634 Mon Sep 17 00:00:00 2001 From: OverMighty Date: Mon, 4 May 2020 11:37:02 +0200 Subject: [PATCH 4/4] fix: add support for args with spaces to MultiMC::messageReceived() Previously, when the main instance of MultiMC would receive an `import` or `launch` message from another instance, it would split the message on each space, and only read the first word of the argument (zip path/URL or instance ID). This commit fixes that problem by sectioning the message string instead. --- application/MultiMC.cpp | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/application/MultiMC.cpp b/application/MultiMC.cpp index 393ea0467..86f426f81 100644 --- a/application/MultiMC.cpp +++ b/application/MultiMC.cpp @@ -867,8 +867,7 @@ void MultiMC::messageReceived(const QString& message) return; } - QStringList args = message.split(' '); - QString command = args.takeFirst(); + QString command = message.section(' ', 0, 0); if(command == "activate") { @@ -876,21 +875,23 @@ void MultiMC::messageReceived(const QString& message) } else if(command == "import") { - if(args.isEmpty()) + QString arg = message.section(' ', 1); + if(arg.isEmpty()) { qWarning() << "Received" << command << "message without a zip path/URL."; return; } - m_mainWindow->droppedURLs({ QUrl(args.takeFirst()) }); + m_mainWindow->droppedURLs({ QUrl(arg) }); } else if(command == "launch") { - if(args.isEmpty()) + QString arg = message.section(' ', 1); + if(arg.isEmpty()) { qWarning() << "Received" << command << "message without an instance ID."; return; } - auto inst = instances()->getInstanceById(args.takeFirst()); + auto inst = instances()->getInstanceById(arg); if(inst) { launch(inst, true, nullptr);