aboutsummaryrefslogtreecommitdiff
path: root/lib/Plugins.Runtime/src/RuntimePluginLoader.cs
blob: 698ce56919b1a3fed781ac021f32988e758143de (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
/*
* Copyright (c) 2023 Vaughn Nugent
* 
* Library: VNLib
* Package: VNLib.Plugins.Runtime
* File: RuntimePluginLoader.cs 
*
* RuntimePluginLoader.cs is part of VNLib.Plugins.Runtime which is part of the larger 
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Runtime is free software: you can redistribute it and/or modify 
* it under the terms of the GNU General Public License as published
* by the Free Software Foundation, either version 2 of the License,
* or (at your option) any later version.
*
* VNLib.Plugins.Runtime is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License 
* along with VNLib.Plugins.Runtime. If not, see http://www.gnu.org/licenses/.
*/

using System;
using System.IO;
using System.Text.Json;
using System.Reflection;
using System.Runtime.Loader;
using System.Threading.Tasks;

using McMaster.NETCore.Plugins;

using VNLib.Utils;
using VNLib.Utils.IO;
using VNLib.Utils.Logging;

namespace VNLib.Plugins.Runtime
{
    /// <summary>
    /// A runtime .NET assembly loader specialized to load
    /// assemblies that export <see cref="IPlugin"/> types.
    /// </summary>
    public sealed class RuntimePluginLoader : VnDisposeable
    {
        private readonly PluginLoader Loader;
        private readonly string PluginPath;
        private readonly JsonDocument HostConfig;
        private readonly ILogProvider? Log;

        /// <summary>
        /// Gets the plugin lifetime manager.
        /// </summary>
        public PluginController Controller { get; }
        
        /// <summary>
        /// The path of the plugin's configuration file. (Default = pluginPath.json)
        /// </summary>
        public string PluginConfigPath { get; }
        
        /// <summary>
        /// Creates a new <see cref="RuntimePluginLoader"/> with the specified 
        /// assembly location and host config.
        /// </summary>
        /// <param name="pluginPath"></param>
        /// <param name="log">A nullable log provider</param>
        /// <param name="hostConfig">The configuration DOM to merge with plugin config DOM and pass to enabled plugins</param>
        /// <param name="unloadable">A value that specifies if the assembly can be unloaded</param>
        /// <param name="hotReload">A value that spcifies if the loader will listen for changes to the assembly file and reload the plugins</param>
        /// <remarks>
        /// The <paramref name="log"/> argument may be null if <paramref name="unloadable"/> is false
        /// </remarks>
        /// <exception cref="ArgumentNullException"></exception>
        public RuntimePluginLoader(string pluginPath, JsonDocument? hostConfig = null, ILogProvider? log = null, bool unloadable = false, bool hotReload = false)
            :this(
            new PluginConfig(pluginPath)
            {
                IsUnloadable = unloadable || hotReload,
                EnableHotReload = hotReload,
                ReloadDelay = TimeSpan.FromSeconds(1),
                PreferSharedTypes = true,
                DefaultContext = AssemblyLoadContext.Default
            },
            hostConfig, log)
        {
        }
        
        /// <summary>
        /// Creates a new <see cref="RuntimePluginLoader"/> with the specified config and host config dom.
        /// </summary>
        /// <param name="config">The plugin's loader configuration </param>
        /// <param name="hostConfig">The host/process configuration DOM</param>
        /// <param name="log">A log provider to write plugin unload log events to</param>
        /// <exception cref="ArgumentNullException"></exception>
        public RuntimePluginLoader(PluginConfig config, JsonDocument? hostConfig, ILogProvider? log)
        {
            //Shared types is required so the default load context shares types
            config.PreferSharedTypes = true;

            //Default to empty config if null
            HostConfig = hostConfig ?? JsonDocument.Parse("{}");

            Loader = new(config);
            PluginPath = config.MainAssemblyPath;
            Log = log;

            //Only regiser reload handler if the load context is unloadable
            if (config.IsUnloadable)
            {
                //Init reloaded event handler
                Loader.Reloaded += Loader_Reloaded;
            }

            //Set the config path default 
            PluginConfigPath = Path.ChangeExtension(PluginPath, ".json");

            //Init container
            Controller = new();
        }

        private async void Loader_Reloaded(object sender, PluginReloadedEventArgs eventArgs)
        {
            try
            {
                //All plugins must be unloaded forst
                UnloadAll();

                //Reload the assembly and 
                await InitializeController();

                //Load plugins
                LoadPlugins();
            }
            catch (Exception ex)
            {
                Log?.Error("Failed reload plugins for {loader}\n{ex}", PluginPath, ex);
            }
        }

        /// <summary>
        /// Initializes the plugin loader, and populates the <see cref="Controller"/>
        /// with initialized plugins.
        /// </summary>
        /// <returns>A task that represents the initialization</returns>
        /// <exception cref="IOException"></exception>
        /// <exception cref="FileNotFoundException"></exception>
        public async Task InitializeController()
        {
            JsonDocument? pluginConfig = null;

            try
            {
                //Get the plugin's configuration file
                if (FileOperations.FileExists(PluginConfigPath))
                {
                    pluginConfig = await this.GetPluginConfigAsync();
                }
                else
                {
                    //Set plugin config dom to an empty object if the file does not exist
                    pluginConfig = JsonDocument.Parse("{}");
                }

                //Load the main assembly
                Assembly PluginAsm = Loader.LoadDefaultAssembly();

                //Init container from the assembly
                Controller.InitializePlugins(PluginAsm);

                string[] cliArgs = Environment.GetCommandLineArgs();

                //Configure log/doms
                Controller.ConfigurePlugins(HostConfig, pluginConfig, cliArgs);
            }
            finally
            {
                pluginConfig?.Dispose();
            }
        }

        /// <summary>
        /// Loads all configured plugins by calling <see cref="IPlugin.Load"/>
        /// event hook on the current thread. Loading exceptions are aggregated so not
        /// to block individual loading.
        /// </summary>
        /// <exception cref="AggregateException"></exception>
        public void LoadPlugins() => Controller.LoadPlugins();

        /// <summary>
        /// Manually reload the internal <see cref="PluginLoader"/>
        /// which will reload the assembly and its plugins
        /// </summary>
        public void ReloadPlugins() => Loader.Reload();

        /// <summary>
        /// Attempts to unload all plugins. 
        /// </summary>
        /// <exception cref="AggregateException"></exception>
        public void UnloadAll() => Controller.UnloadPlugins();

        ///<inheritdoc/>
        protected override void Free()
        {
            Controller.Dispose();
            Loader.Dispose();
        }

    }
}