From 05fdea3267dd1596e704545bfddc8625ebe38cea Mon Sep 17 00:00:00 2001 From: Glavo Date: Tue, 18 Jan 2022 12:21:28 +0800 Subject: [PATCH] Improve the format of openjfx-dependencies.json --- .../hmcl/util/SelfDependencyPatcher.java | 73 +++---------- build.gradle.kts | 100 ++++++++++-------- 2 files changed, 69 insertions(+), 104 deletions(-) diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/SelfDependencyPatcher.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/SelfDependencyPatcher.java index 9293be5a1..5784a3462 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/SelfDependencyPatcher.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/SelfDependencyPatcher.java @@ -45,17 +45,13 @@ import com.google.gson.Gson; import com.google.gson.reflect.TypeToken; import org.jackhuang.hmcl.util.io.ChecksumMismatchException; import org.jackhuang.hmcl.util.io.IOUtils; -import org.jackhuang.hmcl.util.platform.Architecture; -import org.jackhuang.hmcl.util.platform.OperatingSystem; +import org.jackhuang.hmcl.util.platform.Platform; import javax.swing.*; import java.awt.*; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; -import java.io.IOException; -import java.io.InputStream; -import java.io.OutputStream; -import java.io.UncheckedIOException; +import java.io.*; import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; @@ -80,77 +76,37 @@ public final class SelfDependencyPatcher { static class DependencyDescriptor { private static final Path DEPENDENCIES_DIR_PATH = HMCL_DIRECTORY.resolve("dependencies"); - public static final String CURRENT_ARCH_CLASSIFIER = currentArchClassifier(); public static final List JFX_DEPENDENCIES = readDependencies(); private static List readDependencies() { - String content; - try (InputStream in = SelfDependencyPatcher.class.getResourceAsStream(DEPENDENCIES_LIST_FILE)) { - content = IOUtils.readFullyAsString(in, UTF_8); + //noinspection ConstantConditions + try (Reader reader = new InputStreamReader(SelfDependencyPatcher.class.getResourceAsStream(DEPENDENCIES_LIST_FILE), UTF_8)) { + Map> allDependencies = + new Gson().fromJson(reader, new TypeToken>>(){}.getType()); + return allDependencies.get(Platform.getPlatform().toString()); } catch (IOException e) { throw new UncheckedIOException(e); } - return new Gson().fromJson(content, TypeToken.getParameterized(List.class, DependencyDescriptor.class).getType()); - } - - private static String currentArchClassifier() { - if (OperatingSystem.CURRENT_OS == OperatingSystem.LINUX) { - switch (Architecture.CURRENT_ARCH) { - case X86_64: - return "linux"; - case ARM32: - return "linux-arm32-monocle"; - case ARM64: - return "linux-aarch64"; - } - } else if (OperatingSystem.CURRENT_OS == OperatingSystem.OSX) { - switch (Architecture.CURRENT_ARCH) { - case X86_64: - return "mac"; - case ARM64: - return "mac-aarch64"; - } - } else if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { - switch (Architecture.CURRENT_ARCH) { - case X86_64: - return "win"; - case X86: - return "win-x86"; - } - } - return null; } public String module; public String groupId; public String artifactId; public String version; - public Map sha1; + public String classifier; + public String sha1; public String filename() { - if (CURRENT_ARCH_CLASSIFIER == null) { - return null; - } - return artifactId + "-" + version + "-" + CURRENT_ARCH_CLASSIFIER + ".jar"; + return artifactId + "-" + version + "-" + classifier + ".jar"; } public String sha1() { - if (CURRENT_ARCH_CLASSIFIER == null) { - return null; - } - return sha1.get(CURRENT_ARCH_CLASSIFIER); + return sha1; } public Path localPath() { - if (CURRENT_ARCH_CLASSIFIER == null) { - return null; - } return DEPENDENCIES_DIR_PATH.resolve(filename()); } - - public boolean isSupported() { - return CURRENT_ARCH_CLASSIFIER != null && sha1.containsKey(CURRENT_ARCH_CLASSIFIER); - } } static final class Repository { @@ -222,7 +178,7 @@ public final class SelfDependencyPatcher { } // We can only self-patch JavaFX on specific platform. - if (DependencyDescriptor.CURRENT_ARCH_CLASSIFIER == null) { + if (JFX_DEPENDENCIES == null) { throw new IncompatibleVersionException(); } @@ -297,12 +253,10 @@ public final class SelfDependencyPatcher { LOG.info(" - Loading dependencies..."); Set modules = JFX_DEPENDENCIES.stream() - .filter(DependencyDescriptor::isSupported) .map(it -> it.module) .collect(toSet()); Path[] jars = JFX_DEPENDENCIES.stream() - .filter(DependencyDescriptor::isSupported) .map(DependencyDescriptor::localPath) .toArray(Path[]::new); @@ -396,9 +350,6 @@ public final class SelfDependencyPatcher { List missing = new ArrayList<>(); for (DependencyDescriptor dependency : JFX_DEPENDENCIES) { - if (!dependency.isSupported()) { - continue; - } if (!Files.exists(dependency.localPath())) { missing.add(dependency); continue; diff --git a/build.gradle.kts b/build.gradle.kts index 67449e0db..d004479b8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -104,24 +104,37 @@ tasks.create("checkTranslations") { defaultTasks("clean", "build") +data class Platform( + val name: String, + val classifier: String, + val groupId: String = "org.openjfx", + val unsupportedModules: List = listOf() +) { + val modules: List = jfxModules.filter { it !in unsupportedModules } + + fun fileUrl( + module: String, classifier: String, ext: String, + repo: String = "https://repo1.maven.org/maven2" + ): java.net.URL = + java.net.URL( + "$repo/${groupId.replace('.', '/')}/javafx-$module/$jfxVersion/javafx-$module-$jfxVersion-$classifier.$ext" + ) +} + val jfxModules = listOf("base", "graphics", "controls", "fxml", "media", "web") -val jfxClassifier = listOf( - "linux", "linux-arm32-monocle", "linux-aarch64", - "mac", "mac-aarch64", - "win", "win-x86" -) val jfxVersion = "17" val jfxMirrorRepos = listOf("https://maven.aliyun.com/repository/central") val jfxDependenciesFile = project("HMCL").buildDir.resolve("openjfx-dependencies.json") -val jfxUnsupported = mapOf( - "linux-arm32-monocle" to listOf("media", "web") +val jfxPlatforms = listOf( + Platform("windows-x86", "win-x86"), + Platform("windows-x86_64", "win"), + Platform("osx-x86_64", "mac"), + Platform("osx-arm64", "mac-aarch64"), + Platform("linux-x86_64", "linux"), + Platform("linux-arm32", "linux-arm32-monocle", unsupportedModules = listOf("media", "web")), + Platform("linux-arm64", "linux-aarch64"), ) -fun isSupported(module: String, classifier: String) = when (classifier) { - "linux-arm32-monocle" -> module != "media" && module != "web" - else -> true -} - val jfxInClasspath = try { Class.forName("javafx.application.Application", false, this.javaClass.classLoader) @@ -134,23 +147,28 @@ if (!jfxInClasspath && JavaVersion.current() >= JavaVersion.VERSION_11) { val os = System.getProperty("os.name").toLowerCase().let { osName -> when { osName.contains("win") -> "win" - osName.contains("mac") -> "mac" + osName.contains("mac") -> "osx" osName.contains("linux") || osName.contains("unix") -> "linux" else -> null } } - val classifier = if (os == null) null else when (System.getProperty("os.arch").toLowerCase()) { - "x86_64", "x86-64", "amd64", "ia32e", "em64t", "x64" -> os - "x86", "x86_32", "x86-32", "i386", "i486", "i586", "i686", "i86pc", "ia32", "x32" -> "$os-x86" - "arm64", "aarch64", "armv8", "armv9" -> "$os-aarch64" + val arch = when (System.getProperty("os.arch").toLowerCase()) { + "x86_64", "x86-64", "amd64", "ia32e", "em64t", "x64" -> "x86_64" + "x86", "x86_32", "x86-32", "i386", "i486", "i586", "i686", "i86pc", "ia32", "x32" -> "x86" + "arm64", "aarch64", "armv8", "armv9" -> "arm64" else -> null } - if (classifier != null && classifier in jfxClassifier) { - rootProject.subprojects { - for (module in jfxModules) { - dependencies.add("compileOnly", "org.openjfx:javafx-$module:$jfxVersion:$classifier") + if (os != null && arch != null) { + val platform = jfxPlatforms.find { it.name == "$os-arch" } + if (platform != null) { + val groupId = platform.groupId + val classifier = platform.classifier + rootProject.subprojects { + for (module in jfxModules) { + dependencies.add("compileOnly", "$groupId:javafx-$module:$jfxVersion:$classifier") + } } } } @@ -160,18 +178,17 @@ rootProject.tasks.create("generateOpenJFXDependencies") { outputs.file(jfxDependenciesFile) doLast { - val jfxDependencies = jfxModules.map { module -> - linkedMapOf( - "module" to "javafx.$module", - "groupId" to "org.openjfx", - "artifactId" to "javafx-$module", - "version" to jfxVersion, - "sha1" to jfxClassifier.filter { classifier -> isSupported(module, classifier) } - .associateWith { classifier -> - java.net.URL("https://repo1.maven.org/maven2/org/openjfx/javafx-$module/$jfxVersion/javafx-$module-$jfxVersion-$classifier.jar.sha1") - .readText() - } - ) + val jfxDependencies = jfxPlatforms.associate { platform -> + platform.name to platform.modules.map { module -> + mapOf( + "module" to "javafx.$module", + "groupId" to platform.groupId, + "artifactId" to "javafx-$module", + "version" to jfxVersion, + "classifier" to platform.classifier, + "sha1" to platform.fileUrl(module, platform.classifier, "jar.sha1").readText() + ) + } } jfxDependenciesFile.parentFile.mkdirs() @@ -185,16 +202,13 @@ rootProject.tasks.create("generateOpenJFXDependencies") { rootProject.tasks.create("preTouchOpenJFXDependencies") { doLast { for (repo in jfxMirrorRepos) { - for (module in jfxModules) { - for (classifier in jfxClassifier) { - if (isSupported(module, classifier)) { - val jarUrl = - java.net.URL("$repo/org/openjfx/javafx-$module/$jfxVersion/javafx-$module-$jfxVersion-$classifier.jar") - try { - jarUrl.readBytes() - } catch (e: Throwable) { - logger.warn("An exception occurred while pre touching $jarUrl", e) - } + for (platform in jfxPlatforms) { + for (module in platform.modules) { + val url = platform.fileUrl(module, platform.classifier, "jar") + try { + url.readBytes() + } catch (e: Throwable) { + logger.warn("An exception occurred while pre touching $url", e) } } }