Apply some more suggestions from @flowln

Co-authored-by: flow <flowlnlnln@gmail.com>
Signed-off-by: timoreo <contact@timoreo.fr>
This commit is contained in:
timoreo 2022-10-24 14:55:51 +02:00
parent dcbd6cc1af
commit 6a5db12c6a
No known key found for this signature in database
GPG Key ID: 121A72C3512BA288
2 changed files with 68 additions and 52 deletions

View File

@ -2,8 +2,10 @@
#include <QMessageBox>
#include <QPushButton>
#include <memory>
#include <utility>
#include "Application.h"
#include "FileSystem.h"
#include "InstanceList.h"
#include "Json.h"
#include "MMCZip.h"
#include "SysInfo.h"
@ -34,8 +36,10 @@ void JavaDownloader::downloadMojangJavaList(const QString& OS, bool isLegacy)
netJob->addNetAction(Net::Download::makeByteArray(
QUrl("https://piston-meta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"), response));
connect(this, &Task::aborted,
[isLegacy] { QDir(FS::PathCombine("java", (isLegacy ? "java-legacy" : "java-current"))).removeRecursively(); });
connect(this, &Task::aborted, [isLegacy] {
QDir(FS::PathCombine(QCoreApplication::applicationDirPath(), "java", (isLegacy ? "java-legacy" : "java-current")))
.removeRecursively();
});
connect(netJob, &NetJob::finished, [netJob, response, this] {
// delete so that it's not called on a deleted job
@ -70,7 +74,7 @@ void JavaDownloader::downloadMojangJavaList(const QString& OS, bool isLegacy)
}
void JavaDownloader::parseMojangManifest(bool isLegacy, const QJsonArray& versionArray)
{
setStatus(tr("Downloading java from Mojang"));
setStatus(tr("Downloading Java from Mojang"));
auto url = Json::ensureString(Json::ensureObject(Json::ensureObject(versionArray[0]), "manifest"), "url");
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
auto files = new QByteArray();
@ -100,32 +104,41 @@ void JavaDownloader::parseMojangManifest(bool isLegacy, const QJsonArray& versio
}
void JavaDownloader::downloadMojangJava(bool isLegacy, const QJsonDocument& doc)
{ // valid json doc, begin making jre spot
auto output = FS::PathCombine(QString("java"), (isLegacy ? "java-legacy" : "java-current"));
auto output = FS::PathCombine(QCoreApplication::applicationDirPath(), QString("java"), (isLegacy ? "java-legacy" : "java-current"));
FS::ensureFolderPathExists(output);
std::vector<File> toDownload;
auto list = Json::ensureObject(Json::ensureObject(doc.object()), "files");
for (const auto& paths : list.keys()) {
auto file = FS::PathCombine(output, paths);
auto type = Json::requireString(Json::requireObject(list, paths), "type");
const QJsonObject& meta = Json::ensureObject(list, paths);
auto type = Json::ensureString(meta, "type");
if (type == "directory") {
FS::ensureFolderPathExists(file);
} else if (type == "link") {
// this is linux only !
auto target = FS::PathCombine(file, "../" + Json::requireString(Json::requireObject(list, paths), "target"));
auto path = Json::ensureString(meta, "target");
if (!path.isEmpty()) {
auto target = FS::PathCombine(file, "../" + path);
QFile(target).link(file);
}
} else if (type == "file") {
// TODO download compressed version if it exists ?
auto raw = Json::requireObject(Json::requireObject(Json::requireObject(list, paths), "downloads"), "raw");
auto isExec = Json::ensureBoolean(Json::requireObject(list, paths), "executable", false);
auto f = File{ file, Json::requireString(raw, "url"), QByteArray::fromHex(Json::ensureString(raw, "sha1").toLatin1()), isExec };
auto raw = Json::ensureObject(Json::ensureObject(meta, "downloads"), "raw");
auto isExec = Json::ensureBoolean(meta, "executable", false);
auto url = Json::ensureString(raw, "url");
if (!url.isEmpty() && QUrl(url).isValid()) {
auto f = File{ file, url, QByteArray::fromHex(Json::ensureString(raw, "sha1").toLatin1()), isExec };
toDownload.push_back(f);
}
}
}
auto elementDownload = new NetJob("JRE::FileDownload", APPLICATION->network());
for (const auto& file : toDownload) {
auto dl = Net::Download::makeFile(file.url, file.path);
if (!file.hash.isEmpty()) {
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, file.hash));
}
if (file.isExec) {
connect(dl.get(), &Net::Download::succeeded,
[file] { QFile(file.path).setPermissions(QFile(file.path).permissions() | QFileDevice::Permissions(0x1111)); });
@ -161,7 +174,7 @@ void JavaDownloader::downloadAzulMeta(const QString& OS, bool isLegacy, const Ne
"&os=%2"
"&arch=%3"
"&hw_bitness=%4"
"&ext=zip" // as a zip for all os, even linux NOTE !! Linux ARM is .deb only !!
"&ext=zip" // as a zip for all os, even linux NOTE !! Linux ARM is .deb or .tar.gz only !!
"&bundle_type=jre" // jre only
"&latest=true" // only get the one latest entry
)
@ -218,27 +231,30 @@ void JavaDownloader::mojangOStoAzul(const QString& OS, QString& azulOS, QString&
}
void JavaDownloader::downloadAzulJava(bool isLegacy, const QJsonArray& array)
{ // JRE found ! download the zip
setStatus(tr("Downloading java from Azul"));
setStatus(tr("Downloading Java from Azul"));
auto downloadURL = QUrl(array[0].toObject()["url"].toString());
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
auto temp = std::make_unique<QTemporaryFile>(FS::PathCombine(APPLICATION->root(), "temp", "XXXXXX.zip"));
FS::ensureFolderPathExists(FS::PathCombine(APPLICATION->root(), "temp"));
// Have to open at least once to generate path
temp->open();
temp->close();
download->addNetAction(Net::Download::makeFile(downloadURL, temp->fileName()));
auto path = APPLICATION->instances()->getStagedInstancePath();
auto temp = FS::PathCombine(path, "azulJRE.zip");
download->addNetAction(Net::Download::makeFile(downloadURL, temp));
connect(download, &NetJob::finished, [download, this] {
disconnect(this, &Task::aborted, download, &NetJob::abort);
download->deleteLater();
});
connect(download, &NetJob::aborted, [path] { APPLICATION->instances()->destroyStagingPath(path); });
connect(download, &NetJob::progress, this, &JavaDownloader::progress);
connect(download, &NetJob::failed, this, &JavaDownloader::emitFailed);
connect(download, &NetJob::failed, this, [this, path](QString reason) {
APPLICATION->instances()->destroyStagingPath(path);
emitFailed(std::move(reason));
});
connect(this, &Task::aborted, download, &NetJob::abort);
connect(download, &NetJob::succeeded, [isLegacy, file = std::move(temp), downloadURL, this] {
connect(download, &NetJob::succeeded, [isLegacy, temp, downloadURL, path, this] {
setStatus(tr("Extracting java"));
auto output = FS::PathCombine(QCoreApplication::applicationDirPath(), "java", isLegacy ? "java-legacy" : "java-current");
// This should do all of the extracting and creating folders
MMCZip::extractDir(file->fileName(), downloadURL.fileName().chopped(4), output);
MMCZip::extractDir(temp, downloadURL.fileName().chopped(4), output);
APPLICATION->instances()->destroyStagingPath(path);
emitSucceeded();
});
download->start();
@ -280,26 +296,27 @@ void JavaDownloader::showPrompts(QWidget* parent)
return;
}
// Selection using QMessageBox for java 8 or 17
QMessageBox box(QMessageBox::Icon::Question, tr("Java version"),
tr("Do you want to download Java version 8 or 17?\n Java 8 is recommended for minecraft versions below 1.17\n Java 17 "
"is recommended for minecraft versions above or equal to 1.17"),
QMessageBox box(
QMessageBox::Icon::Question, tr("Java version"),
tr("Do you want to download Java version 8 or 17?\n Java 8 is recommended for older Minecraft versions, below 1.17\n Java 17 "
"is recommended for newer Minecraft versions, starting from 1.17"),
QMessageBox::NoButton, parent);
auto yes = box.addButton("Java 17", QMessageBox::AcceptRole);
auto no = box.addButton("Java 8", QMessageBox::AcceptRole);
auto both = box.addButton(tr("Download both"), QMessageBox::AcceptRole);
auto cancel = box.addButton(QMessageBox::Cancel);
if (QFileInfo::exists(FS::PathCombine(QString("java"), "java-legacy"))) {
if (QFileInfo::exists(FS::PathCombine(QCoreApplication::applicationDirPath(), QString("java"), "java-legacy"))) {
no->setEnabled(false);
}
if (QFileInfo::exists(FS::PathCombine(QString("java"), "java-current"))) {
if (QFileInfo::exists(FS::PathCombine(QCoreApplication::applicationDirPath(), QString("java"), "java-current"))) {
yes->setEnabled(false);
}
if (!yes->isEnabled() || !no->isEnabled()) {
both->setEnabled(false);
}
if (!yes->isEnabled() && !no->isEnabled()) {
QMessageBox::warning(parent, tr("Already installed!"), tr("Both versions of java are already installed!"));
QMessageBox::information(parent, tr("Already installed!"), tr("Both versions of Java are already installed!"));
return;
}
box.exec();
@ -311,8 +328,9 @@ void JavaDownloader::showPrompts(QWidget* parent)
auto down = new JavaDownloader(isLegacy, version);
ProgressDialog dialog(parent);
dialog.setSkipButton(true, tr("Abort"));
if (dialog.execWithTask(down) && box.clickedButton() == both) {
bool finished_successfully = dialog.execWithTask(down);
// Run another download task for the other option as well!
if (finished_successfully && box.clickedButton() == both) {
auto dwn = new JavaDownloader(false, version);
ProgressDialog dg(parent);
dg.setSkipButton(true, tr("Abort"));

View File

@ -61,9 +61,6 @@
<bool>false</bool>
</property>
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0" colspan="3">
<widget class="QLineEdit" name="javaPathTextBox"/>
</item>
<item row="1" column="0">
<widget class="QPushButton" name="javaDetectBtn">
<property name="text">
@ -71,20 +68,6 @@
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="javaBrowseBtn">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="javaTestBtn">
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
<item row="2" column="0">
<widget class="QCheckBox" name="skipCompatibilityCheckbox">
<property name="toolTip">
@ -95,7 +78,24 @@
</property>
</widget>
</item>
<item row="2" column="1">
<item row="0" column="0" colspan="2">
<widget class="QLineEdit" name="javaPathTextBox"/>
</item>
<item row="0" column="2">
<widget class="QPushButton" name="javaBrowseBtn">
<property name="text">
<string>Browse...</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QPushButton" name="javaTestBtn">
<property name="text">
<string>Test</string>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QPushButton" name="javaDownloadBtn">
<property name="text">
<string>Download Java...</string>
@ -635,8 +635,6 @@
<tabstop>javaSettingsGroupBox</tabstop>
<tabstop>javaPathTextBox</tabstop>
<tabstop>javaDetectBtn</tabstop>
<tabstop>javaBrowseBtn</tabstop>
<tabstop>javaTestBtn</tabstop>
<tabstop>memoryGroupBox</tabstop>
<tabstop>minMemSpinBox</tabstop>
<tabstop>maxMemSpinBox</tabstop>