From 0863f9b7e5f165f2b1db8750b20ed35bc0c3701a Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 Sep 2017 00:23:48 -0400 Subject: revamp mod compatibility fields to allow broader use of mod data records (#361) --- .../Framework/Models/ModCompatibility.cs | 55 ++++++++++++++++++++++ .../Framework/Models/ModDataRecord.cs | 30 ++++++------ 2 files changed, 69 insertions(+), 16 deletions(-) create mode 100644 src/StardewModdingAPI/Framework/Models/ModCompatibility.cs (limited to 'src/StardewModdingAPI/Framework/Models') diff --git a/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs b/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs new file mode 100644 index 00000000..54737e6c --- /dev/null +++ b/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs @@ -0,0 +1,55 @@ +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)); + } + } +} diff --git a/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs b/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs index 8126022d..de219076 100644 --- a/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs +++ b/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs @@ -1,3 +1,4 @@ +using System.Linq; using Newtonsoft.Json; using StardewModdingAPI.Framework.Serialisation; @@ -16,25 +17,22 @@ namespace StardewModdingAPI.Framework.Models /// The mod name. public string Name { get; set; } - /// The oldest incompatible mod version, or null for all past versions. - [JsonConverter(typeof(SFieldConverter))] - public ISemanticVersion LowerVersion { get; set; } - - /// The most recent incompatible mod version. - [JsonConverter(typeof(SFieldConverter))] - public ISemanticVersion UpperVersion { get; set; } - - /// A label to show to the user instead of , when the manifest version differs from the user-facing version. - public string UpperVersionLabel { get; set; } - /// The URLs the user can check for a newer version. public string[] UpdateUrls { get; set; } - /// The reason phrase to show in the warning, or null to use the default value. - /// "this version is incompatible with the latest version of the game" - public string ReasonPhrase { get; set; } + /// The compatibility of given mod versions (if any). + [JsonConverter(typeof(SFieldConverter))] + public ModCompatibility[] Compatibility { get; set; } = new ModCompatibility[0]; + - /// Indicates how SMAPI should treat the mod. - public ModStatus Status { get; set; } = ModStatus.AssumeBroken; + /********* + ** Public methods + *********/ + /// Get the compatibility record for a given version, if any. + /// The mod version to check. + public ModCompatibility GetCompatibility(ISemanticVersion version) + { + return this.Compatibility.FirstOrDefault(p => p.MatchesVersion(version)); + } } } -- cgit