aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Plugins
diff options
context:
space:
mode:
Diffstat (limited to 'MediaBrowser.Common/Plugins')
-rw-r--r--MediaBrowser.Common/Plugins/BasePlugin.cs215
-rw-r--r--MediaBrowser.Common/Plugins/BasePluginOfT.cs205
-rw-r--r--MediaBrowser.Common/Plugins/IHasPluginConfiguration.cs27
-rw-r--r--MediaBrowser.Common/Plugins/IPlugin.cs52
-rw-r--r--MediaBrowser.Common/Plugins/IPluginAssembly.cs13
-rw-r--r--MediaBrowser.Common/Plugins/IPluginManager.cs96
-rw-r--r--MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs19
-rw-r--r--MediaBrowser.Common/Plugins/LocalPlugin.cs139
-rw-r--r--MediaBrowser.Common/Plugins/PluginManifest.cs108
9 files changed, 651 insertions, 223 deletions
diff --git a/MediaBrowser.Common/Plugins/BasePlugin.cs b/MediaBrowser.Common/Plugins/BasePlugin.cs
index 1ff2e98ef..8972089a8 100644
--- a/MediaBrowser.Common/Plugins/BasePlugin.cs
+++ b/MediaBrowser.Common/Plugins/BasePlugin.cs
@@ -1,15 +1,19 @@
+#nullable disable
+
using System;
using System.IO;
-using MediaBrowser.Common.Configuration;
+using System.Reflection;
using MediaBrowser.Model.Plugins;
-using MediaBrowser.Model.Serialization;
namespace MediaBrowser.Common.Plugins
{
+ /// <summary>
+ /// Provides a common base class for all plugins.
+ /// </summary>
public abstract class BasePlugin : IPlugin, IPluginAssembly
{
/// <summary>
- /// Gets the name of the plugin
+ /// Gets the name of the plugin.
/// </summary>
/// <value>The name.</value>
public abstract string Name { get; }
@@ -27,42 +31,53 @@ namespace MediaBrowser.Common.Plugins
public virtual Guid Id { get; private set; }
/// <summary>
- /// Gets the plugin version
+ /// Gets the plugin version.
/// </summary>
/// <value>The version.</value>
public Version Version { get; private set; }
/// <summary>
- /// Gets the path to the assembly file
+ /// Gets the path to the assembly file.
/// </summary>
/// <value>The assembly file path.</value>
public string AssemblyFilePath { get; private set; }
/// <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>
+ public string DataFolderPath { get; private set; }
+
+ /// <summary>
+ /// Gets a value indicating whether the plugin can be uninstalled.
+ /// </summary>
+ public bool CanUninstall => !Path.GetDirectoryName(AssemblyFilePath)
+ .Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), StringComparison.Ordinal);
+
+ /// <summary>
/// Gets the plugin info.
/// </summary>
/// <returns>PluginInfo.</returns>
public virtual PluginInfo GetPluginInfo()
{
- var info = new PluginInfo
- {
- Name = Name,
- Version = Version.ToString(),
- Description = Description,
- Id = Id.ToString()
- };
+ var info = new PluginInfo(
+ Name,
+ Version,
+ Description,
+ Id,
+ CanUninstall);
return info;
}
/// <summary>
- /// Called when just before the plugin is uninstalled from the server.
+ /// Called just before the plugin is uninstalled from the server.
/// </summary>
public virtual void OnUninstalling()
{
-
}
+ /// <inheritdoc />
public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
{
AssemblyFilePath = assemblyFilePath;
@@ -70,180 +85,10 @@ namespace MediaBrowser.Common.Plugins
Version = assemblyVersion;
}
+ /// <inheritdoc />
public void SetId(Guid assemblyId)
{
Id = assemblyId;
}
-
- /// <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>
- public string DataFolderPath { get; private set; }
- }
-
- /// <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>
- /// 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);
-
- private Action<string> _directoryCreateFn;
- public void SetStartupInfo(Action<string> directoryCreateFn)
- {
- // hack alert, until the .net core transition is complete
- _directoryCreateFn = directoryCreateFn;
- }
-
- /// <summary>
- /// Gets the name the assembly file
- /// </summary>
- /// <value>The name of the assembly file.</value>
- protected string AssemblyFileName => Path.GetFileName(AssemblyFilePath);
-
- /// <summary>
- /// The _configuration sync lock
- /// </summary>
- private readonly object _configurationSyncLock = new object();
- /// <summary>
- /// The _configuration
- /// </summary>
- private TConfigurationType _configuration;
- /// <summary>
- /// Gets the plugin's 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;
- }
-
- private TConfigurationType LoadConfiguration()
- {
- var path = ConfigurationFilePath;
-
- try
- {
- return (TConfigurationType)XmlSerializer.DeserializeFromFile(typeof(TConfigurationType), path);
- }
- catch
- {
- return (TConfigurationType)Activator.CreateInstance(typeof(TConfigurationType));
- }
- }
-
- /// <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>
- /// 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;
- }
-
- /// <summary>
- /// The _save lock
- /// </summary>
- private readonly object _configurationSaveLock = new object();
-
- /// <summary>
- /// Saves the current configuration to the file system
- /// </summary>
- public virtual void SaveConfiguration()
- {
- lock (_configurationSaveLock)
- {
- _directoryCreateFn(Path.GetDirectoryName(ConfigurationFilePath));
-
- XmlSerializer.SerializeToFile(Configuration, ConfigurationFilePath);
- }
- }
-
- /// <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">configuration</exception>
- public virtual void UpdateConfiguration(BasePluginConfiguration configuration)
- {
- if (configuration == null)
- {
- throw new ArgumentNullException(nameof(configuration));
- }
-
- Configuration = (TConfigurationType)configuration;
-
- SaveConfiguration();
- }
-
- /// <summary>
- /// Gets the plugin's configuration
- /// </summary>
- /// <value>The configuration.</value>
- BasePluginConfiguration IHasPluginConfiguration.Configuration => Configuration;
-
- public override PluginInfo GetPluginInfo()
- {
- var info = base.GetPluginInfo();
-
- info.ConfigurationFileName = ConfigurationFileName;
-
- return info;
- }
- }
-
- public interface IPluginAssembly
- {
- void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion);
- void SetId(Guid assemblyId);
}
}
diff --git a/MediaBrowser.Common/Plugins/BasePluginOfT.cs b/MediaBrowser.Common/Plugins/BasePluginOfT.cs
new file mode 100644
index 000000000..afda83a7c
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/BasePluginOfT.cs
@@ -0,0 +1,205 @@
+#nullable disable
+#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;
+
+ var assembly = GetType().Assembly;
+ var assemblyName = assembly.GetName();
+ var assemblyFilePath = assembly.Location;
+
+ var dataFolderPath = Path.Combine(ApplicationPaths.PluginsPath, Path.GetFileNameWithoutExtension(assemblyFilePath));
+ if (Version != null && !Directory.Exists(dataFolderPath))
+ {
+ // Try again with the version number appended to the folder name.
+ dataFolderPath += "_" + Version.ToString();
+ }
+
+ 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);
+
+ 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)
+ {
+ _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;
+ }
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Plugins/IHasPluginConfiguration.cs b/MediaBrowser.Common/Plugins/IHasPluginConfiguration.cs
new file mode 100644
index 000000000..af9272caa
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/IHasPluginConfiguration.cs
@@ -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);
+ }
+}
diff --git a/MediaBrowser.Common/Plugins/IPlugin.cs b/MediaBrowser.Common/Plugins/IPlugin.cs
index 32527c299..01e0a536d 100644
--- a/MediaBrowser.Common/Plugins/IPlugin.cs
+++ b/MediaBrowser.Common/Plugins/IPlugin.cs
@@ -1,51 +1,52 @@
+#nullable disable
+
using System;
using MediaBrowser.Model.Plugins;
namespace MediaBrowser.Common.Plugins
{
/// <summary>
- /// Interface IPlugin
+ /// Defines the <see cref="IPlugin" />.
/// </summary>
public interface IPlugin
{
/// <summary>
- /// Gets the name of the plugin
+ /// 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
+ /// Gets the plugin version.
/// </summary>
- /// <value>The version.</value>
Version Version { get; }
/// <summary>
- /// Gets the path to the assembly file
+ /// Gets the path to the assembly file.
/// </summary>
- /// <value>The assembly file path.</value>
string AssemblyFilePath { get; }
/// <summary>
- /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed
+ /// Gets a value indicating whether the plugin can be uninstalled.
+ /// </summary>
+ bool CanUninstall { get; }
+
+ /// <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();
@@ -55,29 +56,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>
- /// 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">configuration</exception>
- void UpdateConfiguration(BasePluginConfiguration configuration);
-
- /// <summary>
- /// Gets the plugin's configuration
- /// </summary>
- /// <value>The configuration.</value>
- BasePluginConfiguration Configuration { get; }
-
- void SetStartupInfo(Action<string> directoryCreateFn);
- }
}
diff --git a/MediaBrowser.Common/Plugins/IPluginAssembly.cs b/MediaBrowser.Common/Plugins/IPluginAssembly.cs
new file mode 100644
index 000000000..6df4fbb76
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/IPluginAssembly.cs
@@ -0,0 +1,13 @@
+#pragma warning disable CS1591
+
+using System;
+
+namespace MediaBrowser.Common.Plugins
+{
+ public interface IPluginAssembly
+ {
+ void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion);
+
+ void SetId(Guid assemblyId);
+ }
+}
diff --git a/MediaBrowser.Common/Plugins/IPluginManager.cs b/MediaBrowser.Common/Plugins/IPluginManager.cs
new file mode 100644
index 000000000..176bcbbd5
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/IPluginManager.cs
@@ -0,0 +1,96 @@
+using System;
+using System.Collections.Generic;
+using System.Reflection;
+using System.Threading.Tasks;
+using MediaBrowser.Model.Plugins;
+using MediaBrowser.Model.Updates;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace MediaBrowser.Common.Plugins
+{
+ /// <summary>
+ /// Defines the <see cref="IPluginManager" />.
+ /// </summary>
+ public interface IPluginManager
+ {
+ /// <summary>
+ /// Gets the Plugins.
+ /// </summary>
+ IReadOnlyList<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>
+ /// Generates a manifest from repository data.
+ /// </summary>
+ /// <param name="packageInfo">The <see cref="PackageInfo"/> used to generate a manifest.</param>
+ /// <param name="version">Version to be installed.</param>
+ /// <param name="path">The path where to save the manifest.</param>
+ /// <param name="status">Initial status of the plugin.</param>
+ /// <returns>True if successful.</returns>
+ Task<bool> GenerateManifest(PackageInfo packageInfo, Version version, string path, PluginStatus status);
+
+ /// <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);
+ }
+}
diff --git a/MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs b/MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs
new file mode 100644
index 000000000..3afe874c5
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/IPluginServiceRegistrator.cs
@@ -0,0 +1,19 @@
+namespace MediaBrowser.Common.Plugins
+{
+ using Microsoft.Extensions.DependencyInjection;
+
+ /// <summary>
+ /// Defines the <see cref="IPluginServiceRegistrator" />.
+ /// </summary>
+ public interface IPluginServiceRegistrator
+ {
+ /// <summary>
+ /// Registers the plugin's services with the service collection.
+ /// </summary>
+ /// <remarks>
+ /// This interface is only used for service registration and requires a parameterless constructor.
+ /// </remarks>
+ /// <param name="serviceCollection">The service collection.</param>
+ void RegisterServices(IServiceCollection serviceCollection);
+ }
+}
diff --git a/MediaBrowser.Common/Plugins/LocalPlugin.cs b/MediaBrowser.Common/Plugins/LocalPlugin.cs
new file mode 100644
index 000000000..4c8e2d504
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/LocalPlugin.cs
@@ -0,0 +1,139 @@
+using System;
+using System.Collections.Generic;
+using MediaBrowser.Model.Plugins;
+
+namespace MediaBrowser.Common.Plugins
+{
+ /// <summary>
+ /// Local plugin class.
+ /// </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="path">The plugin path.</param>
+ /// <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)
+ {
+ Path = path;
+ DllFiles = Array.Empty<string>();
+ _supported = isSupported;
+ Manifest = manifest;
+ }
+
+ /// <summary>
+ /// Gets the plugin id.
+ /// </summary>
+ public Guid Id => Manifest.Id;
+
+ /// <summary>
+ /// Gets the plugin name.
+ /// </summary>
+ public string Name => Manifest.Name;
+
+ /// <summary>
+ /// Gets the plugin version.
+ /// </summary>
+ public Version Version
+ {
+ get
+ {
+ if (_version == null)
+ {
+ _version = Version.Parse(Manifest.Version);
+ }
+
+ return _version;
+ }
+ }
+
+ /// <summary>
+ /// Gets the plugin path.
+ /// </summary>
+ public string Path { get; }
+
+ /// <summary>
+ /// Gets or sets the list of dll files for this plugin.
+ /// </summary>
+ public IReadOnlyList<string> DllFiles { get; set; }
+
+ /// <summary>
+ /// Gets or sets the instance of this plugin.
+ /// </summary>
+ public IPlugin? Instance { get; set; }
+
+ /// <summary>
+ /// Gets a value indicating whether Jellyfin supports this version of the plugin, and it's enabled.
+ /// </summary>
+ 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"/>.
+ /// </summary>
+ /// <param name="a">The first item.</param>
+ /// <param name="b">The second item.</param>
+ /// <returns>Comparison result.</returns>
+ public static int Compare(LocalPlugin a, LocalPlugin b)
+ {
+ 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.Equals(b.Id) && compare == 0)
+ {
+ compare = a.Id.CompareTo(b.Id);
+ }
+
+ 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)
+ {
+ return obj is LocalPlugin other && this.Equals(other);
+ }
+
+ /// <inheritdoc />
+ public override int GetHashCode()
+ {
+ return Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
+ }
+
+ /// <inheritdoc />
+ public bool Equals(LocalPlugin? other)
+ {
+ if (other == null)
+ {
+ return false;
+ }
+
+ return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && Id.Equals(other.Id) && Version.Equals(other.Version);
+ }
+ }
+}
diff --git a/MediaBrowser.Common/Plugins/PluginManifest.cs b/MediaBrowser.Common/Plugins/PluginManifest.cs
new file mode 100644
index 000000000..2910dbe14
--- /dev/null
+++ b/MediaBrowser.Common/Plugins/PluginManifest.cs
@@ -0,0 +1,108 @@
+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; }
+ }
+}