diff options
author | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2023-04-09 13:12:30 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <Pathoschild@users.noreply.github.com> | 2023-04-09 13:12:30 -0400 |
commit | 5486146a05dec8a5c09e9585e5b610b89feaf731 (patch) | |
tree | 52c91cb3c4349976ec77877b963d444cadd4587c /src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs | |
parent | 53e0e8cd2410e63c7725206db4a8156cc460d111 (diff) | |
parent | a4b193b920879b7db255f387af8e3c9b4fa2b46a (diff) | |
download | SMAPI-5486146a05dec8a5c09e9585e5b610b89feaf731.tar.gz SMAPI-5486146a05dec8a5c09e9585e5b610b89feaf731.tar.bz2 SMAPI-5486146a05dec8a5c09e9585e5b610b89feaf731.zip |
Merge pull request #876 from jltaylor-us/update-manifest
Add `UpdateManifest` update keys
Diffstat (limited to 'src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs')
-rw-r--r-- | src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs new file mode 100644 index 00000000..df752713 --- /dev/null +++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs @@ -0,0 +1,71 @@ +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 +{ + /// <summary>Metadata about an update manifest "page".</summary> + internal class UpdateManifestModPage : GenericModPage + { + /********* + ** Fields + *********/ + /// <summary>The mods from the update manifest.</summary> + private readonly IDictionary<string, UpdateManifestModModel> Mods; + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="url">The URL of the update manifest file.</param> + /// <param name="manifest">The parsed update manifest.</param> + 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()); + } + + /// <summary>Return the mod name for the given subkey, if it exists in this update manifest.</summary> + /// <param name="subkey">The subkey.</param> + /// <returns>The mod name for the given subkey, or <see langword="null"/> if this manifest does not contain the given subkey.</returns> + public override string? GetName(string? subkey) + { + return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? mod) + ? mod.Name + : null; + } + + /// <summary>Return the mod URL for the given subkey, if it exists in this update manifest.</summary> + /// <param name="subkey">The subkey.</param> + /// <returns>The mod URL for the given subkey, or <see langword="null"/> if this manifest does not contain the given subkey.</returns> + public override string? GetUrl(string? subkey) + { + return subkey is not null && this.Mods.TryGetValue(subkey.TrimStart('@'), out UpdateManifestModModel? mod) + ? mod.ModPageUrl + : null; + } + + + /********* + ** Private methods + *********/ + /// <summary>Convert the raw download info from an update manifest to <see cref="IModDownload"/>.</summary> + /// <param name="mods">The mods from the update manifest.</param> + private IEnumerable<IModDownload> ParseDownloads(IDictionary<string, UpdateManifestModModel>? 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); + } + } + + } +} |