feat: MinecraftVersionParser

This commit is contained in:
CyberL1 2024-06-29 14:04:23 +02:00
parent 11444c75b1
commit 4b7eec2057
4 changed files with 82 additions and 9 deletions

View File

@ -0,0 +1,76 @@
using System.Text.RegularExpressions;
namespace Minecraft_Realms_Emulator.Helpers
{
public class MinecraftVersionParser
{
public class MinecraftVersion : IComparable<MinecraftVersion>
{
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);
}
}
}
}

View File

@ -21,7 +21,6 @@
</PackageReference>
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="8.0.4" />
<PackageReference Include="Semver" Version="2.3.0" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2" />
</ItemGroup>

View File

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

View File

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