#region ========================================================================= USING =====================================================================================
using Lumina.Plugins.Contracts.Core.Plugins;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
#endregion
namespace Lumina.Infrastructure.Core.Plugins;
///
/// Result of loading the plugins from the plugins directory.
///
/// The plugins that were loaded successfully.
/// The errors that occurred while loading the plugins.
internal sealed record PluginLoadResult(IReadOnlyList Plugins, IReadOnlyList Errors);
///
/// Loads the plugins from the plugins directory, discovers their descriptors and registers their services.
///
internal static class PluginLoader
{
///
/// Loads the plugins from the , discovers their descriptors and registers their services.
///
/// The directory where the plugin assemblies are located.
/// The service collection to which the plugin services are added.
/// The result of loading the plugins.
public static PluginLoadResult LoadPlugins(string pluginsDirectory, IServiceCollection services)
{
List plugins = [];
List errors = [];
if (!Directory.Exists(pluginsDirectory))
return new PluginLoadResult(plugins, errors);
foreach (string assemblyPath in Directory.EnumerateFiles(pluginsDirectory, "*.dll"))
{
// a failing plugin must not prevent the other plugins from loading, so each assembly is isolated in its own load context
PluginLoadContext loadContext = new(pluginsDirectory, Path.GetFileNameWithoutExtension(assemblyPath));
try
{
Assembly assembly = loadContext.LoadFromAssemblyPath(assemblyPath);
foreach (Type type in assembly.GetTypes())
{
if (type.IsClass && !type.IsAbstract && typeof(IPlugin).IsAssignableFrom(type))
{
IPlugin plugin = (IPlugin)Activator.CreateInstance(type)!;
if (plugin is IPluginServiceRegistrator serviceRegistrator)
serviceRegistrator.RegisterServices(services);
plugins.Add(plugin);
break; // only one plugin descriptor per assembly
}
}
}
catch (Exception exception)
{
errors.Add($"Failed to load plugin assembly '{Path.GetFileName(assemblyPath)}': {exception.Message}");
}
}
return new PluginLoadResult(plugins, errors);
}
}