summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs')
-rw-r--r--src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs80
1 files changed, 48 insertions, 32 deletions
diff --git a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs
index 109175b5..bbc7b5da 100644
--- a/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs
+++ b/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs
@@ -1,58 +1,74 @@
-// Copyright 2022 Jamie Taylor
-using System;
+// Copyright 2022 Jamie Taylor
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
-namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest {
+namespace StardewModdingAPI.Web.Framework.Clients.UpdateManifest
+{
/// <summary>Metadata about an update manifest "page".</summary>
- internal class UpdateManifestModPage : GenericModPage {
- /// <summary>The update manifest model.</summary>
- private UpdateManifestModel manifest;
-
- /// <summary>Constuct an instance.</summary>
- /// <param name="id">The "id" (i.e., URL) of this update manifest.</param>
- /// <param name="manifest">The manifest object model.</param>
- public UpdateManifestModPage(string id, UpdateManifestModel manifest) : base(ModSiteKey.UpdateManifest, id) {
+ 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.IsSubkeyStrict = true;
- this.manifest = manifest;
- this.SetInfo(name: id, url: id, version: null, downloads: TranslateDownloads(manifest).ToArray());
+ this.Mods = manifest.Mods ?? new Dictionary<string, UpdateManifestModModel>();
+ 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) {
- if (subkey is null)
- return null;
- this.manifest.Subkeys.TryGetValue(subkey, out UpdateManifestModModel? modModel);
- return modModel?.Name;
+ public override string? GetName(string? subkey)
+ {
+ return subkey is not null && this.Mods.TryGetValue(subkey, out UpdateManifestModModel? modModel)
+ ? modModel.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) {
- if (subkey is null)
- return null;
- this.manifest.Subkeys.TryGetValue(subkey, out UpdateManifestModModel? modModel);
- return modModel?.Url;
+ public override string? GetUrl(string? subkey)
+ {
+ return subkey is not null && this.Mods.TryGetValue(subkey, out UpdateManifestModModel? modModel)
+ ? modModel.Url
+ : null;
}
/*********
** Private methods
*********/
- /// <summary>Translate the downloads from the manifest's object model into <see cref="IModDownload"/> objects.</summary>
- /// <param name="manifest">The manifest object model.</param>
- /// <returns>An <see cref="IModDownload"/> for each <see cref="UpdateManifestVersionModel"/> in the manifest.</returns>
- private static IEnumerable<IModDownload> 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);
- }
+ /// <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)
+ {
+ if (mod.Versions is null)
+ continue;
+
+ foreach (UpdateManifestVersionModel version in mod.Versions)
+ yield return new UpdateManifestModDownload(modKey, mod.Name ?? modKey, version.Version, version.DownloadFileUrl ?? version.DownloadPageUrl);
}
}
}
-} \ No newline at end of file
+}