#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Infrastructure.Plugins;
using Lumina.Plugins.Contracts.Core.Plugins;
using System;
using System.Collections.Generic;
using System.Linq;
#endregion
namespace Lumina.Infrastructure.Core.Plugins;
///
/// Manager of the plugins loaded by the host application.
///
internal sealed class PluginManager : IPluginManager
{
private readonly IReadOnlyDictionary _pluginsById;
// The load contexts of the loaded plugins are held for the lifetime of the host, because a collectible load context is only kept alive by a direct reference to the
// context object itself, and the garbage collector would otherwise unload the plugin assemblies mid-request, when a lazily resolved dependency is first used.
private readonly IReadOnlyList _loadContexts;
///
/// Initializes a new instance of the class.
///
/// The plugins that were loaded successfully.
/// The load contexts of the loaded plugins, kept referenced for the lifetime of the host.
public PluginManager(IEnumerable plugins, IReadOnlyList loadContexts)
{
_pluginsById = plugins.ToDictionary(plugin => plugin.Id);
_loadContexts = loadContexts;
}
///
/// Gets the plugins that were loaded successfully.
///
/// The collection of loaded plugins.
public IReadOnlyList GetPlugins()
{
return [.. _pluginsById.Values];
}
///
/// Gets the plugin identified by .
///
/// The unique identifier of the plugin.
/// The plugin, or when no plugin with the provided Id was loaded.
public IPlugin? GetPlugin(Guid pluginId)
{
return _pluginsById.GetValueOrDefault(pluginId);
}
}