Skip to content

Commit

Permalink
Enhance booting (HMCL-dev#2531)
Browse files Browse the repository at this point in the history
* Enhance booting

* Delete @Booting

* Rollback OperatingSystem

* Delete Booting

* cleanup

* Update FractureiserDetector

---------

Co-authored-by: Glavo <zjx001202@gmail.com>
  • Loading branch information
burningtnt and Glavo authored Sep 30, 2023
1 parent 9fb2bc4 commit 54188bf
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 57 deletions.
2 changes: 1 addition & 1 deletion HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public static void main(String[] args) {
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()
Expand Down
2 changes: 1 addition & 1 deletion HMCL/src/main/java/org/jackhuang/hmcl/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void showWarningAndContinue(String message) {
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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static boolean isOwnerChanged() {
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");
}
Expand Down Expand Up @@ -121,7 +121,7 @@ public synchronized static void init() throws IOException {
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ public Object finish(Map<String, Object> settings) {
}

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;
Expand Down Expand Up @@ -141,7 +141,7 @@ public void execute() throws Exception {
if (background_gif.isFile())
zip.putFile(background_gif, "background.gif");

zip.putFile(launcherJar.get(), launcherJar.get().getFileName().toString());
zip.putFile(launcherJar, launcherJar.getFileName().toString());
}
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public ModpackInfoPage(WizardController controller, HMCLGameRepository gameRepos
launchArguments.set(versionSetting.getMinecraftArgs());
javaArguments.set(versionSetting.getJavaArgs());

canIncludeLauncher = JarUtils.thisJar().isPresent();
canIncludeLauncher = JarUtils.thisJarPath() != null;

next.set(e -> onNext());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ public static boolean isOfficial() {
}

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);
}
}
13 changes: 8 additions & 5 deletions HMCL/src/main/java/org/jackhuang/hmcl/upgrade/UpdateHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,11 @@ private static Optional<Path> tryRename(Path path, String newVersion) {
}

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 ===
Expand Down Expand Up @@ -226,10 +230,10 @@ private static Optional<Path> getParentApplicationLocation() {
}

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;
}
}
Expand All @@ -253,5 +257,4 @@ private static void breakForceUpdateFeature() {
}
}
}
// ====
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
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>
Expand All @@ -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();
}
}
}
54 changes: 31 additions & 23 deletions HMCLCore/src/main/java/org/jackhuang/hmcl/util/io/JarUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,53 +17,61 @@
*/
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;

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;
}

public static String getManifestAttribute(String name, String defaultValue) {
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,7 @@ public String getCheckedName() {
}

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);
Expand Down

0 comments on commit 54188bf

Please sign in to comment.