From 477a1a88c6b7f5e5a78a2bbc4a6fe7781b2a0525 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 30 Sep 2015 22:52:55 +0200 Subject: [PATCH] GH-1262 fix relative paths for java binaries --- application/MainWindow.cpp | 13 ++----------- application/pages/InstanceSettingsPage.cpp | 17 ++++++++++++++--- application/pages/global/JavaPage.cpp | 17 ++++++++++++++--- depends/util/include/pathutils.h | 14 +++++++++++++- depends/util/src/pathutils.cpp | 18 ++++++++++++++++++ logic/launch/steps/CheckJava.cpp | 3 ++- logic/launch/steps/LaunchMinecraft.cpp | 3 ++- 7 files changed, 65 insertions(+), 20 deletions(-) diff --git a/application/MainWindow.cpp b/application/MainWindow.cpp index ecf0f371c..1562efc5e 100644 --- a/application/MainWindow.cpp +++ b/application/MainWindow.cpp @@ -1677,17 +1677,8 @@ void MainWindow::checkSetDefaultJava() break; } QString currentJavaPath = MMC->settings()->get("JavaPath").toString(); - if (currentJavaPath.isEmpty()) - { - askForJava = true; - break; - } - if(!currentJavaPath.contains('/')) - { - currentJavaPath = QStandardPaths::findExecutable(currentJavaPath); - } - QFile currentJavaBin(currentJavaPath); - if(!currentJavaBin.exists()) + QString actualPath = ResolveExecutable(currentJavaPath); + if (currentJavaPath.isNull()) { askForJava = true; break; diff --git a/application/pages/InstanceSettingsPage.cpp b/application/pages/InstanceSettingsPage.cpp index 91b997d69..35cbefe53 100644 --- a/application/pages/InstanceSettingsPage.cpp +++ b/application/pages/InstanceSettingsPage.cpp @@ -10,6 +10,7 @@ #include "MultiMC.h" #include +#include InstanceSettingsPage::InstanceSettingsPage(BaseInstance *inst, QWidget *parent) : QWidget(parent), ui(new Ui::InstanceSettingsPage), m_instance(inst) @@ -184,11 +185,21 @@ void InstanceSettingsPage::on_javaDetectBtn_clicked() void InstanceSettingsPage::on_javaBrowseBtn_clicked() { - QString dir = QFileDialog::getOpenFileName(this, tr("Find Java executable")); - if (!dir.isNull()) + QString raw_path = QFileDialog::getOpenFileName(this, tr("Find Java executable")); + QString cooked_path = NormalizePath(raw_path); + + // do not allow current dir - it's dirty. Do not allow dirs that don't exist + if(cooked_path.isEmpty()) { - ui->javaPathTextBox->setText(dir); + return; } + + QFileInfo javaInfo(cooked_path);; + if(!javaInfo.exists() || !javaInfo.isExecutable()) + { + return; + } + ui->javaPathTextBox->setText(cooked_path); } void InstanceSettingsPage::on_javaTestBtn_clicked() diff --git a/application/pages/global/JavaPage.cpp b/application/pages/global/JavaPage.cpp index 9b35d1f3e..18882ad93 100644 --- a/application/pages/global/JavaPage.cpp +++ b/application/pages/global/JavaPage.cpp @@ -105,13 +105,24 @@ void JavaPage::on_javaDetectBtn_clicked() ui->javaPathTextBox->setText(java->path); } } + void JavaPage::on_javaBrowseBtn_clicked() { - QString dir = QFileDialog::getOpenFileName(this, tr("Find Java executable")); - if (!dir.isNull()) + QString raw_path = QFileDialog::getOpenFileName(this, tr("Find Java executable")); + QString cooked_path = NormalizePath(raw_path); + + // do not allow current dir - it's dirty. Do not allow dirs that don't exist + if(cooked_path.isEmpty()) { - ui->javaPathTextBox->setText(dir); + return; } + + QFileInfo javaInfo(cooked_path);; + if(!javaInfo.exists() || !javaInfo.isExecutable()) + { + return; + } + ui->javaPathTextBox->setText(cooked_path); } void JavaPage::on_javaTestBtn_clicked() diff --git a/depends/util/include/pathutils.h b/depends/util/include/pathutils.h index 6d52097fa..ff23fa5d0 100644 --- a/depends/util/include/pathutils.h +++ b/depends/util/include/pathutils.h @@ -25,6 +25,18 @@ MULTIMC_UTIL_EXPORT QString PathCombine(QString path1, QString path2, QString pa MULTIMC_UTIL_EXPORT QString AbsolutePath(QString path); +/** + * Resolve an executable + * + * Will resolve: + * single executable (by name) + * relative path + * absolute path + * + * @return absolute path to executable or null string + */ +MULTIMC_UTIL_EXPORT QString ResolveExecutable(QString path); + /** * Normalize path * @@ -33,7 +45,7 @@ MULTIMC_UTIL_EXPORT QString AbsolutePath(QString path); * * Returns false if the path logic somehow filed (and normalizedPath in invalid) */ -QString NormalizePath(QString path); +MULTIMC_UTIL_EXPORT QString NormalizePath(QString path); MULTIMC_UTIL_EXPORT QString RemoveInvalidFilenameChars(QString string, QChar replaceWith = '-'); diff --git a/depends/util/src/pathutils.cpp b/depends/util/src/pathutils.cpp index 35c7f9016..af1843baa 100644 --- a/depends/util/src/pathutils.cpp +++ b/depends/util/src/pathutils.cpp @@ -40,6 +40,24 @@ QString AbsolutePath(QString path) return QFileInfo(path).absolutePath(); } +QString ResolveExecutable(QString path) +{ + if (path.isEmpty()) + { + return QString(); + } + if(!path.contains('/')) + { + path = QStandardPaths::findExecutable(path); + } + QFileInfo pathInfo(path); + if(!pathInfo.exists() || !pathInfo.isExecutable()) + { + return QString(); + } + return pathInfo.absoluteFilePath(); +} + /** * Normalize path * diff --git a/logic/launch/steps/CheckJava.cpp b/logic/launch/steps/CheckJava.cpp index 9c5fd404d..7482624fe 100644 --- a/logic/launch/steps/CheckJava.cpp +++ b/logic/launch/steps/CheckJava.cpp @@ -15,6 +15,7 @@ #include "CheckJava.h" #include +#include #include #include @@ -22,7 +23,7 @@ void CheckJava::executeTask() { auto instance = m_parent->instance(); auto settings = instance->settings(); - m_javaPath = settings->get("JavaPath").toString(); + m_javaPath = ResolveExecutable(settings->get("JavaPath").toString()); bool perInstance = settings->get("OverrideJava").toBool() || settings->get("OverrideJavaLocation").toBool(); auto realJavaPath = QStandardPaths::findExecutable(m_javaPath); diff --git a/logic/launch/steps/LaunchMinecraft.cpp b/logic/launch/steps/LaunchMinecraft.cpp index 954380031..84abb3755 100644 --- a/logic/launch/steps/LaunchMinecraft.cpp +++ b/logic/launch/steps/LaunchMinecraft.cpp @@ -16,6 +16,7 @@ #include "LaunchMinecraft.h" #include #include +#include #include LaunchMinecraft::LaunchMinecraft(LaunchTask *parent) : LaunchStep(parent) @@ -33,7 +34,7 @@ void LaunchMinecraft::executeTask() QString allArgs = args.join(", "); emit logLine("Java Arguments:\n[" + m_parent->censorPrivateInfo(allArgs) + "]\n\n", MessageLevel::MultiMC); - auto javaPath = instance->settings()->get("JavaPath").toString(); + auto javaPath = ResolveExecutable(instance->settings()->get("JavaPath").toString()); m_process.setProcessEnvironment(instance->createEnvironment());