mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-06-15 20:20:25 +01:00
Merge pull request #4709 from BaronGreenback/PluginDowngrade
This commit is contained in:
@@ -2,11 +2,16 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using MediaBrowser.Common.Plugins;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace MediaBrowser.Common
|
||||
{
|
||||
/// <summary>
|
||||
/// Delegate used with GetExports{T}.
|
||||
/// </summary>
|
||||
/// <param name="type">Type to create.</param>
|
||||
/// <returns>New instance of type <param>type</param>.</returns>
|
||||
public delegate object CreationDelegate(Type type);
|
||||
|
||||
/// <summary>
|
||||
/// An interface to be implemented by the applications hosting a kernel.
|
||||
/// </summary>
|
||||
@@ -53,6 +58,11 @@ namespace MediaBrowser.Common
|
||||
/// <value>The application version.</value>
|
||||
Version ApplicationVersion { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the service provider.
|
||||
/// </summary>
|
||||
IServiceProvider ServiceProvider { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the application version.
|
||||
/// </summary>
|
||||
@@ -71,12 +81,6 @@ namespace MediaBrowser.Common
|
||||
/// </summary>
|
||||
string ApplicationUserAgentAddress { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugins.
|
||||
/// </summary>
|
||||
/// <value>The plugins.</value>
|
||||
IReadOnlyList<IPlugin> Plugins { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets all plugin assemblies which implement a custom rest api.
|
||||
/// </summary>
|
||||
@@ -101,6 +105,22 @@ namespace MediaBrowser.Common
|
||||
/// <returns><see cref="IReadOnlyCollection{T}" />.</returns>
|
||||
IReadOnlyCollection<T> GetExports<T>(bool manageLifetime = true);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the exports.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type.</typeparam>
|
||||
/// <param name="defaultFunc">Delegate function that gets called to create the object.</param>
|
||||
/// <param name="manageLifetime">If set to <c>true</c> [manage lifetime].</param>
|
||||
/// <returns><see cref="IReadOnlyCollection{T}" />.</returns>
|
||||
IReadOnlyCollection<T> GetExports<T>(CreationDelegate defaultFunc, bool manageLifetime = true);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the export types.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type.</typeparam>
|
||||
/// <returns>IEnumerable{Type}.</returns>
|
||||
IEnumerable<Type> GetExportTypes<T>();
|
||||
|
||||
/// <summary>
|
||||
/// Resolves this instance.
|
||||
/// </summary>
|
||||
@@ -114,12 +134,6 @@ namespace MediaBrowser.Common
|
||||
/// <returns>A task.</returns>
|
||||
Task Shutdown();
|
||||
|
||||
/// <summary>
|
||||
/// Removes the plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugin">The plugin.</param>
|
||||
void RemovePlugin(IPlugin plugin);
|
||||
|
||||
/// <summary>
|
||||
/// Initializes this instance.
|
||||
/// </summary>
|
||||
|
||||
@@ -31,6 +31,7 @@ namespace MediaBrowser.Common.Json
|
||||
WriteIndented = false,
|
||||
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
||||
NumberHandling = JsonNumberHandling.AllowReadingFromString,
|
||||
PropertyNameCaseInsensitive = true,
|
||||
Converters =
|
||||
{
|
||||
new JsonGuidConverter(),
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
#pragma warning disable SA1402
|
||||
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
@@ -7,7 +5,6 @@ using System.Runtime.InteropServices;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
@@ -64,14 +61,12 @@ namespace MediaBrowser.Common.Plugins
|
||||
/// <returns>PluginInfo.</returns>
|
||||
public virtual PluginInfo GetPluginInfo()
|
||||
{
|
||||
var info = new PluginInfo
|
||||
{
|
||||
Name = Name,
|
||||
Version = Version.ToString(),
|
||||
Description = Description,
|
||||
Id = Id.ToString(),
|
||||
CanUninstall = CanUninstall
|
||||
};
|
||||
var info = new PluginInfo(
|
||||
Name,
|
||||
Version,
|
||||
Description,
|
||||
Id,
|
||||
CanUninstall);
|
||||
|
||||
return info;
|
||||
}
|
||||
@@ -97,207 +92,4 @@ namespace MediaBrowser.Common.Plugins
|
||||
Id = assemblyId;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Provides a common base class for all plugins.
|
||||
/// </summary>
|
||||
/// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
|
||||
public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
|
||||
where TConfigurationType : BasePluginConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// The configuration sync lock.
|
||||
/// </summary>
|
||||
private readonly object _configurationSyncLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// The configuration save lock.
|
||||
/// </summary>
|
||||
private readonly object _configurationSaveLock = new object();
|
||||
|
||||
private Action<string> _directoryCreateFn;
|
||||
|
||||
/// <summary>
|
||||
/// The configuration.
|
||||
/// </summary>
|
||||
private TConfigurationType _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="applicationPaths">The application paths.</param>
|
||||
/// <param name="xmlSerializer">The XML serializer.</param>
|
||||
protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
|
||||
{
|
||||
ApplicationPaths = applicationPaths;
|
||||
XmlSerializer = xmlSerializer;
|
||||
if (this is IPluginAssembly assemblyPlugin)
|
||||
{
|
||||
var assembly = GetType().Assembly;
|
||||
var assemblyName = assembly.GetName();
|
||||
var assemblyFilePath = assembly.Location;
|
||||
|
||||
var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
|
||||
|
||||
assemblyPlugin.SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
|
||||
|
||||
var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
|
||||
if (idAttributes.Length > 0)
|
||||
{
|
||||
var attribute = (GuidAttribute)idAttributes[0];
|
||||
var assemblyId = new Guid(attribute.Value);
|
||||
|
||||
assemblyPlugin.SetId(assemblyId);
|
||||
}
|
||||
}
|
||||
|
||||
if (this is IHasPluginConfiguration hasPluginConfiguration)
|
||||
{
|
||||
hasPluginConfiguration.SetStartupInfo(s => Directory.CreateDirectory(s));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the application paths.
|
||||
/// </summary>
|
||||
/// <value>The application paths.</value>
|
||||
protected IApplicationPaths ApplicationPaths { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the XML serializer.
|
||||
/// </summary>
|
||||
/// <value>The XML serializer.</value>
|
||||
protected IXmlSerializer XmlSerializer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of configuration this plugin uses.
|
||||
/// </summary>
|
||||
/// <value>The type of the configuration.</value>
|
||||
public Type ConfigurationType => typeof(TConfigurationType);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the event handler that is triggered when this configuration changes.
|
||||
/// </summary>
|
||||
public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name the assembly file.
|
||||
/// </summary>
|
||||
/// <value>The name of the assembly file.</value>
|
||||
protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the plugin configuration.
|
||||
/// </summary>
|
||||
/// <value>The configuration.</value>
|
||||
public TConfigurationType Configuration
|
||||
{
|
||||
get
|
||||
{
|
||||
// Lazy load
|
||||
if (_configuration == null)
|
||||
{
|
||||
lock (_configurationSyncLock)
|
||||
{
|
||||
if (_configuration == null)
|
||||
{
|
||||
_configuration = LoadConfiguration();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _configuration;
|
||||
}
|
||||
|
||||
protected set => _configuration = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the configuration file. Subclasses should override.
|
||||
/// </summary>
|
||||
/// <value>The name of the configuration file.</value>
|
||||
public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full path to the configuration file.
|
||||
/// </summary>
|
||||
/// <value>The configuration file path.</value>
|
||||
public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin configuration.
|
||||
/// </summary>
|
||||
/// <value>The configuration.</value>
|
||||
BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
|
||||
|
||||
/// <inheritdoc />
|
||||
public void SetStartupInfo(Action<string> directoryCreateFn)
|
||||
{
|
||||
// hack alert, until the .net core transition is complete
|
||||
_directoryCreateFn = directoryCreateFn;
|
||||
}
|
||||
|
||||
private TConfigurationType LoadConfiguration()
|
||||
{
|
||||
var path = ConfigurationFilePath;
|
||||
|
||||
try
|
||||
{
|
||||
return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
|
||||
}
|
||||
catch
|
||||
{
|
||||
var config = (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
|
||||
SaveConfiguration(config);
|
||||
return config;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the current configuration to the file system.
|
||||
/// </summary>
|
||||
/// <param name="config">Configuration to save.</param>
|
||||
public virtual void SaveConfiguration(TConfigurationType config)
|
||||
{
|
||||
lock (_configurationSaveLock)
|
||||
{
|
||||
_directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
|
||||
|
||||
XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the current configuration to the file system.
|
||||
/// </summary>
|
||||
public virtual void SaveConfiguration()
|
||||
{
|
||||
SaveConfiguration(Configuration);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
|
||||
{
|
||||
if (configuration == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configuration));
|
||||
}
|
||||
|
||||
Configuration = (TConfigurationType)configuration;
|
||||
|
||||
SaveConfiguration(Configuration);
|
||||
|
||||
ConfigurationChanged?.Invoke(this, configuration);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PluginInfo GetPluginInfo()
|
||||
{
|
||||
var info = base.GetPluginInfo();
|
||||
|
||||
info.ConfigurationFileName = ConfigurationFileName;
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
208
MediaBrowser.Common/Plugins/BasePluginOfT.cs
Normal file
208
MediaBrowser.Common/Plugins/BasePluginOfT.cs
Normal file
@@ -0,0 +1,208 @@
|
||||
#pragma warning disable SA1649 // File name should match first type name
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using MediaBrowser.Common.Configuration;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using MediaBrowser.Model.Serialization;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
/// <summary>
|
||||
/// Provides a common base class for all plugins.
|
||||
/// </summary>
|
||||
/// <typeparam name="TConfigurationType">The type of the T configuration type.</typeparam>
|
||||
public abstract class BasePlugin<TConfigurationType> : BasePlugin, IHasPluginConfiguration
|
||||
where TConfigurationType : BasePluginConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// The configuration sync lock.
|
||||
/// </summary>
|
||||
private readonly object _configurationSyncLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// The configuration save lock.
|
||||
/// </summary>
|
||||
private readonly object _configurationSaveLock = new object();
|
||||
|
||||
/// <summary>
|
||||
/// The configuration.
|
||||
/// </summary>
|
||||
private TConfigurationType _configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="BasePlugin{TConfigurationType}" /> class.
|
||||
/// </summary>
|
||||
/// <param name="applicationPaths">The application paths.</param>
|
||||
/// <param name="xmlSerializer">The XML serializer.</param>
|
||||
protected BasePlugin(IApplicationPaths applicationPaths, IXmlSerializer xmlSerializer)
|
||||
{
|
||||
ApplicationPaths = applicationPaths;
|
||||
XmlSerializer = xmlSerializer;
|
||||
if (this is IPluginAssembly assemblyPlugin)
|
||||
{
|
||||
var assembly = GetType().Assembly;
|
||||
var assemblyName = assembly.GetName();
|
||||
var assemblyFilePath = assembly.Location;
|
||||
|
||||
var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
|
||||
if (!Directory.Exists(dataFolderPath) && Version != null)
|
||||
{
|
||||
// Try again with the version number appended to the folder name.
|
||||
dataFolderPath = dataFolderPath + "_" + Version.ToString();
|
||||
}
|
||||
|
||||
assemblyPlugin.SetAttributes(assemblyFilePath, dataFolderPath, assemblyName.Version);
|
||||
|
||||
var idAttributes = assembly.GetCustomAttributes(typeof(GuidAttribute), true);
|
||||
if (idAttributes.Length > 0)
|
||||
{
|
||||
var attribute = (GuidAttribute)idAttributes[0];
|
||||
var assemblyId = new Guid(attribute.Value);
|
||||
|
||||
assemblyPlugin.SetId(assemblyId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the application paths.
|
||||
/// </summary>
|
||||
/// <value>The application paths.</value>
|
||||
protected IApplicationPaths ApplicationPaths { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the XML serializer.
|
||||
/// </summary>
|
||||
/// <value>The XML serializer.</value>
|
||||
protected IXmlSerializer XmlSerializer { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the type of configuration this plugin uses.
|
||||
/// </summary>
|
||||
/// <value>The type of the configuration.</value>
|
||||
public Type ConfigurationType => typeof(TConfigurationType);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the event handler that is triggered when this configuration changes.
|
||||
/// </summary>
|
||||
public EventHandler<BasePluginConfiguration> ConfigurationChanged { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name the assembly file.
|
||||
/// </summary>
|
||||
/// <value>The name of the assembly file.</value>
|
||||
protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the plugin configuration.
|
||||
/// </summary>
|
||||
/// <value>The configuration.</value>
|
||||
public TConfigurationType Configuration
|
||||
{
|
||||
get
|
||||
{
|
||||
// Lazy load
|
||||
if (_configuration == null)
|
||||
{
|
||||
lock (_configurationSyncLock)
|
||||
{
|
||||
if (_configuration == null)
|
||||
{
|
||||
_configuration = LoadConfiguration();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return _configuration;
|
||||
}
|
||||
|
||||
protected set => _configuration = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the name of the configuration file. Subclasses should override.
|
||||
/// </summary>
|
||||
/// <value>The name of the configuration file.</value>
|
||||
public virtual string ConfigurationFileName => Path.ChangeExtension(AssemblyFileName, ".xml");
|
||||
|
||||
/// <summary>
|
||||
/// Gets the full path to the configuration file.
|
||||
/// </summary>
|
||||
/// <value>The configuration file path.</value>
|
||||
public string ConfigurationFilePath => Path.Combine(ApplicationPaths.PluginConfigurationsPath, ConfigurationFileName);
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin configuration.
|
||||
/// </summary>
|
||||
/// <value>The configuration.</value>
|
||||
BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
|
||||
|
||||
/// <summary>
|
||||
/// Saves the current configuration to the file system.
|
||||
/// </summary>
|
||||
/// <param name="config">Configuration to save.</param>
|
||||
public virtual void SaveConfiguration(TConfigurationType config)
|
||||
{
|
||||
lock (_configurationSaveLock)
|
||||
{
|
||||
var folder = Path.GetDirectoryName(ConfigurationFilePath);
|
||||
if (!Directory.Exists(folder))
|
||||
{
|
||||
Directory.CreateDirectory(folder);
|
||||
}
|
||||
|
||||
XmlSerializer.SerializeToFile(config, ConfigurationFilePath);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Saves the current configuration to the file system.
|
||||
/// </summary>
|
||||
public virtual void SaveConfiguration()
|
||||
{
|
||||
SaveConfiguration(Configuration);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
|
||||
{
|
||||
if (configuration == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(configuration));
|
||||
}
|
||||
|
||||
Configuration = (TConfigurationType)configuration;
|
||||
|
||||
SaveConfiguration(Configuration);
|
||||
|
||||
ConfigurationChanged?.Invoke(this, configuration);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override PluginInfo GetPluginInfo()
|
||||
{
|
||||
var info = base.GetPluginInfo();
|
||||
|
||||
info.ConfigurationFileName = ConfigurationFileName;
|
||||
|
||||
return info;
|
||||
}
|
||||
|
||||
private TConfigurationType LoadConfiguration()
|
||||
{
|
||||
var path = ConfigurationFilePath;
|
||||
|
||||
try
|
||||
{
|
||||
return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
|
||||
}
|
||||
catch
|
||||
{
|
||||
var config = (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
|
||||
SaveConfiguration(config);
|
||||
return config;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
27
MediaBrowser.Common/Plugins/IHasPluginConfiguration.cs
Normal file
27
MediaBrowser.Common/Plugins/IHasPluginConfiguration.cs
Normal file
@@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IHasPluginConfiguration" />.
|
||||
/// </summary>
|
||||
public interface IHasPluginConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of configuration this plugin uses.
|
||||
/// </summary>
|
||||
Type ConfigurationType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin's configuration.
|
||||
/// </summary>
|
||||
BasePluginConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Completely overwrites the current configuration with a new copy.
|
||||
/// </summary>
|
||||
/// <param name="configuration">The configuration.</param>
|
||||
void UpdateConfiguration(BasePluginConfiguration configuration);
|
||||
}
|
||||
}
|
||||
@@ -1,44 +1,36 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
/// <summary>
|
||||
/// Interface IPlugin.
|
||||
/// Defines the <see cref="IPlugin" />.
|
||||
/// </summary>
|
||||
public interface IPlugin
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the name of the plugin.
|
||||
/// </summary>
|
||||
/// <value>The name.</value>
|
||||
string Name { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the description.
|
||||
/// Gets the Description.
|
||||
/// </summary>
|
||||
/// <value>The description.</value>
|
||||
string Description { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the unique id.
|
||||
/// </summary>
|
||||
/// <value>The unique id.</value>
|
||||
Guid Id { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin version.
|
||||
/// </summary>
|
||||
/// <value>The version.</value>
|
||||
Version Version { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the path to the assembly file.
|
||||
/// </summary>
|
||||
/// <value>The assembly file path.</value>
|
||||
string AssemblyFilePath { get; }
|
||||
|
||||
/// <summary>
|
||||
@@ -49,11 +41,10 @@ namespace MediaBrowser.Common.Plugins
|
||||
/// <summary>
|
||||
/// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed.
|
||||
/// </summary>
|
||||
/// <value>The data folder path.</value>
|
||||
string DataFolderPath { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin info.
|
||||
/// Gets the <see cref="PluginInfo"/>.
|
||||
/// </summary>
|
||||
/// <returns>PluginInfo.</returns>
|
||||
PluginInfo GetPluginInfo();
|
||||
@@ -63,29 +54,4 @@ namespace MediaBrowser.Common.Plugins
|
||||
/// </summary>
|
||||
void OnUninstalling();
|
||||
}
|
||||
|
||||
public interface IHasPluginConfiguration
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the type of configuration this plugin uses.
|
||||
/// </summary>
|
||||
/// <value>The type of the configuration.</value>
|
||||
Type ConfigurationType { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin's configuration.
|
||||
/// </summary>
|
||||
/// <value>The configuration.</value>
|
||||
BasePluginConfiguration Configuration { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Completely overwrites the current configuration with a new copy
|
||||
/// Returns true or false indicating success or failure.
|
||||
/// </summary>
|
||||
/// <param name="configuration">The configuration.</param>
|
||||
/// <exception cref="ArgumentNullException"><c>configuration</c> is <c>null</c>.</exception>
|
||||
void UpdateConfiguration(BasePluginConfiguration configuration);
|
||||
|
||||
void SetStartupInfo(Action<string> directoryCreateFn);
|
||||
}
|
||||
}
|
||||
|
||||
86
MediaBrowser.Common/Plugins/IPluginManager.cs
Normal file
86
MediaBrowser.Common/Plugins/IPluginManager.cs
Normal file
@@ -0,0 +1,86 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IPluginManager" />.
|
||||
/// </summary>
|
||||
public interface IPluginManager
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets the Plugins.
|
||||
/// </summary>
|
||||
IList<LocalPlugin> Plugins { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates the plugins.
|
||||
/// </summary>
|
||||
void CreatePlugins();
|
||||
|
||||
/// <summary>
|
||||
/// Returns all the assemblies.
|
||||
/// </summary>
|
||||
/// <returns>An IEnumerable{Assembly}.</returns>
|
||||
IEnumerable<Assembly> LoadAssemblies();
|
||||
|
||||
/// <summary>
|
||||
/// Registers the plugin's services with the DI.
|
||||
/// Note: DI is not yet instantiated yet.
|
||||
/// </summary>
|
||||
/// <param name="serviceCollection">A <see cref="ServiceCollection"/> instance.</param>
|
||||
void RegisterServices(IServiceCollection serviceCollection);
|
||||
|
||||
/// <summary>
|
||||
/// Saves the manifest back to disk.
|
||||
/// </summary>
|
||||
/// <param name="manifest">The <see cref="PluginManifest"/> to save.</param>
|
||||
/// <param name="path">The path where to save the manifest.</param>
|
||||
/// <returns>True if successful.</returns>
|
||||
bool SaveManifest(PluginManifest manifest, string path);
|
||||
|
||||
/// <summary>
|
||||
/// Imports plugin details from a folder.
|
||||
/// </summary>
|
||||
/// <param name="folder">Folder of the plugin.</param>
|
||||
void ImportPluginFrom(string folder);
|
||||
|
||||
/// <summary>
|
||||
/// Disable the plugin.
|
||||
/// </summary>
|
||||
/// <param name="assembly">The <see cref="Assembly"/> of the plug to disable.</param>
|
||||
void FailPlugin(Assembly assembly);
|
||||
|
||||
/// <summary>
|
||||
/// Disable the plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugin">The <see cref="LocalPlugin"/> of the plug to disable.</param>
|
||||
void DisablePlugin(LocalPlugin plugin);
|
||||
|
||||
/// <summary>
|
||||
/// Enables the plugin, disabling all other versions.
|
||||
/// </summary>
|
||||
/// <param name="plugin">The <see cref="LocalPlugin"/> of the plug to disable.</param>
|
||||
void EnablePlugin(LocalPlugin plugin);
|
||||
|
||||
/// <summary>
|
||||
/// Attempts to find the plugin with and id of <paramref name="id"/>.
|
||||
/// </summary>
|
||||
/// <param name="id">Id of plugin.</param>
|
||||
/// <param name="version">The version of the plugin to locate.</param>
|
||||
/// <returns>A <see cref="LocalPlugin"/> if located, or null if not.</returns>
|
||||
LocalPlugin? GetPlugin(Guid id, Version? version = null);
|
||||
|
||||
/// <summary>
|
||||
/// Removes the plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugin">The plugin.</param>
|
||||
/// <returns>Outcome of the operation.</returns>
|
||||
bool RemovePlugin(LocalPlugin plugin);
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
#nullable enable
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
@@ -9,36 +10,48 @@ namespace MediaBrowser.Common.Plugins
|
||||
/// </summary>
|
||||
public class LocalPlugin : IEquatable<LocalPlugin>
|
||||
{
|
||||
private readonly bool _supported;
|
||||
private Version? _version;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="LocalPlugin"/> class.
|
||||
/// </summary>
|
||||
/// <param name="id">The plugin id.</param>
|
||||
/// <param name="name">The plugin name.</param>
|
||||
/// <param name="version">The plugin version.</param>
|
||||
/// <param name="path">The plugin path.</param>
|
||||
public LocalPlugin(Guid id, string name, Version version, string path)
|
||||
/// <param name="isSupported"><b>True</b> if Jellyfin supports this version of the plugin.</param>
|
||||
/// <param name="manifest">The manifest record for this plugin, or null if one does not exist.</param>
|
||||
public LocalPlugin(string path, bool isSupported, PluginManifest manifest)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Version = version;
|
||||
Path = path;
|
||||
DllFiles = new List<string>();
|
||||
_supported = isSupported;
|
||||
Manifest = manifest;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin id.
|
||||
/// </summary>
|
||||
public Guid Id { get; }
|
||||
public Guid Id => Manifest.Id;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin name.
|
||||
/// </summary>
|
||||
public string Name { get; }
|
||||
public string Name => Manifest.Name;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin version.
|
||||
/// </summary>
|
||||
public Version Version { get; }
|
||||
public Version Version
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_version == null)
|
||||
{
|
||||
_version = Version.Parse(Manifest.Version);
|
||||
}
|
||||
|
||||
return _version;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin path.
|
||||
@@ -51,26 +64,19 @@ namespace MediaBrowser.Common.Plugins
|
||||
public List<string> DllFiles { get; }
|
||||
|
||||
/// <summary>
|
||||
/// == operator.
|
||||
/// Gets or sets the instance of this plugin.
|
||||
/// </summary>
|
||||
/// <param name="left">Left item.</param>
|
||||
/// <param name="right">Right item.</param>
|
||||
/// <returns>Comparison result.</returns>
|
||||
public static bool operator ==(LocalPlugin left, LocalPlugin right)
|
||||
{
|
||||
return left.Equals(right);
|
||||
}
|
||||
public IPlugin? Instance { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// != operator.
|
||||
/// Gets a value indicating whether Jellyfin supports this version of the plugin, and it's enabled.
|
||||
/// </summary>
|
||||
/// <param name="left">Left item.</param>
|
||||
/// <param name="right">Right item.</param>
|
||||
/// <returns>Comparison result.</returns>
|
||||
public static bool operator !=(LocalPlugin left, LocalPlugin right)
|
||||
{
|
||||
return !left.Equals(right);
|
||||
}
|
||||
public bool IsEnabledAndSupported => _supported && Manifest.Status >= PluginStatus.Active;
|
||||
|
||||
/// <summary>
|
||||
/// Gets a value indicating whether the plugin has a manifest.
|
||||
/// </summary>
|
||||
public PluginManifest Manifest { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Compare two <see cref="LocalPlugin"/>.
|
||||
@@ -80,10 +86,15 @@ namespace MediaBrowser.Common.Plugins
|
||||
/// <returns>Comparison result.</returns>
|
||||
public static int Compare(LocalPlugin a, LocalPlugin b)
|
||||
{
|
||||
var compare = string.Compare(a.Name, b.Name, true, CultureInfo.InvariantCulture);
|
||||
if (a == null || b == null)
|
||||
{
|
||||
throw new ArgumentNullException(a == null ? nameof(a) : nameof(b));
|
||||
}
|
||||
|
||||
var compare = string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
|
||||
|
||||
// Id is not equal but name is.
|
||||
if (a.Id != b.Id && compare == 0)
|
||||
if (!a.Id.Equals(b.Id) && compare == 0)
|
||||
{
|
||||
compare = a.Id.CompareTo(b.Id);
|
||||
}
|
||||
@@ -91,8 +102,20 @@ namespace MediaBrowser.Common.Plugins
|
||||
return compare == 0 ? a.Version.CompareTo(b.Version) : compare;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the plugin information.
|
||||
/// </summary>
|
||||
/// <returns>A <see cref="PluginInfo"/> instance containing the information.</returns>
|
||||
public PluginInfo GetPluginInfo()
|
||||
{
|
||||
var inst = Instance?.GetPluginInfo() ?? new PluginInfo(Manifest.Name, Version, Manifest.Description, Manifest.Id, true);
|
||||
inst.Status = Manifest.Status;
|
||||
inst.HasImage = !string.IsNullOrEmpty(Manifest.ImagePath);
|
||||
return inst;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override bool Equals(object obj)
|
||||
public override bool Equals(object? obj)
|
||||
{
|
||||
return obj is LocalPlugin other && this.Equals(other);
|
||||
}
|
||||
@@ -104,16 +127,14 @@ namespace MediaBrowser.Common.Plugins
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool Equals(LocalPlugin other)
|
||||
public bool Equals(LocalPlugin? other)
|
||||
{
|
||||
// Do not use == or != for comparison as this class overrides the operators.
|
||||
if (object.ReferenceEquals(other, null))
|
||||
if (other == null)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase)
|
||||
&& Id.Equals(other.Id);
|
||||
return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && Id.Equals(other.Id) && Version.Equals(other.Version);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
110
MediaBrowser.Common/Plugins/PluginManifest.cs
Normal file
110
MediaBrowser.Common/Plugins/PluginManifest.cs
Normal file
@@ -0,0 +1,110 @@
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Text.Json.Serialization;
|
||||
using MediaBrowser.Model.Plugins;
|
||||
|
||||
namespace MediaBrowser.Common.Plugins
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines a Plugin manifest file.
|
||||
/// </summary>
|
||||
public class PluginManifest
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="PluginManifest"/> class.
|
||||
/// </summary>
|
||||
public PluginManifest()
|
||||
{
|
||||
Category = string.Empty;
|
||||
Changelog = string.Empty;
|
||||
Description = string.Empty;
|
||||
Id = Guid.Empty;
|
||||
Name = string.Empty;
|
||||
Owner = string.Empty;
|
||||
Overview = string.Empty;
|
||||
TargetAbi = string.Empty;
|
||||
Version = string.Empty;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the category of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("category")]
|
||||
public string Category { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the changelog information.
|
||||
/// </summary>
|
||||
[JsonPropertyName("changelog")]
|
||||
public string Changelog { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the description of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("description")]
|
||||
public string Description { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Global Unique Identifier for the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("guid")]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Name of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("name")]
|
||||
public string Name { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets an overview of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("overview")]
|
||||
public string Overview { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the owner of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("owner")]
|
||||
public string Owner { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the compatibility version for the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("targetAbi")]
|
||||
public string TargetAbi { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the timestamp of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("timestamp")]
|
||||
public DateTime Timestamp { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the Version number of the plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("version")]
|
||||
public string Version { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating the operational status of this plugin.
|
||||
/// </summary>
|
||||
[JsonPropertyName("status")]
|
||||
public PluginStatus Status { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets a value indicating whether this plugin should automatically update.
|
||||
/// </summary>
|
||||
[JsonPropertyName("autoUpdate")]
|
||||
public bool AutoUpdate { get; set; } = true; // DO NOT MOVE THIS INTO THE CONSTRUCTOR.
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the ImagePath
|
||||
/// Gets or sets a value indicating whether this plugin has an image.
|
||||
/// Image must be located in the local plugin folder.
|
||||
/// </summary>
|
||||
[JsonPropertyName("imagePath")]
|
||||
public string? ImagePath { get; set; }
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
#pragma warning disable CS1591
|
||||
#nullable enable
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -9,6 +9,9 @@ using MediaBrowser.Model.Updates;
|
||||
|
||||
namespace MediaBrowser.Common.Updates
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="IInstallationManager" />.
|
||||
/// </summary>
|
||||
public interface IInstallationManager : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
@@ -21,12 +24,13 @@ namespace MediaBrowser.Common.Updates
|
||||
/// </summary>
|
||||
/// <param name="manifestName">Name of the repository.</param>
|
||||
/// <param name="manifest">The URL to query.</param>
|
||||
/// <param name="filterIncompatible">Filter out incompatible plugins.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task{IReadOnlyList{PackageInfo}}.</returns>
|
||||
Task<IList<PackageInfo>> GetPackages(string manifestName, string manifest, CancellationToken cancellationToken = default);
|
||||
Task<IList<PackageInfo>> GetPackages(string manifestName, string manifest, bool filterIncompatible, CancellationToken cancellationToken = default);
|
||||
|
||||
/// <summary>
|
||||
/// Gets all available packages.
|
||||
/// Gets all available packages that are supported by this version.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>Task{IReadOnlyList{PackageInfo}}.</returns>
|
||||
@@ -37,33 +41,33 @@ namespace MediaBrowser.Common.Updates
|
||||
/// </summary>
|
||||
/// <param name="availablePackages">The available packages.</param>
|
||||
/// <param name="name">The name of the plugin.</param>
|
||||
/// <param name="guid">The id of the plugin.</param>
|
||||
/// <param name="id">The id of the plugin.</param>
|
||||
/// <param name="specificVersion">The version of the plugin.</param>
|
||||
/// <returns>All plugins matching the requirements.</returns>
|
||||
IEnumerable<PackageInfo> FilterPackages(
|
||||
IEnumerable<PackageInfo> availablePackages,
|
||||
string name = null,
|
||||
Guid guid = default,
|
||||
Version specificVersion = null);
|
||||
string? name = null,
|
||||
Guid? id = default,
|
||||
Version? specificVersion = null);
|
||||
|
||||
/// <summary>
|
||||
/// Returns all compatible versions ordered from newest to oldest.
|
||||
/// </summary>
|
||||
/// <param name="availablePackages">The available packages.</param>
|
||||
/// <param name="name">The name.</param>
|
||||
/// <param name="guid">The guid of the plugin.</param>
|
||||
/// <param name="id">The id of the plugin.</param>
|
||||
/// <param name="minVersion">The minimum required version of the plugin.</param>
|
||||
/// <param name="specificVersion">The specific version of the plugin to install.</param>
|
||||
/// <returns>All compatible versions ordered from newest to oldest.</returns>
|
||||
IEnumerable<InstallationInfo> GetCompatibleVersions(
|
||||
IEnumerable<PackageInfo> availablePackages,
|
||||
string name = null,
|
||||
Guid guid = default,
|
||||
Version minVersion = null,
|
||||
Version specificVersion = null);
|
||||
string? name = null,
|
||||
Guid? id = default,
|
||||
Version? minVersion = null,
|
||||
Version? specificVersion = null);
|
||||
|
||||
/// <summary>
|
||||
/// Returns the available plugin updates.
|
||||
/// Returns the available compatible plugin updates.
|
||||
/// </summary>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>The available plugin updates.</returns>
|
||||
@@ -81,7 +85,7 @@ namespace MediaBrowser.Common.Updates
|
||||
/// Uninstalls a plugin.
|
||||
/// </summary>
|
||||
/// <param name="plugin">The plugin.</param>
|
||||
void UninstallPlugin(IPlugin plugin);
|
||||
void UninstallPlugin(LocalPlugin plugin);
|
||||
|
||||
/// <summary>
|
||||
/// Cancels the installation.
|
||||
|
||||
@@ -1,14 +1,21 @@
|
||||
#pragma warning disable CS1591
|
||||
|
||||
using System;
|
||||
using MediaBrowser.Model.Updates;
|
||||
|
||||
namespace MediaBrowser.Common.Updates
|
||||
{
|
||||
/// <summary>
|
||||
/// Defines the <see cref="InstallationEventArgs" />.
|
||||
/// </summary>
|
||||
public class InstallationEventArgs : EventArgs
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="InstallationInfo"/>.
|
||||
/// </summary>
|
||||
public InstallationInfo InstallationInfo { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the <see cref="VersionInfo"/>.
|
||||
/// </summary>
|
||||
public VersionInfo VersionInfo { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user