summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/UpdateManifest/UpdateManifestModPage.cs
blob: bbc7b5da637fff2acd6c80f649b8bc9f0d51e625 (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Copyright 2022 Jamie Taylor
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
    {
        /*********
        ** 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.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)
        {
            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)
        {
            return subkey is not null && this.Mods.TryGetValue(subkey, out UpdateManifestModModel? modModel)
                ? modModel.Url
                : 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)
            {
                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);
            }
        }

    }
}