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 <memory>
@ -23,6 +41,7 @@ QString downloadTypeToString(DownloadType javaDownload)
case DownloadType::Archive:
return "archive";
}
return "";
}
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
#include <QDateTime>
#include <QString>
#include <memory>
#include "java/JavaVersion.h"
namespace JavaRuntime {

View File

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

View File

@ -21,9 +21,13 @@
#include "MMCZip.h"
#include "Application.h"
#include "net/ChecksumValidator.h"
#include "net/NetJob.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()
{
// JRE found ! download the zip
@ -32,7 +36,15 @@ void ArchiveJavaDownloader::executeTask()
MetaEntryPtr entry = APPLICATION->metacache()->resolveEntry("java", m_url.toLocalFile());
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();
connect(download.get(), &NetJob::finished, [download, this] { disconnect(this, &Task::aborted, download.get(), &NetJob::abort); });
@ -45,7 +57,7 @@ void ArchiveJavaDownloader::executeTask()
extractJava(fullPath);
});
download->start();
};
}
void ArchiveJavaDownloader::extractJava(QString input)
{
@ -85,4 +97,4 @@ void ArchiveJavaDownloader::extractJava(QString input)
stepProgress(*progressStep);
});
zipTask->start();
};
}

View File

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

View File

@ -30,14 +30,24 @@ struct File {
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()
{
setStatus(tr("Downloading Java"));
auto download = makeShared<NetJob>(QString("JRE::DownloadJava"), APPLICATION->network());
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::progress, this, &ManifestJavaDownloader::progress);
@ -56,7 +66,7 @@ void ManifestJavaDownloader::executeTask()
downloadJava(doc);
});
download->start();
};
}
void ManifestJavaDownloader::downloadJava(const QJsonDocument& doc)
{
@ -111,4 +121,4 @@ void ManifestJavaDownloader::downloadJava(const QJsonDocument& doc)
connect(this, &Task::aborted, elementDownload, &NetJob::abort);
connect(elementDownload, &NetJob::succeeded, [this] { emitSucceeded(); });
elementDownload->start();
};
}

View File

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

View File

@ -34,7 +34,11 @@
*/
#include "VerifyJavaInstall.h"
#include <memory>
#include "Application.h"
#include "java/JavaInstall.h"
#include "java/JavaInstallList.h"
#include "java/JavaVersion.h"
#include "minecraft/MinecraftInstance.h"
#include "minecraft/PackProfile.h"
@ -46,6 +50,7 @@ void VerifyJavaInstall::executeTask()
auto settings = instance->settings();
auto storedVersion = settings->get("JavaVersion").toString();
auto ignoreCompatibility = settings->get("IgnoreJavaCompatibility").toBool();
auto automaticJavaSwitch = settings->get("AutomaticJavaSwitch").toBool();
auto compatibleMajors = packProfile->getProfile()->getCompatibleJavaMajors();
@ -62,16 +67,38 @@ void VerifyJavaInstall::executeTask()
return;
}
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:")
.arg(javaVersion.major()),
MessageLevel::Error);
for (auto major : compatibleMajors) {
emit logLine(tr("Java version %1").arg(major), MessageLevel::Error);
}
emit logLine(tr("Go to instance Java settings to change your Java version or disable the Java compatibility check if you know what "
"you're doing."),
MessageLevel::Error);
auto logFail = [this, &javaVersion, compatibleMajors] {
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:")
.arg(javaVersion.major()),
MessageLevel::Error);
for (auto major : compatibleMajors) {
emit logLine(tr("Java version %1").arg(major), MessageLevel::Error);
}
emit logLine(tr("Go to instance Java settings to change your Java version or disable the Java compatibility check if you know what "
"you're doing."),
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();
}
}