summaryrefslogtreecommitdiff
path: root/src/SMAPI.Toolkit/Framework/Clients/Wiki/WikiClient.cs
blob: 7f06d170624fdb2ef3e3cb7f27465f4b82ca290e (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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using HtmlAgilityPack;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit.Framework.UpdateData;

namespace StardewModdingAPI.Toolkit.Framework.Clients.Wiki
{
    /// <summary>An HTTP client for fetching mod metadata from the wiki.</summary>
    public class WikiClient : IDisposable
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying HTTP client.</summary>
        private readonly IClient Client;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="userAgent">The user agent for the wiki API.</param>
        /// <param name="baseUrl">The base URL for the wiki API.</param>
        public WikiClient(string userAgent, string baseUrl = "https://stardewvalleywiki.com/mediawiki/api.php")
        {
            this.Client = new FluentClient(baseUrl).SetUserAgent(userAgent);
        }

        /// <summary>Fetch mods from the compatibility list.</summary>
        public async Task<WikiModList> FetchModsAsync()
        {
            // fetch HTML
            ResponseModel response = await this.Client
                .GetAsync("")
                .WithArguments(new
                {
                    action = "parse",
                    page = "Modding:Mod_compatibility",
                    format = "json"
                })
                .As<ResponseModel>();
            string html = response.Parse.Text["*"];

            // parse HTML
            var doc = new HtmlDocument();
            doc.LoadHtml(html);

            // fetch game versions
            string? stableVersion = doc.DocumentNode.SelectSingleNode("//div[@class='game-stable-version']")?.InnerText;
            string? betaVersion = doc.DocumentNode.SelectSingleNode("//div[@class='game-beta-version']")?.InnerText;
            if (betaVersion == stableVersion)
                betaVersion = null;

            // parse mod data overrides
            Dictionary<string, WikiDataOverrideEntry> overrides = new Dictionary<string, WikiDataOverrideEntry>(StringComparer.OrdinalIgnoreCase);
            {
                HtmlNodeCollection modNodes = doc.DocumentNode.SelectNodes("//table[@id='mod-overrides-list']//tr[@class='mod']");
                if (modNodes == null)
                    throw new InvalidOperationException("Can't parse wiki compatibility list, no mod data overrides section found.");

                foreach (WikiDataOverrideEntry entry in this.ParseOverrideEntries(modNodes))
                {
                    if (entry.Ids.Any() != true || !entry.HasChanges)
                        continue;

                    foreach (string id in entry.Ids)
                        overrides[id] = entry;
                }
            }

            // parse mod entries
            WikiModEntry[] mods;
            {
                HtmlNodeCollection modNodes = doc.DocumentNode.SelectNodes("//table[@id='mod-list']//tr[@class='mod']");
                if (modNodes == null)
                    throw new InvalidOperationException("Can't parse wiki compatibility list, no mods found.");
                mods = this.ParseModEntries(modNodes, overrides).ToArray();
            }

            // build model
            return new WikiModList(
                stableVersion: stableVersion,
                betaVersion: betaVersion,
                mods: mods
            );
        }

        /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
        public void Dispose()
        {
            this.Client.Dispose();
        }


        /*********
        ** Private methods
        *********/
        /// <summary>Parse valid mod compatibility entries.</summary>
        /// <param name="nodes">The HTML compatibility entries.</param>
        /// <param name="overridesById">The mod data overrides to apply, if any.</param>
        private IEnumerable<WikiModEntry> ParseModEntries(IEnumerable<HtmlNode> nodes, IDictionary<string, WikiDataOverrideEntry> overridesById)
        {
            foreach (HtmlNode node in nodes)
            {
                // extract fields
                string[] names = this.GetAttributeAsCsv(node, "data-name");
                string[] authors = this.GetAttributeAsCsv(node, "data-author");
                string[] ids = this.GetAttributeAsCsv(node, "data-id");
                string[] warnings = this.GetAttributeAsCsv(node, "data-warnings");
                int? nexusID = this.GetAttributeAsNullableInt(node, "data-nexus-id");
                int? chucklefishID = this.GetAttributeAsNullableInt(node, "data-cf-id");
                int? curseForgeID = this.GetAttributeAsNullableInt(node, "data-curseforge-id");
                string? curseForgeKey = this.GetAttribute(node, "data-curseforge-key");
                int? modDropID = this.GetAttributeAsNullableInt(node, "data-moddrop-id");
                string? githubRepo = this.GetAttribute(node, "data-github");
                string? customSourceUrl = this.GetAttribute(node, "data-custom-source");
                string? customUrl = this.GetAttribute(node, "data-url");
                string? anchor = this.GetAttribute(node, "id");
                string? contentPackFor = this.GetAttribute(node, "data-content-pack-for");
                string? devNote = this.GetAttribute(node, "data-dev-note");
                string? pullRequestUrl = this.GetAttribute(node, "data-pr");

                // parse stable compatibility
                WikiCompatibilityInfo compatibility = new(
                    status: this.GetAttributeAsEnum<WikiCompatibilityStatus>(node, "data-status") ?? WikiCompatibilityStatus.Ok,
                    brokeIn: this.GetAttribute(node, "data-broke-in"),
                    unofficialVersion: this.GetAttributeAsSemanticVersion(node, "data-unofficial-version"),
                    unofficialUrl: this.GetAttribute(node, "data-unofficial-url"),
                    summary: this.GetInnerHtml(node, "mod-summary")?.Trim()
                );

                // parse beta compatibility
                WikiCompatibilityInfo? betaCompatibility = null;
                {
                    WikiCompatibilityStatus? betaStatus = this.GetAttributeAsEnum<WikiCompatibilityStatus>(node, "data-beta-status");
                    if (betaStatus.HasValue)
                    {
                        betaCompatibility = new WikiCompatibilityInfo(
                            status: betaStatus.Value,
                            brokeIn: this.GetAttribute(node, "data-beta-broke-in"),
                            unofficialVersion: this.GetAttributeAsSemanticVersion(node, "data-beta-unofficial-version"),
                            unofficialUrl: this.GetAttribute(node, "data-beta-unofficial-url"),
                            summary: this.GetInnerHtml(node, "mod-beta-summary")
                        );
                    }
                }

                // find data overrides
                WikiDataOverrideEntry? overrides = ids
                    .Select(id => overridesById.TryGetValue(id, out overrides) ? overrides : null)
                    .FirstOrDefault(p => p != null);

                // yield model
                yield return new WikiModEntry(
                    id: ids,
                    name: names,
                    author: authors,
                    nexusId: nexusID,
                    chucklefishId: chucklefishID,
                    curseForgeId: curseForgeID,
                    curseForgeKey: curseForgeKey,
                    modDropId: modDropID,
                    githubRepo: githubRepo,
                    customSourceUrl: customSourceUrl,
                    customUrl: customUrl,
                    contentPackFor: contentPackFor,
                    compatibility: compatibility,
                    betaCompatibility: betaCompatibility,
                    warnings: warnings,
                    pullRequestUrl: pullRequestUrl,
                    devNote: devNote,
                    overrides: overrides,
                    anchor: anchor
                );
            }
        }

        /// <summary>Parse valid mod data override entries.</summary>
        /// <param name="nodes">The HTML mod data override entries.</param>
        private IEnumerable<WikiDataOverrideEntry> ParseOverrideEntries(IEnumerable<HtmlNode> nodes)
        {
            foreach (HtmlNode node in nodes)
            {
                yield return new WikiDataOverrideEntry
                {
                    Ids = this.GetAttributeAsCsv(node, "data-id"),
                    ChangeLocalVersions = this.GetAttributeAsChangeDescriptor(node, "data-local-version",
                        raw => SemanticVersion.TryParse(raw, out ISemanticVersion? version) ? version.ToString() : raw
                    ),
                    ChangeRemoteVersions = this.GetAttributeAsChangeDescriptor(node, "data-remote-version",
                        raw => SemanticVersion.TryParse(raw, out ISemanticVersion? version) ? version.ToString() : raw
                    ),

                    ChangeUpdateKeys = this.GetAttributeAsChangeDescriptor(node, "data-update-keys",
                        raw => UpdateKey.TryParse(raw, out UpdateKey key) ? key.ToString() : raw
                    )
                };
            }
        }

        /// <summary>Get an attribute value.</summary>
        /// <param name="element">The element whose attributes to read.</param>
        /// <param name="name">The attribute name.</param>
        private string? GetAttribute(HtmlNode element, string name)
        {
            string value = element.GetAttributeValue(name, null);
            if (string.IsNullOrWhiteSpace(value))
                return null;

            return WebUtility.HtmlDecode(value);
        }

        /// <summary>Get an attribute value and parse it as a change descriptor.</summary>
        /// <param name="element">The element whose attributes to read.</param>
        /// <param name="name">The attribute name.</param>
        /// <param name="formatValue">Format an raw entry value when applying changes.</param>
        private ChangeDescriptor? GetAttributeAsChangeDescriptor(HtmlNode element, string name, Func<string, string> formatValue)
        {
            string? raw = this.GetAttribute(element, name);
            return raw != null
                ? ChangeDescriptor.Parse(raw, out _, formatValue)
                : null;
        }

        /// <summary>Get an attribute value and parse it as a comma-delimited list of strings.</summary>
        /// <param name="element">The element whose attributes to read.</param>
        /// <param name="name">The attribute name.</param>
        private string[] GetAttributeAsCsv(HtmlNode element, string name)
        {
            string? raw = this.GetAttribute(element, name);
            return !string.IsNullOrWhiteSpace(raw)
                ? raw.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).Select(p => p.Trim()).ToArray()
                : Array.Empty<string>();
        }

        /// <summary>Get an attribute value and parse it as an enum value.</summary>
        /// <typeparam name="TEnum">The enum type.</typeparam>
        /// <param name="element">The element whose attributes to read.</param>
        /// <param name="name">The attribute name.</param>
        private TEnum? GetAttributeAsEnum<TEnum>(HtmlNode element, string name) where TEnum : struct
        {
            string? raw = this.GetAttribute(element, name);
            if (raw == null)
                return null;
            if (!Enum.TryParse(raw, true, out TEnum value) && Enum.IsDefined(typeof(TEnum), value))
                throw new InvalidOperationException($"Unknown {typeof(TEnum).Name} value '{raw}' when parsing compatibility list.");
            return value;
        }

        /// <summary>Get an attribute value and parse it as a semantic version.</summary>
        /// <param name="element">The element whose attributes to read.</param>
        /// <param name="name">The attribute name.</param>
        private ISemanticVersion? GetAttributeAsSemanticVersion(HtmlNode element, string name)
        {
            string? raw = this.GetAttribute(element, name);
            return SemanticVersion.TryParse(raw, out ISemanticVersion? version)
                ? version
                : null;
        }

        /// <summary>Get an attribute value and parse it as a nullable int.</summary>
        /// <param name="element">The element whose attributes to read.</param>
        /// <param name="name">The attribute name.</param>
        private int? GetAttributeAsNullableInt(HtmlNode element, string name)
        {
            string? raw = this.GetAttribute(element, name);
            if (raw != null && int.TryParse(raw, out int value))
                return value;
            return null;
        }

        /// <summary>Get the text of an element with the given class name.</summary>
        /// <param name="container">The metadata container.</param>
        /// <param name="className">The field name.</param>
        private string? GetInnerHtml(HtmlNode container, string className)
        {
            return container.Descendants().FirstOrDefault(p => p.HasClass(className))?.InnerHtml;
        }

        /// <summary>The response model for the MediaWiki parse API.</summary>
        [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
        [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
        private class ResponseModel
        {
            /*********
            ** Accessors
            *********/
            /// <summary>The parse API results.</summary>
            public ResponseParseModel Parse { get; }


            /*********
            ** Public methods
            *********/
            /// <summary>Construct an instance.</summary>
            /// <param name="parse">The parse API results.</param>
            public ResponseModel(ResponseParseModel parse)
            {
                this.Parse = parse;
            }
        }

        /// <summary>The inner response model for the MediaWiki parse API.</summary>
        [SuppressMessage("ReSharper", "ClassNeverInstantiated.Local")]
        [SuppressMessage("ReSharper", "CollectionNeverUpdated.Local")]
        [SuppressMessage("ReSharper", "UnusedAutoPropertyAccessor.Local")]
        private class ResponseParseModel
        {
            /*********
            ** Accessors
            *********/
            /// <summary>The parsed text.</summary>
            public IDictionary<string, string> Text { get; } = new Dictionary<string, string>();
        }
    }
}