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 repository containing the mod. public ModRepositoryKey Repository { 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 repository containing the mod. /// The mod ID within the repository. public UpdateKey(string rawText, ModRepositoryKey repository, string id) { this.RawText = rawText; this.Repository = repository; this.ID = id; this.LooksValid = repository != ModRepositoryKey.Unknown && !string.IsNullOrWhiteSpace(id); } /// Construct an instance. /// The mod repository containing the mod. /// The mod ID within the repository. public UpdateKey(ModRepositoryKey repository, string id) : this($"{repository}:{id}", repository, id) { } /// Parse a raw update key. /// The raw update key to parse. public static UpdateKey Parse(string raw) { // split parts string[] parts = raw?.Split(':'); if (parts == null || parts.Length != 2) return new UpdateKey(raw, ModRepositoryKey.Unknown, null); // extract parts string repositoryKey = parts[0].Trim(); string id = parts[1].Trim(); if (string.IsNullOrWhiteSpace(id)) id = null; // parse if (!Enum.TryParse(repositoryKey, true, out ModRepositoryKey repository)) return new UpdateKey(raw, ModRepositoryKey.Unknown, id); if (id == null) return new UpdateKey(raw, repository, null); return new UpdateKey(raw, repository, id); } /// Get a string that represents the current object. public override string ToString() { return this.LooksValid ? $"{this.Repository}:{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.Repository == other.Repository && 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.Repository}:{this.ID}".ToLower().GetHashCode(); } } }