Clang format
Signed-off-by: timoreo <contact@timoreo.fr>
This commit is contained in:
parent
3433c102b7
commit
9a4a92de4f
@ -1,158 +1,155 @@
|
|||||||
#include "JavaDownloader.h"
|
#include "JavaDownloader.h"
|
||||||
#include "net/NetJob.h"
|
|
||||||
#include "Application.h"
|
#include "Application.h"
|
||||||
#include "FileSystem.h"
|
#include "FileSystem.h"
|
||||||
#include "quazip.h"
|
|
||||||
#include "MMCZip.h"
|
#include "MMCZip.h"
|
||||||
#include "net/ChecksumValidator.h"
|
#include "net/ChecksumValidator.h"
|
||||||
|
#include "net/NetJob.h"
|
||||||
|
#include "quazip.h"
|
||||||
|
|
||||||
//Quick & dirty struct to store files
|
// Quick & dirty struct to store files
|
||||||
struct File{
|
struct File {
|
||||||
QString path;
|
QString path;
|
||||||
QString url;
|
QString url;
|
||||||
QByteArray hash;
|
QByteArray hash;
|
||||||
};
|
};
|
||||||
|
|
||||||
void JavaDownloader::downloadJava(bool isLegacy, const QString& OS) {
|
void JavaDownloader::downloadJava(bool isLegacy, const QString& OS)
|
||||||
|
{
|
||||||
auto netJob = new NetJob(QString("JRE::QueryVersions"), APPLICATION->network());
|
auto netJob = new NetJob(QString("JRE::QueryVersions"), APPLICATION->network());
|
||||||
auto response = new QByteArray();
|
auto response = new QByteArray();
|
||||||
netJob->addNetAction(Net::Download::makeByteArray(QUrl("https://piston-meta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"), response));
|
netJob->addNetAction(Net::Download::makeByteArray(
|
||||||
QObject::connect(netJob, &NetJob::finished,[netJob, response]{
|
QUrl("https://piston-meta.mojang.com/v1/products/java-runtime/2ec0cc96c44e5a76b9c8b7c39df7210883d12871/all.json"), response));
|
||||||
|
QObject::connect(netJob, &NetJob::finished, [netJob, response] {
|
||||||
netJob->deleteLater();
|
netJob->deleteLater();
|
||||||
delete response;
|
delete response;
|
||||||
});
|
});
|
||||||
QObject::connect(netJob, &NetJob::succeeded,[response, &OS, isLegacy] {
|
QObject::connect(netJob, &NetJob::succeeded, [response, &OS, isLegacy] {
|
||||||
QJsonParseError parse_error{};
|
QJsonParseError parse_error{};
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
QJsonDocument doc = QJsonDocument::fromJson(*response, &parse_error);
|
||||||
if (parse_error.error != QJsonParseError::NoError) {
|
if (parse_error.error != QJsonParseError::NoError) {
|
||||||
qWarning() << "Error while parsing JSON response at " << parse_error.offset
|
qWarning() << "Error while parsing JSON response at " << parse_error.offset << " reason: " << parse_error.errorString();
|
||||||
<< " reason: " << parse_error.errorString();
|
|
||||||
qWarning() << *response;
|
qWarning() << *response;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto versionArray = doc.object()[OS].toObject()[isLegacy ? "jre-legacy" : "java-runtime-gamma"].toArray();
|
auto versionArray = doc.object()[OS].toObject()[isLegacy ? "jre-legacy" : "java-runtime-gamma"].toArray();
|
||||||
if(!versionArray.empty()){
|
if (!versionArray.empty()) {
|
||||||
auto url = versionArray[0].toObject()["manifest"].toObject()["url"].toString();
|
auto url = versionArray[0].toObject()["manifest"].toObject()["url"].toString();
|
||||||
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
|
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
|
||||||
auto files = new QByteArray();
|
auto files = new QByteArray();
|
||||||
|
|
||||||
download->addNetAction(Net::Download::makeByteArray(QUrl(url), files));
|
download->addNetAction(Net::Download::makeByteArray(QUrl(url), files));
|
||||||
|
|
||||||
QObject::connect(download, &NetJob::finished,[download, files]{
|
QObject::connect(download, &NetJob::finished, [download, files] {
|
||||||
download->deleteLater();
|
download->deleteLater();
|
||||||
delete files;
|
delete files;
|
||||||
});
|
});
|
||||||
QObject::connect(download, &NetJob::succeeded,[files, isLegacy] {
|
QObject::connect(download, &NetJob::succeeded, [files, isLegacy] {
|
||||||
QJsonParseError parse_error{};
|
QJsonParseError parse_error{};
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(*files, &parse_error);
|
QJsonDocument doc = QJsonDocument::fromJson(*files, &parse_error);
|
||||||
if (parse_error.error != QJsonParseError::NoError) {
|
if (parse_error.error != QJsonParseError::NoError) {
|
||||||
qWarning() << "Error while parsing JSON response at " << parse_error.offset
|
qWarning() << "Error while parsing JSON response at " << parse_error.offset << " reason: " << parse_error.errorString();
|
||||||
<< " reason: " << parse_error.errorString();
|
|
||||||
qWarning() << *files;
|
qWarning() << *files;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//valid json doc, begin making jre spot
|
// valid json doc, begin making jre spot
|
||||||
auto output = FS::PathCombine(QCoreApplication::applicationDirPath(), QString("java/") + (isLegacy ? "java-legacy" : "java-current"));
|
auto output =
|
||||||
|
FS::PathCombine(QCoreApplication::applicationDirPath(), QString("java/") + (isLegacy ? "java-legacy" : "java-current"));
|
||||||
FS::ensureFolderPathExists(output);
|
FS::ensureFolderPathExists(output);
|
||||||
std::vector<File> toDownload;
|
std::vector<File> toDownload;
|
||||||
auto list = doc.object()["files"].toObject();
|
auto list = doc.object()["files"].toObject();
|
||||||
for(auto element : list){
|
for (auto element : list) {
|
||||||
auto obj = element.toObject();
|
auto obj = element.toObject();
|
||||||
for(const auto& paths : obj.keys()){
|
for (const auto& paths : obj.keys()) {
|
||||||
auto file = FS::PathCombine(output,paths);
|
auto file = FS::PathCombine(output, paths);
|
||||||
|
|
||||||
auto type = obj[paths].toObject()["type"].toString();
|
auto type = obj[paths].toObject()["type"].toString();
|
||||||
if(type == "directory"){
|
if (type == "directory") {
|
||||||
FS::ensureFolderPathExists(file);
|
FS::ensureFolderPathExists(file);
|
||||||
}else if(type == "link"){
|
} else if (type == "link") {
|
||||||
//this is linux only !
|
// this is linux only !
|
||||||
auto target = FS::PathCombine(file,"../"+obj[paths].toObject()["target"].toString());
|
auto target = FS::PathCombine(file, "../" + obj[paths].toObject()["target"].toString());
|
||||||
QFile(target).link(file);
|
QFile(target).link(file);
|
||||||
}else if(type == "file"){
|
} else if (type == "file") {
|
||||||
//TODO download compressed version if it exists ?
|
// TODO download compressed version if it exists ?
|
||||||
auto raw = obj[paths].toObject()["downloads"].toObject()["raw"].toObject();
|
auto raw = obj[paths].toObject()["downloads"].toObject()["raw"].toObject();
|
||||||
auto f = File{file,raw["url"].toString(), QByteArray::fromHex(raw["sha1"].toString().toLatin1())};
|
auto f = File{ file, raw["url"].toString(), QByteArray::fromHex(raw["sha1"].toString().toLatin1()) };
|
||||||
toDownload.push_back(f);
|
toDownload.push_back(f);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
auto elementDownload = new NetJob("JRE::FileDownload", APPLICATION->network());
|
auto elementDownload = new NetJob("JRE::FileDownload", APPLICATION->network());
|
||||||
for(const auto& file : toDownload){
|
for (const auto& file : toDownload) {
|
||||||
auto dl = Net::Download::makeFile(file.url,file.path);
|
auto dl = Net::Download::makeFile(file.url, file.path);
|
||||||
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, file.hash));
|
dl->addValidator(new Net::ChecksumValidator(QCryptographicHash::Sha1, file.hash));
|
||||||
elementDownload->addNetAction(dl);
|
elementDownload->addNetAction(dl);
|
||||||
}
|
}
|
||||||
QObject::connect(elementDownload, &NetJob::finished,[elementDownload]{
|
QObject::connect(elementDownload, &NetJob::finished, [elementDownload] { elementDownload->deleteLater(); });
|
||||||
elementDownload->deleteLater();
|
|
||||||
});
|
|
||||||
elementDownload->start();
|
elementDownload->start();
|
||||||
});
|
});
|
||||||
download->start();
|
download->start();
|
||||||
}else{
|
} else {
|
||||||
//mojang does not have a JRE for us, let's get azul zulu
|
// mojang does not have a JRE for us, let's get azul zulu
|
||||||
QString javaVersion = isLegacy ? QString("8.0") : QString("17.0");
|
QString javaVersion = isLegacy ? QString("8.0") : QString("17.0");
|
||||||
QString azulOS;
|
QString azulOS;
|
||||||
QString arch;
|
QString arch;
|
||||||
QString bitness;
|
QString bitness;
|
||||||
|
|
||||||
if(OS == "mac-os-arm64"){
|
if (OS == "mac-os-arm64") {
|
||||||
//macos arm64
|
// macos arm64
|
||||||
azulOS = "macos";
|
azulOS = "macos";
|
||||||
arch = "arm";
|
arch = "arm";
|
||||||
bitness = "64";
|
bitness = "64";
|
||||||
}else if(OS == "linux-aarch64"){
|
} else if (OS == "linux-aarch64") {
|
||||||
//linux aarch64
|
// linux aarch64
|
||||||
azulOS = "linux";
|
azulOS = "linux";
|
||||||
arch = "arm";
|
arch = "arm";
|
||||||
bitness = "64";
|
bitness = "64";
|
||||||
}
|
}
|
||||||
auto metaResponse = new QByteArray();
|
auto metaResponse = new QByteArray();
|
||||||
auto downloadJob = new NetJob(QString("JRE::QueryAzulMeta"), APPLICATION->network());
|
auto downloadJob = new NetJob(QString("JRE::QueryAzulMeta"), APPLICATION->network());
|
||||||
downloadJob->addNetAction(Net::Download::makeByteArray(QString(
|
downloadJob->addNetAction(Net::Download::makeByteArray(QString("https://api.azul.com/zulu/download/community/v1.0/bundles/?"
|
||||||
"https://api.azul.com/zulu/download/community/v1.0/bundles/?"
|
"java_version=%1"
|
||||||
"java_version=%1"
|
"&os=%2"
|
||||||
"&os=%2"
|
"&arch=%3"
|
||||||
"&arch=%3"
|
"&hw_bitness=%4"
|
||||||
"&hw_bitness=%4"
|
"&ext=zip" // as a zip for all os, even linux
|
||||||
"&ext=zip" //as a zip for all os, even linux
|
"&bundle_type=jre" // jre only
|
||||||
"&bundle_type=jre" //jre only
|
"&latest=true" // only get the one latest entry
|
||||||
"&latest=true" //only get the one latest entry
|
)
|
||||||
).arg(javaVersion,azulOS,arch,bitness), metaResponse));
|
.arg(javaVersion, azulOS, arch, bitness),
|
||||||
QObject::connect(downloadJob, &NetJob::finished,[downloadJob, metaResponse]{
|
metaResponse));
|
||||||
|
QObject::connect(downloadJob, &NetJob::finished, [downloadJob, metaResponse] {
|
||||||
downloadJob->deleteLater();
|
downloadJob->deleteLater();
|
||||||
delete metaResponse;
|
delete metaResponse;
|
||||||
});
|
});
|
||||||
|
|
||||||
QObject::connect(downloadJob, &NetJob::succeeded,[metaResponse, isLegacy] {
|
QObject::connect(downloadJob, &NetJob::succeeded, [metaResponse, isLegacy] {
|
||||||
QJsonParseError parse_error{};
|
QJsonParseError parse_error{};
|
||||||
QJsonDocument doc = QJsonDocument::fromJson(*metaResponse, &parse_error);
|
QJsonDocument doc = QJsonDocument::fromJson(*metaResponse, &parse_error);
|
||||||
if (parse_error.error != QJsonParseError::NoError) {
|
if (parse_error.error != QJsonParseError::NoError) {
|
||||||
qWarning() << "Error while parsing JSON response at " << parse_error.offset
|
qWarning() << "Error while parsing JSON response at " << parse_error.offset << " reason: " << parse_error.errorString();
|
||||||
<< " reason: " << parse_error.errorString();
|
|
||||||
qWarning() << *metaResponse;
|
qWarning() << *metaResponse;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto array = doc.array();
|
auto array = doc.array();
|
||||||
if(!array.empty()){
|
if (!array.empty()) {
|
||||||
//JRE found ! download the zip
|
// JRE found ! download the zip
|
||||||
auto downloadURL = QUrl(array[0].toObject()["url"].toString());
|
auto downloadURL = QUrl(array[0].toObject()["url"].toString());
|
||||||
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
|
auto download = new NetJob(QString("JRE::DownloadJava"), APPLICATION->network());
|
||||||
const QString path = downloadURL.host() + '/' + downloadURL.path();
|
const QString path = downloadURL.host() + '/' + downloadURL.path();
|
||||||
auto entry = APPLICATION->metacache()->resolveEntry("general", path);
|
auto entry = APPLICATION->metacache()->resolveEntry("general", path);
|
||||||
entry->setStale(true);
|
entry->setStale(true);
|
||||||
download->addNetAction(Net::Download::makeCached(downloadURL,entry));
|
download->addNetAction(Net::Download::makeCached(downloadURL, entry));
|
||||||
auto zippath = entry->getFullPath();
|
auto zippath = entry->getFullPath();
|
||||||
QObject::connect(download, &NetJob::finished,[download]{
|
QObject::connect(download, &NetJob::finished, [download] { download->deleteLater(); });
|
||||||
download->deleteLater();
|
QObject::connect(download, &NetJob::succeeded, [isLegacy, zippath] {
|
||||||
|
auto output = FS::PathCombine(FS::PathCombine(QCoreApplication::applicationDirPath(), "java"),
|
||||||
});
|
isLegacy ? "java-legacy" : "java-current");
|
||||||
QObject::connect(download, &NetJob::succeeded,[isLegacy, zippath]{
|
// This should do all of the extracting and creating folders
|
||||||
auto output = FS::PathCombine(FS::PathCombine(QCoreApplication::applicationDirPath(), "java"),isLegacy ? "java-legacy" : "java-current");
|
|
||||||
//This should do all of the extracting and creating folders
|
|
||||||
MMCZip::extractDir(zippath, output);
|
MMCZip::extractDir(zippath, output);
|
||||||
});
|
});
|
||||||
}else{
|
} else {
|
||||||
qWarning() << "No suitable JRE found !!";
|
qWarning() << "No suitable JRE found !!";
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -160,5 +157,4 @@ void JavaDownloader::downloadJava(bool isLegacy, const QString& OS) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
netJob->start();
|
netJob->start();
|
||||||
|
|
||||||
}
|
}
|
@ -3,6 +3,6 @@
|
|||||||
#include <QString>
|
#include <QString>
|
||||||
|
|
||||||
namespace JavaDownloader {
|
namespace JavaDownloader {
|
||||||
/*Downloads the java to the runtimes folder*/
|
/*Downloads the java to the runtimes folder*/
|
||||||
void downloadJava(bool isLegacy, const QString& OS);
|
void downloadJava(bool isLegacy, const QString& OS);
|
||||||
}
|
} // namespace JavaDownloader
|
||||||
|
Loading…
x
Reference in New Issue
Block a user