using System; using System.Diagnostics.CodeAnalysis; using Newtonsoft.Json; namespace StardewModdingAPI { /// A semantic version with an optional release tag. public class SemanticVersion : ISemanticVersion { /********* ** Fields *********/ /// The underlying semantic version implementation. private readonly ISemanticVersion Version; /********* ** Accessors *********/ /// public int MajorVersion => this.Version.MajorVersion; /// public int MinorVersion => this.Version.MinorVersion; /// public int PatchVersion => this.Version.PatchVersion; /// public string? PrereleaseTag => this.Version.PrereleaseTag; /// public string? BuildMetadata => this.Version.BuildMetadata; /********* ** 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 prerelease tag. /// Optional build metadata. This is ignored when determining version precedence. public SemanticVersion(int majorVersion, int minorVersion, int patchVersion, string? prereleaseTag = null, string? buildMetadata = null) : this(majorVersion, minorVersion, patchVersion, 0, prereleaseTag, buildMetadata) { } /// 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 prerelease tag. /// The platform-specific version (if applicable). /// Optional build metadata. This is ignored when determining version precedence. [JsonConstructor] internal SemanticVersion(int majorVersion, int minorVersion, int patchVersion, int platformRelease, string? prereleaseTag = null, string? buildMetadata = null) : this(new Toolkit.SemanticVersion(majorVersion, minorVersion, patchVersion, platformRelease, prereleaseTag, buildMetadata)) { } /// Construct an instance. /// The semantic version string. /// The is null. /// The is not a valid semantic version. public SemanticVersion(string version) : this(version, allowNonStandard: false) { } /// Construct an instance. /// The semantic version string. /// Whether to recognize non-standard semver extensions. /// The is null. /// The is not a valid semantic version. internal SemanticVersion(string version, bool allowNonStandard) : this(new Toolkit.SemanticVersion(version, allowNonStandard)) { } /// Construct an instance. /// The assembly version. /// The is null. public SemanticVersion(Version version) : this(new Toolkit.SemanticVersion(version)) { } /// Construct an instance. /// The underlying semantic version implementation. internal SemanticVersion(ISemanticVersion version) { this.Version = version; } /// public bool IsPrerelease() { return this.Version.IsPrerelease(); } /// /// The implementation is defined by Semantic Version 2.0 (https://semver.org/). public int CompareTo(ISemanticVersion? other) { return this.Version.CompareTo(other); } /// public bool IsOlderThan(ISemanticVersion? other) { return this.Version.IsOlderThan(other); } /// public bool IsOlderThan(string? other) { return this.Version.IsOlderThan(other); } /// public bool IsNewerThan(ISemanticVersion? other) { return this.Version.IsNewerThan(other); } /// public bool IsNewerThan(string? other) { return this.Version.IsNewerThan(other); } /// public bool IsBetween(ISemanticVersion? min, ISemanticVersion? max) { return this.Version.IsBetween(min, max); } /// public bool IsBetween(string? min, string? max) { return this.Version.IsBetween(min, max); } /// public bool Equals(ISemanticVersion? other) { return other != null && this.CompareTo(other) == 0; } /// public override string ToString() { return this.Version.ToString(); } /// public bool IsNonStandard() { return this.Version.IsNonStandard(); } /// Parse a version string without throwing an exception if it fails. /// The version string. /// The parsed representation. /// Returns whether parsing the version succeeded. public static bool TryParse(string version, [NotNullWhen(true)] out ISemanticVersion? parsed) { if (Toolkit.SemanticVersion.TryParse(version, out ISemanticVersion? versionImpl)) { parsed = new SemanticVersion(versionImpl); return true; } parsed = null; return false; } } }