创建 OSVersion 工具类 (#4480)

This commit is contained in:
Glavo 2025-09-14 20:13:43 +08:00 committed by GitHub
parent c2d5a07053
commit a10e9a04b1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 265 additions and 192 deletions

View File

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

View File

@ -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" +

View File

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

View File

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

View File

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

View File

@ -0,0 +1,184 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> 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 <https://www.gnu.org/licenses/>.
*/
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<Windows> {
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("^(?<major>\\d+)\\.(?<minor>\\d+)(\\.(?<build>\\d+)(\\.(?<revision>\\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();
}
}
}

View File

@ -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("(?<version>\\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) {

View File

@ -50,7 +50,7 @@ final class WindowsGPUDetector {
private static List<GraphicsCard> detectByCim() {
try {
String getCimInstance = OperatingSystem.SYSTEM_VERSION.startsWith("6.1")
String getCimInstance = OperatingSystem.SYSTEM_VERSION.getVersion().startsWith("6.1")
? "Get-WmiObject"
: "Get-CimInstance";

View File

@ -1,143 +0,0 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> 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 <https://www.gnu.org/licenses/>.
*/
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 <a href="https://learn.microsoft.com/windows/win32/sysinfo/operating-system-version">Operating System Version</a>
*/
public final class WindowsVersion implements Comparable<WindowsVersion> {
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("^(?<major>\\d+)\\.(?<minor>\\d+)(\\.(?<build>\\d+)(\\.(?<revision>\\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;
}
}

View File

@ -0,0 +1,57 @@
/*
* Hello Minecraft! Launcher
* Copyright (C) 2025 huangyuhui <huanghongxun2008@126.com> 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 <https://www.gnu.org/licenses/>.
*/
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")));
}
}

View File

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