Cleaning the plugin manager

This commit is contained in:
Zoe Roux 2020-07-06 02:20:23 +02:00
parent 2f5b59afed
commit 81797948df

View File

@ -50,16 +50,12 @@ namespace Kyoo.Controllers
public T GetPlugin<T>(string name) public T GetPlugin<T>(string name)
{ {
if (_plugins == null) return (T)_plugins?.FirstOrDefault(x => x.Name == name && x is T);
return default;
return (T)(from plugin in _plugins where plugin.Name == name && plugin is T select plugin).FirstOrDefault();
} }
public IEnumerable<T> GetPlugins<T>() public IEnumerable<T> GetPlugins<T>()
{ {
if (_plugins == null) return _plugins?.OfType<T>() ?? new List<T>();
return new List<T>();
return from plugin in _plugins where plugin is T select (T)plugin;
} }
public IEnumerable<IPlugin> GetAllPlugins() public IEnumerable<IPlugin> GetAllPlugins()
@ -70,20 +66,20 @@ namespace Kyoo.Controllers
public void ReloadPlugins() public void ReloadPlugins()
{ {
string pluginFolder = _config.GetValue<string>("plugins"); string pluginFolder = _config.GetValue<string>("plugins");
if (!Directory.Exists(pluginFolder)) if (!Directory.Exists(pluginFolder))
return; return;
string[] pluginsPaths = Directory.GetFiles(pluginFolder);
_plugins = pluginsPaths.Select(path => string[] pluginsPaths = Directory.GetFiles(pluginFolder);
_plugins = pluginsPaths.SelectMany(path =>
{ {
path = Path.GetFullPath(path);
try try
{ {
PluginDependencyLoader loader = new PluginDependencyLoader(Path.GetFullPath(path)); PluginDependencyLoader loader = new PluginDependencyLoader(path);
Assembly ass = loader.LoadFromAssemblyPath(Path.GetFullPath(path)); Assembly ass = loader.LoadFromAssemblyPath(path);
return (from type in ass.GetTypes() return ass.GetTypes()
where typeof(IPlugin).IsAssignableFrom(type) .Where(x => typeof(IPlugin).IsAssignableFrom(x))
select (IPlugin) ActivatorUtilities.CreateInstance(_provider, type)).FirstOrDefault(); .Select(x => (IPlugin)ActivatorUtilities.CreateInstance(_provider, x));
} }
catch (Exception ex) catch (Exception ex)
{ {
@ -91,6 +87,7 @@ namespace Kyoo.Controllers
return null; return null;
} }
}).Where(x => x != null).ToList(); }).Where(x => x != null).ToList();
if (!_plugins.Any()) if (!_plugins.Any())
{ {
Console.WriteLine("No plugin enabled."); Console.WriteLine("No plugin enabled.");