using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Toolkit.Framework.UpdateData; using StardewModdingAPI.Web.Framework.Clients.UpdateManifest.ResponseModels; namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest { /// Metadata about an update manifest "page". internal class UpdateManifestModPage : GenericModPage { /********* ** Fields *********/ /// The mods from the update manifest. private readonly IDictionary Mods; /********* ** Public methods *********/ /// Construct an instance. /// The URL of the update manifest file. /// The parsed update manifest. public UpdateManifestModPage(string url, UpdateManifestModel manifest) : base(ModSiteKey.UpdateManifest, url) { this.RequireSubkey = true; this.Mods = manifest.Mods; this.SetInfo(name: url, url: url, version: null, downloads: this.ParseDownloads(manifest.Mods).ToArray()); } /// Return the mod name for the given subkey, if it exists in this update manifest. /// The subkey. /// The mod name for the given subkey, or if this manifest does not contain the given subkey. public override string? GetName(string? subkey) { return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? mod) ? mod.Name : null; } /// Return the mod URL for the given subkey, if it exists in this update manifest. /// The subkey. /// The mod URL for the given subkey, or if this manifest does not contain the given subkey. public override string? GetUrl(string? subkey) { return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? mod) ? mod.ModPageUrl : null; } /********* ** Private methods *********/ /// Convert the raw download info from an update manifest to . /// The mods from the update manifest. private IEnumerable ParseDownloads(IDictionary? mods) { if (mods is null) yield break; foreach ((string modKey, UpdateManifestModModel mod) in mods) { foreach (UpdateManifestVersionModel version in mod.Versions) yield return new UpdateManifestModDownload(modKey, mod.Name ?? modKey, version.Version, version.ModPageUrl); } } } }