using System; namespace StardewModdingAPI.Toolkit.Framework.UpdateData { /// A namespaced mod ID which uniquely identifies a mod within a mod repository. public class UpdateKey : IEquatable { /********* ** Accessors *********/ /// The raw update key text. public string RawText { get; } /// The mod site containing the mod. public ModSiteKey Site { get; } /// The mod ID within the repository. public string ID { get; } /// Whether the update key seems to be valid. public bool LooksValid { get; } /********* ** Public methods *********/ /// Construct an instance. /// 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) { this.RawText = rawText?.Trim(); this.Site = site; this.ID = id?.Trim(); this.LooksValid = site != ModSiteKey.Unknown && !string.IsNullOrWhiteSpace(id); } /// 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) { } /// Parse a raw update key. /// The raw update key to parse. public static UpdateKey Parse(string raw) { // extract site + ID string rawSite; string id; { string[] parts = raw?.Trim().Split(':'); if (parts == null || parts.Length != 2) return new UpdateKey(raw, ModSiteKey.Unknown, null); rawSite = parts[0].Trim(); id = parts[1].Trim(); } if (string.IsNullOrWhiteSpace(id)) id = null; // parse if (!Enum.TryParse(rawSite, true, out ModSiteKey site)) return new UpdateKey(raw, ModSiteKey.Unknown, id); if (id == null) return new UpdateKey(raw, site, null); return new UpdateKey(raw, site, id); } /// Get a string that represents the current object. public override string ToString() { return this.LooksValid ? UpdateKey.GetString(this.Site, this.ID) : this.RawText; } /// Indicates whether the current object is equal to another object of the same type. /// An object to compare with this object. public bool Equals(UpdateKey other) { return other != null && this.Site == other.Site && string.Equals(this.ID, other.ID, StringComparison.InvariantCultureIgnoreCase); } /// Determines whether the specified object is equal to the current object. /// The object to compare with the current object. public override bool Equals(object obj) { return obj is UpdateKey other && this.Equals(other); } /// Serves as the default hash function. /// A hash code for the current object. public override int GetHashCode() { return $"{this.Site}:{this.ID}".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) { return $"{site}:{id}".Trim(); } } }