aboutsummaryrefslogtreecommitdiff
path: root/launcher/java
diff options
context:
space:
mode:
authorPetr Mrázek <peterix@gmail.com>2021-07-25 19:11:59 +0200
committerPetr Mrázek <peterix@gmail.com>2021-07-25 19:50:44 +0200
commit20b9f2b42a3b58b6081af271774fbcc34025dccb (patch)
tree064fa59facb3357139b47bd4e60bfc8edb35ca11 /launcher/java
parentdd133680858351e3e07690e286882327a4f42ba5 (diff)
downloadPrismLauncher-20b9f2b42a3b58b6081af271774fbcc34025dccb.tar.gz
PrismLauncher-20b9f2b42a3b58b6081af271774fbcc34025dccb.tar.bz2
PrismLauncher-20b9f2b42a3b58b6081af271774fbcc34025dccb.zip
NOISSUE Flatten gui and logic libraries into MultiMC
Diffstat (limited to 'launcher/java')
-rw-r--r--launcher/java/JavaChecker.cpp166
-rw-r--r--launcher/java/JavaChecker.h61
-rw-r--r--launcher/java/JavaCheckerJob.cpp44
-rw-r--r--launcher/java/JavaCheckerJob.h61
-rw-r--r--launcher/java/JavaInstall.cpp28
-rw-r--r--launcher/java/JavaInstall.h38
-rw-r--r--launcher/java/JavaInstallList.cpp208
-rw-r--r--launcher/java/JavaInstallList.h81
-rw-r--r--launcher/java/JavaUtils.cpp399
-rw-r--r--launcher/java/JavaUtils.h42
-rw-r--r--launcher/java/JavaVersion.cpp121
-rw-r--r--launcher/java/JavaVersion.h49
-rw-r--r--launcher/java/JavaVersion_test.cpp116
-rw-r--r--launcher/java/launch/CheckJava.cpp139
-rw-r--r--launcher/java/launch/CheckJava.h45
15 files changed, 1598 insertions, 0 deletions
diff --git a/launcher/java/JavaChecker.cpp b/launcher/java/JavaChecker.cpp
new file mode 100644
index 00000000..d78d6505
--- /dev/null
+++ b/launcher/java/JavaChecker.cpp
@@ -0,0 +1,166 @@
+#include "JavaChecker.h"
+#include "JavaUtils.h"
+#include <FileSystem.h>
+#include <Commandline.h>
+#include <QFile>
+#include <QProcess>
+#include <QMap>
+#include <QCoreApplication>
+#include <QDebug>
+
+#include "Env.h"
+
+JavaChecker::JavaChecker(QObject *parent) : QObject(parent)
+{
+}
+
+void JavaChecker::performCheck()
+{
+ QString checkerJar = FS::PathCombine(ENV.getJarsPath(), "JavaCheck.jar");
+
+ QStringList args;
+
+ process.reset(new QProcess());
+ if(m_args.size())
+ {
+ auto extraArgs = Commandline::splitArgs(m_args);
+ args.append(extraArgs);
+ }
+ if(m_minMem != 0)
+ {
+ args << QString("-Xms%1m").arg(m_minMem);
+ }
+ if(m_maxMem != 0)
+ {
+ args << QString("-Xmx%1m").arg(m_maxMem);
+ }
+ if(m_permGen != 64)
+ {
+ args << QString("-XX:PermSize=%1m").arg(m_permGen);
+ }
+
+ args.append({"-jar", checkerJar});
+ process->setArguments(args);
+ process->setProgram(m_path);
+ process->setProcessChannelMode(QProcess::SeparateChannels);
+ process->setProcessEnvironment(CleanEnviroment());
+ qDebug() << "Running java checker: " + m_path + args.join(" ");;
+
+ connect(process.get(), SIGNAL(finished(int, QProcess::ExitStatus)), this, SLOT(finished(int, QProcess::ExitStatus)));
+ connect(process.get(), SIGNAL(error(QProcess::ProcessError)), this, SLOT(error(QProcess::ProcessError)));
+ connect(process.get(), SIGNAL(readyReadStandardOutput()), this, SLOT(stdoutReady()));
+ connect(process.get(), SIGNAL(readyReadStandardError()), this, SLOT(stderrReady()));
+ connect(&killTimer, SIGNAL(timeout()), SLOT(timeout()));
+ killTimer.setSingleShot(true);
+ killTimer.start(15000);
+ process->start();
+}
+
+void JavaChecker::stdoutReady()
+{
+ QByteArray data = process->readAllStandardOutput();
+ QString added = QString::fromLocal8Bit(data);
+ added.remove('\r');
+ m_stdout += added;
+}
+
+void JavaChecker::stderrReady()
+{
+ QByteArray data = process->readAllStandardError();
+ QString added = QString::fromLocal8Bit(data);
+ added.remove('\r');
+ m_stderr += added;
+}
+
+void JavaChecker::finished(int exitcode, QProcess::ExitStatus status)
+{
+ killTimer.stop();
+ QProcessPtr _process = process;
+ process.reset();
+
+ JavaCheckResult result;
+ {
+ result.path = m_path;
+ result.id = m_id;
+ }
+ result.errorLog = m_stderr;
+ result.outLog = m_stdout;
+ qDebug() << "STDOUT" << m_stdout;
+ qWarning() << "STDERR" << m_stderr;
+ qDebug() << "Java checker finished with status " << status << " exit code " << exitcode;
+
+ if (status == QProcess::CrashExit || exitcode == 1)
+ {
+ result.validity = JavaCheckResult::Validity::Errored;
+ emit checkFinished(result);
+ return;
+ }
+
+ bool success = true;
+
+ QMap<QString, QString> results;
+ QStringList lines = m_stdout.split("\n", QString::SkipEmptyParts);
+ for(QString line : lines)
+ {
+ line = line.trimmed();
+
+ auto parts = line.split('=', QString::SkipEmptyParts);
+ if(parts.size() != 2 || parts[0].isEmpty() || parts[1].isEmpty())
+ {
+ success = false;
+ }
+ else
+ {
+ results.insert(parts[0], parts[1]);
+ }
+ }
+
+ if(!results.contains("os.arch") || !results.contains("java.version") || !results.contains("java.vendor") || !success)
+ {
+ result.validity = JavaCheckResult::Validity::ReturnedInvalidData;
+ emit checkFinished(result);
+ return;
+ }
+
+ auto os_arch = results["os.arch"];
+ auto java_version = results["java.version"];
+ auto java_vendor = results["java.vendor"];
+ bool is_64 = os_arch == "x86_64" || os_arch == "amd64";
+
+
+ result.validity = JavaCheckResult::Validity::Valid;
+ result.is_64bit = is_64;
+ result.mojangPlatform = is_64 ? "64" : "32";
+ result.realPlatform = os_arch;
+ result.javaVersion = java_version;
+ result.javaVendor = java_vendor;
+ qDebug() << "Java checker succeeded.";
+ emit checkFinished(result);
+}
+
+void JavaChecker::error(QProcess::ProcessError err)
+{
+ if(err == QProcess::FailedToStart)
+ {
+ killTimer.stop();
+ qDebug() << "Java checker has failed to start.";
+ JavaCheckResult result;
+ {
+ result.path = m_path;
+ result.id = m_id;
+ }
+
+ emit checkFinished(result);
+ return;
+ }
+}
+
+void JavaChecker::timeout()
+{
+ // NO MERCY. NO ABUSE.
+ if(process)
+ {
+ qDebug() << "Java checker has been killed by timeout.";
+ process->kill();
+ }
+}
diff --git a/launcher/java/JavaChecker.h b/launcher/java/JavaChecker.h
new file mode 100644
index 00000000..122861cf
--- /dev/null
+++ b/launcher/java/JavaChecker.h
@@ -0,0 +1,61 @@
+#pragma once
+#include <QProcess>
+#include <QTimer>
+#include <memory>
+
+#include "QObjectPtr.h"
+
+#include "JavaVersion.h"
+
+class JavaChecker;
+
+struct JavaCheckResult
+{
+ QString path;
+ QString mojangPlatform;
+ QString realPlatform;
+ JavaVersion javaVersion;
+ QString javaVendor;
+ QString outLog;
+ QString errorLog;
+ bool is_64bit = false;
+ int id;
+ enum class Validity
+ {
+ Errored,
+ ReturnedInvalidData,
+ Valid
+ } validity = Validity::Errored;
+};
+
+typedef shared_qobject_ptr<QProcess> QProcessPtr;
+typedef shared_qobject_ptr<JavaChecker> JavaCheckerPtr;
+class JavaChecker : public QObject
+{
+ Q_OBJECT
+public:
+ explicit JavaChecker(QObject *parent = 0);
+ void performCheck();
+
+ QString m_path;
+ QString m_args;
+ int m_id = 0;
+ int m_minMem = 0;
+ int m_maxMem = 0;
+ int m_permGen = 64;
+
+signals:
+ void checkFinished(JavaCheckResult result);
+private:
+ QProcessPtr process;
+ QTimer killTimer;
+ QString m_stdout;
+ QString m_stderr;
+public
+slots:
+ void timeout();
+ void finished(int exitcode, QProcess::ExitStatus);
+ void error(QProcess::ProcessError);
+ void stdoutReady();
+ void stderrReady();
+};
diff --git a/launcher/java/JavaCheckerJob.cpp b/launcher/java/JavaCheckerJob.cpp
new file mode 100644
index 00000000..67d70066
--- /dev/null
+++ b/launcher/java/JavaCheckerJob.cpp
@@ -0,0 +1,44 @@
+/* Copyright 2013-2021 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 "JavaCheckerJob.h"
+
+#include <QDebug>
+
+void JavaCheckerJob::partFinished(JavaCheckResult result)
+{
+ num_finished++;
+ qDebug() << m_job_name.toLocal8Bit() << "progress:" << num_finished << "/"
+ << javacheckers.size();
+ setProgress(num_finished, javacheckers.size());
+
+ javaresults.replace(result.id, result);
+
+ if (num_finished == javacheckers.size())
+ {
+ emitSucceeded();
+ }
+}
+
+void JavaCheckerJob::executeTask()
+{
+ qDebug() << m_job_name.toLocal8Bit() << " started.";
+ for (auto iter : javacheckers)
+ {
+ javaresults.append(JavaCheckResult());
+ connect(iter.get(), SIGNAL(checkFinished(JavaCheckResult)), SLOT(partFinished(JavaCheckResult)));
+ iter->performCheck();
+ }
+}
diff --git a/launcher/java/JavaCheckerJob.h b/launcher/java/JavaCheckerJob.h
new file mode 100644
index 00000000..c0986420
--- /dev/null
+++ b/launcher/java/JavaCheckerJob.h
@@ -0,0 +1,61 @@
+/* Copyright 2013-2021 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 <QtNetwork>
+#include "JavaChecker.h"
+#include "tasks/Task.h"
+
+class JavaCheckerJob;
+typedef shared_qobject_ptr<JavaCheckerJob> JavaCheckerJobPtr;
+
+// FIXME: this just seems horribly redundant
+class JavaCheckerJob : public Task
+{
+ Q_OBJECT
+public:
+ explicit JavaCheckerJob(QString job_name) : Task(), m_job_name(job_name) {};
+ virtual ~JavaCheckerJob() {};
+
+ bool addJavaCheckerAction(JavaCheckerPtr base)
+ {
+ javacheckers.append(base);
+ // if this is already running, the action needs to be started right away!
+ if (isRunning())
+ {
+ setProgress(num_finished, javacheckers.size());
+ connect(base.get(), &JavaChecker::checkFinished, this, &JavaCheckerJob::partFinished);
+ base->performCheck();
+ }
+ return true;
+ }
+ QList<JavaCheckResult> getResults()
+ {
+ return javaresults;
+ }
+
+private slots:
+ void partFinished(JavaCheckResult result);
+
+protected:
+ virtual void executeTask() override;
+
+private:
+ QString m_job_name;
+ QList<JavaCheckerPtr> javacheckers;
+ QList<JavaCheckResult> javaresults;
+ int num_finished = 0;
+};
diff --git a/launcher/java/JavaInstall.cpp b/launcher/java/JavaInstall.cpp
new file mode 100644
index 00000000..5bcf7bcb
--- /dev/null
+++ b/launcher/java/JavaInstall.cpp
@@ -0,0 +1,28 @@
+#include "JavaInstall.h"
+#include <MMCStrings.h>
+
+bool JavaInstall::operator<(const JavaInstall &rhs)
+{
+ auto archCompare = Strings::naturalCompare(arch, rhs.arch, Qt::CaseInsensitive);
+ if(archCompare != 0)
+ return archCompare < 0;
+ if(id < rhs.id)
+ {
+ return true;
+ }
+ if(id > rhs.id)
+ {
+ return false;
+ }
+ return Strings::naturalCompare(path, rhs.path, Qt::CaseInsensitive) < 0;
+}
+
+bool JavaInstall::operator==(const JavaInstall &rhs)
+{
+ return arch == rhs.arch && id == rhs.id && path == rhs.path;
+}
+
+bool JavaInstall::operator>(const JavaInstall &rhs)
+{
+ return (!operator<(rhs)) && (!operator==(rhs));
+}
diff --git a/launcher/java/JavaInstall.h b/launcher/java/JavaInstall.h
new file mode 100644
index 00000000..64be40d1
--- /dev/null
+++ b/launcher/java/JavaInstall.h
@@ -0,0 +1,38 @@
+#pragma once
+
+#include "BaseVersion.h"
+#include "JavaVersion.h"
+
+struct JavaInstall : public BaseVersion
+{
+ JavaInstall(){}
+ JavaInstall(QString id, QString arch, QString path)
+ : id(id), arch(arch), path(path)
+ {
+ }
+ virtual QString descriptor()
+ {
+ return id.toString();
+ }
+
+ virtual QString name()
+ {
+ return id.toString();
+ }
+
+ virtual QString typeString() const
+ {
+ return arch;
+ }
+
+ bool operator<(const JavaInstall & rhs);
+ bool operator==(const JavaInstall & rhs);
+ bool operator>(const JavaInstall & rhs);
+
+ JavaVersion id;
+ QString arch;
+ QString path;
+ bool recommended = false;
+};
+
+typedef std::shared_ptr<JavaInstall> JavaInstallPtr;
diff --git a/launcher/java/JavaInstallList.cpp b/launcher/java/JavaInstallList.cpp
new file mode 100644
index 00000000..0bded03c
--- /dev/null
+++ b/launcher/java/JavaInstallList.cpp
@@ -0,0 +1,208 @@
+/* Copyright 2013-2021 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 <QtNetwork>
+#include <QtXml>
+#include <QRegExp>
+
+#include <QDebug>
+
+#include "java/JavaInstallList.h"
+#include "java/JavaCheckerJob.h"
+#include "java/JavaUtils.h"
+#include "MMCStrings.h"
+#include "minecraft/VersionFilterData.h"
+
+JavaInstallList::JavaInstallList(QObject *parent) : BaseVersionList(parent)
+{
+}
+
+shared_qobject_ptr<Task> JavaInstallList::getLoadTask()
+{
+ load();
+ return getCurrentTask();
+}
+
+shared_qobject_ptr<Task> JavaInstallList::getCurrentTask()
+{
+ if(m_status == Status::InProgress)
+ {
+ return m_loadTask;
+ }
+ return nullptr;
+}
+
+void JavaInstallList::load()
+{
+ if(m_status != Status::InProgress)
+ {
+ m_status = Status::InProgress;
+ m_loadTask = new JavaListLoadTask(this);
+ m_loadTask->start();
+ }
+}
+
+const BaseVersionPtr JavaInstallList::at(int i) const
+{
+ return m_vlist.at(i);
+}
+
+bool JavaInstallList::isLoaded()
+{
+ return m_status == JavaInstallList::Status::Done;
+}
+
+int JavaInstallList::count() const
+{
+ return m_vlist.count();
+}
+
+QVariant JavaInstallList::data(const QModelIndex &index, int role) const
+{
+ if (!index.isValid())
+ return QVariant();
+
+ if (index.row() > count())
+ return QVariant();
+
+ auto version = std::dynamic_pointer_cast<JavaInstall>(m_vlist[index.row()]);
+ switch (role)
+ {
+ case VersionPointerRole:
+ return qVariantFromValue(m_vlist[index.row()]);
+ case VersionIdRole:
+ return version->descriptor();
+ case VersionRole:
+ return version->id.toString();
+ case RecommendedRole:
+ return version->recommended;
+ case PathRole:
+ return version->path;
+ case ArchitectureRole:
+ return version->arch;
+ default:
+ return QVariant();
+ }
+}
+
+BaseVersionList::RoleList JavaInstallList::providesRoles() const
+{
+ return {VersionPointerRole, VersionIdRole, VersionRole, RecommendedRole, PathRole, ArchitectureRole};
+}
+
+
+void JavaInstallList::updateListData(QList<BaseVersionPtr> versions)
+{
+ beginResetModel();
+ m_vlist = versions;
+ sortVersions();
+ if(m_vlist.size())
+ {
+ auto best = std::dynamic_pointer_cast<JavaInstall>(m_vlist[0]);
+ best->recommended = true;
+ }
+ endResetModel();
+ m_status = Status::Done;
+ m_loadTask.reset();
+}
+
+bool sortJavas(BaseVersionPtr left, BaseVersionPtr right)
+{
+ auto rleft = std::dynamic_pointer_cast<JavaInstall>(left);
+ auto rright = std::dynamic_pointer_cast<JavaInstall>(right);
+ return (*rleft) > (*rright);
+}
+
+void JavaInstallList::sortVersions()
+{
+ beginResetModel();
+ std::sort(m_vlist.begin(), m_vlist.end(), sortJavas);
+ endResetModel();
+}
+
+JavaListLoadTask::JavaListLoadTask(JavaInstallList *vlist) : Task()
+{
+ m_list = vlist;
+ m_currentRecommended = NULL;
+}
+
+JavaListLoadTask::~JavaListLoadTask()
+{
+}
+
+void JavaListLoadTask::executeTask()
+{
+ setStatus(tr("Detecting Java installations..."));
+
+ JavaUtils ju;
+ QList<QString> candidate_paths = ju.FindJavaPaths();
+
+ m_job = new JavaCheckerJob("Java detection");
+ connect(m_job.get(), &Task::finished, this, &JavaListLoadTask::javaCheckerFinished);
+ connect(m_job.get(), &Task::progress, this, &Task::setProgress);
+
+ qDebug() << "Probing the following Java paths: ";
+ int id = 0;
+ for(QString candidate : candidate_paths)
+ {
+ qDebug() << " " << candidate;
+
+ auto candidate_checker = new JavaChecker();
+ candidate_checker->m_path = candidate;
+ candidate_checker->m_id = id;
+ m_job->addJavaCheckerAction(JavaCheckerPtr(candidate_checker));
+
+ id++;
+ }
+
+ m_job->start();
+}
+
+void JavaListLoadTask::javaCheckerFinished()
+{
+ QList<JavaInstallPtr> candidates;
+ auto results = m_job->getResults();
+
+ qDebug() << "Found the following valid Java installations:";
+ for(JavaCheckResult result : results)
+ {
+ if(result.validity == JavaCheckResult::Validity::Valid)
+ {
+ JavaInstallPtr javaVersion(new JavaInstall());
+
+ javaVersion->id = result.javaVersion;
+ javaVersion->arch = result.mojangPlatform;
+ javaVersion->path = result.path;
+ candidates.append(javaVersion);
+
+ qDebug() << " " << javaVersion->id.toString() << javaVersion->arch << javaVersion->path;
+ }
+ }
+
+ QList<BaseVersionPtr> javas_bvp;
+ for (auto java : candidates)
+ {
+ //qDebug() << java->id << java->arch << " at " << java->path;
+ BaseVersionPtr bp_java = std::dynamic_pointer_cast<BaseVersion>(java);
+
+ if (bp_java)
+ {
+ javas_bvp.append(java);
+ }
+ }
+
+ m_list->updateListData(javas_bvp);
+ emitSucceeded();
+}
diff --git a/launcher/java/JavaInstallList.h b/launcher/java/JavaInstallList.h
new file mode 100644
index 00000000..e5c72b17
--- /dev/null
+++ b/launcher/java/JavaInstallList.h
@@ -0,0 +1,81 @@
+/* Copyright 2013-2021 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 <QObject>
+#include <QAbstractListModel>
+
+#include "BaseVersionList.h"
+#include "tasks/Task.h"
+
+#include "JavaCheckerJob.h"
+#include "JavaInstall.h"
+
+#include "QObjectPtr.h"
+
+class JavaListLoadTask;
+
+class JavaInstallList : public BaseVersionList
+{
+ Q_OBJECT
+ enum class Status
+ {
+ NotDone,
+ InProgress,
+ Done
+ };
+public:
+ explicit JavaInstallList(QObject *parent = 0);
+
+ shared_qobject_ptr<Task> getLoadTask() override;
+ bool isLoaded() override;
+ const BaseVersionPtr at(int i) const override;
+ int count() const override;
+ void sortVersions() override;
+
+ QVariant data(const QModelIndex &index, int role) const override;
+ RoleList providesRoles() const override;
+
+public slots:
+ void updateListData(QList<BaseVersionPtr> versions) override;
+
+protected:
+ void load();
+ shared_qobject_ptr<Task> getCurrentTask();
+
+protected:
+ Status m_status = Status::NotDone;
+ shared_qobject_ptr<JavaListLoadTask> m_loadTask;
+ QList<BaseVersionPtr> m_vlist;
+};
+
+class JavaListLoadTask : public Task
+{
+ Q_OBJECT
+
+public:
+ explicit JavaListLoadTask(JavaInstallList *vlist);
+ virtual ~JavaListLoadTask();
+
+ void executeTask() override;
+public slots:
+ void javaCheckerFinished();
+
+protected:
+ shared_qobject_ptr<JavaCheckerJob> m_job;
+ JavaInstallList *m_list;
+ JavaInstall *m_currentRecommended;
+};
diff --git a/launcher/java/JavaUtils.cpp b/launcher/java/JavaUtils.cpp
new file mode 100644
index 00000000..4b231e6a
--- /dev/null
+++ b/launcher/java/JavaUtils.cpp
@@ -0,0 +1,399 @@
+/* Copyright 2013-2021 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 <QStringList>
+#include <QString>
+#include <QDir>
+#include <QStringList>
+
+#include <settings/Setting.h>
+
+#include <QDebug>
+#include "java/JavaUtils.h"
+#include "java/JavaInstallList.h"
+#include "FileSystem.h"
+
+#define IBUS "@im=ibus"
+
+JavaUtils::JavaUtils()
+{
+}
+
+#ifdef Q_OS_LINUX
+static QString processLD_LIBRARY_PATH(const QString & LD_LIBRARY_PATH)
+{
+ QDir mmcBin(QCoreApplication::applicationDirPath());
+ auto items = LD_LIBRARY_PATH.split(':');
+ QStringList final;
+ for(auto & item: items)
+ {
+ QDir test(item);
+ if(test == mmcBin)
+ {
+ qDebug() << "Env:LD_LIBRARY_PATH ignoring path" << item;
+ continue;
+ }
+ final.append(item);
+ }
+ return final.join(':');
+}
+#endif
+
+QProcessEnvironment CleanEnviroment()
+{
+ // prepare the process environment
+ QProcessEnvironment rawenv = QProcessEnvironment::systemEnvironment();
+ QProcessEnvironment env;
+
+ QStringList ignored =
+ {
+ "JAVA_ARGS",
+ "CLASSPATH",
+ "CONFIGPATH",
+ "JAVA_HOME",
+ "JRE_HOME",
+ "_JAVA_OPTIONS",
+ "JAVA_OPTIONS",
+ "JAVA_TOOL_OPTIONS"
+ };
+ for(auto key: rawenv.keys())
+ {
+ auto value = rawenv.value(key);
+ // filter out dangerous java crap
+ if(ignored.contains(key))
+ {
+ qDebug() << "Env: ignoring" << key << value;
+ continue;
+ }
+ // filter MultiMC-related things
+ if(key.startsWith("QT_"))
+ {
+ qDebug() << "Env: ignoring" << key << value;
+ continue;
+ }
+#ifdef Q_OS_LINUX
+ // Do not pass LD_* variables to java. They were intended for MultiMC
+ if(key.startsWith("LD_"))
+ {
+ qDebug() << "Env: ignoring" << key << value;
+ continue;
+ }
+ // Strip IBus
+ // IBus is a Linux IME framework. For some reason, it breaks MC?
+ if (key == "XMODIFIERS" && value.contains(IBUS))
+ {
+ QString save = value;
+ value.replace(IBUS, "");
+ qDebug() << "Env: stripped" << IBUS << "from" << save << ":" << value;
+ }
+ if(key == "GAME_PRELOAD")
+ {
+ env.insert("LD_PRELOAD", value);
+ continue;
+ }
+ if(key == "GAME_LIBRARY_PATH")
+ {
+ env.insert("LD_LIBRARY_PATH", processLD_LIBRARY_PATH(value));
+ continue;
+ }
+#endif
+ // qDebug() << "Env: " << key << value;
+ env.insert(key, value);
+ }
+#ifdef Q_OS_LINUX
+ // HACK: Workaround for QTBUG42500
+ if(!env.contains("LD_LIBRARY_PATH"))
+ {
+ env.insert("LD_LIBRARY_PATH", "");
+ }
+#endif
+
+ return env;
+}
+
+JavaInstallPtr JavaUtils::MakeJavaPtr(QString path, QString id, QString arch)
+{
+ JavaInstallPtr javaVersion(new JavaInstall());
+
+ javaVersion->id = id;
+ javaVersion->arch = arch;
+ javaVersion->path = path;
+
+ return javaVersion;
+}
+
+JavaInstallPtr JavaUtils::GetDefaultJava()
+{
+ JavaInstallPtr javaVersion(new JavaInstall());
+
+ javaVersion->id = "java";
+ javaVersion->arch = "unknown";
+#if defined(Q_OS_WIN32)
+ javaVersion->path = "javaw";
+#else
+ javaVersion->path = "java";
+#endif
+
+ return javaVersion;
+}
+
+#if defined(Q_OS_WIN32)
+QList<JavaInstallPtr> JavaUtils::FindJavaFromRegistryKey(DWORD keyType, QString keyName, QString keyJavaDir, QString subkeySuffix)
+{
+ QList<JavaInstallPtr> javas;
+
+ QString archType = "unknown";
+ if (keyType == KEY_WOW64_64KEY)
+ archType = "64";
+ else if (keyType == KEY_WOW64_32KEY)
+ archType = "32";
+
+ HKEY jreKey;
+ if (RegOpenKeyExA(HKEY_LOCAL_MACHINE, keyName.toStdString().c_str(), 0,
+ KEY_READ | keyType | KEY_ENUMERATE_SUB_KEYS, &jreKey) == ERROR_SUCCESS)
+ {
+ // 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)
+ {
+ value = new char[valueSz];
+ RegQueryValueExA(jreKey, "CurrentVersion", NULL, NULL, (BYTE *)value, &valueSz);
+ }
+
+ 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);
+
+ // Iterate until RegEnumKeyEx fails
+ if (numSubKeys > 0)
+ {
+ for (DWORD i = 0; i < numSubKeys; i++)
+ {
+ 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 + subkeySuffix;
+
+ 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, keyJavaDir.toStdString().c_str(), NULL, NULL, (BYTE *)value,
+ &valueSz) == ERROR_MORE_DATA)
+ {
+ value = new char[valueSz];
+ RegQueryValueEx(newKey, keyJavaDir.toStdString().c_str(), NULL, NULL, (BYTE *)value,
+ &valueSz);
+
+ // Now, we construct the version object and add it to the list.
+ JavaInstallPtr javaVersion(new JavaInstall());
+
+ javaVersion->id = subKeyName;
+ javaVersion->arch = archType;
+ javaVersion->path =
+ QDir(FS::PathCombine(value, "bin")).absoluteFilePath("javaw.exe");
+ javas.append(javaVersion);
+ }
+
+ RegCloseKey(newKey);
+ }
+ }
+ }
+ }
+
+ RegCloseKey(jreKey);
+ }
+
+ return javas;
+}
+
+QList<QString> JavaUtils::FindJavaPaths()
+{
+ QList<JavaInstallPtr> java_candidates;
+
+ // Oracle
+ QList<JavaInstallPtr> JRE64s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment", "JavaHome");
+ QList<JavaInstallPtr> JDK64s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\Java Development Kit", "JavaHome");
+ QList<JavaInstallPtr> JRE32s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Runtime Environment", "JavaHome");
+ QList<JavaInstallPtr> JDK32s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\Java Development Kit", "JavaHome");
+
+ // Oracle for Java 9 and newer
+ QList<JavaInstallPtr> NEWJRE64s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\JRE", "JavaHome");
+ QList<JavaInstallPtr> NEWJDK64s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_64KEY, "SOFTWARE\\JavaSoft\\JDK", "JavaHome");
+ QList<JavaInstallPtr> NEWJRE32s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\JRE", "JavaHome");
+ QList<JavaInstallPtr> NEWJDK32s = this->FindJavaFromRegistryKey(
+ KEY_WOW64_32KEY, "SOFTWARE\\JavaSoft\\JDK", "JavaHome");
+
+ // AdoptOpenJDK
+ QList<JavaInstallPtr> ADOPTOPENJRE32s = this->FindJavaFromRegistryKey(
+ KEY