mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-27 01:50:53 +01:00
Initial check-in
This commit is contained in:
58
MediaBrowser.Common/Plugins/BasePlugin.cs
Normal file
58
MediaBrowser.Common/Plugins/BasePlugin.cs
Normal file
@@ -0,0 +1,58 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using MediaBrowser.Common.Json;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
public abstract class BasePlugin<TConfigurationType> : IDisposable, IPlugin
|
||||
where TConfigurationType : BasePluginConfiguration, new()
|
||||
{
|
||||
public string Path { get; set; }
|
||||
public TConfigurationType Configuration { get; private set; }
|
||||
|
||||
private string ConfigurationPath
|
||||
{
|
||||
get
|
||||
{
|
||||
return System.IO.Path.Combine(Path, "config.js");
|
||||
}
|
||||
}
|
||||
|
||||
public void Init()
|
||||
{
|
||||
Configuration = GetConfiguration();
|
||||
|
||||
if (Configuration.Enabled)
|
||||
{
|
||||
InitInternal();
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract void InitInternal();
|
||||
|
||||
public virtual void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
private TConfigurationType GetConfiguration()
|
||||
{
|
||||
if (!File.Exists(ConfigurationPath))
|
||||
{
|
||||
return new TConfigurationType();
|
||||
}
|
||||
|
||||
return JsonSerializer.Deserialize<TConfigurationType>(ConfigurationPath);
|
||||
}
|
||||
}
|
||||
|
||||
public interface IPlugin
|
||||
{
|
||||
string Path { get; set; }
|
||||
|
||||
void Init();
|
||||
void Dispose();
|
||||
}
|
||||
}
|
||||
18
MediaBrowser.Common/Plugins/BasePluginConfiguration.cs
Normal file
18
MediaBrowser.Common/Plugins/BasePluginConfiguration.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
public class BasePluginConfiguration
|
||||
{
|
||||
public bool Enabled { get; set; }
|
||||
|
||||
public BasePluginConfiguration()
|
||||
{
|
||||
Enabled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
90
MediaBrowser.Common/Plugins/PluginController.cs
Normal file
90
MediaBrowser.Common/Plugins/PluginController.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
public class PluginController
|
||||
{
|
||||
public string PluginsPath { get; set; }
|
||||
|
||||
public PluginController(string pluginFolderPath)
|
||||
{
|
||||
PluginsPath = pluginFolderPath;
|
||||
}
|
||||
|
||||
public IEnumerable<IPlugin> GetAllPlugins()
|
||||
{
|
||||
AppDomain.CurrentDomain.AssemblyResolve -= new ResolveEventHandler(CurrentDomain_AssemblyResolve);
|
||||
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
|
||||
|
||||
if (!Directory.Exists(PluginsPath))
|
||||
{
|
||||
Directory.CreateDirectory(PluginsPath);
|
||||
}
|
||||
|
||||
List<IPlugin> plugins = new List<IPlugin>();
|
||||
|
||||
foreach (string folder in Directory.GetDirectories(PluginsPath, "*", SearchOption.TopDirectoryOnly))
|
||||
{
|
||||
IPlugin plugin = GetPluginFromDirectory(folder);
|
||||
|
||||
plugin.Path = folder;
|
||||
|
||||
if (plugin != null)
|
||||
{
|
||||
plugins.Add(plugin);
|
||||
}
|
||||
}
|
||||
|
||||
return plugins;
|
||||
}
|
||||
|
||||
private IPlugin GetPluginFromDirectory(string path)
|
||||
{
|
||||
string dll = Directory.GetFiles(path, "*.dll", SearchOption.TopDirectoryOnly).FirstOrDefault();
|
||||
|
||||
if (!string.IsNullOrEmpty(dll))
|
||||
{
|
||||
return GetPluginFromDll(dll);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
private IPlugin GetPluginFromDll(string path)
|
||||
{
|
||||
return FindPlugin(Assembly.Load(File.ReadAllBytes(path)));
|
||||
}
|
||||
|
||||
private IPlugin FindPlugin(Assembly assembly)
|
||||
{
|
||||
var plugin = assembly.GetTypes().Where(type => typeof(IPlugin).IsAssignableFrom(type)).FirstOrDefault();
|
||||
|
||||
if (plugin != null)
|
||||
{
|
||||
return plugin.GetConstructor(Type.EmptyTypes).Invoke(null) as IPlugin;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
|
||||
{
|
||||
AssemblyName assemblyName = new AssemblyName(args.Name);
|
||||
|
||||
IEnumerable<string> dllPaths = Directory.GetFiles(PluginsPath, "*.dll", SearchOption.AllDirectories);
|
||||
|
||||
string dll = dllPaths.FirstOrDefault(f => Path.GetFileNameWithoutExtension(f) == assemblyName.Name);
|
||||
|
||||
if (!string.IsNullOrEmpty(dll))
|
||||
{
|
||||
return Assembly.Load(File.ReadAllBytes(dll));
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user