Delete VersionNumber.VERSION_COMPARATOR (#2671)

This commit is contained in:
Glavo 2024-01-23 14:34:29 +08:00 committed by GitHub
parent 2cad6f33f5
commit 23f58e63aa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 24 additions and 18 deletions

View File

@ -511,7 +511,7 @@ public final class LauncherHelper {
case MODDED_JAVA_16:
// Minecraft<=1.17.1+Forge[37.0.0,37.0.60) not compatible with Java 17
String forgePatchVersion = analyzer.getVersion(LibraryAnalyzer.LibraryType.FORGE).orElse(null);
if (forgePatchVersion != null && VersionNumber.VERSION_COMPARATOR.compare(forgePatchVersion, "37.0.60") < 0)
if (forgePatchVersion != null && VersionNumber.compare(forgePatchVersion, "37.0.60") < 0)
suggestions.add(i18n("launch.advice.forge37_0_60"));
else
suggestions.add(i18n("launch.advice.modded_java", 16, gameVersion));

View File

@ -309,7 +309,7 @@ public class DownloadPage extends Control implements DecoratorPage {
}
for (String gameVersion : control.versions.keys().stream()
.sorted(VersionNumber.VERSION_COMPARATOR.reversed())
.sorted(Collections.reverseOrder(VersionNumber::compare))
.collect(Collectors.toList())) {
ComponentList sublist = new ComponentList(() ->
control.versions.get(gameVersion).stream()

View File

@ -45,7 +45,7 @@ public final class NativePatcher {
public static Version patchNative(Version version, String gameVersion, JavaVersion javaVersion, VersionSetting settings) {
if (settings.getNativesDirType() == NativesDirectoryType.CUSTOM) {
if (gameVersion != null && VersionNumber.VERSION_COMPARATOR.compare(gameVersion, "1.19") < 0)
if (gameVersion != null && VersionNumber.compare(gameVersion, "1.19") < 0)
return version;
ArrayList<Library> newLibraries = new ArrayList<>();
@ -66,7 +66,7 @@ public final class NativePatcher {
final boolean useNativeOpenAL = settings.isUseNativeOpenAL();
if (OperatingSystem.CURRENT_OS.isLinuxOrBSD() && (useNativeGLFW || useNativeOpenAL)
&& VersionNumber.VERSION_COMPARATOR.compare(gameVersion, "1.19") >= 0) {
&& VersionNumber.compare(gameVersion, "1.19") >= 0) {
version = version.setLibraries(version.getLibraries().stream()
.filter(library -> {

View File

@ -213,7 +213,7 @@ public class MaintainTask extends Task<Version> {
Optional<String> bslVersion = libraryAnalyzer.getVersion(BOOTSTRAP_LAUNCHER);
if (bslVersion.isPresent()) {
if (VersionNumber.VERSION_COMPARATOR.compare(bslVersion.get(), "0.1.17") < 0) {
if (VersionNumber.compare(bslVersion.get(), "0.1.17") < 0) {
// The default ignoreList will be applied to all components of libraries in classpath,
// so if game directory located in some directory like /Users/asm, all libraries will be ignored,
// which is not expected. We fix this here.

View File

@ -100,7 +100,7 @@ public final class ForgeInstallTask extends Task<Version> {
@Override
public void execute() throws IOException, VersionMismatchException, UnsupportedInstallationException {
String originalMainClass = version.resolve(dependencyManager.getGameRepository()).getMainClass();
if (VersionNumber.VERSION_COMPARATOR.compare("1.13", remote.getGameVersion()) <= 0) {
if (VersionNumber.compare("1.13", remote.getGameVersion()) <= 0) {
// Forge 1.13 is not compatible with fabric.
if (!LibraryAnalyzer.VANILLA_MAIN.equals(originalMainClass)
&& !LibraryAnalyzer.MOD_LAUNCHER_MAIN.equals(originalMainClass)

View File

@ -65,7 +65,7 @@ public final class GameVerificationFixTask extends Task<Void> {
File jar = dependencyManager.getGameRepository().getVersionJar(version);
LibraryAnalyzer analyzer = LibraryAnalyzer.analyze(version);
if (jar.exists() && VersionNumber.VERSION_COMPARATOR.compare(gameVersion, "1.6") < 0 && analyzer.has(LibraryAnalyzer.LibraryType.FORGE)) {
if (jar.exists() && VersionNumber.compare(gameVersion, "1.6") < 0 && analyzer.has(LibraryAnalyzer.LibraryType.FORGE)) {
try (FileSystem fs = CompressingUtils.createWritableZipFileSystem(jar.toPath(), StandardCharsets.UTF_8)) {
Files.deleteIfExists(fs.getPath("META-INF/MOJANG_C.DSA"));
Files.deleteIfExists(fs.getPath("META-INF/MOJANG_C.SF"));

View File

@ -67,7 +67,7 @@ public class JavaDownloadTask extends Task<Void> {
if (!osDownloads.containsKey(javaVersion.getComponent())) throw new UnsupportedPlatformException();
List<JavaDownloads.JavaDownload> candidates = osDownloads.get(javaVersion.getComponent());
for (JavaDownloads.JavaDownload download : candidates) {
if (VersionNumber.VERSION_COMPARATOR.compare(download.getVersion().getName(), Integer.toString(javaVersion.getMajorVersion())) >= 0) {
if (VersionNumber.compare(download.getVersion().getName(), Integer.toString(javaVersion.getMajorVersion())) >= 0) {
this.download = download;
return new GetTask(NetworkUtils.toURL(downloadProvider.injectURL(download.getManifest().getUrl())));
}

View File

@ -230,7 +230,7 @@ public class DefaultLauncher extends Launcher {
Path tempNativeFolder = null;
if ((OperatingSystem.CURRENT_OS == OperatingSystem.LINUX || OperatingSystem.CURRENT_OS == OperatingSystem.OSX)
&& !StringUtils.isASCII(nativeFolderPath)
&& gameVersion.isPresent() && VersionNumber.VERSION_COMPARATOR.compare(gameVersion.get(), "1.19") < 0) {
&& gameVersion.isPresent() && VersionNumber.compare(gameVersion.get(), "1.19") < 0) {
tempNativeFolder = Paths.get("/", "tmp", "hmcl-natives-" + UUID.randomUUID());
nativeFolderPath = tempNativeFolder + File.pathSeparator + nativeFolderPath;
}
@ -259,7 +259,7 @@ public class DefaultLauncher extends Launcher {
if (StringUtils.isNotBlank(options.getServerIp())) {
String[] args = options.getServerIp().split(":");
if (VersionNumber.VERSION_COMPARATOR.compare(gameVersion.orElse("0.0"), "1.20") < 0) {
if (VersionNumber.compare(gameVersion.orElse("0.0"), "1.20") < 0) {
res.add("--server");
res.add(args[0]);
res.add("--port");
@ -357,7 +357,7 @@ public class DefaultLauncher extends Launcher {
}
private boolean isUsingLog4j() {
return VersionNumber.VERSION_COMPARATOR.compare(repository.getGameVersion(version).orElse("1.7"), "1.7") >= 0;
return VersionNumber.compare(repository.getGameVersion(version).orElse("1.7"), "1.7") >= 0;
}
public File getLog4jConfigurationFile() {
@ -367,7 +367,7 @@ public class DefaultLauncher extends Launcher {
public void extractLog4jConfigurationFile() throws IOException {
File targetFile = getLog4jConfigurationFile();
InputStream source;
if (VersionNumber.VERSION_COMPARATOR.compare(repository.getGameVersion(version).orElse("0.0"), "1.12") < 0) {
if (VersionNumber.compare(repository.getGameVersion(version).orElse("0.0"), "1.12") < 0) {
source = DefaultLauncher.class.getResourceAsStream("/assets/game/log4j2-1.7.xml");
} else {
source = DefaultLauncher.class.getResourceAsStream("/assets/game/log4j2-1.12.xml");

View File

@ -172,7 +172,7 @@ public enum Architecture {
case "loongarch32":
return LOONGARCH32;
case "loongarch64": {
if (VersionNumber.VERSION_COMPARATOR.compare(System.getProperty("os.version"), "5.19") < 0)
if (VersionNumber.compare(System.getProperty("os.version"), "5.19") < 0)
return LOONGARCH64_OW;
return LOONGARCH64;
}

View File

@ -18,7 +18,11 @@
package org.jackhuang.hmcl.util.versioning;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Deque;
import java.util.Iterator;
import java.util.Objects;
/**
* Copied from org.apache.maven.artifact.versioning.ComparableVersion
@ -29,13 +33,15 @@ import java.util.*;
*/
public final class VersionNumber implements Comparable<VersionNumber> {
public static final Comparator<String> VERSION_COMPARATOR = Comparator.comparing(VersionNumber::asVersion);
public static VersionNumber asVersion(String version) {
Objects.requireNonNull(version);
return new VersionNumber(version);
}
public static int compare(String version1, String version2) {
return asVersion(version1).compareTo(asVersion(version2));
}
public static String normalize(String str) {
return new VersionNumber(str).getCanonical();
}
@ -346,7 +352,7 @@ public final class VersionNumber implements Comparable<VersionNumber> {
private static final int MAX_LONGITEM_LENGTH = 18;
private final String value;
public final ListItem items;
private final ListItem items;
private final String canonical;
private VersionNumber(String version) {

View File

@ -94,7 +94,7 @@ public class VersionNumberTest {
@Test
public void testSorting() {
final Comparator<String> comparator = VersionNumber.VERSION_COMPARATOR.thenComparing(String::compareTo);
final Comparator<String> comparator = ((Comparator<String>) VersionNumber::compare).thenComparing(String::compareTo);
final List<String> input = Collections.unmodifiableList(Arrays.asList(
"0",
"0.10.0",