NOISSUE Add --server argument for --launch

This commit is contained in:
Janrupf 2021-05-23 14:42:20 +02:00
parent 58ab005f7e
commit 52c1150522
2 changed files with 65 additions and 3 deletions

View File

@ -191,6 +191,11 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
parser.addOption("launch"); parser.addOption("launch");
parser.addShortOpt("launch", 'l'); parser.addShortOpt("launch", 'l');
parser.addDocumentation("launch", "Launch the specified instance (by instance ID)"); parser.addDocumentation("launch", "Launch the specified instance (by instance ID)");
// --server
parser.addOption("server");
parser.addShortOpt("server", 's');
parser.addDocumentation("server", "Join the specified server on launch "
"(only valid in combination with --launch)");
// --alive // --alive
parser.addSwitch("alive"); parser.addSwitch("alive");
parser.addDocumentation("alive", "Write a small '" + liveCheckFile + "' file after MultiMC starts"); parser.addDocumentation("alive", "Write a small '" + liveCheckFile + "' file after MultiMC starts");
@ -232,6 +237,7 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
} }
} }
m_instanceIdToLaunch = args["launch"].toString(); m_instanceIdToLaunch = args["launch"].toString();
m_serverToJoin = args["server"].toString();
m_liveCheck = args["alive"].toBool(); m_liveCheck = args["alive"].toBool();
m_zipToImport = args["import"].toUrl(); m_zipToImport = args["import"].toUrl();
@ -293,6 +299,13 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
return; return;
} }
if(m_instanceIdToLaunch.isEmpty() && !m_serverToJoin.isEmpty())
{
std::cerr << "--server can only be used in combination with --launch!" << std::endl;
m_status = MultiMC::Failed;
return;
}
/* /*
* Establish the mechanism for communication with an already running MultiMC that uses the same data path. * Establish the mechanism for communication with an already running MultiMC that uses the same data path.
* If there is one, tell it what the user actually wanted to do and exit. * If there is one, tell it what the user actually wanted to do and exit.
@ -318,7 +331,15 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
} }
else else
{ {
m_peerInstance->sendMessage("launch " + m_instanceIdToLaunch, timeout); if(!m_serverToJoin.isEmpty())
{
m_peerInstance->sendMessage(
"launch-with-server " + m_instanceIdToLaunch + " " + m_serverToJoin, timeout);
}
else
{
m_peerInstance->sendMessage("launch " + m_instanceIdToLaunch, timeout);
}
} }
m_status = MultiMC::Succeeded; m_status = MultiMC::Succeeded;
return; return;
@ -399,6 +420,10 @@ MultiMC::MultiMC(int &argc, char **argv) : QApplication(argc, argv)
{ {
qDebug() << "ID of instance to launch : " << m_instanceIdToLaunch; qDebug() << "ID of instance to launch : " << m_instanceIdToLaunch;
} }
if(!m_serverToJoin.isEmpty())
{
qDebug() << "Address of server to join :" << m_serverToJoin;
}
qDebug() << "<> Paths set."; qDebug() << "<> Paths set.";
} }
@ -844,8 +869,19 @@ void MultiMC::performMainStartupAction()
auto inst = instances()->getInstanceById(m_instanceIdToLaunch); auto inst = instances()->getInstanceById(m_instanceIdToLaunch);
if(inst) if(inst)
{ {
qDebug() << "<> Instance launching:" << m_instanceIdToLaunch; MinecraftServerTargetPtr serverToJoin = nullptr;
launch(inst, true, nullptr);
if(!m_serverToJoin.isEmpty())
{
serverToJoin.reset(new MinecraftServerTarget(MinecraftServerTarget::parse(m_serverToJoin)));
qDebug() << "<> Instance" << m_instanceIdToLaunch << "launching with server" << m_serverToJoin;
}
else
{
qDebug() << "<> Instance" << m_instanceIdToLaunch << "launching";
}
launch(inst, true, nullptr, serverToJoin);
return; return;
} }
} }
@ -927,6 +963,31 @@ void MultiMC::messageReceived(const QString& message)
launch(inst, true, nullptr); launch(inst, true, nullptr);
} }
} }
else if(command == "launch-with-server")
{
QString instanceID = message.section(' ', 1, 1);
QString serverToJoin = message.section(' ', 2, 2);
if(instanceID.isEmpty())
{
qWarning() << "Received" << command << "message without an instance ID.";
return;
}
if(serverToJoin.isEmpty())
{
qWarning() << "Received" << command << "message without a server to join.";
return;
}
auto inst = instances()->getInstanceById(instanceID);
if(inst)
{
launch(
inst,
true,
nullptr,
std::make_shared<MinecraftServerTarget>(MinecraftServerTarget::parse(serverToJoin))
);
}
}
else else
{ {
qWarning() << "Received invalid message" << message; qWarning() << "Received invalid message" << message;

View File

@ -228,6 +228,7 @@ private:
SetupWizard * m_setupWizard = nullptr; SetupWizard * m_setupWizard = nullptr;
public: public:
QString m_instanceIdToLaunch; QString m_instanceIdToLaunch;
QString m_serverToJoin;
bool m_liveCheck = false; bool m_liveCheck = false;
QUrl m_zipToImport; QUrl m_zipToImport;
std::unique_ptr<QFile> logFile; std::unique_ptr<QFile> logFile;