summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/Clients/CurseForge/CurseForgeClient.cs
blob: d351b42d4541be4e88caeaef4d9ad088330b8b79 (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
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Pathoschild.Http.Client;
using StardewModdingAPI.Toolkit.Framework.UpdateData;
using StardewModdingAPI.Web.Framework.Clients.CurseForge.ResponseModels;

namespace StardewModdingAPI.Web.Framework.Clients.CurseForge
{
    /// <summary>An HTTP client for fetching mod metadata from the CurseForge API.</summary>
    internal class CurseForgeClient : ICurseForgeClient
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying HTTP client.</summary>
        private readonly IClient Client;

        /// <summary>A regex pattern which matches a version number in a CurseForge mod file name.</summary>
        private readonly Regex VersionInNamePattern = new(@"^(?:.+? | *)v?(\d+\.\d+(?:\.\d+)?(?:-.+?)?) *(?:\.(?:zip|rar|7z))?$", RegexOptions.Compiled);


        /*********
        ** Accessors
        *********/
        /// <summary>The unique key for the mod site.</summary>
        public ModSiteKey SiteKey => ModSiteKey.CurseForge;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="userAgent">The user agent for the API client.</param>
        /// <param name="apiUrl">The base URL for the CurseForge API.</param>
        public CurseForgeClient(string userAgent, string apiUrl)
        {
            this.Client = new FluentClient(apiUrl).SetUserAgent(userAgent);
        }

        /// <summary>Get update check info about a mod.</summary>
        /// <param name="id">The mod ID.</param>
        public async Task<IModPage?> GetModData(string id)
        {
            IModPage page = new GenericModPage(this.SiteKey, id);

            // get ID
            if (!uint.TryParse(id, out uint parsedId))
                return page.SetError(RemoteModStatus.DoesNotExist, $"The value '{id}' isn't a valid CurseForge mod ID, must be an integer ID.");

            // get raw data
            ModModel? mod = await this.Client
                .GetAsync($"addon/{parsedId}")
                .As<ModModel?>();
            if (mod == null)
                return page.SetError(RemoteModStatus.DoesNotExist, "Found no CurseForge mod with this ID.");

            // get downloads
            List<IModDownload> downloads = new List<IModDownload>();
            foreach (ModFileModel file in mod.LatestFiles)
            {
                downloads.Add(
                    new GenericModDownload(name: file.DisplayName ?? file.FileName, description: null, version: this.GetRawVersion(file))
                );
            }

            // return info
            return page.SetInfo(name: mod.Name, version: null, url: mod.WebsiteUrl, downloads: downloads);
        }

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


        /*********
        ** Private methods
        *********/
        /// <summary>Get a raw version string for a mod file, if available.</summary>
        /// <param name="file">The file whose version to get.</param>
        private string? GetRawVersion(ModFileModel file)
        {
            Match match = this.VersionInNamePattern.Match(file.DisplayName ?? "");
            if (!match.Success)
                match = this.VersionInNamePattern.Match(file.FileName);

            return match.Success
                ? match.Groups[1].Value
                : null;
        }
    }
}