using System; using Newtonsoft.Json; namespace StardewModdingAPI { /// A semantic version with an optional release tag. public class SemanticVersion : ISemanticVersion { /********* ** Properties *********/ /// The underlying semantic version implementation. private readonly Toolkit.ISemanticVersion Version; /********* ** Accessors *********/ /// The major version incremented for major API changes. public int MajorVersion => this.Version.Major; /// The minor version incremented for backwards-compatible changes. public int MinorVersion => this.Version.Minor; /// The patch version for backwards-compatible bug fixes. public int PatchVersion => this.Version.Patch; /// An optional build tag. public string Build => this.Version.Tag; /********* ** Public methods *********/ /// Construct an instance. /// The major version incremented for major API changes. /// The minor version incremented for backwards-compatible changes. /// The patch version for backwards-compatible bug fixes. /// An optional build tag. [JsonConstructor] public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, string build = null) : this(new Toolkit.SemanticVersion(majorVersion, minorVersion, patchVersion, build)) { } /// Construct an instance. /// The semantic version string. /// The is null. /// The is not a valid semantic version. public SemanticVersion(string version) : this(new Toolkit.SemanticVersion(version)) { } /// Construct an instance. /// The assembly version. /// The is null. public SemanticVersion(Version version) : this(new Toolkit.SemanticVersion(version)) { } /// Whether this is a pre-release version. public bool IsPrerelease() { return !string.IsNullOrWhiteSpace(this.Build); } /// 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. /// The value is null. /// The implementation is defined by Semantic Version 2.0 (http://semver.org/). public int CompareTo(ISemanticVersion other) { Toolkit.ISemanticVersion toolkitOther = new Toolkit.SemanticVersion(other.MajorVersion, other.MinorVersion, other.PatchVersion, other.Build); return this.Version.CompareTo(toolkitOther); } /// Get whether this version is older than the specified version. /// The version to compare with this instance. public bool IsOlderThan(ISemanticVersion other) { return this.CompareTo(other) < 0; } /// Get whether this version is older than the specified version. /// The version to compare with this instance. /// The specified version is not a valid semantic version. public bool IsOlderThan(string other) { return this.IsOlderThan(new SemanticVersion(other)); } /// Get whether this version is newer than the specified version. /// The version to compare with this instance. public bool IsNewerThan(ISemanticVersion other) { return this.CompareTo(other) > 0; } /// Get whether this version is newer than the specified version. /// The version to compare with this instance. /// The specified version is not a valid semantic version. public bool IsNewerThan(string other) { return this.IsNewerThan(new SemanticVersion(other)); } /// Get whether this version is between two specified versions (inclusively). /// The minimum version. /// The maximum version. public bool IsBetween(ISemanticVersion min, ISemanticVersion max) { return this.CompareTo(min) >= 0 && this.CompareTo(max) <= 0; } /// Get whether this version is between two specified versions (inclusively). /// The minimum version. /// The maximum version. /// One of the specified versions is not a valid semantic version. public bool IsBetween(string min, string max) { return this.IsBetween(new SemanticVersion(min), new SemanticVersion(max)); } /// Indicates whether the current object is equal to another object of the same type. /// true if the current object is equal to the parameter; otherwise, false. /// An object to compare with this object. public bool Equals(ISemanticVersion other) { return other != null && this.CompareTo(other) == 0; } /// Get a string representation of the version. public override string ToString() { return this.Version.ToString(); } /// Parse a version string without throwing an exception if it fails. /// The version string. /// The parsed representation. /// Returns whether parsing the version succeeded. internal static bool TryParse(string version, out ISemanticVersion parsed) { if (Toolkit.SemanticVersion.TryParse(version, out Toolkit.ISemanticVersion versionImpl)) { parsed = new SemanticVersion(versionImpl); return true; } parsed = null; return false; } /********* ** Private methods *********/ /// Construct an instance. /// The underlying semantic version implementation. private SemanticVersion(Toolkit.ISemanticVersion version) { this.Version = version; } } }