From 8a1982326799dfe7f3d078beba32348befa1c9e6 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Fri, 16 Feb 2018 23:12:01 -0500 Subject: add mod page URL to missing-dependency errors (#437) --- src/SMAPI/Framework/ModData/ModDatabase.cs | 58 ++++++++++++++++++++++++------ 1 file changed, 48 insertions(+), 10 deletions(-) (limited to 'src/SMAPI/Framework/ModData') diff --git a/src/SMAPI/Framework/ModData/ModDatabase.cs b/src/SMAPI/Framework/ModData/ModDatabase.cs index af067f8f..332c5c48 100644 --- a/src/SMAPI/Framework/ModData/ModDatabase.cs +++ b/src/SMAPI/Framework/ModData/ModDatabase.cs @@ -15,19 +15,24 @@ namespace StardewModdingAPI.Framework.ModData /// The underlying mod data records indexed by default display name. private readonly IDictionary Records; + /// Get an update URL for an update key (if valid). + private readonly Func GetUpdateUrl; + /********* ** Public methods *********/ /// Construct an empty instance. public ModDatabase() - : this(new Dictionary()) { } + : this(new Dictionary(), key => null) { } /// Construct an instance. /// The underlying mod data records indexed by default display name. - public ModDatabase(IDictionary records) + /// Get an update URL for an update key (if valid). + public ModDatabase(IDictionary records, Func getUpdateUrl) { this.Records = records; + this.GetUpdateUrl = getUpdateUrl; } /// Get a parsed representation of the which match a given manifest. @@ -74,21 +79,54 @@ namespace StardewModdingAPI.Framework.ModData /// The unique mod ID. public string GetDisplayNameFor(string id) { - foreach (var entry in this.Records) - { - if (entry.Value.ID != null && entry.Value.ID.Equals(id, StringComparison.InvariantCultureIgnoreCase)) - return entry.Key; - } + return this.TryGetRaw(id, out string displayName, out ModDataRecord _) + ? displayName + : null; + } - return null; + /// Get the mod page URL for a mod (if available). + /// The unique mod ID. + public string GetModPageUrlFor(string id) + { + // get raw record + if (!this.TryGetRaw(id, out string _, out ModDataRecord record)) + return null; + + // get update key + ModDataField updateKeyField = record.GetFields().FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey); + if (updateKeyField == null) + return null; + + // get update URL + return this.GetUpdateUrl(updateKeyField.Value); } /********* ** Private models *********/ - /// Get the data record matching a given manifest. - /// The mod manifest. + /// Get a raw data record. + /// The mod ID to match. + /// The mod's default display name. + /// The raw mod record. + private bool TryGetRaw(string id, out string displayName, out ModDataRecord record) + { + foreach (var entry in this.Records) + { + if (entry.Value.ID != null && entry.Value.ID.Equals(id, StringComparison.InvariantCultureIgnoreCase)) + { + displayName = entry.Key; + record = entry.Value; + return true; + } + } + + displayName = null; + record = null; + return false; + } + /// Get a raw data record. + /// The mod manifest whose fields to match. /// The mod's default display name. /// The raw mod record. private bool TryGetRaw(IManifest manifest, out string displayName, out ModDataRecord record) -- cgit