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
|
/*
* Copyright (c) 2023 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Runtime
* File: PluginController.cs
*
* PluginController.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.Linq;
using System.Text.Json;
using System.Reflection;
using System.Collections.Generic;
using VNLib.Utils.Extensions;
namespace VNLib.Plugins.Runtime
{
/// <summary>
/// Manages the lifetime of a collection of <see cref="IPlugin"/> instances,
/// and their dependent event listeners
/// </summary>
public sealed class PluginController : IPluginEventRegistrar
{
private readonly List<LivePlugin> _plugins;
private readonly List<KeyValuePair<IPluginEventListener, object?>> _listeners;
internal PluginController()
{
_plugins = new ();
_listeners = new ();
}
/// <summary>
/// The current collection of plugins. Valid before the unload event.
/// </summary>
public IEnumerable<LivePlugin> Plugins => _plugins;
///<inheritdoc/>
///<exception cref="ArgumentNullException"></exception>
public void Register(IPluginEventListener listener, object? state = null)
{
_ = listener ?? throw new ArgumentNullException(nameof(listener));
_listeners.Add(new(listener, state));
}
///<inheritdoc/>
public bool Unregister(IPluginEventListener listener)
{
//Remove listener
return _listeners.RemoveAll(p => p.Key == listener) > 0;
}
internal void InitializePlugins(Assembly asm)
{
//get all Iplugin types
Type[] types = asm.GetTypes().Where(static type => !type.IsAbstract && typeof(IPlugin).IsAssignableFrom(type)).ToArray();
//Initialize the new plugin instances
IPlugin[] plugins = types.Select(static t => (IPlugin)Activator.CreateInstance(t)!).ToArray();
//Crate new containers
LivePlugin[] lps = plugins.Select(p => new LivePlugin(p, asm)).ToArray();
//Store containers
_plugins.AddRange(lps);
}
internal void ConfigurePlugins(JsonDocument hostDom, JsonDocument pluginDom, string[] cliArgs)
{
_plugins.TryForeach(lp => lp.InitConfig(hostDom, pluginDom));
_plugins.TryForeach(lp => lp.InitLog(cliArgs));
}
internal void LoadPlugins()
{
//Load all plugins
_plugins.TryForeach(static p => p.LoadPlugin());
//Notify event handlers
_listeners.TryForeach(l => l.Key.OnPluginLoaded(this, l.Value));
}
internal void UnloadPlugins()
{
try
{
//Notify event handlers
_listeners.TryForeach(l => l.Key.OnPluginUnloaded(this, l.Value));
//Unload plugin instances
_plugins.TryForeach(static p => p.UnloadPlugin());
}
finally
{
//Always
_plugins.Clear();
}
}
internal void Dispose()
{
_plugins.Clear();
_listeners.Clear();
}
}
}
|