summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/GenericModPage.cs
blob: e939f1d854ad515b2e1194a043eaf76e1fa90b10 (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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using StardewModdingAPI.Toolkit.Framework.UpdateData;

namespace StardewModdingAPI.Web.Framework.Clients
{
    /// <summary>Generic metadata about a mod page.</summary>
    internal class GenericModPage : IModPage
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The mod site containing the mod.</summary>
        public ModSiteKey Site { get; set; }

        /// <summary>The mod's unique ID within the site.</summary>
        public string Id { get; set; }

        /// <summary>The mod name.</summary>
        public string? Name { get; set; }

        /// <summary>The mod's semantic version number.</summary>
        public string? Version { get; set; }

        /// <summary>The mod's web URL.</summary>
        public string? Url { get; set; }

        /// <summary>The mod downloads.</summary>
        public IModDownload[] Downloads { get; set; } = Array.Empty<IModDownload>();

        /// <summary>The mod availability status on the remote site.</summary>
        public RemoteModStatus Status { get; set; } = RemoteModStatus.InvalidData;

        /// <summary>A user-friendly error which indicates why fetching the mod info failed (if applicable).</summary>
        public string? Error { get; set; }

        /// <summary>Whether the mod data is valid.</summary>
        [MemberNotNullWhen(true, nameof(IModPage.Name), nameof(IModPage.Url))]
        public bool IsValid => this.Status == RemoteModStatus.Ok;

        /// <summary>Whether this mod page requires string subkey matching, in which case a subkey that isn't found will return no update instead of falling back to one without.</summary>
        public bool IsSubkeyStrict { get; set; } = false;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="site">The mod site containing the mod.</param>
        /// <param name="id">The mod's unique ID within the site.</param>
        public GenericModPage(ModSiteKey site, string id)
        {
            this.Site = site;
            this.Id = id;
        }

        /// <summary>Set the fetched mod info.</summary>
        /// <param name="name">The mod name.</param>
        /// <param name="version">The mod's semantic version number.</param>
        /// <param name="url">The mod's web URL.</param>
        /// <param name="downloads">The mod downloads.</param>
        public IModPage SetInfo(string name, string? version, string url, IEnumerable<IModDownload> downloads)
        {
            this.Name = name;
            this.Version = version;
            this.Url = url;
            this.Downloads = downloads.ToArray();
            this.Status = RemoteModStatus.Ok;

            return this;
        }

        /// <summary>Set a mod fetch error.</summary>
        /// <param name="status">The mod availability status on the remote site.</param>
        /// <param name="error">A user-friendly error which indicates why fetching the mod info failed (if applicable).</param>
        public IModPage SetError(RemoteModStatus status, string error)
        {
            this.Status = status;
            this.Error = error;

            return this;
        }

        /// <summary>Get the mod name for an update subkey, if different from the mod page name.</summary>
        /// <param name="subkey">The update subkey.</param>
        public virtual string? GetName(string? subkey)
        {
            return this.Name;
        }

        /// <summary>Get the mod page URL for an update subkey, if different from the mod page it was fetched from.</summary>
        /// <param name="subkey">The update subkey.</param>
        public virtual string? GetUrl(string? subkey)
        {
            return this.Url;
        }
    }
}