From d97b11060c310f1fa6064f02496e6a6162a44573 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 24 May 2020 00:21:51 -0400 Subject: add update subkeys --- .../Framework/UpdateData/UpdateKey.cs | 51 +++++++++++++++++----- 1 file changed, 39 insertions(+), 12 deletions(-) (limited to 'src/SMAPI.Toolkit/Framework') diff --git a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs index f6044148..7e4d0220 100644 --- a/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs +++ b/src/SMAPI.Toolkit/Framework/UpdateData/UpdateKey.cs @@ -17,6 +17,9 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The mod ID within the repository. public string ID { get; } + /// If specified, a substring in download names/descriptions to match. + public string Subkey { get; } + /// Whether the update key seems to be valid. public bool LooksValid { get; } @@ -28,11 +31,13 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// The raw update key text. /// The mod site containing the mod. /// The mod ID within the site. - public UpdateKey(string rawText, ModSiteKey site, string id) + /// If specified, a substring in download names/descriptions to match. + public UpdateKey(string rawText, ModSiteKey site, string id, string subkey) { this.RawText = rawText?.Trim(); this.Site = site; this.ID = id?.Trim(); + this.Subkey = subkey?.Trim(); this.LooksValid = site != ModSiteKey.Unknown && !string.IsNullOrWhiteSpace(id); @@ -41,8 +46,9 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// Construct an instance. /// The mod site containing the mod. /// The mod ID within the site. - public UpdateKey(ModSiteKey site, string id) - : this(UpdateKey.GetString(site, id), site, id) { } + /// If specified, a substring in download names/descriptions to match. + public UpdateKey(ModSiteKey site, string id, string subkey) + : this(UpdateKey.GetString(site, id, subkey), site, id, subkey) { } /// Parse a raw update key. /// The raw update key to parse. @@ -54,7 +60,7 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData { string[] parts = raw?.Trim().Split(':'); if (parts == null || parts.Length != 2) - return new UpdateKey(raw, ModSiteKey.Unknown, null); + return new UpdateKey(raw, ModSiteKey.Unknown, null, null); rawSite = parts[0].Trim(); id = parts[1].Trim(); @@ -62,20 +68,32 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData if (string.IsNullOrWhiteSpace(id)) id = null; + // extract subkey + string subkey = null; + if (id != null) + { + string[] parts = id.Split('@'); + if (parts.Length == 2) + { + id = parts[0].Trim(); + subkey = $"@{parts[1]}".Trim(); + } + } + // parse if (!Enum.TryParse(rawSite, true, out ModSiteKey site)) - return new UpdateKey(raw, ModSiteKey.Unknown, id); + return new UpdateKey(raw, ModSiteKey.Unknown, id, subkey); if (id == null) - return new UpdateKey(raw, site, null); + return new UpdateKey(raw, site, null, subkey); - return new UpdateKey(raw, site, id); + return new UpdateKey(raw, site, id, subkey); } /// Get a string that represents the current object. public override string ToString() { return this.LooksValid - ? UpdateKey.GetString(this.Site, this.ID) + ? UpdateKey.GetString(this.Site, this.ID, this.Subkey) : this.RawText; } @@ -83,10 +101,18 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// An object to compare with this object. public bool Equals(UpdateKey other) { + if (!this.LooksValid) + { + return + other?.LooksValid == false + && this.RawText.Equals(other.RawText, StringComparison.OrdinalIgnoreCase); + } + return other != null && this.Site == other.Site - && string.Equals(this.ID, other.ID, StringComparison.InvariantCultureIgnoreCase); + && string.Equals(this.ID, other.ID, StringComparison.OrdinalIgnoreCase) + && string.Equals(this.Subkey, other.Subkey, StringComparison.OrdinalIgnoreCase); } /// Determines whether the specified object is equal to the current object. @@ -100,15 +126,16 @@ namespace StardewModdingAPI.Toolkit.Framework.UpdateData /// A hash code for the current object. public override int GetHashCode() { - return $"{this.Site}:{this.ID}".ToLower().GetHashCode(); + return this.ToString().ToLower().GetHashCode(); } /// Get the string representation of an update key. /// The mod site containing the mod. /// The mod ID within the repository. - public static string GetString(ModSiteKey site, string id) + /// If specified, a substring in download names/descriptions to match. + public static string GetString(ModSiteKey site, string id, string subkey = null) { - return $"{site}:{id}".Trim(); + return $"{site}:{id}{subkey}".Trim(); } } } -- cgit