using System; namespace StardewModdingAPI.Framework.Models { /// Specifies the compatibility of a given mod version range. internal class ModCompatibility { /********* ** Accessors *********/ /// The lowest version in the range, or null for all past versions. public ISemanticVersion LowerVersion { get; } /// The highest version in the range, or null for all future versions. public ISemanticVersion UpperVersion { get; } /// The mod compatibility. public ModStatus Status { get; } /// The reason phrase to show in log output, or null to use the default value. /// For example, "this version is incompatible with the latest version of the game". public string ReasonPhrase { get; } /********* ** Public methods *********/ /// Construct an instance. /// A version range, which consists of two version strings separated by a '~' character. Either side can be left blank for an unbounded range. /// The mod compatibility. /// The reason phrase to show in log output, or null to use the default value. public ModCompatibility(string versionRange, ModStatus status, string reasonPhrase) { // extract version strings string[] versions = versionRange.Split('~'); if (versions.Length != 2) throw new FormatException($"Could not parse '{versionRange}' as a version range. It must have two version strings separated by a '~' character (either side can be left blank for an unbounded range)."); // initialise this.LowerVersion = !string.IsNullOrWhiteSpace(versions[0]) ? new SemanticVersion(versions[0]) : null; this.UpperVersion = !string.IsNullOrWhiteSpace(versions[1]) ? new SemanticVersion(versions[1]) : null; this.Status = status; this.ReasonPhrase = reasonPhrase; } /// Get whether a given version is contained within this compatibility range. /// The version to check. public bool MatchesVersion(ISemanticVersion version) { return (this.LowerVersion == null || !version.IsOlderThan(this.LowerVersion)) && (this.UpperVersion == null || !version.IsNewerThan(this.UpperVersion)); } } }