Autodetect Java?

Signed-off-by: Trial97 <alexandru.tripon97@gmail.com>
This commit is contained in:
Trial97 2024-01-26 00:22:05 +02:00
parent f36be3f0e3
commit 81282bf7e0
No known key found for this signature in database
GPG Key ID: 55EF5DA53DB36318
8 changed files with 112 additions and 20 deletions

View File

@ -1,3 +1,21 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "java/JavaRuntime.h" #include "java/JavaRuntime.h"
#include <memory> #include <memory>
@ -23,6 +41,7 @@ QString downloadTypeToString(DownloadType javaDownload)
case DownloadType::Archive: case DownloadType::Archive:
return "archive"; return "archive";
} }
return "";
} }
MetaPtr parseJavaMeta(const QJsonObject& in) MetaPtr parseJavaMeta(const QJsonObject& in)
{ {

View File

@ -1,8 +1,27 @@
// SPDX-License-Identifier: GPL-3.0-only
/*
* Prism Launcher - Minecraft Launcher
* Copyright (c) 2023 Trial97 <alexandru.tripon97@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once #pragma once
#include <QDateTime> #include <QDateTime>
#include <QString> #include <QString>
#include <memory>
#include "java/JavaVersion.h" #include "java/JavaVersion.h"
namespace JavaRuntime { namespace JavaRuntime {

View File

@ -393,6 +393,7 @@ QList<QString> JavaUtils::FindJavaPaths()
scanJavaDir(snap + dirPath); scanJavaDir(snap + dirPath);
} }
}; };
scanJavaDir(FS::PathCombine(APPLICATION->dataRoot(), "java"));
// oracle RPMs // oracle RPMs
scanJavaDirs("/usr/java"); scanJavaDirs("/usr/java");
// general locations used by distro packaging // general locations used by distro packaging

View File

@ -21,9 +21,13 @@
#include "MMCZip.h" #include "MMCZip.h"
#include "Application.h" #include "Application.h"
#include "net/ChecksumValidator.h"
#include "net/NetJob.h" #include "net/NetJob.h"
#include "tasks/Task.h" #include "tasks/Task.h"
ArchiveJavaDownloader::ArchiveJavaDownloader(QUrl url, QString final_path, QString checksumType, QString checksumHash)
: m_url(url), m_final_path(final_path), m_checksum_type(checksumType), m_checksum_hash(checksumHash){};
void ArchiveJavaDownloader::executeTask() void ArchiveJavaDownloader::executeTask()
{ {
// JRE found ! download the zip // JRE found ! download the zip
@ -32,7 +36,15 @@ void ArchiveJavaDownloader::executeTask()
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("java", m_url.toLocalFile()); MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("java", m_url.toLocalFile());
auto download = makeShared<NetJob>(QString("JRE::DownloadJava"), APPLICATION->network()); auto download = makeShared<NetJob>(QString("JRE::DownloadJava"), APPLICATION->network());
download->addNetAction(Net::Download::makeCached(m_url, entry)); auto action = Net::Download::makeCached(m_url, entry);
if (!m_checksum_hash.isEmpty() && !m_checksum_type.isEmpty()) {
auto hashType = QCryptographicHash::Algorithm::Sha1;
if (m_checksum_type == "sha256") {
hashType = QCryptographicHash::Algorithm::Sha256;
}
action->addValidator(new Net::ChecksumValidator(hashType, m_checksum_hash.toLatin1()));
}
download->addNetAction(action);
auto fullPath = entry->getFullPath(); auto fullPath = entry->getFullPath();
connect(download.get(), &NetJob::finished, [download, this] { disconnect(this, &Task::aborted, download.get(), &NetJob::abort); }); connect(download.get(), &NetJob::finished, [download, this] { disconnect(this, &Task::aborted, download.get(), &NetJob::abort); });
@ -45,7 +57,7 @@ void ArchiveJavaDownloader::executeTask()
extractJava(fullPath); extractJava(fullPath);
}); });
download->start(); download->start();
}; }
void ArchiveJavaDownloader::extractJava(QString input) void ArchiveJavaDownloader::extractJava(QString input)
{ {
@ -85,4 +97,4 @@ void ArchiveJavaDownloader::extractJava(QString input)
stepProgress(*progressStep); stepProgress(*progressStep);
}); });
zipTask->start(); zipTask->start();
}; }

View File

@ -24,7 +24,7 @@
class ArchiveJavaDownloader : public Task { class ArchiveJavaDownloader : public Task {
Q_OBJECT Q_OBJECT
public: public:
ArchiveJavaDownloader(QUrl url, QString final_path); ArchiveJavaDownloader(QUrl url, QString final_path, QString checksumType = "", QString checksumHash = "");
virtual ~ArchiveJavaDownloader() = default; virtual ~ArchiveJavaDownloader() = default;
[[nodiscard]] bool canAbort() const override { return true; } [[nodiscard]] bool canAbort() const override { return true; }
@ -36,6 +36,8 @@ class ArchiveJavaDownloader : public Task {
protected: protected:
QUrl m_url; QUrl m_url;
QString m_final_path; QString m_final_path;
QString m_checksum_type;
QString m_checksum_hash;
Task::Ptr m_current_task; Task::Ptr m_current_task;
}; };

View File

@ -30,14 +30,24 @@ struct File {
bool isExec; bool isExec;
}; };
ManifestJavaDownloader::ManifestJavaDownloader(QUrl url, QString final_path) : m_url(url), m_final_path(final_path){}; ManifestJavaDownloader::ManifestJavaDownloader(QUrl url, QString final_path, QString checksumType, QString checksumHash)
: m_url(url), m_final_path(final_path), m_checksum_type(checksumType), m_checksum_hash(checksumHash){};
void ManifestJavaDownloader::executeTask() void ManifestJavaDownloader::executeTask()
{ {
setStatus(tr("Downloading Java")); setStatus(tr("Downloading Java"));
auto download = makeShared<NetJob>(QString("JRE::DownloadJava"), APPLICATION->network()); auto download = makeShared<NetJob>(QString("JRE::DownloadJava"), APPLICATION->network());
auto files = std::make_shared<QByteArray>(); auto files = std::make_shared<QByteArray>();
download->addNetAction(Net::Download::makeByteArray(m_url, files)); auto action = Net::Download::makeByteArray(m_url, files);
if (!m_checksum_hash.isEmpty() && !m_checksum_type.isEmpty()) {
auto hashType = QCryptographicHash::Algorithm::Sha1;
if (m_checksum_type == "sha256") {
hashType = QCryptographicHash::Algorithm::Sha256;
}
action->addValidator(new Net::ChecksumValidator(hashType, m_checksum_hash.toLatin1()));
}
download->addNetAction(action);
connect(download.get(), &NetJob::finished, [download, this] { disconnect(this, &Task::aborted, download.get(), &NetJob::abort); }); connect(download.get(), &NetJob::finished, [download, this] { disconnect(this, &Task::aborted, download.get(), &NetJob::abort); });
connect(download.get(), &NetJob::progress, this, &ManifestJavaDownloader::progress); connect(download.get(), &NetJob::progress, this, &ManifestJavaDownloader::progress);
@ -56,7 +66,7 @@ void ManifestJavaDownloader::executeTask()
downloadJava(doc); downloadJava(doc);
}); });
download->start(); download->start();
}; }
void ManifestJavaDownloader::downloadJava(const QJsonDocument& doc) void ManifestJavaDownloader::downloadJava(const QJsonDocument& doc)
{ {
@ -111,4 +121,4 @@ void ManifestJavaDownloader::downloadJava(const QJsonDocument& doc)
connect(this, &Task::aborted, elementDownload, &NetJob::abort); connect(this, &Task::aborted, elementDownload, &NetJob::abort);
connect(elementDownload, &NetJob::succeeded, [this] { emitSucceeded(); }); connect(elementDownload, &NetJob::succeeded, [this] { emitSucceeded(); });
elementDownload->start(); elementDownload->start();
}; }

View File

@ -24,7 +24,7 @@
class ManifestJavaDownloader : public Task { class ManifestJavaDownloader : public Task {
Q_OBJECT Q_OBJECT
public: public:
ManifestJavaDownloader(QUrl url, QString final_path); ManifestJavaDownloader(QUrl url, QString final_path, QString checksumType = "", QString checksumHash = "");
virtual ~ManifestJavaDownloader() = default; virtual ~ManifestJavaDownloader() = default;
[[nodiscard]] bool canAbort() const override { return true; } [[nodiscard]] bool canAbort() const override { return true; }
@ -36,6 +36,8 @@ class ManifestJavaDownloader : public Task {
protected: protected:
QUrl m_url; QUrl m_url;
QString m_final_path; QString m_final_path;
QString m_checksum_type;
QString m_checksum_hash;
Task::Ptr m_current_task; Task::Ptr m_current_task;
}; };

View File

@ -34,7 +34,11 @@
*/ */
#include "VerifyJavaInstall.h" #include "VerifyJavaInstall.h"
#include <memory>
#include "Application.h"
#include "java/JavaInstall.h"
#include "java/JavaInstallList.h"
#include "java/JavaVersion.h" #include "java/JavaVersion.h"
#include "minecraft/MinecraftInstance.h" #include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h" #include "minecraft/PackProfile.h"
@ -46,6 +50,7 @@ void VerifyJavaInstall::executeTask()
auto settings = instance->settings(); auto settings = instance->settings();
auto storedVersion = settings->get("JavaVersion").toString(); auto storedVersion = settings->get("JavaVersion").toString();
auto ignoreCompatibility = settings->get("IgnoreJavaCompatibility").toBool(); auto ignoreCompatibility = settings->get("IgnoreJavaCompatibility").toBool();
auto automaticJavaSwitch = settings->get("AutomaticJavaSwitch").toBool();
auto compatibleMajors = packProfile->getProfile()->getCompatibleJavaMajors(); auto compatibleMajors = packProfile->getProfile()->getCompatibleJavaMajors();
@ -62,6 +67,7 @@ void VerifyJavaInstall::executeTask()
return; return;
} }
auto logFail = [this, &javaVersion, compatibleMajors] {
emit logLine(tr("This instance is not compatible with Java version %1.\n" emit logLine(tr("This instance is not compatible with Java version %1.\n"
"Please switch to one of the following Java versions for this instance:") "Please switch to one of the following Java versions for this instance:")
.arg(javaVersion.major()), .arg(javaVersion.major()),
@ -74,4 +80,25 @@ void VerifyJavaInstall::executeTask()
MessageLevel::Error); MessageLevel::Error);
emitFailed(QString("Incompatible Java major version")); emitFailed(QString("Incompatible Java major version"));
};
if (automaticJavaSwitch || true) {
settings->set("OverrideJava", true);
auto javas = APPLICATION->javalist().get();
auto task = javas->getLoadTask();
connect(task.get(), &Task::finished, this, [this, javas, compatibleMajors, settings, &logFail] {
for (auto i = 0; i < javas->count(); i++) {
auto java = std::dynamic_pointer_cast<JavaInstall>(javas->at(i));
if (java && compatibleMajors.contains(java->id.major())) {
settings->set("OverrideJavaLocation", true);
settings->set("JavaPath", java->path);
emitSucceeded();
return;
}
}
logFail();
});
} else {
logFail();
}
} }