summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI.Toolkit/Framework/ModData/ModDatabase.cs
blob: 3b98bcf18ef819b1848ce721174e8c0e58f27907 (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
using System;
using System.Collections.Generic;
using System.Linq;

namespace StardewModdingAPI.Toolkit.Framework.ModData
{
    /// <summary>Handles access to SMAPI's internal mod metadata list.</summary>
    public class ModDatabase
    {
        /*********
        ** Properties
        *********/
        /// <summary>The underlying mod data records indexed by default display name.</summary>
        private readonly ModDataRecord[] Records;

        /// <summary>Get an update URL for an update key (if valid).</summary>
        private readonly Func<string, string> GetUpdateUrl;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an empty instance.</summary>
        public ModDatabase()
        : this(new ModDataRecord[0], key => null) { }

        /// <summary>Construct an instance.</summary>
        /// <param name="records">The underlying mod data records indexed by default display name.</param>
        /// <param name="getUpdateUrl">Get an update URL for an update key (if valid).</param>
        public ModDatabase(IEnumerable<ModDataRecord> records, Func<string, string> getUpdateUrl)
        {
            this.Records = records.ToArray();
            this.GetUpdateUrl = getUpdateUrl;
        }

        /// <summary>Get all mod data records.</summary>
        public IEnumerable<ModDataRecord> GetAll()
        {
            return this.Records;
        }

        /// <summary>Get a mod data record.</summary>
        /// <param name="modID">The unique mod ID.</param>
        public ModDataRecord Get(string modID)
        {
            return !string.IsNullOrWhiteSpace(modID)
                ? this.Records.FirstOrDefault(p => p.HasID(modID))
                : null;
        }

        /// <summary>Get the mod page URL for a mod (if available).</summary>
        /// <param name="id">The unique mod ID.</param>
        public string GetModPageUrlFor(string id)
        {
            // get update key
            ModDataRecord record = this.Get(id);
            ModDataField updateKeyField = record?.Fields.FirstOrDefault(p => p.Key == ModDataFieldKey.UpdateKey);
            if (updateKeyField == null)
                return null;

            // get update URL
            return this.GetUpdateUrl(updateKeyField.Value);
        }
    }
}