From 9495cc0f493a11960aa23551e164018681d08f79 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sat, 23 Sep 2017 22:07:29 -0400 Subject: rename mod compatibility records for broader use (#361) --- src/StardewModdingAPI/Framework/IModMetadata.cs | 6 +-- .../Framework/ModLoading/ModMetadata.cs | 12 ++--- .../Framework/ModLoading/ModResolver.cs | 30 ++++++------ .../Framework/Models/ModCompatibility.cs | 40 --------------- .../Framework/Models/ModCompatibilityID.cs | 57 ---------------------- .../Framework/Models/ModDataID.cs | 57 ++++++++++++++++++++++ .../Framework/Models/ModDataRecord.cs | 40 +++++++++++++++ src/StardewModdingAPI/Framework/Models/SConfig.cs | 4 +- .../Framework/Serialisation/SFieldConverter.cs | 10 ++-- 9 files changed, 128 insertions(+), 128 deletions(-) delete mode 100644 src/StardewModdingAPI/Framework/Models/ModCompatibility.cs delete mode 100644 src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs create mode 100644 src/StardewModdingAPI/Framework/Models/ModDataID.cs create mode 100644 src/StardewModdingAPI/Framework/Models/ModDataRecord.cs (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/IModMetadata.cs b/src/StardewModdingAPI/Framework/IModMetadata.cs index 56ac25f1..c21734a7 100644 --- a/src/StardewModdingAPI/Framework/IModMetadata.cs +++ b/src/StardewModdingAPI/Framework/IModMetadata.cs @@ -1,4 +1,4 @@ -using StardewModdingAPI.Framework.Models; +using StardewModdingAPI.Framework.Models; using StardewModdingAPI.Framework.ModLoading; namespace StardewModdingAPI.Framework @@ -18,8 +18,8 @@ namespace StardewModdingAPI.Framework /// The mod manifest. IManifest Manifest { get; } - /// Optional metadata about a mod version that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code. - ModCompatibility Compatibility { get; } + /// >Metadata about the mod from SMAPI's internal data (if any). + ModDataRecord DataRecord { get; } /// The metadata resolution status. ModMetadataStatus Status { get; } diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs b/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs index ab590e10..5055da75 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/ModMetadata.cs @@ -1,4 +1,4 @@ -using StardewModdingAPI.Framework.Models; +using StardewModdingAPI.Framework.Models; namespace StardewModdingAPI.Framework.ModLoading { @@ -17,8 +17,8 @@ namespace StardewModdingAPI.Framework.ModLoading /// The mod manifest. public IManifest Manifest { get; } - /// Optional metadata about a mod version that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code. - public ModCompatibility Compatibility { get; } + /// Metadata about the mod from SMAPI's internal data (if any). + public ModDataRecord DataRecord { get; } /// The metadata resolution status. public ModMetadataStatus Status { get; private set; } @@ -37,13 +37,13 @@ namespace StardewModdingAPI.Framework.ModLoading /// The mod's display name. /// The mod's full directory path. /// The mod manifest. - /// Optional metadata about a mod version that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code. - public ModMetadata(string displayName, string directoryPath, IManifest manifest, ModCompatibility compatibility) + /// Metadata about the mod from SMAPI's internal data (if any). + public ModMetadata(string displayName, string directoryPath, IManifest manifest, ModDataRecord dataRecord) { this.DisplayName = displayName; this.DirectoryPath = directoryPath; this.Manifest = manifest; - this.Compatibility = compatibility; + this.DataRecord = dataRecord; } /// Set the mod status. diff --git a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs index 02fd85ea..2da10348 100644 --- a/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs +++ b/src/StardewModdingAPI/Framework/ModLoading/ModResolver.cs @@ -17,11 +17,11 @@ namespace StardewModdingAPI.Framework.ModLoading /// Get manifest metadata for each folder in the given root path. /// The root path to search for mods. /// The JSON helper with which to read manifests. - /// Metadata about mods that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code. + /// Metadata about mods from SMAPI's internal data. /// Returns the manifests by relative folder. - public IEnumerable ReadManifests(string rootPath, JsonHelper jsonHelper, IEnumerable compatibilityRecords) + public IEnumerable ReadManifests(string rootPath, JsonHelper jsonHelper, IEnumerable dataRecords) { - compatibilityRecords = compatibilityRecords.ToArray(); + dataRecords = dataRecords.ToArray(); foreach (DirectoryInfo modDir in this.GetModFolders(rootPath)) { @@ -54,15 +54,15 @@ namespace StardewModdingAPI.Framework.ModLoading } // validate metadata - ModCompatibility compatibility = null; + ModDataRecord dataRecord = null; if (manifest != null) { // get unique key for lookups string key = !string.IsNullOrWhiteSpace(manifest.UniqueID) ? manifest.UniqueID : manifest.EntryDll; - // get compatibility record - compatibility = ( - from mod in compatibilityRecords + // get data record + dataRecord = ( + from mod in dataRecords where mod.ID.Any(p => p.Matches(key, manifest)) && (mod.LowerVersion == null || !manifest.Version.IsOlderThan(mod.LowerVersion)) @@ -79,7 +79,7 @@ namespace StardewModdingAPI.Framework.ModLoading ? ModMetadataStatus.Found : ModMetadataStatus.Failed; - yield return new ModMetadata(displayName, modDir.FullName, manifest, compatibility).SetStatus(status, error); + yield return new ModMetadata(displayName, modDir.FullName, manifest, dataRecord).SetStatus(status, error); } } @@ -99,22 +99,22 @@ namespace StardewModdingAPI.Framework.ModLoading // validate compatibility { - ModCompatibility compatibility = mod.Compatibility; - switch (compatibility?.Status) + ModDataRecord dataRecord = mod.DataRecord; + switch (dataRecord?.Status) { case ModStatus.Obsolete: - mod.SetStatus(ModMetadataStatus.Failed, $"it's obsolete: {compatibility.ReasonPhrase}"); + mod.SetStatus(ModMetadataStatus.Failed, $"it's obsolete: {dataRecord.ReasonPhrase}"); continue; case ModStatus.AssumeBroken: { - string reasonPhrase = compatibility.ReasonPhrase ?? "it's no longer compatible"; + string reasonPhrase = dataRecord.ReasonPhrase ?? "it's no longer compatible"; string error = $"{reasonPhrase}. Please check for a "; - if (mod.Manifest.Version.Equals(compatibility.UpperVersion) && compatibility.UpperVersionLabel == null) + if (mod.Manifest.Version.Equals(dataRecord.UpperVersion) && dataRecord.UpperVersionLabel == null) error += "newer version"; else - error += $"version newer than {compatibility.UpperVersionLabel ?? compatibility.UpperVersion.ToString()}"; - error += " at " + string.Join(" or ", compatibility.UpdateUrls); + error += $"version newer than {dataRecord.UpperVersionLabel ?? dataRecord.UpperVersion.ToString()}"; + error += " at " + string.Join(" or ", dataRecord.UpdateUrls); mod.SetStatus(ModMetadataStatus.Failed, error); continue; diff --git a/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs b/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs deleted file mode 100644 index 7489a306..00000000 --- a/src/StardewModdingAPI/Framework/Models/ModCompatibility.cs +++ /dev/null @@ -1,40 +0,0 @@ -using Newtonsoft.Json; -using StardewModdingAPI.Framework.Serialisation; - -namespace StardewModdingAPI.Framework.Models -{ - /// Metadata about a mod version that SMAPI should assume is compatible or broken, regardless of whether it detects incompatible code. - internal class ModCompatibility - { - /********* - ** Accessors - *********/ - /// The unique mod IDs. - [JsonConverter(typeof(SFieldConverter))] - public ModCompatibilityID[] ID { get; set; } - - /// 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; } - - /// Indicates how SMAPI should treat the mod. - public ModStatus Status { get; set; } = ModStatus.AssumeBroken; - } -} diff --git a/src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs b/src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs deleted file mode 100644 index 98e70116..00000000 --- a/src/StardewModdingAPI/Framework/Models/ModCompatibilityID.cs +++ /dev/null @@ -1,57 +0,0 @@ -using System; -using Newtonsoft.Json; - -namespace StardewModdingAPI.Framework.Models -{ - /// Uniquely identifies a mod for compatibility checks. - internal class ModCompatibilityID - { - /********* - ** Accessors - *********/ - /// The unique mod ID. - public string ID { get; set; } - - /// The mod name to disambiguate non-unique IDs, or null to ignore the mod name. - public string Name { get; set; } - - /// The author name to disambiguate non-unique IDs, or null to ignore the author. - public string Author { get; set; } - - - /********* - ** Public methods - *********/ - /// Construct an instance. - public ModCompatibilityID() { } - - /// Construct an instance. - /// The mod ID or a JSON string matching the fields. - public ModCompatibilityID(string data) - { - // JSON can be stuffed into the ID string as a convenience hack to keep JSON mod lists - // formatted readably. The tradeoff is that the format is a bit more magical, but that's - // probably acceptable since players aren't meant to edit it. It's also fairly clear what - // the JSON strings do, if not necessarily how. - if (data.StartsWith("{")) - JsonConvert.PopulateObject(data, this); - else - this.ID = data; - } - - /// Get whether this ID matches a given mod manifest. - /// The mod's unique ID, or a substitute ID if it isn't set in the manifest. - /// The manifest to check. - public bool Matches(string id, IManifest manifest) - { - return - this.ID.Equals(id, StringComparison.InvariantCultureIgnoreCase) - && ( - this.Author == null - || this.Author.Equals(manifest.Author, StringComparison.InvariantCultureIgnoreCase) - || (manifest.ExtraFields.ContainsKey("Authour") && this.Author.Equals(manifest.ExtraFields["Authour"].ToString(), StringComparison.InvariantCultureIgnoreCase)) - ) - && (this.Name == null || this.Name.Equals(manifest.Name, StringComparison.InvariantCultureIgnoreCase)); - } - } -} diff --git a/src/StardewModdingAPI/Framework/Models/ModDataID.cs b/src/StardewModdingAPI/Framework/Models/ModDataID.cs new file mode 100644 index 00000000..5b45b507 --- /dev/null +++ b/src/StardewModdingAPI/Framework/Models/ModDataID.cs @@ -0,0 +1,57 @@ +using System; +using Newtonsoft.Json; + +namespace StardewModdingAPI.Framework.Models +{ + /// Uniquely identifies a mod in SMAPI's internal data. + internal class ModDataID + { + /********* + ** Accessors + *********/ + /// The unique mod ID. + public string ID { get; set; } + + /// The mod name to disambiguate non-unique IDs, or null to ignore the mod name. + public string Name { get; set; } + + /// The author name to disambiguate non-unique IDs, or null to ignore the author. + public string Author { get; set; } + + + /********* + ** Public methods + *********/ + /// Construct an instance. + public ModDataID() { } + + /// Construct an instance. + /// The mod ID or a JSON string matching the fields. + public ModDataID(string data) + { + // JSON can be stuffed into the ID string as a convenience hack to keep JSON mod lists + // formatted readably. The tradeoff is that the format is a bit more magical, but that's + // probably acceptable since players aren't meant to edit it. It's also fairly clear what + // the JSON strings do, if not necessarily how. + if (data.StartsWith("{")) + JsonConvert.PopulateObject(data, this); + else + this.ID = data; + } + + /// Get whether this ID matches a given mod manifest. + /// The mod's unique ID, or a substitute ID if it isn't set in the manifest. + /// The manifest to check. + public bool Matches(string id, IManifest manifest) + { + return + this.ID.Equals(id, StringComparison.InvariantCultureIgnoreCase) + && ( + this.Author == null + || this.Author.Equals(manifest.Author, StringComparison.InvariantCultureIgnoreCase) + || (manifest.ExtraFields.ContainsKey("Authour") && this.Author.Equals(manifest.ExtraFields["Authour"].ToString(), StringComparison.InvariantCultureIgnoreCase)) + ) + && (this.Name == null || this.Name.Equals(manifest.Name, StringComparison.InvariantCultureIgnoreCase)); + } + } +} diff --git a/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs b/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs new file mode 100644 index 00000000..d40f2c78 --- /dev/null +++ b/src/StardewModdingAPI/Framework/Models/ModDataRecord.cs @@ -0,0 +1,40 @@ +using Newtonsoft.Json; +using StardewModdingAPI.Framework.Serialisation; + +namespace StardewModdingAPI.Framework.Models +{ + /// Metadata about a mod from SMAPI's internal data. + internal class ModDataRecord + { + /********* + ** Accessors + *********/ + /// The unique mod IDs. + [JsonConverter(typeof(SFieldConverter))] + public ModDataID[] ID { get; set; } + + /// 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; } + + /// Indicates how SMAPI should treat the mod. + public ModStatus Status { get; set; } = ModStatus.AssumeBroken; + } +} diff --git a/src/StardewModdingAPI/Framework/Models/SConfig.cs b/src/StardewModdingAPI/Framework/Models/SConfig.cs index 720d4a6d..401e1a3a 100644 --- a/src/StardewModdingAPI/Framework/Models/SConfig.cs +++ b/src/StardewModdingAPI/Framework/Models/SConfig.cs @@ -21,7 +21,7 @@ namespace StardewModdingAPI.Framework.Models /// Whether SMAPI should log more information about the game context. public bool VerboseLogging { get; set; } - /// A list of mod versions which should be considered compatible or incompatible regardless of whether SMAPI detects incompatible code. - public ModCompatibility[] ModCompatibility { get; set; } + /// Extra metadata about mods. + public ModDataRecord[] ModData { get; set; } } } diff --git a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs b/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs index 5419896f..59cc1582 100644 --- a/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs +++ b/src/StardewModdingAPI/Framework/Serialisation/SFieldConverter.cs @@ -27,7 +27,7 @@ namespace StardewModdingAPI.Framework.Serialisation return objectType == typeof(ISemanticVersion) || objectType == typeof(IManifestDependency[]) - || objectType == typeof(ModCompatibilityID[]); + || objectType == typeof(ModDataID[]); } /// Reads the JSON representation of the object. @@ -83,14 +83,14 @@ namespace StardewModdingAPI.Framework.Serialisation } // mod compatibility ID - if (objectType == typeof(ModCompatibilityID[])) + if (objectType == typeof(ModDataID[])) { - List result = new List(); + List result = new List(); foreach (JToken child in JArray.Load(reader).Children()) { result.Add(child is JValue value - ? new ModCompatibilityID(value.Value()) - : child.ToObject() + ? new ModDataID(value.Value()) + : child.ToObject() ); } return result.ToArray(); -- cgit