From c523a2c752bb7b071715f6c4eac18f36bcd2c162 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Wed, 20 Feb 2013 00:07:52 +0100 Subject: implement commandline parsing --- main.cpp | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 4 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 6e840317..efabc20a 100644 --- a/main.cpp +++ b/main.cpp @@ -1,5 +1,6 @@ - /* Copyright 2013 MultiMC Contributors + * + * Authors: Orochimarufan * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -14,26 +15,105 @@ * limitations under the License. */ -#include "gui/mainwindow.h" +#include + #include +#include -#include "data/appsettings.h" +#include "gui/mainwindow.h" +#include "data/appsettings.h" #include "data/loginresponse.h" +#include "util/cmdutils.h" + +using namespace Util::Commandline; + int main(int argc, char *argv[]) { + // initialize Qt QApplication app(argc, argv); app.setOrganizationName("Forkk"); app.setApplicationName("MultiMC 5"); - + + // Print app header + std::cout << "MultiMC 5" << std::endl; + std::cout << "(c) 2013 MultiMC contributors" << std::endl << std::endl; + + // Commandline parsing + Parser parser(FlagStyle::GNU, ArgumentStyle::SpaceAndEquals); + + // --help + parser.addSwitch("help"); + parser.addShortOpt("help", 'h'); + parser.addDocumentation("help", "displays help on command line parameters"); + // --dir + parser.addOption("dir", app.applicationDirPath()); + parser.addShortOpt("dir", 'd'); + parser.addDocumentation("dir", "use the supplied directory as MultiMC root instead of the binary location (use '.' for current)"); + // --update + parser.addOption("update"); + parser.addShortOpt("update", 'u'); + parser.addDocumentation("update", "replaces the given file with the running executable", ""); + // --quietupdate + parser.addSwitch("quietupdate"); + parser.addShortOpt("quietupdate", 'U'); + parser.addDocumentation("quietupdate", "doesn't restart MultiMC after installing updates"); + // --launch + parser.addOption("launch"); + parser.addShortOpt("launch", 'l'); + parser.addDocumentation("launch", "tries to launch the given instance", ""); + + // parse the arguments + QHash args; + try { + args = parser.parse(app.arguments()); + } catch(ParsingError e) { + std::cerr << "CommandLineError: " << e.what() << std::endl; + return 1; + } + + // display help and exit + if (args["help"].toBool()) { + std::cout << qPrintable(parser.compileHelp(app.arguments()[0])); + return 0; + } + + // update + // Note: cwd is always the current executable path! + if (!args["update"].isNull()) + { + std::cout << "Performing MultiMC update: " << qPrintable(args["update"].toString()) << std::endl; + QDir::setCurrent(app.applicationDirPath()); + QFile file(app.applicationFilePath()); + file.copy(args["update"].toString()); + if(args["quietupdate"].toBool()) + return 0; + } + + // change directory + QDir::setCurrent(args["dir"].toString()); + + // launch instance. + if (!args["launch"].isNull()) + { + std::cout << "Launching instance: " << qPrintable(args["launch"].toString()) << std::endl; + // TODO: make it launch the an instance. + // needs the new instance model to be complete + std::cerr << "Launching Instances is not implemented yet!" << std::endl; + return 255; + } + + // load settings settings = new AppSettings(&app); // Register meta types. qRegisterMetaType("LoginResponse"); + // show window MainWindow mainWin; mainWin.show(); + // loop return app.exec(); } -- cgit v1.2.3 From 576e979df4a54df9bf5ffeae3559f488b3045268 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Thu, 21 Feb 2013 19:35:52 +0100 Subject: Implement About Dialog Prepared XDG icon theme in :/icons/multimc. will only be usefull as soon as Qt decides to support custom fallback themes. use the resources directly for now. --- main.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index efabc20a..fb7a5061 100644 --- a/main.cpp +++ b/main.cpp @@ -27,6 +27,8 @@ #include "util/cmdutils.h" +#include "config.h" + using namespace Util::Commandline; int main(int argc, char *argv[]) @@ -46,7 +48,11 @@ int main(int argc, char *argv[]) // --help parser.addSwitch("help"); parser.addShortOpt("help", 'h'); - parser.addDocumentation("help", "displays help on command line parameters"); + parser.addDocumentation("help", "display this help and exit."); + // --version + parser.addSwitch("version"); + parser.addShortOpt("version", 'V'); + parser.addDocumentation("version", "display program version and exit."); // --dir parser.addOption("dir", app.applicationDirPath()); parser.addShortOpt("dir", 'd'); @@ -70,6 +76,7 @@ int main(int argc, char *argv[]) args = parser.parse(app.arguments()); } catch(ParsingError e) { std::cerr << "CommandLineError: " << e.what() << std::endl; + std::cerr << "Try '%1 -h' to get help on MultiMC's command line parameters." << std::endl; return 1; } @@ -79,16 +86,24 @@ int main(int argc, char *argv[]) return 0; } + // display version and exit + if (args["version"].toBool()) { + std::cout << VERSION_STR << " " << JENKINS_BUILD_TAG << " " << (ARCH==x64?"x86_64":"x86") << std::endl; + return 0; + } + // update // Note: cwd is always the current executable path! if (!args["update"].isNull()) { std::cout << "Performing MultiMC update: " << qPrintable(args["update"].toString()) << std::endl; + QString cwd = QDir::currentPath(); QDir::setCurrent(app.applicationDirPath()); QFile file(app.applicationFilePath()); file.copy(args["update"].toString()); if(args["quietupdate"].toBool()) return 0; + QDir::setCurrent(cwd); } // change directory -- cgit v1.2.3 From 9f174ad4e7853b5864d7478ce97d7afa75d76636 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Fri, 22 Feb 2013 16:17:31 +0100 Subject: Implement Instance launching Use --launch to test --- main.cpp | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 107 insertions(+), 12 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 4a4d8e7e..4f072b38 100644 --- a/main.cpp +++ b/main.cpp @@ -22,9 +22,14 @@ #include #include "gui/mainwindow.h" +#include "gui/logindialog.h" +#include "gui/taskdialog.h" +#include "instancelist.h" #include "appsettings.h" #include "data/loginresponse.h" +#include "tasks/logintask.h" +#include "data/minecraftprocess.h" #include "data/plugin/pluginmanager.h" @@ -35,6 +40,98 @@ using namespace Util::Commandline; +// Commandline instance launcher +class InstanceLauncher : public QObject +{ + Q_OBJECT +private: + InstanceList instances; + QString instId; + InstancePtr instance; + MinecraftProcess *proc; +public: + InstanceLauncher(QString instId) : QObject(), instances(settings->getInstanceDir()) + { + this->instId = instId; + } + +private: + InstancePtr findInstance(QString instId) + { + QListIterator iter(instances); + InstancePtr inst; + while(iter.hasNext()) + { + inst = iter.next(); + if (inst->id() == instId) + break; + } + if (inst->id() != instId) + return InstancePtr(); + else + return iter.peekPrevious(); + } + +private slots: + void onTerminated() + { + std::cout << "Minecraft exited" << std::endl; + QApplication::instance()->quit(); + } + + void onLoginComplete(LoginResponse response) + { + // TODO: console + proc = new MinecraftProcess(instance, response.getUsername(), response.getSessionID(), nullptr); + connect(proc, SIGNAL(ended()), SLOT(onTerminated())); + proc->launch(); + /*if (proc->pid() == 0) + { + std::cout << "Could not start instance." << std::endl; + QApplication::instance()->quit(); + return; + }*/ + } + + void doLogin(const QString &errorMsg) + { + LoginDialog* loginDlg = new LoginDialog(nullptr, errorMsg); + if (loginDlg->exec()) + { + UserInfo uInfo(loginDlg->getUsername(), loginDlg->getPassword()); + + TaskDialog* tDialog = new TaskDialog(nullptr); + LoginTask* loginTask = new LoginTask(uInfo, tDialog); + connect(loginTask, SIGNAL(loginComplete(LoginResponse)), + SLOT(onLoginComplete(LoginResponse)), Qt::QueuedConnection); + connect(loginTask, SIGNAL(loginFailed(QString)), + SLOT(doLogin(QString)), Qt::QueuedConnection); + tDialog->exec(loginTask); + } + //onLoginComplete(LoginResponse("Offline","Offline", 1)); + } + +public: + int launch() + { + std::cout << "Loading Instances..." << std::endl; + instances.loadList(); + + std::cout << "Launching Instance '" << qPrintable(instId) << "'" << std::endl; + instance = findInstance(instId); + if (instance.isNull()) + { + std::cout << "Could not find instance requested. note that you have to specify the ID, not the NAME" << std::endl; + return 1; + } + + std::cout << "Logging in..." << std::endl; + doLogin(""); + + return QApplication::instance()->exec(); + } +}; + int main(int argc, char *argv[]) { // initialize Qt @@ -44,7 +141,7 @@ int main(int argc, char *argv[]) // Print app header std::cout << "MultiMC 5" << std::endl; - std::cout << "(c) 2013 MultiMC contributors" << std::endl << std::endl; + std::cout << "(c) 2013 MultiMC Contributors" << std::endl << std::endl; // Commandline parsing Parser parser(FlagStyle::GNU, ArgumentStyle::SpaceAndEquals); @@ -92,7 +189,9 @@ int main(int argc, char *argv[]) // display version and exit if (args["version"].toBool()) { - std::cout << VERSION_STR << " " << JENKINS_BUILD_TAG << " " << (ARCH==x64?"x86_64":"x86") << std::endl; + std::cout << "Version " << VERSION_STR << std::endl; + std::cout << "Git " << GIT_COMMIT << std::endl; + std::cout << "Tag: " << JENKINS_BUILD_TAG << " " << (ARCH==x64?"x86_64":"x86") << std::endl; return 0; } @@ -113,16 +212,6 @@ int main(int argc, char *argv[]) // change directory QDir::setCurrent(args["dir"].toString()); - // launch instance. - if (!args["launch"].isNull()) - { - std::cout << "Launching instance: " << qPrintable(args["launch"].toString()) << std::endl; - // TODO: make it launch the an instance. - // needs the new instance model to be complete - std::cerr << "Launching Instances is not implemented yet!" << std::endl; - return 255; - } - // load settings settings = new AppSettings(&app); @@ -133,6 +222,10 @@ int main(int argc, char *argv[]) PluginManager::get().loadPlugins(PathCombine(qApp->applicationDirPath(), "plugins")); PluginManager::get().initInstanceTypes(); + // launch instance. + if (!args["launch"].isNull()) + return InstanceLauncher(args["launch"].toString()).launch(); + // show main window MainWindow mainWin; mainWin.show(); @@ -140,3 +233,5 @@ int main(int argc, char *argv[]) // loop return app.exec(); } + +#include "main.moc" -- cgit v1.2.3 From 3a173648e789f30b2843241ee38e694d16e10358 Mon Sep 17 00:00:00 2001 From: Orochimarufan Date: Fri, 22 Feb 2013 18:18:23 +0100 Subject: Implement ConsoleWindow --- main.cpp | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) (limited to 'main.cpp') diff --git a/main.cpp b/main.cpp index 4f072b38..5cac6ab0 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,7 @@ #include "gui/mainwindow.h" #include "gui/logindialog.h" #include "gui/taskdialog.h" +#include "gui/consolewindow.h" #include "instancelist.h" #include "appsettings.h" @@ -49,6 +50,7 @@ private: QString instId; InstancePtr instance; MinecraftProcess *proc; + ConsoleWindow *console; public: InstanceLauncher(QString instId) : QObject(), instances(settings->getInstanceDir()) { @@ -82,15 +84,12 @@ private slots: void onLoginComplete(LoginResponse response) { // TODO: console - proc = new MinecraftProcess(instance, response.getUsername(), response.getSessionID(), nullptr); + console = new ConsoleWindow(); + proc = new MinecraftProcess(instance, response.getUsername(), response.getSessionID(), console); + //if (instance->getShowConsole()) + console->show(); connect(proc, SIGNAL(ended()), SLOT(onTerminated())); proc->launch(); - /*if (proc->pid() == 0) - { - std::cout << "Could not start instance." << std::endl; - QApplication::instance()->quit(); - return; - }*/ } void doLogin(const QString &errorMsg) -- cgit v1.2.3