67 lines
2.0 KiB
C#
67 lines
2.0 KiB
C#
using System.Reflection;
|
|
using Inspectron.Settings;
|
|
using Ninject;
|
|
using Serilog;
|
|
|
|
namespace VisionBuilder.UI.Common.Plugins;
|
|
|
|
public class PluginLoader
|
|
{
|
|
private static PluginLoader _instance;
|
|
public static PluginLoader Instance => _instance ??= new PluginLoader();
|
|
|
|
const string PLUGINS_DIRECTORY = "Plugins";
|
|
private PluginLoader()
|
|
{
|
|
Initialize();
|
|
}
|
|
|
|
public List<IPlugin> Plugins { get; } = new List<IPlugin>();
|
|
|
|
|
|
public void Initialize()
|
|
{
|
|
Log.Information("Loading plugins...");
|
|
var pluginsPath = Path.Combine("..", "Data", PLUGINS_DIRECTORY);
|
|
Directory.CreateDirectory(pluginsPath);
|
|
var pluginFolders = Directory.GetDirectories(pluginsPath);
|
|
|
|
foreach (string folder in pluginFolders)
|
|
{
|
|
var assemblyPath = Path.Combine(folder, Path.GetFileNameWithoutExtension(folder) + ".dll");
|
|
|
|
if (!File.Exists(assemblyPath))
|
|
{
|
|
Log.Warning("Plugin assembly not found: {AssemblyPath}", assemblyPath);
|
|
continue;
|
|
}
|
|
try
|
|
{
|
|
LoadPluginAssembly(assemblyPath);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Log.Error(ex, "Failed to load plugin from {AssemblyPath}", assemblyPath);
|
|
}
|
|
}
|
|
Log.Information("Plugin loading completed.");
|
|
}
|
|
|
|
private void LoadPluginAssembly(string assemblyPath)
|
|
{
|
|
var absPath = Path.GetFullPath(assemblyPath);
|
|
var loadContext = new PluginLoadContext(absPath);
|
|
var assembly = loadContext.LoadFromAssemblyPath(absPath);
|
|
Log.Information("Loaded plugin assembly: {AssemblyName}", assembly.GetName().Name);
|
|
foreach (var type in assembly.GetTypes())
|
|
{
|
|
if (type.IsAssignableTo(typeof(IPlugin)) && !type.IsAbstract)
|
|
{
|
|
Plugins.Add((IPlugin)Activator.CreateInstance(type));
|
|
Log.Information("Discovered plugin: {PluginName}", type.Name);
|
|
}
|
|
|
|
|
|
}
|
|
}
|
|
} |