From 6b26eceb57b8c1bdf245ec02ff979504701ede92 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Thu, 23 Feb 2017 23:36:14 -0500 Subject: [PATCH] move incompatible mod logic into mod registry --- .../Framework/ModRegistry.cs | 30 ++++++++++++++ .../Framework/Models/IncompatibleMod.cs | 40 +++++++++++-------- src/StardewModdingAPI/Program.cs | 35 +++++++--------- src/StardewModdingAPI/SemanticVersion.cs | 4 ++ 4 files changed, 73 insertions(+), 36 deletions(-) diff --git a/src/StardewModdingAPI/Framework/ModRegistry.cs b/src/StardewModdingAPI/Framework/ModRegistry.cs index 209f1928..233deb3c 100644 --- a/src/StardewModdingAPI/Framework/ModRegistry.cs +++ b/src/StardewModdingAPI/Framework/ModRegistry.cs @@ -3,6 +3,8 @@ using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Reflection; +using System.Text.RegularExpressions; +using StardewModdingAPI.Framework.Models; namespace StardewModdingAPI.Framework { @@ -18,10 +20,21 @@ namespace StardewModdingAPI.Framework /// The friendly mod names treated as deprecation warning sources (assembly full name => mod name). private readonly IDictionary ModNamesByAssembly = new Dictionary(); + /// The mod versions which should be disabled due to incompatibility. + private readonly IncompatibleMod[] IncompatibleMods; + /********* ** Public methods *********/ + /// Construct an instance. + /// The mod versions which should be disabled due to incompatibility. + public ModRegistry(IEnumerable incompatibleMods) + { + this.IncompatibleMods = incompatibleMods.ToArray(); + } + + /**** ** IModRegistry ****/ @@ -113,5 +126,22 @@ namespace StardewModdingAPI.Framework // no known assembly found return null; } + + /// Get a record indicating why a mod is incompatible (if applicable). + /// The mod manifest. + /// Returns the incompatibility record if applicable, else null. + internal IncompatibleMod GetIncompatibilityRecord(IManifest manifest) + { + string key = !string.IsNullOrWhiteSpace(manifest.UniqueID) ? manifest.UniqueID : manifest.EntryDll; + return ( + from mod in this.IncompatibleMods + where + mod.ID == key + && (mod.LowerSemanticVersion == null || !manifest.Version.IsOlderThan(mod.LowerSemanticVersion)) + && !manifest.Version.IsNewerThan(mod.UpperSemanticVersion) + && (string.IsNullOrWhiteSpace(mod.ForceCompatibleVersion) || !Regex.IsMatch(manifest.Version.ToString(), mod.ForceCompatibleVersion, RegexOptions.IgnoreCase)) + select mod + ).FirstOrDefault(); + } } } \ No newline at end of file diff --git a/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs b/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs index bcf5639c..29e18ddb 100644 --- a/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs +++ b/src/StardewModdingAPI/Framework/Models/IncompatibleMod.cs @@ -1,4 +1,5 @@ -using System.Text.RegularExpressions; +using System.Runtime.Serialization; +using Newtonsoft.Json; namespace StardewModdingAPI.Framework.Models { @@ -8,6 +9,9 @@ namespace StardewModdingAPI.Framework.Models /********* ** Accessors *********/ + /**** + ** From config + ****/ /// The unique mod ID. public string ID { get; set; } @@ -34,24 +38,28 @@ namespace StardewModdingAPI.Framework.Models public string ReasonPhrase { get; set; } + /**** + ** Injected + ****/ + /// The semantic version corresponding to . + [JsonIgnore] + public ISemanticVersion LowerSemanticVersion { get; set; } + + /// The semantic version corresponding to . + [JsonIgnore] + public ISemanticVersion UpperSemanticVersion { get; set; } + + /********* - ** Public methods + ** Private methods *********/ - /// Get whether the specified version is compatible according to this metadata. - /// The current version of the matching mod. - public bool IsCompatible(ISemanticVersion version) + /// The method called when the model finishes deserialising. + /// The deserialisation context. + [OnDeserialized] + private void OnDeserialized(StreamingContext context) { - ISemanticVersion lowerVersion = this.LowerVersion != null ? new SemanticVersion(this.LowerVersion) : null; - ISemanticVersion upperVersion = new SemanticVersion(this.UpperVersion); - - // ignore versions not in range - if (lowerVersion != null && version.IsOlderThan(lowerVersion)) - return true; - if (version.IsNewerThan(upperVersion)) - return true; - - // allow versions matching override - return !string.IsNullOrWhiteSpace(this.ForceCompatibleVersion) && Regex.IsMatch(version.ToString(), this.ForceCompatibleVersion, RegexOptions.IgnoreCase); + this.LowerSemanticVersion = this.LowerVersion != null ? new SemanticVersion(this.LowerVersion) : null; + this.UpperSemanticVersion = this.UpperVersion != null ? new SemanticVersion(this.UpperVersion) : null; } } } diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs index 9a6f07ef..3b6d1702 100644 --- a/src/StardewModdingAPI/Program.cs +++ b/src/StardewModdingAPI/Program.cs @@ -64,7 +64,7 @@ namespace StardewModdingAPI internal SGame GameInstance; /// Tracks the installed mods. - internal readonly ModRegistry ModRegistry = new ModRegistry(); + internal readonly ModRegistry ModRegistry; /// Manages deprecation warnings. internal readonly DeprecationManager DeprecationManager; @@ -92,6 +92,7 @@ namespace StardewModdingAPI // initialise this.Monitor = new Monitor("SMAPI", this.ConsoleManager, this.LogFile, this.ExitGameImmediately) { WriteToConsole = writeToConsole }; + this.ModRegistry = new ModRegistry(this.Settings.IncompatibleMods); this.DeprecationManager = new DeprecationManager(this.Monitor, this.ModRegistry); } @@ -388,28 +389,22 @@ namespace StardewModdingAPI continue; } - // validate known incompatible mods + // validate compatibility + IncompatibleMod compatibility = this.ModRegistry.GetIncompatibilityRecord(manifest); + if (compatibility != null) { - string modKey = !string.IsNullOrWhiteSpace(manifest.UniqueID) ? manifest.UniqueID : manifest.EntryDll; - IncompatibleMod compatibility = this.Settings.IncompatibleMods.FirstOrDefault(p => p.ID == modKey); - if(compatibility != null) - { - if (!compatibility.IsCompatible(manifest.Version)) - { - bool hasOfficialUrl = !string.IsNullOrWhiteSpace(compatibility.UpdateUrl); - bool hasUnofficialUrl = !string.IsNullOrWhiteSpace(compatibility.UnofficialUpdateUrl); + bool hasOfficialUrl = !string.IsNullOrWhiteSpace(compatibility.UpdateUrl); + bool hasUnofficialUrl = !string.IsNullOrWhiteSpace(compatibility.UnofficialUpdateUrl); - string reasonPhrase = compatibility.ReasonPhrase ?? "it isn't compatible with the latest version of the game"; - string warning = $"Skipped {compatibility.Name} because {reasonPhrase}. Please check for a version newer than {compatibility.UpperVersion} here:"; - if (hasOfficialUrl) - warning += !hasUnofficialUrl ? $" {compatibility.UpdateUrl}" : $"{Environment.NewLine}- official mod: {compatibility.UpdateUrl}"; - if (hasUnofficialUrl) - warning += $"{Environment.NewLine}- unofficial update: {compatibility.UnofficialUpdateUrl}"; + string reasonPhrase = compatibility.ReasonPhrase ?? "it isn't compatible with the latest version of the game"; + string warning = $"Skipped {compatibility.Name} because {reasonPhrase}. Please check for a version newer than {compatibility.UpperVersion} here:"; + if (hasOfficialUrl) + warning += !hasUnofficialUrl ? $" {compatibility.UpdateUrl}" : $"{Environment.NewLine}- official mod: {compatibility.UpdateUrl}"; + if (hasUnofficialUrl) + warning += $"{Environment.NewLine}- unofficial update: {compatibility.UnofficialUpdateUrl}"; - this.Monitor.Log(warning, LogLevel.Error); - continue; - } - } + this.Monitor.Log(warning, LogLevel.Error); + continue; } // validate SMAPI version diff --git a/src/StardewModdingAPI/SemanticVersion.cs b/src/StardewModdingAPI/SemanticVersion.cs index 3cb592e2..9610562f 100644 --- a/src/StardewModdingAPI/SemanticVersion.cs +++ b/src/StardewModdingAPI/SemanticVersion.cs @@ -63,9 +63,13 @@ namespace StardewModdingAPI /// 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) { + if(other == null) + throw new ArgumentNullException(nameof(other)); + const int same = 0; const int curNewer = 1; const int curOlder = -1;