using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; using System.Linq; using StardewModdingAPI.Toolkit.Framework.UpdateData; namespace StardewModdingAPI.Web.Framework.Clients { /// Generic metadata about a mod page. internal class GenericModPage : IModPage { /********* ** Accessors *********/ /// The mod site containing the mod. public ModSiteKey Site { get; set; } /// The mod's unique ID within the site. public string Id { get; set; } /// The mod name. public string? Name { get; set; } /// The mod's semantic version number. public string? Version { get; set; } /// The mod's web URL. public string? Url { get; set; } /// The mod downloads. public IModDownload[] Downloads { get; set; } = Array.Empty(); /// The mod availability status on the remote site. public RemoteModStatus Status { get; set; } = RemoteModStatus.InvalidData; /// A user-friendly error which indicates why fetching the mod info failed (if applicable). public string? Error { get; set; } /// Whether the mod data is valid. [MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))] public bool IsValid => this.Status == RemoteModStatus.Ok; /// Whether this mod page requires update subkeys and does not allow matching downloads without them. public bool RequireSubkey { get; set; } = false; /********* ** Public methods *********/ /// Construct an instance. /// The mod site containing the mod. /// The mod's unique ID within the site. public GenericModPage(ModSiteKey site, string id) { this.Site = site; this.Id = id; } /// Set the fetched mod info. /// The mod name. /// The mod's semantic version number. /// The mod's web URL. /// The mod downloads. public IModPage SetInfo(string name, string? version, string url, IEnumerable downloads) { this.Name = name; this.Version = version; this.Url = url; this.Downloads = downloads.ToArray(); this.Status = RemoteModStatus.Ok; return this; } /// Set a mod fetch error. /// The mod availability status on the remote site. /// A user-friendly error which indicates why fetching the mod info failed (if applicable). public IModPage SetError(RemoteModStatus status, string error) { this.Status = status; this.Error = error; return this; } /// Get the mod name for an update subkey, if different from the mod page name. /// The update subkey. public virtual string? GetName(string? subkey) { return this.Name; } /// Get the mod page URL for an update subkey, if different from the mod page it was fetched from. /// The update subkey. public virtual string? GetUrl(string? subkey) { return this.Url; } } }