diff --git a/Minecraft-Realms-Emulator/Helpers/MinecraftVersionParser.cs b/Minecraft-Realms-Emulator/Helpers/MinecraftVersionParser.cs new file mode 100644 index 0000000..73dfd8e --- /dev/null +++ b/Minecraft-Realms-Emulator/Helpers/MinecraftVersionParser.cs @@ -0,0 +1,76 @@ +using System.Text.RegularExpressions; + +namespace Minecraft_Realms_Emulator.Helpers +{ + public class MinecraftVersionParser + { + public class MinecraftVersion : IComparable + { + public int Major { get; private set; } + public int Minor { get; private set; } + public int Patch { get; private set; } + public string PreRelease { get; private set; } + public string Snapshot { get; private set; } + + private static readonly Regex VersionRegex = new(@"^(\d+)\.(\d+)(\.(\d+))?(-[a-zA-Z0-9\-]+)?$|^(\d{2})w(\d{2})([a-z])$"); + + public MinecraftVersion(string version) + { + var match = VersionRegex.Match(version); + if (!match.Success) + { + throw new ArgumentException("Invalid version format", nameof(version)); + } + + if (match.Groups[1].Success) + { + Major = int.Parse(match.Groups[1].Value); + Minor = int.Parse(match.Groups[2].Value); + Patch = match.Groups[4].Success ? int.Parse(match.Groups[4].Value) : 0; + PreRelease = match.Groups[5].Success ? match.Groups[5].Value.Substring(1) : null; + } + else if (match.Groups[6].Success) + { + Major = 0; + Minor = int.Parse(match.Groups[6].Value); + Patch = int.Parse(match.Groups[7].Value); + Snapshot = match.Groups[8].Value; + } + } + + public int CompareTo(MinecraftVersion other) + { + if (other == null) return 1; + + if (Snapshot != null && other.Snapshot != null) + { + int minorComparisonS = Minor.CompareTo(other.Minor); + if (minorComparisonS != 0) return minorComparisonS; + + int patchComparisonS = Patch.CompareTo(other.Patch); + if (patchComparisonS != 0) return patchComparisonS; + + return string.Compare(Snapshot, other.Snapshot, StringComparison.Ordinal); + } + + if (Snapshot != null) return -1; + if (other.Snapshot != null) return 1; + + int majorComparison = Major.CompareTo(other.Major); + if (majorComparison != 0) return majorComparison; + + int minorComparison = Minor.CompareTo(other.Minor); + if (minorComparison != 0) return minorComparison; + + int patchComparison = Patch.CompareTo(other.Patch); + if (patchComparison != 0) return patchComparison; + + if (PreRelease == null && other.PreRelease == null) return 0; + if (PreRelease == null) return 1; + if (other.PreRelease == null) return -1; + + return string.Compare(PreRelease, other.PreRelease, StringComparison.Ordinal); + } + } + } +} \ No newline at end of file diff --git a/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj b/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj index 523a8cb..22280c8 100644 --- a/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj +++ b/Minecraft-Realms-Emulator/Minecraft-Realms-Emulator.csproj @@ -21,7 +21,6 @@ - diff --git a/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs b/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs index 05751f9..77ed285 100644 --- a/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Modes/External/WorldsController.cs @@ -8,7 +8,6 @@ using Minecraft_Realms_Emulator.Helpers; using Minecraft_Realms_Emulator.Requests; using Minecraft_Realms_Emulator.Responses; using Newtonsoft.Json; -using Semver; namespace Minecraft_Realms_Emulator.Modes.External { @@ -66,7 +65,7 @@ namespace Minecraft_Realms_Emulator.Modes.External { Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot); - int versionsCompared = SemVersion.Parse(gameVersion, SemVersionStyles.OptionalPatch).ComparePrecedenceTo(SemVersion.Parse(activeSlot?.Version ?? gameVersion, SemVersionStyles.OptionalPatch)); + int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion)); string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE); WorldResponse response = new() @@ -103,7 +102,7 @@ namespace Minecraft_Realms_Emulator.Modes.External { Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot); - int versionsCompared = SemVersion.Parse(gameVersion, SemVersionStyles.OptionalPatch).ComparePrecedenceTo(SemVersion.Parse(activeSlot.Version, SemVersionStyles.OptionalPatch)); + int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot.Version)); string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE); WorldResponse response = new() @@ -156,7 +155,7 @@ namespace Minecraft_Realms_Emulator.Modes.External foreach (var slot in world.Slots) { - int versionsCompared = SemVersion.Parse(gameVersion, SemVersionStyles.OptionalPatch).ComparePrecedenceTo(SemVersion.Parse(slot.Version, SemVersionStyles.OptionalPatch)); + int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(slot.Version)); string compatibility = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE); slots.Add(new SlotResponse() diff --git a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs index b70be8d..361df93 100644 --- a/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs +++ b/Minecraft-Realms-Emulator/Modes/Realms/Controllers/WorldsController.cs @@ -9,7 +9,6 @@ using Minecraft_Realms_Emulator.Modes.Realms.Helpers; using Minecraft_Realms_Emulator.Requests; using Minecraft_Realms_Emulator.Responses; using Newtonsoft.Json; -using Semver; using System.Net; using System.Net.Sockets; @@ -69,7 +68,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers { Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot); - int versionsCompared = SemVersion.Parse(gameVersion, SemVersionStyles.OptionalPatch).ComparePrecedenceTo(SemVersion.Parse(activeSlot?.Version ?? gameVersion, SemVersionStyles.Any)); + int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion)); string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE); WorldResponse response = new() @@ -106,7 +105,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers { Slot activeSlot = world.Slots.Find(s => s.SlotId == world.ActiveSlot); - int versionsCompared = SemVersion.Parse(gameVersion, SemVersionStyles.OptionalPatch).ComparePrecedenceTo(SemVersion.Parse(activeSlot.Version, SemVersionStyles.OptionalPatch)); + int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion)); string isCompatible = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE); WorldResponse response = new() @@ -159,7 +158,7 @@ namespace Minecraft_Realms_Emulator.Modes.Realms.Controllers foreach (var slot in world.Slots) { - int versionsCompared = SemVersion.Parse(gameVersion, SemVersionStyles.OptionalPatch).ComparePrecedenceTo(SemVersion.Parse(slot.Version, SemVersionStyles.OptionalPatch)); + int versionsCompared = new MinecraftVersionParser.MinecraftVersion(gameVersion).CompareTo(new MinecraftVersionParser.MinecraftVersion(activeSlot?.Version ?? gameVersion)); string compatibility = versionsCompared == 0 ? nameof(CompatibilityEnum.COMPATIBLE) : versionsCompared < 0 ? nameof(CompatibilityEnum.NEEDS_DOWNGRADE) : nameof(CompatibilityEnum.NEEDS_UPGRADE); slots.Add(new SlotResponse()