From a10e9a04b1720175955c044ac8683409f085ca1e Mon Sep 17 00:00:00 2001 From: Glavo Date: Sun, 14 Sep 2025 20:13:43 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=9B=E5=BB=BA=20OSVersion=20=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=20(#4480)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/jackhuang/hmcl/Launcher.java | 4 +- .../jackhuang/hmcl/countly/CrashReport.java | 2 +- .../jackhuang/hmcl/game/LauncherHelper.java | 6 +- .../jackhuang/hmcl/util/NativePatcher.java | 8 +- .../jackhuang/hmcl/game/OSRestriction.java | 2 +- .../hmcl/util/platform/OSVersion.java | 184 ++++++++++++++++++ .../hmcl/util/platform/OperatingSystem.java | 35 ++-- .../platform/windows/WindowsGPUDetector.java | 2 +- .../util/platform/windows/WindowsVersion.java | 143 -------------- .../jackhuang/hmcl/util/OSVersionTest.java | 57 ++++++ .../platform/windows/WindowsVersionTest.java | 14 -- 11 files changed, 265 insertions(+), 192 deletions(-) create mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OSVersion.java delete mode 100644 HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersion.java create mode 100644 HMCLCore/src/test/java/org/jackhuang/hmcl/util/OSVersionTest.java diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java b/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java index 3b1b6b925..35e428fb5 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/Launcher.java @@ -243,8 +243,8 @@ public final class Launcher extends Application { try { LOG.info("*** " + Metadata.TITLE + " ***"); LOG.info("Operating System: " + (OperatingSystem.OS_RELEASE_PRETTY_NAME == null - ? OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION - : OperatingSystem.OS_RELEASE_PRETTY_NAME + " (" + OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION + ')')); + ? OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION.getVersion() + : OperatingSystem.OS_RELEASE_PRETTY_NAME + " (" + OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION.getVersion() + ')')); if (OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { LOG.info("Processor Identifier: " + System.getenv("PROCESSOR_IDENTIFIER")); } diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/countly/CrashReport.java b/HMCL/src/main/java/org/jackhuang/hmcl/countly/CrashReport.java index 8b1c96eea..26b4257f2 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/countly/CrashReport.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/countly/CrashReport.java @@ -42,7 +42,7 @@ public class CrashReport { "\n Content: \n " + stackTrace + "\n\n" + "-- System Details --\n" + - " Operating System: " + OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION + "\n" + + " Operating System: " + OperatingSystem.SYSTEM_NAME + ' ' + OperatingSystem.SYSTEM_VERSION.getVersion() + "\n" + " System Architecture: " + Architecture.SYSTEM_ARCH.getDisplayName() + "\n" + " Java Architecture: " + Architecture.CURRENT_ARCH.getDisplayName() + "\n" + " Java Version: " + System.getProperty("java.version") + ", " + System.getProperty("java.vendor") + "\n" + diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java index cc625246e..32f4e82a2 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/game/LauncherHelper.java @@ -160,10 +160,8 @@ public final class LauncherHelper { }), Task.composeAsync(() -> { Renderer renderer = setting.getRenderer(); - if (renderer != Renderer.DEFAULT - && OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS - && OperatingSystem.WINDOWS_VERSION != null) { - Library lib = NativePatcher.getWindowsMesaLoader(java, renderer, OperatingSystem.WINDOWS_VERSION); + if (renderer != Renderer.DEFAULT && OperatingSystem.CURRENT_OS == OperatingSystem.WINDOWS) { + Library lib = NativePatcher.getWindowsMesaLoader(java, renderer, OperatingSystem.SYSTEM_VERSION); if (lib == null) return null; File file = dependencyManager.getGameRepository().getLibraryFile(version.get(), lib); diff --git a/HMCL/src/main/java/org/jackhuang/hmcl/util/NativePatcher.java b/HMCL/src/main/java/org/jackhuang/hmcl/util/NativePatcher.java index 7e1c8d694..db4550494 100644 --- a/HMCL/src/main/java/org/jackhuang/hmcl/util/NativePatcher.java +++ b/HMCL/src/main/java/org/jackhuang/hmcl/util/NativePatcher.java @@ -24,9 +24,9 @@ import org.jackhuang.hmcl.setting.VersionSetting; import org.jackhuang.hmcl.util.gson.JsonUtils; import org.jackhuang.hmcl.util.platform.Architecture; import org.jackhuang.hmcl.java.JavaRuntime; +import org.jackhuang.hmcl.util.platform.OSVersion; import org.jackhuang.hmcl.util.platform.OperatingSystem; import org.jackhuang.hmcl.util.platform.Platform; -import org.jackhuang.hmcl.util.platform.windows.WindowsVersion; import org.jackhuang.hmcl.util.versioning.GameVersionNumber; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -181,13 +181,13 @@ public final class NativePatcher { return version.setLibraries(newLibraries); } - public static @Nullable Library getWindowsMesaLoader(@NotNull JavaRuntime javaVersion, @NotNull Renderer renderer, @NotNull WindowsVersion windowsVersion) { + public static @Nullable Library getWindowsMesaLoader(@NotNull JavaRuntime javaVersion, @NotNull Renderer renderer, @NotNull OSVersion windowsVersion) { if (renderer == Renderer.DEFAULT) return null; - if (windowsVersion.compareTo(WindowsVersion.WINDOWS_10) >= 0) { + if (windowsVersion.isAtLeast(OSVersion.WINDOWS_10)) { return getNatives(javaVersion.getPlatform()).get("mesa-loader"); - } else if (windowsVersion.compareTo(WindowsVersion.WINDOWS_7) >= 0) { + } else if (windowsVersion.isAtLeast(OSVersion.WINDOWS_7)) { if (renderer == Renderer.LLVMPIPE) return getNatives(javaVersion.getPlatform()).get("software-renderer-loader"); else diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/OSRestriction.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/OSRestriction.java index 98f91f0b6..2f103e326 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/game/OSRestriction.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/game/OSRestriction.java @@ -85,7 +85,7 @@ public final class OSRestriction { return false; if (version != null) - if (Lang.test(() -> !Pattern.compile(version).matcher(OperatingSystem.SYSTEM_VERSION).matches())) + if (Lang.test(() -> !Pattern.compile(version).matcher(OperatingSystem.SYSTEM_VERSION.getVersion()).matches())) return false; if (arch != null) diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OSVersion.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OSVersion.java new file mode 100644 index 000000000..5d2beac58 --- /dev/null +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/OSVersion.java @@ -0,0 +1,184 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 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 org.jackhuang.hmcl.util.versioning.VersionNumber; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/// @author Glavo +public sealed interface OSVersion { + OSVersion.Windows WINDOWS_2000 = new Windows(5, 0); + OSVersion.Windows WINDOWS_XP = new Windows(5, 1); + OSVersion.Windows WINDOWS_VISTA = new Windows(6, 0); + OSVersion.Windows WINDOWS_7 = new Windows(6, 1); + OSVersion.Windows WINDOWS_8 = new Windows(6, 2); + OSVersion.Windows WINDOWS_8_1 = new Windows(6, 3); + OSVersion.Windows WINDOWS_10 = new Windows(10, 0); + OSVersion.Windows WINDOWS_11 = new Windows(10, 0, 22000); + + static OSVersion of(OperatingSystem os, String version) { + if (Objects.requireNonNull(os) == OperatingSystem.WINDOWS) { + return OSVersion.Windows.parse(version); + } else { + return new Generic(os, VersionNumber.asVersion(version)); + } + } + + @NotNull OperatingSystem getOperatingSystem(); + + @NotNull String getVersion(); + + /// Returns `true` if the current version and `otherVersion` have the same [operating system][#getOperatingSystem()] + /// and the version is not lower than `otherVersion`; otherwise returns `false`. + /// + /// For example, if you want to check that the system is Windows and the version is at least Windows 7, you can do this: + /// + /// ```java + /// version.isAtLeast(OSVersion.WINDOWS_7) + /// ``` + boolean isAtLeast(@NotNull OSVersion otherVersion); + + record Windows(int major, int minor, int build, int revision, + String version) implements OSVersion, Comparable { + private static String toVersion(int major, int minor, int build, int revision) { + StringBuilder builder = new StringBuilder(); + builder.append(major).append('.').append(minor); + if (build > 0 || revision > 0) { + builder.append('.').append(build); + if (revision > 0) { + builder.append('.').append(revision); + } + } + return builder.toString(); + } + + public static OSVersion.Windows parse(String version) { + Matcher matcher = Pattern.compile("^(?\\d+)\\.(?\\d+)(\\.(?\\d+)(\\.(?\\d+))?)?") + .matcher(version); + if (matcher.find()) { + return new Windows( + Integer.parseInt(matcher.group("major")), + Integer.parseInt(matcher.group("minor")), + matcher.group("build") != null ? Integer.parseInt(matcher.group("build")) : 0, + matcher.group("revision") != null ? Integer.parseInt(matcher.group("revision")) : 0, + version + ); + } else { + return new OSVersion.Windows(0, 0, 0, 0, version); + } + } + + public Windows(int major, int minor) { + this(major, minor, 0); + } + + public Windows(int major, int minor, int build) { + this(major, minor, build, 0); + } + + public Windows(int major, int minor, int build, int revision) { + this(major, minor, build, revision, toVersion(major, minor, build, revision)); + } + + @Override + public @NotNull OperatingSystem getOperatingSystem() { + return OperatingSystem.WINDOWS; + } + + @Override + public @NotNull String getVersion() { + return version; + } + + @Override + public boolean isAtLeast(@NotNull OSVersion otherVersion) { + return this == otherVersion || otherVersion instanceof Windows other && this.compareTo(other) >= 0; + } + + @Override + public int compareTo(@NotNull OSVersion.Windows that) { + if (this.major != that.major) + return Integer.compare(this.major, that.major); + if (this.minor != that.minor) + return Integer.compare(this.minor, that.minor); + if (this.build != that.build) + return Integer.compare(this.build, that.build); + return Integer.compare(this.revision, that.revision); + } + + @Override + public int hashCode() { + return Objects.hash(major, minor, build, revision); + } + + @Override + public boolean equals(Object o) { + return this == o || o instanceof Windows that + && this.major == that.major + && this.minor == that.minor + && this.build == that.build + && this.revision == that.revision; + } + + @Override + public @NotNull String toString() { + return version; + } + } + + /// Generic implementation of [OSVersion]. + /// + /// Note: For Windows version numbers, please use [Windows]. + /// + /// @author Glavo + record Generic(@NotNull OperatingSystem os, @NotNull VersionNumber version) implements OSVersion { + public Generic { + Objects.requireNonNull(os); + if (os == OperatingSystem.WINDOWS) { + throw new IllegalArgumentException("Please use the " + Windows.class.getName()); + } + } + + @Override + public @NotNull OperatingSystem getOperatingSystem() { + return os; + } + + @Override + public @NotNull String getVersion() { + return version.toString(); + } + + @Override + public boolean isAtLeast(@NotNull OSVersion otherVersion) { + return this == otherVersion || otherVersion instanceof Generic that + && this.os == that.os + && this.version.compareTo(that.version) >= 0; + } + + @Override + public @NotNull String toString() { + return getVersion(); + } + } +} + 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 ecb098510..a01a28766 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 @@ -20,8 +20,6 @@ package org.jackhuang.hmcl.util.platform; import org.jackhuang.hmcl.util.KeyValuePairUtils; import org.jackhuang.hmcl.util.platform.windows.Kernel32; import org.jackhuang.hmcl.util.platform.windows.WinTypes; -import org.jackhuang.hmcl.util.platform.windows.WindowsVersion; -import org.jetbrains.annotations.Nullable; import java.io.BufferedReader; import java.io.IOException; @@ -108,20 +106,15 @@ public enum OperatingSystem { */ public static final int SYSTEM_BUILD_NUMBER; - /** - * The version number is non-null if and only if the operating system is {@linkplain #WINDOWS}. - */ - public static final @Nullable WindowsVersion WINDOWS_VERSION; - /** * The name of current operating system. */ public static final String SYSTEM_NAME; - /** - * The version of current operating system. - */ - public static final String SYSTEM_VERSION; + /// The version of current operating system. + /// + /// If [#CURRENT_OS] is [#WINDOWS], then [#SYSTEM_VERSION] must be an instance of [OSVersion.Windows]. + public static final OSVersion SYSTEM_VERSION; public static final String OS_RELEASE_NAME; public static final String OS_RELEASE_PRETTY_NAME; @@ -158,14 +151,14 @@ public enum OperatingSystem { if (CURRENT_OS == WINDOWS) { int codePage = -1; - WindowsVersion windowsVersion = null; + OSVersion.Windows windowsVersion = null; Kernel32 kernel32 = Kernel32.INSTANCE; // Get Windows version number if (kernel32 != null) { WinTypes.OSVERSIONINFOEXW osVersionInfo = new WinTypes.OSVERSIONINFOEXW(); if (kernel32.GetVersionExW(osVersionInfo)) { - windowsVersion = new WindowsVersion(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.dwBuildNumber); + windowsVersion = new OSVersion.Windows(osVersionInfo.dwMajorVersion, osVersionInfo.dwMinorVersion, osVersionInfo.dwBuildNumber); } else System.err.println("Failed to obtain OS version number (" + kernel32.GetLastError() + ")"); } @@ -177,7 +170,7 @@ public enum OperatingSystem { Matcher matcher = Pattern.compile("(?\\d+\\.\\d+\\.\\d+\\.\\d+?)]$") .matcher(reader.readLine().trim()); if (matcher.find()) - windowsVersion = new WindowsVersion(matcher.group("version")); + windowsVersion = OSVersion.Windows.parse(matcher.group("version")); } process.destroy(); } catch (Throwable ignored) { @@ -185,7 +178,7 @@ public enum OperatingSystem { } if (windowsVersion == null) - windowsVersion = new WindowsVersion(System.getProperty("os.version")); + windowsVersion = OSVersion.Windows.parse(System.getProperty("os.version")); // Get Code Page @@ -209,19 +202,17 @@ public enum OperatingSystem { String osName = System.getProperty("os.name"); // Java 17 or earlier recognizes Windows 11 as Windows 10 - if (osName.equals("Windows 10") && windowsVersion.compareTo(WindowsVersion.WINDOWS_11) >= 0) + if (osName.equals("Windows 10") && windowsVersion.isAtLeast(OSVersion.WINDOWS_11)) osName = "Windows 11"; SYSTEM_NAME = osName; - SYSTEM_VERSION = windowsVersion.toString(); - SYSTEM_BUILD_NUMBER = windowsVersion.getBuild(); - WINDOWS_VERSION = windowsVersion; + SYSTEM_VERSION = windowsVersion; + SYSTEM_BUILD_NUMBER = windowsVersion.build(); CODE_PAGE = codePage; } else { SYSTEM_NAME = System.getProperty("os.name"); - SYSTEM_VERSION = System.getProperty("os.version"); + SYSTEM_VERSION = OSVersion.of(CURRENT_OS, System.getProperty("os.version")); SYSTEM_BUILD_NUMBER = -1; - WINDOWS_VERSION = null; CODE_PAGE = -1; } @@ -278,7 +269,7 @@ public enum OperatingSystem { } public static boolean isWindows7OrLater() { - return WINDOWS_VERSION != null && WINDOWS_VERSION.compareTo(WindowsVersion.WINDOWS_7) >= 0; + return SYSTEM_VERSION.isAtLeast(OSVersion.WINDOWS_7); } public static Path getWorkingDirectory(String folder) { diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsGPUDetector.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsGPUDetector.java index b2dd9445b..b94c21873 100644 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsGPUDetector.java +++ b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsGPUDetector.java @@ -50,7 +50,7 @@ final class WindowsGPUDetector { private static List detectByCim() { try { - String getCimInstance = OperatingSystem.SYSTEM_VERSION.startsWith("6.1") + String getCimInstance = OperatingSystem.SYSTEM_VERSION.getVersion().startsWith("6.1") ? "Get-WmiObject" : "Get-CimInstance"; diff --git a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersion.java b/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersion.java deleted file mode 100644 index c980d6ce6..000000000 --- a/HMCLCore/src/main/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersion.java +++ /dev/null @@ -1,143 +0,0 @@ - -/* - * Hello Minecraft! Launcher - * Copyright (C) 2025 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.windows; - -import org.jetbrains.annotations.NotNull; - -import java.util.Objects; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * @author Glavo - * @see Operating System Version - */ -public final class WindowsVersion implements Comparable { - - public static final WindowsVersion UNKNOWN = new WindowsVersion(0, 0); - - public static final WindowsVersion WINDOWS_2000 = new WindowsVersion(5, 0); - public static final WindowsVersion WINDOWS_XP = new WindowsVersion(5, 1); - public static final WindowsVersion WINDOWS_VISTA = new WindowsVersion(6, 0); - public static final WindowsVersion WINDOWS_7 = new WindowsVersion(6, 1); - public static final WindowsVersion WINDOWS_8 = new WindowsVersion(6, 2); - public static final WindowsVersion WINDOWS_8_1 = new WindowsVersion(6, 3); - public static final WindowsVersion WINDOWS_10 = new WindowsVersion(10, 0); - public static final WindowsVersion WINDOWS_11 = new WindowsVersion(10, 0, 22000); - - private final int major; - private final int minor; - private final int build; - private final int revision; - private final String version; - - public WindowsVersion(int major, int minor) { - this(major, minor, 0); - } - - public WindowsVersion(int major, int minor, int build) { - this(major, minor, build, 0); - } - - public WindowsVersion(int major, int minor, int build, int revision) { - this.major = major; - this.minor = minor; - this.build = build; - this.revision = revision; - - StringBuilder builder = new StringBuilder(); - builder.append(major).append('.').append(minor); - if (build > 0 || revision > 0) { - builder.append('.').append(build); - if (revision > 0) { - builder.append('.').append(revision); - } - } - this.version = builder.toString(); - } - - public WindowsVersion(@NotNull String version) { - this.version = Objects.requireNonNull(version); - - Matcher matcher = Pattern.compile("^(?\\d+)\\.(?\\d+)(\\.(?\\d+)(\\.(?\\d+))?)?") - .matcher(version); - if (matcher.find()) { - this.major = Integer.parseInt(matcher.group("major")); - this.minor = Integer.parseInt(matcher.group("minor")); - this.build = matcher.group("build") != null ? Integer.parseInt(matcher.group("build")) : 0; - this.revision = matcher.group("revision") != null ? Integer.parseInt(matcher.group("revision")) : 0; - } else { - this.major = 0; - this.minor = 0; - this.build = 0; - this.revision = 0; - } - } - - public int getMajor() { - return major; - } - - public int getMinor() { - return minor; - } - - public int getBuild() { - return build; - } - - public int getRevision() { - return revision; - } - - @Override - public int compareTo(@NotNull WindowsVersion that) { - if (this.major != that.major) - return Integer.compare(this.major, that.major); - if (this.minor != that.minor) - return Integer.compare(this.minor, that.minor); - if (this.build != that.build) - return Integer.compare(this.build, that.build); - return Integer.compare(this.revision, that.revision); - } - - @Override - public int hashCode() { - return Objects.hash(major, minor, build, revision); - } - - @Override - public boolean equals(Object o) { - if (this == o) - return true; - if (!(o instanceof WindowsVersion)) - return false; - WindowsVersion that = (WindowsVersion) o; - - return this.major == that.major && - this.minor == that.minor && - this.build == that.build && - this.revision == that.revision; - } - - @Override - public String toString() { - return version; - } -} diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/OSVersionTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/OSVersionTest.java new file mode 100644 index 000000000..70b8dd84b --- /dev/null +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/OSVersionTest.java @@ -0,0 +1,57 @@ +/* + * Hello Minecraft! Launcher + * Copyright (C) 2025 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; + +import org.jackhuang.hmcl.util.platform.OSVersion; +import org.jackhuang.hmcl.util.platform.OperatingSystem; +import org.jackhuang.hmcl.util.versioning.VersionNumber; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +/// @author Glavo +public class OSVersionTest { + + @Test + public void testNotAcceptSystem() { + assertThrows(IllegalArgumentException.class, () -> new OSVersion.Generic(OperatingSystem.WINDOWS, VersionNumber.ZERO)); + } + + @Test + public void testParse() { + assertEquals(OSVersion.WINDOWS_7, OSVersion.Windows.parse("6.1")); + assertEquals(OSVersion.WINDOWS_10, OSVersion.Windows.parse("10.0")); + assertEquals(new OSVersion.Windows(10, 0, 26120), OSVersion.Windows.parse("10.0.26120")); + assertEquals(new OSVersion.Windows(10, 0, 26120), OSVersion.Windows.parse("10.0.26120-unknown")); + assertEquals(new OSVersion.Windows(10, 0, 26120, 3964), OSVersion.Windows.parse("10.0.26120.3964")); + assertEquals(new OSVersion.Windows(10, 0, 26120, 3964), OSVersion.Windows.parse("10.0.26120.3964-unknown")); + } + + @Test + public void testIsAtLeast() { + assertTrue(OSVersion.WINDOWS_10.isAtLeast(OSVersion.WINDOWS_7)); + assertTrue(OSVersion.WINDOWS_10.isAtLeast(OSVersion.WINDOWS_10)); + assertFalse(OSVersion.WINDOWS_10.isAtLeast(OSVersion.WINDOWS_11)); + assertFalse(OSVersion.WINDOWS_10.isAtLeast(OSVersion.of(OperatingSystem.LINUX, "4.0"))); + + assertTrue(OSVersion.of(OperatingSystem.LINUX, "4.0").isAtLeast(OSVersion.of(OperatingSystem.LINUX, "4.0"))); + assertTrue(OSVersion.of(OperatingSystem.LINUX, "4.0").isAtLeast(OSVersion.of(OperatingSystem.LINUX, "3.0"))); + assertFalse(OSVersion.of(OperatingSystem.LINUX, "4.0").isAtLeast(OSVersion.of(OperatingSystem.LINUX, "5.0"))); + assertFalse(OSVersion.of(OperatingSystem.LINUX, "4.0").isAtLeast(OSVersion.of(OperatingSystem.WINDOWS, "4.0"))); + } +} diff --git a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersionTest.java b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersionTest.java index de938ae42..ae2246dce 100644 --- a/HMCLCore/src/test/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersionTest.java +++ b/HMCLCore/src/test/java/org/jackhuang/hmcl/util/platform/windows/WindowsVersionTest.java @@ -17,24 +17,10 @@ */ package org.jackhuang.hmcl.util.platform.windows; -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - /** * @author Glavo */ public final class WindowsVersionTest { - @Test - public void testParse() { - assertEquals(WindowsVersion.WINDOWS_7, new WindowsVersion("6.1")); - assertEquals(WindowsVersion.WINDOWS_10, new WindowsVersion("10.0")); - assertEquals(new WindowsVersion(10, 0, 26120), new WindowsVersion("10.0.26120")); - assertEquals(new WindowsVersion(10, 0, 26120), new WindowsVersion("10.0.26120-unknown")); - assertEquals(new WindowsVersion(10, 0, 26120, 3964), new WindowsVersion("10.0.26120.3964")); - assertEquals(new WindowsVersion(10, 0, 26120, 3964), new WindowsVersion("10.0.26120.3964-unknown")); - assertEquals(WindowsVersion.UNKNOWN, new WindowsVersion("unknown")); - } }