From 59c900a9ac936491386e70d9c841e572f2a49ecd Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 3 Nov 2016 00:43:41 -0400 Subject: add update check (#154) --- src/StardewModdingAPI/Version.cs | 51 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) (limited to 'src/StardewModdingAPI/Version.cs') diff --git a/src/StardewModdingAPI/Version.cs b/src/StardewModdingAPI/Version.cs index db5a21d4..b1903d7b 100644 --- a/src/StardewModdingAPI/Version.cs +++ b/src/StardewModdingAPI/Version.cs @@ -1,11 +1,20 @@ using System; +using System.Text.RegularExpressions; using Newtonsoft.Json; namespace StardewModdingAPI { /// A semantic version with an optional release tag. - public struct Version + public struct Version : IComparable { + /********* + ** Properties + *********/ + /// A regular expression matching a semantic version string. + /// Derived from https://github.com/maxhauser/semver. + private static readonly Regex Regex = new Regex(@"^(?\d+)(\.(?\d+))?(\.(?\d+))?(?.*)$", RegexOptions.CultureInvariant | RegexOptions.ExplicitCapture); + + /********* ** Accessors *********/ @@ -43,6 +52,44 @@ namespace StardewModdingAPI this.Build = build; } + /// Construct an instance. + /// The semantic version string. + internal Version(string version) + { + var match = Version.Regex.Match(version); + if (!match.Success) + throw new FormatException($"The input '{version}' is not a semantic version."); + + this.MajorVersion = int.Parse(match.Groups["major"].Value); + this.MinorVersion = match.Groups["minor"].Success ? int.Parse(match.Groups["minor"].Value) : 0; + this.PatchVersion = match.Groups["patch"].Success ? int.Parse(match.Groups["patch"].Value) : 0; + this.Build = (match.Groups["build"].Success ? match.Groups["build"].Value : "").Trim(' ', '-', '.'); + } + + /// Get an integer indicating whether this version precedes (less than 0), supercedes (more than 0), or is equivalent to (0) the specified version. + /// The version to compare with this instance. + public int CompareTo(Version other) + { + // compare version numbers + if (this.MajorVersion != other.MajorVersion) + return this.MajorVersion - other.MajorVersion; + if (this.MinorVersion != other.MinorVersion) + return this.MinorVersion - other.MinorVersion; + if (this.PatchVersion != other.PatchVersion) + return this.PatchVersion - other.PatchVersion; + + // stable version (without tag) supercedes prerelease (with tag) + bool curHasTag = !string.IsNullOrWhiteSpace(this.Build); + bool otherHasTag = !string.IsNullOrWhiteSpace(other.Build); + if (!curHasTag && otherHasTag) + return 1; + if (curHasTag && !otherHasTag) + return -1; + + // else compare by string + return string.Compare(this.ToString(), other.ToString(), StringComparison.InvariantCultureIgnoreCase); + } + /// Get a string representation of the version. public override string ToString() { @@ -66,7 +113,7 @@ namespace StardewModdingAPI /// The tag to normalise. private string GetNormalisedTag(string tag) { - tag = tag?.Trim().Trim('-'); + tag = tag?.Trim().Trim('-', '.'); if (string.IsNullOrWhiteSpace(tag) || tag == "0") return null; return tag; -- cgit