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

@@ -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);