From 7b6d9c23b3b37186dc62481dde4999c62941738c Mon Sep 17 00:00:00 2001 From: nordsoft Date: Mon, 7 Nov 2022 13:44:58 +0400 Subject: [PATCH] Attempt to pass lobby parameters as command line arguments --- client/CMT.cpp | 13 ++++++++++++- launcher/lobby/lobby_moc.cpp | 13 +++++++++---- launcher/lobby/lobby_moc.h | 1 + launcher/main.cpp | 4 +++- launcher/main.h | 3 +++ launcher/mainwindow_moc.cpp | 23 +++++++++++++++++++---- launcher/mainwindow_moc.h | 4 +++- 7 files changed, 50 insertions(+), 11 deletions(-) diff --git a/client/CMT.cpp b/client/CMT.cpp index 34f25a030..7052e1d26 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -201,7 +201,8 @@ int main(int argc, char * argv[]) ("donotstartserver,d","do not attempt to start server and just connect to it instead server") ("serverport", po::value(), "override port specified in config file") ("saveprefix", po::value(), "prefix for auto save files") - ("savefrequency", po::value(), "limit auto save creation to each N days"); + ("savefrequency", po::value(), "limit auto save creation to each N days") + ("lobby", po::value>(), "parameters to connect ro remote lobby session"); if(argc > 1) { @@ -483,6 +484,16 @@ int main(int argc, char * argv[]) session["autoSkip"].Bool() = vm.count("autoSkip"); session["oneGoodAI"].Bool() = vm.count("oneGoodAI"); session["aiSolo"].Bool() = false; + + session["lobby"] = false; + if(vm.count("lobby")) + { + auto lobbyParams = vc["lobby"].as>(); + session["lobby"].Bool() = true; + session["address"].String() = lobbyParams[0]; + session["port"].Integer() = std::stoi(lobbyParams[1]); + session["uuid"].String() = lobbyParams[2]; + } if(vm.count("testmap")) { diff --git a/launcher/lobby/lobby_moc.cpp b/launcher/lobby/lobby_moc.cpp index 618cadcd7..034870419 100644 --- a/launcher/lobby/lobby_moc.cpp +++ b/launcher/lobby/lobby_moc.cpp @@ -209,11 +209,16 @@ void Lobby::serverCommand(const ServerCommand & command) try case START: { protocolAssert(args.size() == 1); //actually start game - Settings node = settings.write["server"]; - node["lobby"].Bool() = true; + //Settings node = settings.write["server"]; + gameArguments.clear(); + gameArguments << "--lobby"; + gameArguments << ui->hostEdit->text(); + gameArguments << ui->portEdit->text(); + gameArguments << args[0]; + /*node["lobby"].Bool() = true; node["server"].String() = ui->hostEdit->text().toStdString(); node["serverport"].Integer() = ui->portEdit->text().toInt(); - node["uuid"].String() = args[0].toStdString(); + node["uuid"].String() = args[0].toStdString();*/ startGame = true; //on_startGameButton_clicked //node["names"].Vector().clear(); @@ -264,7 +269,7 @@ void Lobby::dispatchMessage(QString txt) try } if(startGame) - qobject_cast(qApp->activeWindow())->on_startGameButton_clicked(); + qobject_cast(qApp->activeWindow())->startGame(gameArguments); } catch(const ProtocolError & e) { diff --git a/launcher/lobby/lobby_moc.h b/launcher/lobby/lobby_moc.h index a2b503180..afeaf348c 100644 --- a/launcher/lobby/lobby_moc.h +++ b/launcher/lobby/lobby_moc.h @@ -131,6 +131,7 @@ private: QString session; QString username; bool startGame = false; + QStringList gameArguments; private: void protocolAssert(bool); diff --git a/launcher/main.cpp b/launcher/main.cpp index 94bccc43e..8922b0ad2 100644 --- a/launcher/main.cpp +++ b/launcher/main.cpp @@ -24,6 +24,8 @@ int main(int argc, char * argv[]) int result; #ifdef VCMI_IOS { + __argc = argc; + __argv = argv; #endif QApplication vcmilauncher(argc, argv); MainWindow mainWindow; @@ -32,7 +34,7 @@ int main(int argc, char * argv[]) #ifdef VCMI_IOS } if (result == 0) - launchGame(argc, argv); + launchGame(__argc, __argv); #endif return result; } diff --git a/launcher/main.h b/launcher/main.h index 8c77747d2..2464148bb 100644 --- a/launcher/main.h +++ b/launcher/main.h @@ -9,6 +9,9 @@ */ #pragma once +inline int __argc; +inline char ** __argv; + #ifdef VCMI_IOS extern "C" void launchGame(int argc, char * argv[]); #endif diff --git a/launcher/mainwindow_moc.cpp b/launcher/mainwindow_moc.cpp index 95d8be728..434bd1e2d 100644 --- a/launcher/mainwindow_moc.cpp +++ b/launcher/mainwindow_moc.cpp @@ -20,6 +20,7 @@ #include "../lib/logging/CBasicLogConfigurator.h" #include "updatedialog_moc.h" +#include "main.h" void MainWindow::load() { @@ -112,22 +113,36 @@ MainWindow::~MainWindow() delete ui; } -void MainWindow::on_startGameButton_clicked() +void MainWindow::startGame(const QStringList & args) { + __argc = args.size(); + __argv = new char*[__argc]; + for(int i = 0; i < __argc; ++i) + { + const char * s = args[i].toLocal8Bit().constData(); + __argv[i] = new char[strlen(s)]; + strcpy(__argv[i], s); + + } #ifdef Q_OS_IOS qApp->quit(); #else - startExecutable(pathToQString(VCMIDirs::get().clientPath())); + startExecutable(pathToQString(VCMIDirs::get().clientPath()), args); #endif } +void MainWindow::on_startGameButton_clicked() +{ + startGame({}); +} + #ifndef Q_OS_IOS -void MainWindow::startExecutable(QString name) +void MainWindow::startExecutable(QString name, const QStringList & args) { QProcess process; // Start the executable - if(process.startDetached(name, {})) + if(process.startDetached(name, args)) { close(); // exit launcher } diff --git a/launcher/mainwindow_moc.h b/launcher/mainwindow_moc.h index bfe6ad2d5..a933dbb25 100644 --- a/launcher/mainwindow_moc.h +++ b/launcher/mainwindow_moc.h @@ -28,12 +28,14 @@ private: Ui::MainWindow * ui; void load(); #ifndef Q_OS_IOS - void startExecutable(QString name); + void startExecutable(QString name, const QStringList & args); #endif public: explicit MainWindow(QWidget * parent = 0); ~MainWindow(); + + void startGame(const QStringList & args); public slots: void on_startGameButton_clicked();