145 lines
4.9 KiB
C++
145 lines
4.9 KiB
C++
#include "install-java.h"
|
|
#include "network-downloader.h"
|
|
#include "fs-tools.h"
|
|
#include "prog-constains.h"
|
|
|
|
bool checkJava () {
|
|
QString JAVA_HASH = "null";
|
|
|
|
#if defined(Q_OS_WIN)
|
|
#if defined(Q_PROCESSOR_X86_64)
|
|
//JAVA_HASH = "null";
|
|
#elif defined(Q_PROCESSOR_ARM_64)
|
|
//JAVA_HASH = "null";
|
|
#endif
|
|
#elif defined(Q_OS_LINUX)
|
|
#if defined(Q_PROCESSOR_X86_64)
|
|
JAVA_HASH = "916b3a45f8f405ff2227b68e4225574b57e582585c47fd090b00d944af7c790c";
|
|
#elif defined(Q_PROCESSOR_ARM_64)
|
|
#else
|
|
//JAVA_HASH = "null";
|
|
#endif
|
|
#endif
|
|
QString hash = checkDirHash(JAVA_INSTALL_PATH);
|
|
if (JAVA_HASH == "null" || hash != JAVA_HASH)
|
|
qDebug() << "Java dir hash:" << hash;
|
|
|
|
return hash == JAVA_HASH;
|
|
}
|
|
|
|
bool installJava(std::function<void(QString, unsigned int, unsigned int)> logCallback = nullptr) {
|
|
// Detect OS and architecture
|
|
QString javaUrl; // https://github.com/bell-sw/Liberica/releases/tag/17.0.18%2B11
|
|
QString archiveType = "zip";
|
|
|
|
#if defined(Q_OS_WIN)
|
|
#if defined(Q_PROCESSOR_X86_64)
|
|
javaUrl = "https://example.com/java-windows-x64.zip";
|
|
#elif defined(Q_PROCESSOR_X86)
|
|
javaUrl = "https://example.com/java-windows-x86.zip";
|
|
#else
|
|
if (logCallback) logCallback("Unsupported Windows architecture", 0, 0);
|
|
return;
|
|
#endif
|
|
|
|
#elif defined(Q_OS_LINUX)
|
|
#if defined(Q_PROCESSOR_X86_64)
|
|
javaUrl = "https://github.com/bell-sw/Liberica/releases/download/17.0.18%2B11/bellsoft-jdk17.0.18+11-linux-amd64-crac.tar.gz";
|
|
archiveType = "tar.gz";
|
|
#elif defined(Q_PROCESSOR_ARM_64)
|
|
javaUrl = "";
|
|
archiveType = "tar.gz";
|
|
#else
|
|
if (logCallback) logCallback("Unsupported Linux architecture", 0, 0);
|
|
return;
|
|
#endif
|
|
|
|
#elif defined(Q_OS_MACOS)
|
|
#if defined(Q_PROCESSOR_X86_64)
|
|
javaUrl = "https://example.com/java-macos-x64.tar.gz";
|
|
#elif defined(Q_PROCESSOR_ARM_64)
|
|
javaUrl = "https://example.com/java-macos-arm64.tar.gz";
|
|
#else
|
|
if (logCallback) logCallback("Unsupported macOS architecture", 0, 0);
|
|
return;
|
|
#endif
|
|
|
|
#else
|
|
if (logCallback) logCallback("Unsupported OS");
|
|
return;
|
|
#endif
|
|
|
|
if (logCallback) logCallback("Creating directories...", 0, 0);
|
|
QDir tempDir(TEMP_PATH);
|
|
if (!tempDir.exists()) tempDir.mkpath(".");
|
|
QDir javaDir(JAVA_INSTALL_PATH);
|
|
if (!javaDir.exists()) javaDir.mkpath(".");
|
|
|
|
QString archivePath = tempDir.filePath(QFileInfo(QUrl(javaUrl).path()).fileName());
|
|
|
|
// Download archive
|
|
bool isDownloaded = downloadFromUrl(javaUrl, archivePath, logCallback);
|
|
if (!isDownloaded) return false;
|
|
|
|
// Extract archive
|
|
if (logCallback) logCallback("Extracting Java archive...", 0, 0);
|
|
#if defined(Q_OS_WIN)
|
|
// Unzip and move contents up one level
|
|
QProcess proc;
|
|
QString tempExtractDir = QDir(TEMP_PATH).filePath("java_extract");
|
|
QDir().mkpath(tempExtractDir);
|
|
proc.start("powershell", {"-Command", QString("Expand-Archive -Force -Path '%1' -DestinationPath '%2'").arg(archivePath).arg(tempExtractDir)});
|
|
proc.waitForFinished(-1);
|
|
|
|
// Move contents from subfolder to ./java
|
|
QDir extracted(tempExtractDir);
|
|
QStringList entries = extracted.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
for (const QString &e : entries) {
|
|
QString srcPath = extracted.filePath(e);
|
|
QString dstPath = javaDir.filePath(e);
|
|
if (!QFile::rename(srcPath, dstPath)) {
|
|
QDir srcDir(srcPath);
|
|
srcDir.rename(srcPath, dstPath);
|
|
}
|
|
}
|
|
QDir(tempExtractDir).removeRecursively();
|
|
|
|
#elif defined(Q_OS_LINUX) || defined(Q_OS_MACOS)
|
|
// Extract to temp folder first
|
|
QString tempExtractDir = QDir(TEMP_PATH).filePath("java_extract");
|
|
QDir().mkpath(tempExtractDir);
|
|
QProcess proc;
|
|
|
|
if (archiveType == "tar.gz") {
|
|
proc.start("tar", {"-xzf", archivePath, "-C", tempExtractDir});
|
|
} else if (archiveType == "zip") {
|
|
proc.start("unzip", {archivePath, "-d", tempExtractDir});
|
|
} else {
|
|
if (logCallback) logCallback("Unknown archive format", 0, 0);
|
|
return false;
|
|
}
|
|
proc.waitForFinished(-1);
|
|
|
|
// Move contents of the inner folder to ./java
|
|
QDir extracted(tempExtractDir);
|
|
QStringList folders = extracted.entryList(QDir::Dirs | QDir::NoDotAndDotDot);
|
|
QString rootFolder = folders.isEmpty() ? "" : folders.first();
|
|
QDir rootDir(tempExtractDir + "/" + rootFolder);
|
|
QStringList items = rootDir.entryList(QDir::Dirs | QDir::Files | QDir::NoDotAndDotDot);
|
|
for (const QString &item : items) {
|
|
QString src = rootDir.filePath(item);
|
|
QString dst = javaDir.filePath(item);
|
|
QFile::rename(src, dst);
|
|
}
|
|
QDir(tempExtractDir).removeRecursively();
|
|
#endif
|
|
|
|
if (logCallback) logCallback("Extraction complete.", 0, 0);
|
|
|
|
// Check hash
|
|
QString hash = checkDirHash(JAVA_INSTALL_PATH);
|
|
if (logCallback) logCallback("Total Java installation hash: " + hash, 0, 0);
|
|
|
|
return true;
|
|
}
|