// Kyoo - A portable and vast media library solution.
// Copyright (c) Kyoo.
//
// See AUTHORS.md and LICENSE file in the project root for full license information.
//
// Kyoo 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 3 of the License, or
// any later version.
//
// Kyoo 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 Kyoo. If not, see .
using System;
using System.Collections.Generic;
using System.Linq;
using Kyoo.Abstractions.Controllers;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
namespace Kyoo.Host.Controllers;
///
/// An implementation of .
/// This is used to load plugins and retrieve information from them.
///
public class PluginManager : IPluginManager
{
///
/// The service provider. It allow plugin's activation.
///
private readonly IServiceProvider _provider;
///
/// The logger used by this class.
///
private readonly ILogger _logger;
///
/// The list of plugins that are currently loaded.
///
private readonly List _plugins = new();
///
/// Create a new instance.
///
/// A service container to allow initialization of plugins
/// The logger used by this class.
public PluginManager(IServiceProvider provider, ILogger logger)
{
_provider = provider;
_logger = logger;
}
///
public T GetPlugin(string name)
{
return (T)_plugins?.FirstOrDefault(x => x.Name == name && x is T);
}
///
public ICollection GetPlugins()
{
return _plugins?.OfType().ToArray();
}
///
public ICollection GetAllPlugins()
{
return _plugins;
}
///
public void LoadPlugins(ICollection plugins)
{
_plugins.AddRange(plugins);
_logger.LogInformation("Modules enabled: {Plugins}", _plugins.Select(x => x.Name));
}
///
public void LoadPlugins(params Type[] plugins)
{
LoadPlugins(
plugins.Select(x => (IPlugin)ActivatorUtilities.CreateInstance(_provider, x)).ToArray()
);
}
}