summaryrefslogtreecommitdiff
path: root/src/SMAPI/Framework/ModHelpers/ModRegistryHelper.cs
blob: e277e6fa9a73b0bfbf8b0ff23f9bcfccb5f92b5d (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
#nullable disable

using System.Collections.Generic;
using StardewModdingAPI.Framework.Reflection;

namespace StardewModdingAPI.Framework.ModHelpers
{
    /// <summary>Provides metadata about installed mods.</summary>
    internal class ModRegistryHelper : BaseHelper, IModRegistry
    {
        /*********
        ** Fields
        *********/
        /// <summary>The underlying mod registry.</summary>
        private readonly ModRegistry Registry;

        /// <summary>Encapsulates monitoring and logging for the mod.</summary>
        private readonly IMonitor Monitor;

        /// <summary>The mod IDs for APIs accessed by this instanced.</summary>
        private readonly HashSet<string> AccessedModApis = new();

        /// <summary>Generates proxy classes to access mod APIs through an arbitrary interface.</summary>
        private readonly InterfaceProxyFactory ProxyFactory;


        /*********
        ** Public methods
        *********/
        /// <summary>Construct an instance.</summary>
        /// <param name="modID">The unique ID of the relevant mod.</param>
        /// <param name="registry">The underlying mod registry.</param>
        /// <param name="proxyFactory">Generates proxy classes to access mod APIs through an arbitrary interface.</param>
        /// <param name="monitor">Encapsulates monitoring and logging for the mod.</param>
        public ModRegistryHelper(string modID, ModRegistry registry, InterfaceProxyFactory proxyFactory, IMonitor monitor)
            : base(modID)
        {
            this.Registry = registry;
            this.ProxyFactory = proxyFactory;
            this.Monitor = monitor;
        }

        /// <inheritdoc />
        public IEnumerable<IModInfo> GetAll()
        {
            return this.Registry.GetAll();
        }

        /// <inheritdoc />
        public IModInfo Get(string uniqueID)
        {
            return this.Registry.Get(uniqueID);
        }

        /// <inheritdoc />
        public bool IsLoaded(string uniqueID)
        {
            return this.Registry.Get(uniqueID) != null;
        }

        /// <inheritdoc />
        public object GetApi(string uniqueID)
        {
            // validate ready
            if (!this.Registry.AreAllModsInitialized)
            {
                this.Monitor.Log("Tried to access a mod-provided API before all mods were initialized.", LogLevel.Error);
                return null;
            }

            // get raw API
            IModMetadata mod = this.Registry.Get(uniqueID);
            if (mod?.Api != null && this.AccessedModApis.Add(mod.Manifest.UniqueID))
                this.Monitor.Log($"Accessed mod-provided API for {mod.DisplayName}.");
            return mod?.Api;
        }

        /// <inheritdoc />
        public TInterface GetApi<TInterface>(string uniqueID) where TInterface : class
        {
            // get raw API
            object api = this.GetApi(uniqueID);
            if (api == null)
                return null;

            // validate mapping
            if (!typeof(TInterface).IsInterface)
            {
                this.Monitor.Log($"Tried to map a mod-provided API to class '{typeof(TInterface).FullName}'; must be a public interface.", LogLevel.Error);
                return null;
            }
            if (!typeof(TInterface).IsPublic)
            {
                this.Monitor.Log($"Tried to map a mod-provided API to non-public interface '{typeof(TInterface).FullName}'; must be a public interface.", LogLevel.Error);
                return null;
            }

            // get API of type
            return api is TInterface castApi
                ? castApi
                : this.ProxyFactory.CreateProxy<TInterface>(api, sourceModID: this.ModID, targetModID: uniqueID);
        }
    }
}