Adding a plugin manager

This commit is contained in:
Zoe Roux 2020-01-18 00:42:49 +01:00
parent 1b68b1a096
commit 9d37426764
32 changed files with 113 additions and 93 deletions

View File

@ -1,7 +1,7 @@
using System.Threading;
using System.Threading.Tasks;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public interface ICrawler
{

View File

@ -2,7 +2,7 @@
using Kyoo.Models.Watch;
using System.Collections.Generic;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public interface ILibraryManager
{

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public interface IMetadataProvider
{

View File

@ -0,0 +1,11 @@
using System.Collections.Generic;
namespace Kyoo.Controllers
{
public interface IPluginManager
{
public T GetPlugin<T>(string name);
public IEnumerable<T> GetPlugins<T>();
public void ReloadPlugins();
}
}

View File

@ -2,7 +2,7 @@
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Kyoo.InternalAPI.ThumbnailsManager
namespace Kyoo.Controllers.ThumbnailsManager
{
public interface IThumbnailsManager
{

View File

@ -2,7 +2,7 @@ using Kyoo.Models;
using Kyoo.Models.Watch;
using System.Threading.Tasks;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public interface ITranscoder
{

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Newtonsoft.Json;
using System.Collections.Generic;

View File

@ -0,0 +1,7 @@
namespace Kyoo.Models
{
public interface IPlugin
{
public string Name { get; }
}
}

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Newtonsoft.Json;
using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;

View File

@ -1,10 +0,0 @@
using System;
namespace Kyoo.InternalAPI.MetadataProvider
{
[AttributeUsage(AttributeTargets.Class)]
public class MetaProvider : Attribute
{
public MetaProvider() { }
}
}

View File

@ -1,6 +1,6 @@
using Kyoo.Models;
namespace Kyoo.InternalAPI.Utility
namespace Kyoo.Controllers.Utility
{
public static class ImageHelper
{

View File

@ -1,6 +1,6 @@
using System.Text.RegularExpressions;
namespace Kyoo.InternalAPI.Utility
namespace Kyoo.Controllers.Utility
{
public class Slugifier
{

View File

@ -1,5 +1,4 @@
using Kyoo.InternalAPI.Utility;
using Kyoo.Models;
using Kyoo.Models;
using Microsoft.Extensions.Configuration;
using System.Collections.Generic;
using System.Diagnostics;
@ -8,9 +7,10 @@ using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Kyoo.Controllers.Utility;
using Kyoo.Models.Watch;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public class Crawler : ICrawler
{

View File

@ -7,7 +7,7 @@ using System.Data.SQLite;
using System.Diagnostics;
using System.IO;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public class LibraryManager : ILibraryManager
{

View File

@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using Kyoo.Models;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace Kyoo.Controllers
{
public class PluginManager : IPluginManager
{
private readonly IServiceProvider provider;
private readonly IConfiguration config;
private List<IPlugin> plugins;
public PluginManager(IServiceProvider provider, IConfiguration config)
{
this.provider = provider;
this.config = config;
ReloadPlugins();
}
public T GetPlugin<T>(string name)
{
return (T)(from plugin in plugins where plugin.Name == name && plugin is T
select plugin).FirstOrDefault();
}
public IEnumerable<T> GetPlugins<T>()
{
return from plugin in plugins where plugin is T
select (T)plugin;
}
public void ReloadPlugins()
{
string pluginFolder = config.GetValue<string>("plugins");
if (!Directory.Exists(pluginFolder))
return;
string[] pluginsPaths = Directory.GetFiles(pluginFolder);
plugins = pluginsPaths.Select(path =>
{
Assembly ass = Assembly.LoadFile(path);
return (from type in ass.GetTypes()
where typeof(IPlugin).IsAssignableFrom(type)
select (IPlugin)ActivatorUtilities.CreateInstance(provider, type, null)).FirstOrDefault();
}).Where(x => x != null).ToList();
}
}
}

View File

@ -1,6 +1,4 @@
using Kyoo.InternalAPI.MetadataProvider;
using Kyoo.InternalAPI.ThumbnailsManager;
using Kyoo.Models;
using Kyoo.Models;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
@ -8,56 +6,21 @@ using System.IO;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Kyoo.Controllers.ThumbnailsManager;
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public class ProviderManager : IMetadataProvider
{
private readonly List<IMetadataProvider> providers = new List<IMetadataProvider>();
private readonly IEnumerable<IMetadataProvider> providers;
private readonly IThumbnailsManager thumbnailsManager;
private readonly IConfiguration config;
public ProviderManager(IThumbnailsManager thumbnailsManager, IConfiguration configuration)
public ProviderManager(IThumbnailsManager thumbnailsManager, IPluginManager pluginManager, IConfiguration config)
{
this.thumbnailsManager = thumbnailsManager;
config = configuration;
LoadProviders();
}
void LoadProviders()
{
providers.Clear();
string pluginFolder = config.GetValue<string>("providerPlugins");
if (Directory.Exists(pluginFolder))
{
string[] pluginsPaths = Directory.GetFiles(pluginFolder);
List<Assembly> plugins = new List<Assembly>();
List<Type> types = new List<Type>();
for (int i = 0; i < pluginsPaths.Length; i++)
{
plugins.Add(Assembly.LoadFile(pluginsPaths[i]));
types.AddRange(plugins[i].GetTypes());
}
List<Type> providersPlugins = types.FindAll(x =>
{
object[] atr = x.GetCustomAttributes(typeof(MetaProvider), false);
if (atr == null || atr.Length == 0)
return false;
List<Type> interfaces = new List<Type>(x.GetInterfaces());
if (interfaces.Contains(typeof(IMetadataProvider)))
return true;
return false;
});
providers.AddRange(providersPlugins.ConvertAll<IMetadataProvider>(x => Activator.CreateInstance(x) as IMetadataProvider));
}
this.config = config;
providers = pluginManager.GetPlugins<IMetadataProvider>();
}
public Show Merge(IEnumerable<Show> shows)

View File

@ -7,7 +7,7 @@ using System.IO;
using System.Net;
using System.Threading.Tasks;
namespace Kyoo.InternalAPI.ThumbnailsManager
namespace Kyoo.Controllers.ThumbnailsManager
{
public class ThumbnailsManager : IThumbnailsManager
{

View File

@ -1,14 +1,15 @@
using System;
using Kyoo.Models;
using Kyoo.InternalAPI.TranscoderLink;
using Microsoft.Extensions.Configuration;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Kyoo.Controllers.TranscoderLink;
#pragma warning disable 4014
namespace Kyoo.InternalAPI
namespace Kyoo.Controllers
{
public class Transcoder : ITranscoder
{

View File

@ -5,7 +5,7 @@ using Kyoo.Models;
using Kyoo.Models.Watch;
// ReSharper disable InconsistentNaming
namespace Kyoo.InternalAPI.TranscoderLink
namespace Kyoo.Controllers.TranscoderLink
{
public static class TranscoderAPI
{

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;

View File

@ -1,8 +1,6 @@
using Kyoo.InternalAPI;
using Kyoo.Models;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Threading.Tasks;

View File

@ -1,11 +1,10 @@
using Kyoo.InternalAPI;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using System.IO;
namespace Kyoo.Controllers
{
public class ThumbnailController : Controller
public class ThumbnailController : ControllerBase
{
private readonly ILibraryManager libraryManager;
private readonly string peoplePath;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;

View File

@ -1,4 +1,4 @@
using Kyoo.InternalAPI;
using Kyoo.Controllers;
using Kyoo.Models;
using Microsoft.AspNetCore.Mvc;
using System.Diagnostics;

View File

@ -1,5 +1,5 @@
using Kyoo.InternalAPI;
using Kyoo.InternalAPI.ThumbnailsManager;
using Kyoo.Controllers;
using Kyoo.Controllers.ThumbnailsManager;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.SpaServices.AngularCli;
@ -30,12 +30,9 @@ namespace Kyoo
});
services.AddControllers().AddNewtonsoftJson();
//Services needed in the private and in the public API
services.AddSingleton<ILibraryManager, LibraryManager>();
services.AddSingleton<ITranscoder, Transcoder>();
//Services used to get informations about files and register them
services.AddSingleton<ICrawler, Crawler>();
services.AddSingleton<IThumbnailsManager, ThumbnailsManager>();
services.AddSingleton<IMetadataProvider, ProviderManager>();