summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs
blob: 109175b55f8e2b7b0c6c235e63eafb5b718dc469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright 2022 Jamie Taylor
using System;
using System.Collections.Generic;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.UpdateData;

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) {
            this.IsSubkeyStrict = true;
            this.manifest = manifest;
            this.SetInfo(name: id, url: id, version: null, downloads: TranslateDownloads(manifest).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;
        }

        /// <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;
        }


        /*********
        ** 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);
                }
            }
        }

    }
}