修复在精简 JRE 上崩溃的问题 (#1893)

* Do not load javafx.web on unsupported platforms

* No longer requires jdk.zipfs to exist
This commit is contained in:
Glavo
2022-12-02 17:23:08 +08:00
committed by GitHub
parent 9e2c46a6f8
commit d4c0c30283
2 changed files with 22 additions and 5 deletions

View File

@@ -61,6 +61,7 @@ import java.util.List;
import java.util.*; import java.util.*;
import java.util.concurrent.CancellationException; import java.util.concurrent.CancellationException;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.toSet; import static java.util.stream.Collectors.toSet;
@@ -102,14 +103,26 @@ public final class SelfDependencyPatcher {
private static final Path DEPENDENCIES_DIR_PATH = HMCL_DIRECTORY.resolve("dependencies").resolve(Platform.getPlatform().toString()).resolve("openjfx"); private static final Path DEPENDENCIES_DIR_PATH = HMCL_DIRECTORY.resolve("dependencies").resolve(Platform.getPlatform().toString()).resolve("openjfx");
static List<DependencyDescriptor> readDependencies() { static List<DependencyDescriptor> readDependencies() {
ArrayList<DependencyDescriptor> dependencies;
//noinspection ConstantConditions //noinspection ConstantConditions
try (Reader reader = new InputStreamReader(SelfDependencyPatcher.class.getResourceAsStream(DEPENDENCIES_LIST_FILE), UTF_8)) { try (Reader reader = new InputStreamReader(SelfDependencyPatcher.class.getResourceAsStream(DEPENDENCIES_LIST_FILE), UTF_8)) {
Map<String, List<DependencyDescriptor>> allDependencies = Map<String, ArrayList<DependencyDescriptor>> allDependencies =
new Gson().fromJson(reader, new TypeToken<Map<String, List<DependencyDescriptor>>>(){}.getType()); new Gson().fromJson(reader, new TypeToken<Map<String, ArrayList<DependencyDescriptor>>>(){}.getType());
return allDependencies.get(Platform.getPlatform().toString()); dependencies = allDependencies.get(Platform.getPlatform().toString());
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }
try {
ClassLoader classLoader = SelfDependencyPatcher.class.getClassLoader();
Class.forName("netscape.javascript.JSObject", false, classLoader);
Class.forName("org.w3c.dom.html.HTMLDocument", false, classLoader);
} catch (Throwable e) {
LOG.log(Level.WARNING, "Disable javafx.web because JRE is incomplete", e);
dependencies.removeIf(it -> "javafx.web".equals(it.module) || "javafx.media".equals(it.module));
}
return dependencies;
} }
public String module; public String module;

View File

@@ -19,6 +19,7 @@ package org.jackhuang.hmcl.util.io;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile; import org.apache.commons.compress.archivers.zip.ZipFile;
import org.jackhuang.hmcl.util.Lang;
import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.OperatingSystem;
import java.io.File; import java.io.File;
@@ -43,7 +44,7 @@ public final class CompressingUtils {
private static final FileSystemProvider ZIPFS_PROVIDER = FileSystemProvider.installedProviders().stream() private static final FileSystemProvider ZIPFS_PROVIDER = FileSystemProvider.installedProviders().stream()
.filter(it -> "jar".equalsIgnoreCase(it.getScheme())) .filter(it -> "jar".equalsIgnoreCase(it.getScheme()))
.findFirst() .findFirst()
.orElseThrow(() -> new IllegalStateException("Zipfs not supported")); .orElse(null);
private CompressingUtils() { private CompressingUtils() {
} }
@@ -197,6 +198,9 @@ public final class CompressingUtils {
if (useTempFile) if (useTempFile)
env.put("useTempFile", true); env.put("useTempFile", true);
try { try {
if (ZIPFS_PROVIDER == null)
throw new FileSystemNotFoundException("Module jdk.zipfs does not exist");
return ZIPFS_PROVIDER.newFileSystem(zipFile, env); return ZIPFS_PROVIDER.newFileSystem(zipFile, env);
} catch (ZipError error) { } catch (ZipError error) {
// Since Java 8 throws ZipError stupidly // Since Java 8 throws ZipError stupidly
@@ -204,7 +208,7 @@ public final class CompressingUtils {
} catch (UnsupportedOperationException ex) { } catch (UnsupportedOperationException ex) {
throw new ZipException("Not a zip file"); throw new ZipException("Not a zip file");
} catch (FileSystemNotFoundException ex) { } catch (FileSystemNotFoundException ex) {
throw new ZipException("Java Environment is broken"); throw Lang.apply(new ZipException("Java Environment is broken"), it -> it.initCause(ex));
} }
} }