From 17c98655f86f8ea41c4528e3fc25d12388a36861 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 6 Oct 2013 19:54:52 +0100 Subject: First draft of multiple Java installation detection on Windows --- gui/settingsdialog.cpp | 4 +- gui/settingsdialog.ui | 18 +++++-- logic/JavaUtils.cpp | 129 ++++++++++++++++++++++++++++++++----------------- logic/JavaUtils.h | 20 +++++++- 4 files changed, 122 insertions(+), 49 deletions(-) diff --git a/gui/settingsdialog.cpp b/gui/settingsdialog.cpp index c6fe893d..011925b7 100644 --- a/gui/settingsdialog.cpp +++ b/gui/settingsdialog.cpp @@ -185,9 +185,9 @@ void SettingsDialog::loadSettings(SettingsObject *s) void SettingsDialog::on_pushButton_clicked() { JavaUtils jut; - QStringList paths = jut.FindJavaPath(); + auto javas = jut.FindJavaPaths(); - ui->javaPathTextBox->setText(paths.at(0)); + ui->javaPathTextBox->setText(std::get(javas.at(0))); } void SettingsDialog::on_btnBrowse_clicked() diff --git a/gui/settingsdialog.ui b/gui/settingsdialog.ui index d7a134fb..8fa5e96e 100644 --- a/gui/settingsdialog.ui +++ b/gui/settingsdialog.ui @@ -374,6 +374,12 @@ + + + 0 + 0 + + Java path: @@ -381,6 +387,12 @@ + + + 0 + 0 + + JVM arguments: @@ -402,9 +414,6 @@ - - - @@ -418,6 +427,9 @@ + + + diff --git a/logic/JavaUtils.cpp b/logic/JavaUtils.cpp index 1cfd0a77..91fa271c 100644 --- a/logic/JavaUtils.cpp +++ b/logic/JavaUtils.cpp @@ -14,7 +14,6 @@ */ #include "JavaUtils.h" -#include "osutils.h" #include "pathutils.h" #include @@ -22,27 +21,33 @@ #include #include -#if WINDOWS -#include +JavaUtils::JavaUtils() +{ -#endif +} -JavaUtils::JavaUtils() +std::vector JavaUtils::GetDefaultJava() { + std::vector javas; + javas.push_back(std::make_tuple("java", "unknown", "java", false)); + return javas; } #if WINDOWS -QStringList JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaFromRegistryKey(DWORD keyType, QString keyName) { - QStringList paths; + std::vector javas; + + QString archType = "unknown"; + if(keyType == KEY_WOW64_64KEY) archType = "64"; + else if(keyType == KEY_WOW64_32KEY) archType = "32"; 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) + if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName.toStdString().c_str(), 0, KEY_READ | keyType | KEY_ENUMERATE_SUB_KEYS, &jreKey) == ERROR_SUCCESS) { - // Read the current JRE version from the registry. - // This will be used to find the key that contains the JavaHome value. + // Read the current type version from the registry. + // This will be used to find any 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) @@ -51,64 +56,102 @@ QStringList JavaUtils::FindJavaPath() RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE*)value, &valueSz); } - RegCloseKey(jreKey); + QString recommended = value; + + TCHAR subKeyName[255]; + DWORD subKeyNameSize, numSubKeys, retCode; + + // Get the number of subkeys + RegQueryInfoKey(jreKey, NULL, NULL, NULL, &numSubKeys, NULL, NULL, NULL, NULL, NULL, NULL, NULL); - // 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) + // Iterate until RegEnumKeyEx fails + if(numSubKeys > 0) { - // 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) + for(int i = 0; i < numSubKeys; i++) { - value = new char[valueSz]; - RegQueryValueExA(jreKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz); - - paths << QDir(PathCombine(value, "bin")).absoluteFilePath("java.exe"); + subKeyNameSize = 255; + retCode = RegEnumKeyEx(jreKey, i, subKeyName, &subKeyNameSize, NULL, NULL, NULL, NULL); + if(retCode == ERROR_SUCCESS) + { + // Now open the registry key for the version that we just got. + QString newKeyName = keyName + "\\" + subKeyName; + + HKEY newKey; + if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, newKeyName.toStdString().c_str(), 0, KEY_READ | KEY_WOW64_64KEY, &newKey) == ERROR_SUCCESS) + { + // Read the JavaHome value to find where Java is installed. + value = new char[0]; + valueSz = 0; + if (RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz) == ERROR_MORE_DATA) + { + value = new char[valueSz]; + RegQueryValueEx(newKey, "JavaHome", NULL, NULL, (BYTE*)value, &valueSz); + + javas.push_back(std::make_tuple(subKeyName, archType, QDir(PathCombine(value, "bin")).absoluteFilePath("java.exe"), (recommended == subKeyName))); + } + + RegCloseKey(newKey); + } + } } - - RegCloseKey(jreKey); } + + RegCloseKey(jreKey); } - if(paths.length() <= 0) + return javas; +} + +std::vector JavaUtils::FindJavaPaths() +{ + std::vector JRE64s = this->FindJavaFromRegistryKey(KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment"); + std::vector JDK64s = this->FindJavaFromRegistryKey(KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Development Kit"); + std::vector JRE32s = this->FindJavaFromRegistryKey(KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment"); + std::vector JDK32s = this->FindJavaFromRegistryKey(KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Development Kit"); + + std::vector javas; + javas.insert(javas.end(), JRE64s.begin(), JRE64s.end()); + javas.insert(javas.end(), JDK64s.begin(), JDK64s.end()); + javas.insert(javas.end(), JRE32s.begin(), JRE32s.end()); + javas.insert(javas.end(), JDK32s.begin(), JDK32s.end()); + + if(javas.size() <= 0) { QLOG_WARN() << "Failed to find Java in the Windows registry - defaulting to \"java\""; - paths << "java"; + return this->GetDefaultJava(); + } + + QLOG_INFO() << "Found the following Java installations (64 -> 32, JRE -> JDK): "; + + for(auto &java : javas) + { + QString sRec; + if(std::get(java)) sRec = "(Recommended)"; + QLOG_INFO() << std::get(java) << std::get(java) << " at " << std::get(java) << sRec; } - return paths; + return javas; } #elif OSX -QStringList JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaPath() { QLOG_INFO() << "OS X Java detection incomplete - defaulting to \"java\""; - QStringList paths; - paths << "java"; - - return paths; + return this->GetDefaultPath(); } #elif LINUX -QStringList JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaPath() { QLOG_INFO() << "Linux Java detection incomplete - defaulting to \"java\""; - QStringList paths; - paths << "java"; - - return paths; + return this->GetDefaultPath(); } #else -QStringList JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaPath() { QLOG_INFO() << "Unknown operating system build - defaulting to \"java\""; - QStringList paths; - paths << "java"; - - return paths; + return this->GetDefaultPath(); } #endif diff --git a/logic/JavaUtils.h b/logic/JavaUtils.h index fef2a1bf..63daac12 100644 --- a/logic/JavaUtils.h +++ b/logic/JavaUtils.h @@ -17,10 +17,28 @@ #include +#include "osutils.h" + +#if WINDOWS + #include +#endif + +#define JI_ID 0 +#define JI_ARCH 1 +#define JI_PATH 2 +#define JI_REC 3 +typedef std::tuple java_install; + class JavaUtils { public: JavaUtils(); - QStringList FindJavaPath(); + std::vector FindJavaPaths(); + +private: + std::vector GetDefaultJava(); +#if WINDOWS + std::vector FindJavaFromRegistryKey(DWORD keyType, QString keyName); +#endif }; -- cgit v1.2.3 From 8450807c06b3831dfb4279529e93db9444a9e612 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 6 Oct 2013 22:00:47 +0100 Subject: Fix non-Windows naming derp --- logic/JavaUtils.cpp | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/logic/JavaUtils.cpp b/logic/JavaUtils.cpp index 91fa271c..5cec35b6 100644 --- a/logic/JavaUtils.cpp +++ b/logic/JavaUtils.cpp @@ -133,25 +133,25 @@ std::vector JavaUtils::FindJavaPaths() return javas; } #elif OSX -std::vector JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaPaths() { QLOG_INFO() << "OS X Java detection incomplete - defaulting to \"java\""; - return this->GetDefaultPath(); + return this->GetDefaultJava(); } #elif LINUX -std::vector JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaPaths() { QLOG_INFO() << "Linux Java detection incomplete - defaulting to \"java\""; - return this->GetDefaultPath(); + return this->GetDefaultJava(); } #else -std::vector JavaUtils::FindJavaPath() +std::vector JavaUtils::FindJavaPaths() { QLOG_INFO() << "Unknown operating system build - defaulting to \"java\""; - return this->GetDefaultPath(); + return this->GetDefaultJava(); } #endif -- cgit v1.2.3 From 58547d38673c458cf5be44909551b42497e263b7 Mon Sep 17 00:00:00 2001 From: Sky Date: Sun, 6 Oct 2013 23:44:34 +0100 Subject: Automatically try to set the Java path on first start --- MultiMC.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/MultiMC.cpp b/MultiMC.cpp index ef720c33..be1b86da 100644 --- a/MultiMC.cpp +++ b/MultiMC.cpp @@ -16,6 +16,8 @@ #include "logic/InstanceLauncher.h" #include "logic/net/HttpMetaCache.h" +#include "logic/JavaUtils.h" + #include "pathutils.h" #include "cmdutils.h" #include @@ -260,7 +262,18 @@ void MultiMC::initGlobalSettings() m_settings->registerSetting(new Setting("PermGen", 64)); // Java Settings - m_settings->registerSetting(new Setting("JavaPath", "java")); + m_settings->registerSetting(new Setting("JavaPath", "")); + QString currentJavaPath = m_settings->get("JavaPath").toString(); + if(currentJavaPath.isEmpty()) + { + QLOG_INFO() << "Java path not set, attempting to set it automatically..."; + + JavaUtils jut; + auto javas = jut.FindJavaPaths(); + + m_settings->set("JavaPath", std::get(javas.at(0))); + } + m_settings->registerSetting(new Setting("JvmArgs", "")); // Custom Commands -- cgit v1.2.3 From b30a97d4c9315e482341ec863dc5722bf78c4631 Mon Sep 17 00:00:00 2001 From: Sky Date: Mon, 7 Oct 2013 14:56:14 +0100 Subject: Make starting window size set properly on 1.6 instances --- logic/OneSixInstance.cpp | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/logic/OneSixInstance.cpp b/logic/OneSixInstance.cpp index c5d546a3..a2a6bf17 100644 --- a/logic/OneSixInstance.cpp +++ b/logic/OneSixInstance.cpp @@ -143,6 +143,10 @@ MinecraftProcess *OneSixInstance::prepareForLaunch(LoginResponse response) args << version->mainClass; args.append(processMinecraftArgs(response)); + // Set the width and height for 1.6 instances + args << QString("--width") << settings().get("MinecraftWinWidth").toString(); + args << QString("--height") << settings().get("MinecraftWinHeight").toString(); + // create the process and set its parameters MinecraftProcess *proc = new MinecraftProcess(this); proc->setMinecraftArguments(args); -- cgit v1.2.3