// Copyright 2022 Jamie Taylor using System; using System.Collections.Generic; using System.Linq; using StardewModdingAPI.Toolkit.Framework.UpdateData; namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest { /// Metadata about an update manifest "page". internal class UpdateManifestModPage : GenericModPage { /// The update manifest model. private UpdateManifestModel manifest; /// Constuct an instance. /// The "id" (i.e., URL) of this update manifest. /// The manifest object model. public UpdateManifestModPage(string id, UpdateManifestModel manifest) : base(ModSiteKey.UpdateManifest, id) { this.IsSubkeyStrict = true; this.manifest = manifest; this.SetInfo(name: id, url: id, version: null, downloads: TranslateDownloads(manifest).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) { if (subkey is null) return null; this.manifest.Subkeys.TryGetValue(subkey, out UpdateManifestModModel? modModel); return modModel?.Name; } /// 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) { if (subkey is null) return null; this.manifest.Subkeys.TryGetValue(subkey, out UpdateManifestModModel? modModel); return modModel?.Url; } /********* ** Private methods *********/ /// Translate the downloads from the manifest's object model into objects. /// The manifest object model. /// An for each in the manifest. private static IEnumerable TranslateDownloads(UpdateManifestModel manifest) { foreach (var entry in manifest.Subkeys) { foreach (var version in entry.Value.Versions) { yield return new UpdateManifestModDownload(entry.Key, entry.Value.Name, version.Version, version.DownloadFileUrl ?? version.DownloadPageUrl); } } } } }