mirror of
https://github.com/chylex/Minecraft-Phantom-Panel.git
synced 2024-10-30 05:42:47 +01:00
41 lines
1.2 KiB
C#
41 lines
1.2 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using MessagePack;
|
|
|
|
namespace Phantom.Common.Data.Java;
|
|
|
|
[MessagePackObject]
|
|
public sealed record JavaRuntime(
|
|
[property: Key(0)] string MainVersion,
|
|
[property: Key(1)] string FullVersion,
|
|
[property: Key(2)] string DisplayName
|
|
) : IComparable<JavaRuntime> {
|
|
public int CompareTo(JavaRuntime? other) {
|
|
if (ReferenceEquals(this, other)) {
|
|
return 0;
|
|
}
|
|
|
|
if (ReferenceEquals(null, other)) {
|
|
return 1;
|
|
}
|
|
|
|
if (TryParseFullVersion(FullVersion, out var fullVersion) && TryParseFullVersion(other.FullVersion, out var otherFullVersion)) {
|
|
var versionComparison = -fullVersion.CompareTo(otherFullVersion);
|
|
if (versionComparison != 0) {
|
|
return versionComparison;
|
|
}
|
|
}
|
|
|
|
return string.Compare(DisplayName, other.DisplayName, StringComparison.OrdinalIgnoreCase);
|
|
}
|
|
|
|
private static bool TryParseFullVersion(string versionString, [NotNullWhen(true)] out Version? version) {
|
|
int dashIndex = versionString.IndexOf('-');
|
|
var versionSpan = dashIndex != -1 ? versionString.AsSpan(0, dashIndex) : versionString;
|
|
if (versionSpan.Contains('_')) {
|
|
versionSpan = versionSpan.ToString().Replace('_', '.');
|
|
}
|
|
|
|
return Version.TryParse(versionSpan, out version);
|
|
}
|
|
}
|