diff --git a/Netch/Models/GitHubRelease/SuffixVersion.cs b/Netch/Models/GitHubRelease/SuffixVersion.cs new file mode 100644 index 00000000..e52d0aa4 --- /dev/null +++ b/Netch/Models/GitHubRelease/SuffixVersion.cs @@ -0,0 +1,132 @@ +using System; +using System.Text; + +namespace Netch.Models.GitHubRelease +{ + [Serializable] + public struct SuffixVersion : ICloneable, IComparable, IComparable, IEquatable + { + public int Major { get; } + public int Minor { get; } + public int Patch { get; } + public string PreRelease { get; } + public int Build { get; } + + public SuffixVersion(int major, int minor, int patch, string preRelease, int build) + { + Major = major; + Minor = minor; + Patch = patch; + PreRelease = preRelease; + Build = build; + } + + public SuffixVersion(Version version, string preRelease, int build) + { + Major = version.Major; + Minor = version.Minor; + Patch = version.Build; + PreRelease = preRelease; + Build = build; + } + + public static SuffixVersion Parse(string input) + { + var splitStr = input.Split('-'); + var dotNetVersion = Version.Parse(splitStr[0]); + var preRelease = new StringBuilder(); + var build = 0; + + if (splitStr.Length > 1) + foreach (var c in splitStr[1]) + { + if (int.TryParse(c.ToString(), out var n)) + { + build = build * 10 + n; + } + else + { + preRelease.Append(c); + } + } + + return new SuffixVersion(dotNetVersion, preRelease.ToString(), build); + } + + public static bool TryParse(string input, out SuffixVersion result) + { + try + { + result = Parse(input); + return true; + } + catch (Exception) + { + result = default; + return false; + } + } + + + public object Clone() => new SuffixVersion(Major, Major, Patch, PreRelease, Build); + + public int CompareTo(object obj) + { + if (obj is SuffixVersion version) + return CompareTo(version); + return -1; + } + + /// + /// + /// + /// + /// + /// greater than 0 newer + /// + public int CompareTo(SuffixVersion other) + { + var majorComparison = Major.CompareTo(other.Major); + if (majorComparison != 0) + return majorComparison; + var minorComparison = Minor.CompareTo(other.Minor); + if (minorComparison != 0) + return minorComparison; + var patchComparison = Patch.CompareTo(other.Patch); + if (patchComparison != 0) + return patchComparison; + if (PreRelease == string.Empty) + return other.PreRelease == string.Empty ? 0 : 1; + if (other.PreRelease == string.Empty) + return -1; + var suffixComparison = string.Compare(PreRelease, other.PreRelease, StringComparison.Ordinal); + if (suffixComparison != 0) + return suffixComparison; + return Build.CompareTo(other.Build); + } + + public bool Equals(SuffixVersion other) + { + return Major == other.Major && Minor == other.Minor && Patch == other.Patch && PreRelease == other.PreRelease && Build == other.Build; + } + + public override bool Equals(object obj) + { + return obj is SuffixVersion other && Equals(other); + } + + + public override int GetHashCode() + { + unchecked + { + var hashCode = Major; + hashCode = (hashCode * 397) ^ Minor; + hashCode = (hashCode * 397) ^ Patch; + hashCode = (hashCode * 397) ^ (PreRelease != null ? PreRelease.GetHashCode() : 0); + hashCode = (hashCode * 397) ^ Build; + return hashCode; + } + } + } +} \ No newline at end of file diff --git a/Netch/Models/GitHubRelease/VersionUtil.cs b/Netch/Models/GitHubRelease/VersionUtil.cs index fc0015d5..3a283553 100644 --- a/Netch/Models/GitHubRelease/VersionUtil.cs +++ b/Netch/Models/GitHubRelease/VersionUtil.cs @@ -1,5 +1,4 @@ -using System; -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; namespace Netch.Models.GitHubRelease @@ -20,9 +19,7 @@ namespace Netch.Models.GitHubRelease private static bool IsVersionString(string str) { - if (Global.Settings.CheckBetaUpdate) - str = str.Split('-')[0]; - return Version.TryParse(str, out _); + return SuffixVersion.TryParse(str, out _); } /// =0:versions are equal @@ -30,28 +27,9 @@ namespace Netch.Models.GitHubRelease /// <0:version2 is greater public static int CompareVersion(string v1, string v2) { - var version1 = ToVersion(v1); - var version2 = ToVersion(v2); - var res = version1.CompareTo(version2); - return res; - } - - private static Version ToVersion(string versionStr) - { - var v = versionStr.Split('-'); - var version = Version.Parse(v[0]); - if (v.Length == 1) - return version; - var beta = v[1]; - - var result = string.Empty; - foreach (var c in beta) - { - if (int.TryParse(c.ToString(), out var n)) - result += n; - } - - return new Version(version.Major, version.Minor, version.Build, int.Parse(result)); + var version1 = SuffixVersion.Parse(v1); + var version2 = SuffixVersion.Parse(v2); + return version1.CompareTo(version2); } } } \ No newline at end of file