From 1dee4bb60d08995f0fd4eb229f131f2ca546d24c Mon Sep 17 00:00:00 2001 From: Sky Date: Sat, 5 Oct 2013 01:08:13 +0100 Subject: [PATCH 1/4] Add naive Windows Java detection - JavaUtils for finding it on other systems (incomplete) --- CMakeLists.txt | 3 ++ gui/settingsdialog.cpp | 11 +++- gui/settingsdialog.h | 2 + gui/settingsdialog.ui | 2 +- logic/JavaUtils.cpp | 114 +++++++++++++++++++++++++++++++++++++++++ logic/JavaUtils.h | 26 ++++++++++ 6 files changed, 156 insertions(+), 2 deletions(-) create mode 100644 logic/JavaUtils.cpp create mode 100644 logic/JavaUtils.h diff --git a/CMakeLists.txt b/CMakeLists.txt index e0637054..267a57d5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -300,6 +300,9 @@ logic/tasks/ProgressProvider.h logic/tasks/Task.h logic/tasks/Task.cpp +# Utilities +logic/JavaUtils.h +logic/JavaUtils.cpp ) diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index 9736c1c7..b5ff8d56 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -13,10 +13,11 @@ * limitations under the License. */ +#include #include "settingsdialog.h" #include "ui_settingsdialog.h" +#include "logic/JavaUtils.h" -#include #include #include #include @@ -180,3 +181,11 @@ void SettingsDialog::loadSettings(SettingsObject *s) ui->preLaunchCmdTextBox->setText(s->get("PreLaunchCommand").toString()); ui->postExitCmdTextBox->setText(s->get("PostExitCommand").toString()); } + +void SettingsDialog::on_pushButton_clicked() +{ + JavaUtils jut; + QStringList paths = jut.FindJavaPath(); + + ui->javaPathTextBox->setText(paths.at(0)); +} diff --git a/gui/settingsdialog.h b/gui/settingsdialog.h index b0a8c673..2611f105 100644 --- a/gui/settingsdialog.h +++ b/gui/settingsdialog.h @@ -53,6 +53,8 @@ private slots: void on_buttonBox_accepted(); + void on_pushButton_clicked(); + private: Ui::SettingsDialog *ui; }; diff --git a/gui/settingsdialog.ui b/gui/settingsdialog.ui index 0d30e301..f0cb5c3d 100644 --- a/gui/settingsdialog.ui +++ b/gui/settingsdialog.ui @@ -33,7 +33,7 @@ QTabWidget::Rounded - 0 + 2 diff --git a/logic/JavaUtils.cpp b/logic/JavaUtils.cpp new file mode 100644 index 00000000..d89ab9c5 --- /dev/null +++ b/logic/JavaUtils.cpp @@ -0,0 +1,114 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "JavaUtils.h" +#include "osutils.h" +#include "pathutils.h" + +#include +#include +#include +#include + +#if WINDOWS +#include + +#endif + +JavaUtils::JavaUtils() +{ + +} + +#if WINDOWS +QStringList JavaUtils::FindJavaPath() +{ + QStringList paths; + + HKEY jreKey; + QString jreKeyName = "SOFTWARE\\JavaSoft\\Java Runtime Environment"; + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, jreKeyName.toStdString().c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &jreKey) == ERROR_SUCCESS) + { + // Read the current JRE version from the registry. + // This will be used to find the key that contains the JavaHome value. + char *value = new char[0]; + DWORD valueSz = 0; + if (RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE*)value, &valueSz) == ERROR_MORE_DATA) + { + value = new char[valueSz]; + RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE*)value, &valueSz); + } + + RegCloseKey(jreKey); + + // Now open the registry key for the JRE version that we just got. + jreKeyName.append("\\").append(value); + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, jreKeyName.toStdString().c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &jreKey) == ERROR_SUCCESS) + { + // Read the JavaHome value to find where Java is installed. + value = new char[0]; + valueSz = 0; + if (RegQueryValueExA(jreKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz) == ERROR_MORE_DATA) + { + value = new char[valueSz]; + RegQueryValueExA(jreKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz); + + paths << QDir(PathCombine(value, "bin")).absoluteFilePath("java.exe"); + } + + RegCloseKey(jreKey); + } + } + + if(paths.length() <= 0) + { + qWarning() << "Failed to find Java in the Windows registry - defaulting to \"java\""; + paths << "java"; + } + + return paths; +} +#elif OSX +QStringList JavaUtils::FindJavaPath() +{ + qWarning() << "OS X Java detection incomplete - defaulting to \"java\""; + + QStringList paths; + paths << "java"; + + return paths; +} + +#elif LINUX +QStringList JavaUtils::FindJavaPath() +{ + qWarning() << "Linux Java detection incomplete - defaulting to \"java\""; + + QStringList paths; + paths << "java"; + + return paths; +} +#else +QStringList JavaUtils::FindJavaPath() +{ + qWarning() << "Unknown operating system build - defaulting to \"java\""; + + QStringList paths; + paths << "java"; + + return paths; +} +#endif diff --git a/logic/JavaUtils.h b/logic/JavaUtils.h new file mode 100644 index 00000000..fef2a1bf --- /dev/null +++ b/logic/JavaUtils.h @@ -0,0 +1,26 @@ +/* Copyright 2013 MultiMC Contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include + +class JavaUtils +{ +public: + JavaUtils(); + + QStringList FindJavaPath(); +}; From 932376c6e0aad27f65b25574b23e601afb6047d2 Mon Sep 17 00:00:00 2001 From: Sky Date: Sat, 5 Oct 2013 02:02:47 +0100 Subject: [PATCH 2/4] Add "browse" button using Qt's file browser to settings dialog --- gui/settingsdialog.cpp | 9 +++++++++ gui/settingsdialog.h | 2 ++ gui/settingsdialog.ui | 23 +++++++++++++++-------- 3 files changed, 26 insertions(+), 8 deletions(-) diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index b5ff8d56..fd876a97 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -189,3 +189,12 @@ void SettingsDialog::on_pushButton_clicked() ui->javaPathTextBox->setText(paths.at(0)); } + +void SettingsDialog::on_btnBrowse_clicked() +{ + QString dir = QFileDialog::getOpenFileName(this, tr("Find Java executable")); + if(!dir.isNull()) + { + ui->javaPathTextBox->setText(dir); + } +} diff --git a/gui/settingsdialog.h b/gui/settingsdialog.h index 2611f105..a8dfb1c6 100644 --- a/gui/settingsdialog.h +++ b/gui/settingsdialog.h @@ -55,6 +55,8 @@ private slots: void on_pushButton_clicked(); + void on_btnBrowse_clicked(); + private: Ui::SettingsDialog *ui; }; diff --git a/gui/settingsdialog.ui b/gui/settingsdialog.ui index f0cb5c3d..36c117f3 100644 --- a/gui/settingsdialog.ui +++ b/gui/settingsdialog.ui @@ -379,26 +379,33 @@ - - - - + JVM arguments: - + + + + Browse... + + + + + + + + + + Auto-detect - - - From 2398acc9e490ce124aa621c19156c89ef87591f1 Mon Sep 17 00:00:00 2001 From: Sky Date: Sat, 5 Oct 2013 02:07:19 +0100 Subject: [PATCH 3/4] Fix Java detection button sizes --- gui/settingsdialog.ui | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/gui/settingsdialog.ui b/gui/settingsdialog.ui index 36c117f3..d7a134fb 100644 --- a/gui/settingsdialog.ui +++ b/gui/settingsdialog.ui @@ -388,6 +388,12 @@ + + + 0 + 0 + + Browse... @@ -401,6 +407,12 @@ + + + 0 + 0 + + Auto-detect From d48aae7a16e8e5f33683f5a498bb5b1edb21dc31 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 6 Oct 2013 01:09:21 +0100 Subject: [PATCH 4/4] Fix Linux CMake issues arising from Windows workaround --- CMakeLists.txt | 6 ++++-- depends/pack200/CMakeLists.txt | 6 ++++-- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 267a57d5..73c866bb 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,9 @@ cmake_minimum_required(VERSION 2.8.9) -# In Qt 5.1+ we have our own main() function, don't autolink to qtmain on Windows -cmake_policy(SET CMP0020 OLD) +IF(WIN32) + # In Qt 5.1+ we have our own main() function, don't autolink to qtmain on Windows + cmake_policy(SET CMP0020 OLD) +ENDIF() project(MultiMC) diff --git a/depends/pack200/CMakeLists.txt b/depends/pack200/CMakeLists.txt index d71f6535..386c8bb8 100644 --- a/depends/pack200/CMakeLists.txt +++ b/depends/pack200/CMakeLists.txt @@ -1,7 +1,9 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.6) -# In Qt 5.1+ we have our own main() function, don't autolink to qtmain on Windows -cmake_policy(SET CMP0020 OLD) +IF(WIN32) + # In Qt 5.1+ we have our own main() function, don't autolink to qtmain on Windows + cmake_policy(SET CMP0020 OLD) +ENDIF() project(unpack200)