summaryrefslogtreecommitdiff
path: root/src/SMAPI.Web/Framework/ModRepositories/BaseRepository.cs
blob: f9f9f47d6825d3b136794b6914f8c0725b1daebf (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
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using StardewModdingAPI.Toolkit.Framework.UpdateData;

namespace StardewModdingAPI.Web.Framework.ModRepositories
{
    internal abstract class RepositoryBase : IModRepository
    {
        /*********
        ** Accessors
        *********/
        /// <summary>The unique key for this vendor.</summary>
        public ModRepositoryKey VendorKey { get; }


        /*********
        ** Public methods
        *********/
        /// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
        public abstract void Dispose();

        /// <summary>Get metadata about a mod in the repository.</summary>
        /// <param name="id">The mod ID in this repository.</param>
        public abstract Task<ModInfoModel> GetModInfoAsync(string id);


        /*********
        ** Protected methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="vendorKey">The unique key for this vendor.</param>
        protected RepositoryBase(ModRepositoryKey vendorKey)
        {
            this.VendorKey = vendorKey;
        }

        /// <summary>Normalize a version string.</summary>
        /// <param name="version">The version to normalize.</param>
        protected string NormalizeVersion(string version)
        {
            if (string.IsNullOrWhiteSpace(version))
                return null;

            version = version.Trim();
            if (Regex.IsMatch(version, @"^v\d", RegexOptions.CultureInvariant | RegexOptions.IgnoreCase)) // common version prefix
                version = version.Substring(1);

            return version;
        }
    }
}