Enhance booting (#2531)

* Enhance booting

* Delete @Booting

* Rollback OperatingSystem

* Delete Booting

* cleanup

* Update FractureiserDetector

---------

Co-authored-by: Glavo <zjx001202@gmail.com>
This commit is contained in:
Burning_TNT
2023-09-30 14:27:56 +08:00
committed by GitHub
parent 9fb2bc4c7e
commit 54188bf8f2
10 changed files with 72 additions and 57 deletions

View File

@@ -272,7 +272,7 @@ public final class Launcher extends Application {
LOG.info("Java Home: " + System.getProperty("java.home"));
LOG.info("Current Directory: " + System.getProperty("user.dir"));
LOG.info("HMCL Directory: " + Metadata.HMCL_DIRECTORY);
LOG.info("HMCL Jar Path: " + JarUtils.thisJar().map(it -> it.toAbsolutePath().toString()).orElse("Not Found"));
LOG.info("HMCL Jar Path: " + Lang.requireNonNullElse(JarUtils.thisJarPath(), "Not Found"));
LOG.info("Memory: " + Runtime.getRuntime().maxMemory() / 1024 / 1024 + "MB");
LOG.info("Physical memory: " + OperatingSystem.TOTAL_MEMORY + " MB");
LOG.info("Metaspace: " + ManagementFactory.getMemoryPoolMXBeans().stream()

View File

@@ -163,7 +163,7 @@ public final class Main {
SwingUtils.showWarningDialog(message);
}
static void fixLetsEncrypt() {
private static void fixLetsEncrypt() {
try {
KeyStore defaultKeyStore = KeyStore.getInstance(KeyStore.getDefaultType());
Path ksPath = Paths.get(System.getProperty("java.home"), "lib", "security", "cacerts");

View File

@@ -74,7 +74,7 @@ public final class ConfigHolder {
return ownerChanged;
}
public synchronized static void init() throws IOException {
public static void init() throws IOException {
if (configInstance != null) {
throw new IllegalStateException("Configuration is already loaded");
}
@@ -121,7 +121,7 @@ public final class ConfigHolder {
private static Path locateConfig() {
Path exePath = Paths.get("").toAbsolutePath();
try {
Path jarPath = JarUtils.thisJar().orElse(null);
Path jarPath = JarUtils.thisJarPath();
if (jarPath != null && Files.isRegularFile(jarPath) && Files.isWritable(jarPath)) {
jarPath = jarPath.getParent();
exePath = jarPath;

View File

@@ -68,8 +68,8 @@ public final class ExportWizardProvider implements WizardProvider {
}
private Task<?> exportWithLauncher(String modpackType, ModpackExportInfo exportInfo, File modpackFile) {
Optional<Path> launcherJar = JarUtils.thisJar();
boolean packWithLauncher = exportInfo.isPackWithLauncher() && launcherJar.isPresent();
Path launcherJar = JarUtils.thisJarPath();
boolean packWithLauncher = exportInfo.isPackWithLauncher() && launcherJar != null;
return new Task<Object>() {
File tempModpack;
Task<?> exportTask;
@@ -141,7 +141,7 @@ public final class ExportWizardProvider implements WizardProvider {
if (background_gif.isFile())
zip.putFile(background_gif, "background.gif");
zip.putFile(launcherJar.get(), launcherJar.get().getFileName().toString());
zip.putFile(launcherJar, launcherJar.getFileName().toString());
}
}
};

View File

@@ -96,7 +96,7 @@ public final class ModpackInfoPage extends Control implements WizardPage {
launchArguments.set(versionSetting.getMinecraftArgs());
javaArguments.set(versionSetting.getJavaArgs());
canIncludeLauncher = JarUtils.thisJar().isPresent();
canIncludeLauncher = JarUtils.thisJarPath() != null;
next.set(e -> onNext());
}

View File

@@ -142,7 +142,10 @@ public final class IntegrityChecker {
}
private static void verifySelf() throws IOException {
Path self = JarUtils.thisJar().orElseThrow(() -> new IOException("Failed to find current HMCL location"));
Path self = JarUtils.thisJarPath();
if (self == null) {
throw new IOException("Failed to find current HMCL location");
}
requireVerifiedJar(self);
}
}

View File

@@ -186,7 +186,11 @@ public final class UpdateHandler {
}
private static Path getCurrentLocation() throws IOException {
return JarUtils.thisJar().orElseThrow(() -> new IOException("Failed to find current HMCL location"));
Path path = JarUtils.thisJarPath();
if (path == null) {
throw new IOException("Failed to find current HMCL location");
}
return path;
}
// ==== support for old versions ===
@@ -226,10 +230,10 @@ public final class UpdateHandler {
}
private static boolean isFirstLaunchAfterUpgrade() {
Optional<Path> currentPath = JarUtils.thisJar();
if (currentPath.isPresent()) {
Path currentPath = JarUtils.thisJarPath();
if (currentPath != null) {
Path updated = Metadata.HMCL_DIRECTORY.resolve("HMCL-" + Metadata.VERSION + ".jar");
if (currentPath.get().toAbsolutePath().equals(updated.toAbsolutePath())) {
if (currentPath.equals(updated.toAbsolutePath())) {
return true;
}
}
@@ -253,5 +257,4 @@ public final class UpdateHandler {
}
}
}
// ====
}

View File

@@ -5,7 +5,6 @@ import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
/**
* @see <a href="https://github.com/fractureiser-investigation/fractureiser">fractureiser-investigation/fractureiser</a>
@@ -15,41 +14,44 @@ public final class FractureiserDetector {
private FractureiserDetector() {
}
private static final class FractureiserException extends Exception {
}
public static boolean detect() {
try {
ArrayList<Path> badPaths = new ArrayList<>();
if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) {
Path appdata = Paths.get(System.getProperty("user.home"), "AppData");
if (Files.isDirectory(appdata)) {
badPaths.add(appdata.resolve("Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\run.bat"));
check(appdata.resolve("Roaming\\Microsoft\\Windows\\Start Menu\\Programs\\Startup\\run.bat"));
Path falseEdgePath = appdata.resolve("Local\\Microsoft Edge");
if (Files.exists(falseEdgePath)) {
badPaths.add(falseEdgePath.resolve(".ref"));
badPaths.add(falseEdgePath.resolve("client.jar"));
badPaths.add(falseEdgePath.resolve("lib.dll"));
badPaths.add(falseEdgePath.resolve("libWebGL64.jar"));
badPaths.add(falseEdgePath.resolve("run.bat"));
check(falseEdgePath.resolve(".ref"));
check(falseEdgePath.resolve("client.jar"));
check(falseEdgePath.resolve("lib.dll"));
check(falseEdgePath.resolve("libWebGL64.jar"));
check(falseEdgePath.resolve("run.bat"));
}
}
} else if (OperatingSystem.CURRENT_OS == OperatingSystem.LINUX) {
Path dataDir = Paths.get(System.getProperty("user.home"), ".config", ".data");
if (Files.exists(dataDir)) {
badPaths.add(dataDir.resolve(".ref"));
badPaths.add(dataDir.resolve("client.jar"));
badPaths.add(dataDir.resolve("lib.jar"));
}
}
for (Path badPath : badPaths) {
if (Files.isRegularFile(badPath)) {
return true;
check(dataDir.resolve(".ref"));
check(dataDir.resolve("client.jar"));
check(dataDir.resolve("lib.jar"));
}
}
} catch (FractureiserException e) {
return true;
} catch (Throwable ignored) {
}
return false;
}
private static void check(Path path) throws FractureiserException {
if (Files.isRegularFile(path)) {
throw new FractureiserException();
}
}
}

View File

@@ -17,13 +17,15 @@
*/
package org.jackhuang.hmcl.util.io;
import org.jetbrains.annotations.Nullable;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.FileSystemNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Optional;
import java.security.CodeSource;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
@@ -31,26 +33,40 @@ public final class JarUtils {
private JarUtils() {
}
@SuppressWarnings("OptionalUsedAsFieldOrParameterType")
private static final Optional<Path> THIS_JAR;
private static final Path THIS_JAR;
private static final Manifest manifest;
static {
THIS_JAR = Optional.ofNullable(JarUtils.class.getProtectionDomain().getCodeSource())
.map(codeSource -> {
try {
return Paths.get(codeSource.getLocation().toURI());
} catch (FileSystemNotFoundException | IllegalArgumentException | URISyntaxException e) {
return null;
}
})
.filter(Files::isRegularFile);
manifest = THIS_JAR.flatMap(JarUtils::getManifest).orElseGet(Manifest::new);
CodeSource cs = JarUtils.class.getProtectionDomain().getCodeSource();
if (cs == null) {
THIS_JAR = null;
manifest = new Manifest();
} else {
Path path;
try {
path = Paths.get(cs.getLocation().toURI()).toAbsolutePath();
} catch (FileSystemNotFoundException | IllegalArgumentException | URISyntaxException e) {
path = null;
}
if (path == null || !Files.isRegularFile(path)) {
THIS_JAR = null;
manifest = new Manifest();
} else {
THIS_JAR = path;
Manifest mn;
try (JarFile file = new JarFile(path.toFile())) {
mn = file.getManifest();
} catch (IOException e) {
mn = new Manifest();
}
manifest = mn;
}
}
}
public static Optional<Path> thisJar() {
@Nullable
public static Path thisJarPath() {
return THIS_JAR;
}
@@ -58,12 +74,4 @@ public final class JarUtils {
String value = manifest.getMainAttributes().getValue(name);
return value != null ? value : defaultValue;
}
public static Optional<Manifest> getManifest(Path jar) {
try (JarFile file = new JarFile(jar.toFile())) {
return Optional.ofNullable(file.getManifest());
} catch (IOException e) {
return Optional.empty();
}
}
}

View File

@@ -176,8 +176,7 @@ public enum OperatingSystem {
}
TOTAL_MEMORY = getPhysicalMemoryStatus()
.map(PhysicalMemoryStatus::getTotal)
.map(bytes -> (int) (bytes / 1024 / 1024))
.map(physicalMemoryStatus -> (int) (physicalMemoryStatus.getTotal() / 1024 / 1024))
.orElse(1024);
SUGGESTED_MEMORY = TOTAL_MEMORY >= 32768 ? 8192 : (int) (Math.round(1.0 * TOTAL_MEMORY / 4.0 / 128.0) * 128);