From 289472d8ba8f395d7f3a93d15e33c8d4619aa603 Mon Sep 17 00:00:00 2001 From: huanghongxun Date: Tue, 18 May 2021 00:53:09 +0800 Subject: [PATCH] feat: more accurate system architecture normalization --- .../hmcl/util/SelfDependencyPatcher.java | 13 +- .../hmcl/util/platform/Architecture.java | 137 ++++++++++++++++++ .../hmcl/util/platform/OperatingSystem.java | 10 -- 3 files changed, 141 insertions(+), 19 deletions(-) create mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/Architecture.java 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 5754d072d..1837d3664 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/SelfDependencyPatcher.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/SelfDependencyPatcher.java @@ -2,6 +2,7 @@ package org.jackhuang.hmcl.util; import org.jackhuang.hmcl.util.io.ChecksumMismatchException; import org.jackhuang.hmcl.util.io.NetworkUtils; +import org.jackhuang.hmcl.util.platform.Architecture; import org.jackhuang.hmcl.util.platform.OperatingSystem; import javax.swing.*; @@ -72,16 +73,10 @@ public class SelfDependencyPatcher { throw new IncompatibleVersionException(); } - // We can only self-patch JavaFX on x86 platform. + // We can only self-patch JavaFX on x86-64 platform. // For ARM support, user's manual patch is required. - switch (System.getProperty("os.arch", "unknown").toLowerCase()) { - case "amd64": - case "x64": - case "x86-64": - case "x86_64": - break; - default: - throw new IncompatibleVersionException(); + if (Architecture.CURRENT != Architecture.X86_64) { + throw new IncompatibleVersionException(); } // Otherwise we're free to download in Java 11+ diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/Architecture.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/Architecture.java new file mode 100644 index 000000000..597303965 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/Architecture.java @@ -0,0 +1,137 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2020 huangyuhui and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package org.jackhuang.hmcl.util.platform; + +import java.util.Locale; + +import static org.jackhuang.hmcl.util.platform.Platform.BIT_32; +import static org.jackhuang.hmcl.util.platform.Platform.BIT_64; + +public enum Architecture { + X86(BIT_32), + X86_64(BIT_64), + IA32(BIT_32), + IA64(BIT_64), + SPARC32(BIT_32), + SPARC64(BIT_64), + ARM(BIT_32), + ARM64(BIT_64), + MIPS(BIT_32), + MIPS64(BIT_64), + MIPSEL32(BIT_32), + MIPSEL64(BIT_64), + PPC(BIT_32), + PPC64(BIT_64), + PPCLE(BIT_32), + PPCLE64(BIT_64), + S390(BIT_32), + S390X(BIT_64), + RISCV(BIT_64), + UNKNOWN(Platform.UNKNOWN); + + private final Platform platform; + + Architecture(Platform platform) { + this.platform = platform; + } + + public Platform getPlatform() { + return platform; + } + + public static final String SYSTEM_ARCHITECTURE; + public static final Architecture CURRENT; + + private static Architecture normalizeArch(String value) { + value = normalize(value); + if (value.matches("^(x8664|amd64|ia32e|em64t|x64)$")) { + return X86_64; + } + if (value.matches("^(x8632|x86|i[3-6]86|ia32|x32)$")) { + return X86; + } + if (value.matches("^(ia64w?|itanium64)$")) { + return IA64; + } + if ("ia64n".equals(value)) { + return IA32; + } + if (value.matches("^(sparc|sparc32)$")) { + return SPARC32; + } + if (value.matches("^(sparcv9|sparc64)$")) { + return SPARC64; + } + if (value.matches("^(arm|arm32)$")) { + return ARM; + } + if ("aarch64".equals(value)) { + return ARM64; + } + if (value.matches("^(mips|mips32)$")) { + return MIPS; + } + if (value.matches("^(mipsel|mips32el)$")) { + return MIPSEL32; + } + if ("mips64".equals(value)) { + return MIPS64; + } + if ("mips64el".equals(value)) { + return MIPSEL64; + } + if (value.matches("^(ppc|ppc32)$")) { + return PPC; + } + if (value.matches("^(ppcle|ppc32le)$")) { + return PPCLE; + } + if ("ppc64".equals(value)) { + return PPC64; + } + if ("ppc64le".equals(value)) { + return PPCLE64; + } + if ("s390".equals(value)) { + return S390; + } + if ("s390x".equals(value)) { + return S390X; + } + if ("riscv".equals(value)) { + return RISCV; + } + return UNKNOWN; + } + + private static String normalize(String value) { + if (value == null) { + return ""; + } + return value.toLowerCase(Locale.US).replaceAll("[^a-z0-9]+", ""); + } + + static { + String arch = System.getProperty("sun.arch.data.model"); + if (arch == null) + arch = System.getProperty("os.arch"); + SYSTEM_ARCHITECTURE = arch; + + CURRENT = normalizeArch(SYSTEM_ARCHITECTURE); + } +} diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java index 0f9fa004a..78a352e2f 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OperatingSystem.java @@ -93,11 +93,6 @@ public enum OperatingSystem { */ public static final String SYSTEM_VERSION = System.getProperty("os.version"); - /** - * The architecture of current operating system. - */ - public static final String SYSTEM_ARCHITECTURE; - public static final Pattern INVALID_RESOURCE_CHARACTERS; private static final String[] INVALID_RESOURCE_BASENAMES; private static final String[] INVALID_RESOURCE_FULLNAMES; @@ -119,11 +114,6 @@ public enum OperatingSystem { SUGGESTED_MEMORY = (int) (Math.round(1.0 * TOTAL_MEMORY / 4.0 / 128.0) * 128); - String arch = System.getProperty("sun.arch.data.model"); - if (arch == null) - arch = System.getProperty("os.arch"); - SYSTEM_ARCHITECTURE = arch; - // setup the invalid names if (CURRENT_OS == WINDOWS) { // valid names and characters taken from http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/naming_a_file.asp