Merge pull request #4709 from BaronGreenback/PluginDowngrade
(cherry picked from commit 406ae3e43a
)
Signed-off-by: Joshua M. Boniface <joshua@boniface.me>
pull/5296/head
parent
83dd3e2201
commit
1ad8e54035
@ -0,0 +1,688 @@
|
|||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Text;
|
||||||
|
using System.Text.Json;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using MediaBrowser.Common;
|
||||||
|
using MediaBrowser.Common.Extensions;
|
||||||
|
using MediaBrowser.Common.Json;
|
||||||
|
using MediaBrowser.Common.Json.Converters;
|
||||||
|
using MediaBrowser.Common.Plugins;
|
||||||
|
using MediaBrowser.Model.Configuration;
|
||||||
|
using MediaBrowser.Model.Plugins;
|
||||||
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Extensions.Logging;
|
||||||
|
|
||||||
|
namespace Emby.Server.Implementations.Plugins
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the <see cref="PluginManager" />.
|
||||||
|
/// </summary>
|
||||||
|
public class PluginManager : IPluginManager
|
||||||
|
{
|
||||||
|
private readonly string _pluginsPath;
|
||||||
|
private readonly Version _appVersion;
|
||||||
|
private readonly JsonSerializerOptions _jsonOptions;
|
||||||
|
private readonly ILogger<PluginManager> _logger;
|
||||||
|
private readonly IApplicationHost _appHost;
|
||||||
|
private readonly ServerConfiguration _config;
|
||||||
|
private readonly IList<LocalPlugin> _plugins;
|
||||||
|
private readonly Version _minimumVersion;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="PluginManager"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The <see cref="ILogger"/>.</param>
|
||||||
|
/// <param name="appHost">The <see cref="IApplicationHost"/>.</param>
|
||||||
|
/// <param name="config">The <see cref="ServerConfiguration"/>.</param>
|
||||||
|
/// <param name="pluginsPath">The plugin path.</param>
|
||||||
|
/// <param name="appVersion">The application version.</param>
|
||||||
|
public PluginManager(
|
||||||
|
ILogger<PluginManager> logger,
|
||||||
|
IApplicationHost appHost,
|
||||||
|
ServerConfiguration config,
|
||||||
|
string pluginsPath,
|
||||||
|
Version appVersion)
|
||||||
|
{
|
||||||
|
_logger = logger ?? throw new ArgumentNullException(nameof(logger));
|
||||||
|
_pluginsPath = pluginsPath;
|
||||||
|
_appVersion = appVersion ?? throw new ArgumentNullException(nameof(appVersion));
|
||||||
|
_jsonOptions = new JsonSerializerOptions(JsonDefaults.GetOptions())
|
||||||
|
{
|
||||||
|
WriteIndented = true
|
||||||
|
};
|
||||||
|
|
||||||
|
// We need to use the default GUID converter, so we need to remove any custom ones.
|
||||||
|
for (int a = _jsonOptions.Converters.Count - 1; a >= 0; a--)
|
||||||
|
{
|
||||||
|
if (_jsonOptions.Converters[a] is JsonGuidConverter convertor)
|
||||||
|
{
|
||||||
|
_jsonOptions.Converters.Remove(convertor);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_config = config;
|
||||||
|
_appHost = appHost;
|
||||||
|
_minimumVersion = new Version(0, 0, 0, 1);
|
||||||
|
_plugins = Directory.Exists(_pluginsPath) ? DiscoverPlugins().ToList() : new List<LocalPlugin>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the Plugins.
|
||||||
|
/// </summary>
|
||||||
|
public IList<LocalPlugin> Plugins => _plugins;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns all the assemblies.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>An IEnumerable{Assembly}.</returns>
|
||||||
|
public IEnumerable<Assembly> LoadAssemblies()
|
||||||
|
{
|
||||||
|
// Attempt to remove any deleted plugins and change any successors to be active.
|
||||||
|
for (int i = _plugins.Count - 1; i >= 0; i--)
|
||||||
|
{
|
||||||
|
var plugin = _plugins[i];
|
||||||
|
if (plugin.Manifest.Status == PluginStatus.Deleted && DeletePlugin(plugin))
|
||||||
|
{
|
||||||
|
// See if there is another version, and if so make that active.
|
||||||
|
ProcessAlternative(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Now load the assemblies..
|
||||||
|
foreach (var plugin in _plugins)
|
||||||
|
{
|
||||||
|
UpdatePluginSuperceedStatus(plugin);
|
||||||
|
|
||||||
|
if (plugin.IsEnabledAndSupported == false)
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Skipping disabled plugin {Version} of {Name} ", plugin.Version, plugin.Name);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (var file in plugin.DllFiles)
|
||||||
|
{
|
||||||
|
Assembly assembly;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
assembly = Assembly.LoadFrom(file);
|
||||||
|
|
||||||
|
// This force loads all reference dll's that the plugin uses in the try..catch block.
|
||||||
|
// Removing this will cause JF to bomb out if referenced dll's cause issues.
|
||||||
|
assembly.GetExportedTypes();
|
||||||
|
}
|
||||||
|
catch (FileLoadException ex)
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Failed to load assembly {Path}. Disabling plugin.", file);
|
||||||
|
ChangePluginState(plugin, PluginStatus.Malfunctioned);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Loaded assembly {Assembly} from {Path}", assembly.FullName, file);
|
||||||
|
yield return assembly;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates all the plugin instances.
|
||||||
|
/// </summary>
|
||||||
|
public void CreatePlugins()
|
||||||
|
{
|
||||||
|
_ = _appHost.GetExports<IPlugin>(CreatePluginInstance)
|
||||||
|
.Where(i => i != null)
|
||||||
|
.ToArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public void RegisterServices(IServiceCollection serviceCollection)
|
||||||
|
{
|
||||||
|
foreach (var pluginServiceRegistrator in _appHost.GetExportTypes<IPluginServiceRegistrator>())
|
||||||
|
{
|
||||||
|
var plugin = GetPluginByAssembly(pluginServiceRegistrator.Assembly);
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
_logger.LogError("Unable to find plugin in assembly {Assembly}", pluginServiceRegistrator.Assembly.FullName);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
UpdatePluginSuperceedStatus(plugin);
|
||||||
|
if (!plugin.IsEnabledAndSupported)
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var instance = (IPluginServiceRegistrator?)Activator.CreateInstance(pluginServiceRegistrator);
|
||||||
|
instance?.RegisterServices(serviceCollection);
|
||||||
|
}
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch (Exception ex)
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error registering plugin services from {Assembly}.", pluginServiceRegistrator.Assembly.FullName);
|
||||||
|
if (ChangePluginState(plugin, PluginStatus.Malfunctioned))
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Disabling plugin {Path}", plugin.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Imports a plugin manifest from <paramref name="folder"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="folder">Folder of the plugin.</param>
|
||||||
|
public void ImportPluginFrom(string folder)
|
||||||
|
{
|
||||||
|
if (string.IsNullOrEmpty(folder))
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(folder));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Load the plugin.
|
||||||
|
var plugin = LoadManifest(folder);
|
||||||
|
// Make sure we haven't already loaded this.
|
||||||
|
if (_plugins.Any(p => p.Manifest.Equals(plugin.Manifest)))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_plugins.Add(plugin);
|
||||||
|
EnablePlugin(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes the plugin reference '<paramref name="plugin"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The plugin.</param>
|
||||||
|
/// <returns>Outcome of the operation.</returns>
|
||||||
|
public bool RemovePlugin(LocalPlugin plugin)
|
||||||
|
{
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(plugin));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (DeletePlugin(plugin))
|
||||||
|
{
|
||||||
|
ProcessAlternative(plugin);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogWarning("Unable to delete {Path}, so marking as deleteOnStartup.", plugin.Path);
|
||||||
|
// Unable to delete, so disable.
|
||||||
|
if (ChangePluginState(plugin, PluginStatus.Deleted))
|
||||||
|
{
|
||||||
|
ProcessAlternative(plugin);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to find the plugin with and id of <paramref name="id"/>.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="id">The <see cref="Guid"/> of plugin.</param>
|
||||||
|
/// <param name="version">Optional <see cref="Version"/> of the plugin to locate.</param>
|
||||||
|
/// <returns>A <see cref="LocalPlugin"/> if located, or null if not.</returns>
|
||||||
|
public LocalPlugin? GetPlugin(Guid id, Version? version = null)
|
||||||
|
{
|
||||||
|
LocalPlugin? plugin;
|
||||||
|
|
||||||
|
if (version == null)
|
||||||
|
{
|
||||||
|
// If no version is given, return the current instance.
|
||||||
|
var plugins = _plugins.Where(p => p.Id.Equals(id)).ToList();
|
||||||
|
|
||||||
|
plugin = plugins.FirstOrDefault(p => p.Instance != null);
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
plugin = plugins.OrderByDescending(p => p.Version).FirstOrDefault();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Match id and version number.
|
||||||
|
plugin = _plugins.FirstOrDefault(p => p.Id.Equals(id) && p.Version.Equals(version));
|
||||||
|
}
|
||||||
|
|
||||||
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Enables the plugin, disabling all other versions.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The <see cref="LocalPlugin"/> of the plug to disable.</param>
|
||||||
|
public void EnablePlugin(LocalPlugin plugin)
|
||||||
|
{
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(plugin));
|
||||||
|
}
|
||||||
|
|
||||||
|
if (ChangePluginState(plugin, PluginStatus.Active))
|
||||||
|
{
|
||||||
|
// See if there is another version, and if so, supercede it.
|
||||||
|
ProcessAlternative(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disable the plugin.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The <see cref="LocalPlugin"/> of the plug to disable.</param>
|
||||||
|
public void DisablePlugin(LocalPlugin plugin)
|
||||||
|
{
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(plugin));
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update the manifest on disk
|
||||||
|
if (ChangePluginState(plugin, PluginStatus.Disabled))
|
||||||
|
{
|
||||||
|
// If there is another version, activate it.
|
||||||
|
ProcessAlternative(plugin);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Disable the plugin.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assembly">The <see cref="Assembly"/> of the plug to disable.</param>
|
||||||
|
public void FailPlugin(Assembly assembly)
|
||||||
|
{
|
||||||
|
// Only save if disabled.
|
||||||
|
if (assembly == null)
|
||||||
|
{
|
||||||
|
throw new ArgumentNullException(nameof(assembly));
|
||||||
|
}
|
||||||
|
|
||||||
|
var plugin = _plugins.FirstOrDefault(p => p.DllFiles.Contains(assembly.Location));
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
// A plugin's assembly didn't cause this issue, so ignore it.
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangePluginState(plugin, PluginStatus.Malfunctioned);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <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>
|
||||||
|
public bool SaveManifest(PluginManifest manifest, string path)
|
||||||
|
{
|
||||||
|
if (manifest == null)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var data = JsonSerializer.Serialize(manifest, _jsonOptions);
|
||||||
|
File.WriteAllText(Path.Combine(path, "meta.json"), data, Encoding.UTF8);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch (Exception e)
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
|
{
|
||||||
|
_logger.LogWarning(e, "Unable to save plugin manifest. {Path}", path);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Changes a plugin's load status.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The <see cref="LocalPlugin"/> instance.</param>
|
||||||
|
/// <param name="state">The <see cref="PluginStatus"/> of the plugin.</param>
|
||||||
|
/// <returns>Success of the task.</returns>
|
||||||
|
private bool ChangePluginState(LocalPlugin plugin, PluginStatus state)
|
||||||
|
{
|
||||||
|
if (plugin.Manifest.Status == state || string.IsNullOrEmpty(plugin.Path))
|
||||||
|
{
|
||||||
|
// No need to save as the state hasn't changed.
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.Manifest.Status = state;
|
||||||
|
return SaveManifest(plugin.Manifest, plugin.Path);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Finds the plugin record using the assembly.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="assembly">The <see cref="Assembly"/> being sought.</param>
|
||||||
|
/// <returns>The matching record, or null if not found.</returns>
|
||||||
|
private LocalPlugin? GetPluginByAssembly(Assembly assembly)
|
||||||
|
{
|
||||||
|
// Find which plugin it is by the path.
|
||||||
|
return _plugins.FirstOrDefault(p => string.Equals(p.Path, Path.GetDirectoryName(assembly.Location), StringComparison.Ordinal));
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Creates the instance safe.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="type">The type.</param>
|
||||||
|
/// <returns>System.Object.</returns>
|
||||||
|
private IPlugin? CreatePluginInstance(Type type)
|
||||||
|
{
|
||||||
|
// Find the record for this plugin.
|
||||||
|
var plugin = GetPluginByAssembly(type.Assembly);
|
||||||
|
if (plugin?.Manifest.Status < PluginStatus.Active)
|
||||||
|
{
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Creating instance of {Type}", type);
|
||||||
|
var instance = (IPlugin)ActivatorUtilities.CreateInstance(_appHost.ServiceProvider, type);
|
||||||
|
if (plugin == null)
|
||||||
|
{
|
||||||
|
// Create a dummy record for the providers.
|
||||||
|
// TODO: remove this code, if all provided have been released as separate plugins.
|
||||||
|
plugin = new LocalPlugin(
|
||||||
|
instance.AssemblyFilePath,
|
||||||
|
true,
|
||||||
|
new PluginManifest
|
||||||
|
{
|
||||||
|
Id = instance.Id,
|
||||||
|
Status = PluginStatus.Active,
|
||||||
|
Name = instance.Name,
|
||||||
|
Version = instance.Version.ToString()
|
||||||
|
})
|
||||||
|
{
|
||||||
|
Instance = instance
|
||||||
|
};
|
||||||
|
|
||||||
|
_plugins.Add(plugin);
|
||||||
|
|
||||||
|
plugin.Manifest.Status = PluginStatus.Active;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
plugin.Instance = instance;
|
||||||
|
var manifest = plugin.Manifest;
|
||||||
|
var pluginStr = plugin.Instance.Version.ToString();
|
||||||
|
bool changed = false;
|
||||||
|
if (string.Equals(manifest.Version, pluginStr, StringComparison.Ordinal))
|
||||||
|
{
|
||||||
|
// If a plugin without a manifest failed to load due to an external issue (eg config),
|
||||||
|
// this updates the manifest to the actual plugin values.
|
||||||
|
manifest.Version = pluginStr;
|
||||||
|
manifest.Name = plugin.Instance.Name;
|
||||||
|
manifest.Description = plugin.Instance.Description;
|
||||||
|
changed = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
changed = changed || manifest.Status != PluginStatus.Active;
|
||||||
|
manifest.Status = PluginStatus.Active;
|
||||||
|
|
||||||
|
if (changed)
|
||||||
|
{
|
||||||
|
SaveManifest(manifest, plugin.Path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogInformation("Loaded plugin: {PluginName} {PluginVersion}", plugin.Name, plugin.Version);
|
||||||
|
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch (Exception ex)
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error creating {Type}", type.FullName);
|
||||||
|
if (plugin != null)
|
||||||
|
{
|
||||||
|
if (ChangePluginState(plugin, PluginStatus.Malfunctioned))
|
||||||
|
{
|
||||||
|
_logger.LogInformation("Plugin {Path} has been disabled.", plugin.Path);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_logger.LogDebug("Unable to auto-disable.");
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void UpdatePluginSuperceedStatus(LocalPlugin plugin)
|
||||||
|
{
|
||||||
|
if (plugin.Manifest.Status != PluginStatus.Superceded)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var predecessor = _plugins.OrderByDescending(p => p.Version)
|
||||||
|
.FirstOrDefault(p => p.Id.Equals(plugin.Id) && p.IsEnabledAndSupported && p.Version != plugin.Version);
|
||||||
|
if (predecessor != null)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
plugin.Manifest.Status = PluginStatus.Active;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempts to delete a plugin.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">A <see cref="LocalPlugin"/> instance to delete.</param>
|
||||||
|
/// <returns>True if successful.</returns>
|
||||||
|
private bool DeletePlugin(LocalPlugin plugin)
|
||||||
|
{
|
||||||
|
// Attempt a cleanup of old folders.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
Directory.Delete(plugin.Path, true);
|
||||||
|
_logger.LogDebug("Deleted {Path}", plugin.Path);
|
||||||
|
}
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return _plugins.Remove(plugin);
|
||||||
|
}
|
||||||
|
|
||||||
|
private LocalPlugin LoadManifest(string dir)
|
||||||
|
{
|
||||||
|
Version? version;
|
||||||
|
PluginManifest? manifest = null;
|
||||||
|
var metafile = Path.Combine(dir, "meta.json");
|
||||||
|
if (File.Exists(metafile))
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var data = File.ReadAllText(metafile, Encoding.UTF8);
|
||||||
|
manifest = JsonSerializer.Deserialize<PluginManifest>(data, _jsonOptions);
|
||||||
|
}
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch (Exception ex)
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
|
{
|
||||||
|
_logger.LogError(ex, "Error deserializing {Path}.", dir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (manifest != null)
|
||||||
|
{
|
||||||
|
if (!Version.TryParse(manifest.TargetAbi, out var targetAbi))
|
||||||
|
{
|
||||||
|
targetAbi = _minimumVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Version.TryParse(manifest.Version, out version))
|
||||||
|
{
|
||||||
|
manifest.Version = _minimumVersion.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
return new LocalPlugin(dir, _appVersion >= targetAbi, manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
// No metafile, so lets see if the folder is versioned.
|
||||||
|
// TODO: Phase this support out in future versions.
|
||||||
|
metafile = dir.Split(Path.DirectorySeparatorChar, StringSplitOptions.RemoveEmptyEntries)[^1];
|
||||||
|
int versionIndex = dir.LastIndexOf('_');
|
||||||
|
if (versionIndex != -1)
|
||||||
|
{
|
||||||
|
// Get the version number from the filename if possible.
|
||||||
|
metafile = Path.GetFileName(dir[..versionIndex]) ?? dir[..versionIndex];
|
||||||
|
version = Version.TryParse(dir.AsSpan()[(versionIndex + 1)..], out Version? parsedVersion) ? parsedVersion : _appVersion;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Un-versioned folder - Add it under the path name and version it suitable for this instance.
|
||||||
|
version = _appVersion;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Auto-create a plugin manifest, so we can disable it, if it fails to load.
|
||||||
|
manifest = new PluginManifest
|
||||||
|
{
|
||||||
|
Status = PluginStatus.Restart,
|
||||||
|
Name = metafile,
|
||||||
|
AutoUpdate = false,
|
||||||
|
Id = metafile.GetMD5(),
|
||||||
|
TargetAbi = _appVersion.ToString(),
|
||||||
|
Version = version.ToString()
|
||||||
|
};
|
||||||
|
|
||||||
|
return new LocalPlugin(dir, true, manifest);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets the list of local plugins.
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Enumerable of local plugins.</returns>
|
||||||
|
private IEnumerable<LocalPlugin> DiscoverPlugins()
|
||||||
|
{
|
||||||
|
var versions = new List<LocalPlugin>();
|
||||||
|
|
||||||
|
if (!Directory.Exists(_pluginsPath))
|
||||||
|
{
|
||||||
|
// Plugin path doesn't exist, don't try to enumerate sub-folders.
|
||||||
|
return Enumerable.Empty<LocalPlugin>();
|
||||||
|
}
|
||||||
|
|
||||||
|
var directories = Directory.EnumerateDirectories(_pluginsPath, "*.*", SearchOption.TopDirectoryOnly);
|
||||||
|
foreach (var dir in directories)
|
||||||
|
{
|
||||||
|
versions.Add(LoadManifest(dir));
|
||||||
|
}
|
||||||
|
|
||||||
|
string lastName = string.Empty;
|
||||||
|
versions.Sort(LocalPlugin.Compare);
|
||||||
|
// Traverse backwards through the list.
|
||||||
|
// The first item will be the latest version.
|
||||||
|
for (int x = versions.Count - 1; x >= 0; x--)
|
||||||
|
{
|
||||||
|
var entry = versions[x];
|
||||||
|
if (!string.Equals(lastName, entry.Name, StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
entry.DllFiles.AddRange(Directory.EnumerateFiles(entry.Path, "*.dll", SearchOption.AllDirectories));
|
||||||
|
if (entry.IsEnabledAndSupported)
|
||||||
|
{
|
||||||
|
lastName = entry.Name;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (string.IsNullOrEmpty(lastName))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
var manifest = entry.Manifest;
|
||||||
|
var cleaned = false;
|
||||||
|
var path = entry.Path;
|
||||||
|
if (_config.RemoveOldPlugins)
|
||||||
|
{
|
||||||
|
// Attempt a cleanup of old folders.
|
||||||
|
try
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Deleting {Path}", path);
|
||||||
|
Directory.Delete(path, true);
|
||||||
|
cleaned = true;
|
||||||
|
}
|
||||||
|
#pragma warning disable CA1031 // Do not catch general exception types
|
||||||
|
catch (Exception e)
|
||||||
|
#pragma warning restore CA1031 // Do not catch general exception types
|
||||||
|
{
|
||||||
|
_logger.LogWarning(e, "Unable to delete {Path}", path);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (cleaned)
|
||||||
|
{
|
||||||
|
versions.RemoveAt(x);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (manifest == null)
|
||||||
|
{
|
||||||
|
_logger.LogWarning("Unable to disable plugin {Path}", entry.Path);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
ChangePluginState(entry, PluginStatus.Deleted);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Only want plugin folders which have files.
|
||||||
|
return versions.Where(p => p.DllFiles.Count != 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Changes the status of the other versions of the plugin to "Superceded".
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="plugin">The <see cref="LocalPlugin"/> that's master.</param>
|
||||||
|
private void ProcessAlternative(LocalPlugin plugin)
|
||||||
|
{
|
||||||
|
// Detect whether there is another version of this plugin that needs disabling.
|
||||||
|
var previousVersion = _plugins.OrderByDescending(p => p.Version)
|
||||||
|
.FirstOrDefault(
|
||||||
|
p => p.Id.Equals(plugin.Id)
|
||||||
|
&& p.IsEnabledAndSupported
|
||||||
|
&& p.Version != plugin.Version);
|
||||||
|
|
||||||
|
if (previousVersion == null)
|
||||||
|
{
|
||||||
|
// This value is memory only - so that the web will show restart required.
|
||||||
|
plugin.Manifest.Status = PluginStatus.Restart;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (plugin.Manifest.Status == PluginStatus.Active && !ChangePluginState(previousVersion, PluginStatus.Superceded))
|
||||||
|
{
|
||||||
|
_logger.LogError("Unable to enable version {Version} of {Name}", previousVersion.Version, previousVersion.Name);
|
||||||
|
}
|
||||||
|
else if (plugin.Manifest.Status == PluginStatus.Superceded && !ChangePluginState(previousVersion, PluginStatus.Active))
|
||||||
|
{
|
||||||
|
_logger.LogError("Unable to supercede version {Version} of {Name}", previousVersion.Version, previousVersion.Name);
|
||||||
|
}
|
||||||
|
|
||||||
|
// This value is memory only - so that the web will show restart required.
|
||||||
|
plugin.Manifest.Status = PluginStatus.Restart;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,60 +0,0 @@
|
|||||||
using System;
|
|
||||||
|
|
||||||
namespace Emby.Server.Implementations.Plugins
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Defines a Plugin manifest file.
|
|
||||||
/// </summary>
|
|
||||||
public class PluginManifest
|
|
||||||
{
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the category of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string Category { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the changelog information.
|
|
||||||
/// </summary>
|
|
||||||
public string Changelog { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the description of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string Description { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the Global Unique Identifier for the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public Guid Guid { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the Name of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string Name { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets an overview of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string Overview { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the owner of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string Owner { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the compatibility version for the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string TargetAbi { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the timestamp of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public DateTime Timestamp { get; set; }
|
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// Gets or sets the Version number of the plugin.
|
|
||||||
/// </summary>
|
|
||||||
public string Version { get; set; }
|
|
||||||
}
|
|
||||||
}
|
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
@ -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,14 +1,21 @@
|
|||||||
#pragma warning disable CS1591
|
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
using MediaBrowser.Model.Updates;
|
using MediaBrowser.Model.Updates;
|
||||||
|
|
||||||
namespace MediaBrowser.Common.Updates
|
namespace MediaBrowser.Common.Updates
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the <see cref="InstallationEventArgs" />.
|
||||||
|
/// </summary>
|
||||||
public class InstallationEventArgs : EventArgs
|
public class InstallationEventArgs : EventArgs
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="InstallationInfo"/>.
|
||||||
|
/// </summary>
|
||||||
public InstallationInfo InstallationInfo { get; set; }
|
public InstallationInfo InstallationInfo { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the <see cref="VersionInfo"/>.
|
||||||
|
/// </summary>
|
||||||
public VersionInfo VersionInfo { get; set; }
|
public VersionInfo VersionInfo { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,40 @@
|
|||||||
#nullable disable
|
#nullable enable
|
||||||
#pragma warning disable CS1591
|
|
||||||
|
|
||||||
namespace MediaBrowser.Model.Plugins
|
namespace MediaBrowser.Model.Plugins
|
||||||
{
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Defines the <see cref="PluginPageInfo" />.
|
||||||
|
/// </summary>
|
||||||
public class PluginPageInfo
|
public class PluginPageInfo
|
||||||
{
|
{
|
||||||
public string Name { get; set; }
|
/// <summary>
|
||||||
|
/// Gets or sets the name.
|
||||||
|
/// </summary>
|
||||||
|
public string Name { get; set; } = string.Empty;
|
||||||
|
|
||||||
public string DisplayName { get; set; }
|
/// <summary>
|
||||||
|
/// Gets or sets the display name.
|
||||||
|
/// </summary>
|
||||||
|
public string? DisplayName { get; set; }
|
||||||
|
|
||||||
public string EmbeddedResourcePath { get; set; }
|
/// <summary>
|
||||||
|
/// Gets or sets the resource path.
|
||||||
|
/// </summary>
|
||||||
|
public string EmbeddedResourcePath { get; set; } = string.Empty;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets a value indicating whether this plugin should appear in the main menu.
|
||||||
|
/// </summary>
|
||||||
public bool EnableInMainMenu { get; set; }
|
public bool EnableInMainMenu { get; set; }
|
||||||
|
|
||||||
public string MenuSection { get; set; }
|
/// <summary>
|
||||||
|
/// Gets or sets the menu section.
|
||||||
|
/// </summary>
|
||||||
|
public string? MenuSection { get; set; }
|
||||||
|
|
||||||
public string MenuIcon { get; set; }
|
/// <summary>
|
||||||
|
/// Gets or sets the menu icon.
|
||||||
|
/// </summary>
|
||||||
|
public string? MenuIcon { get; set; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,47 @@
|
|||||||
|
namespace MediaBrowser.Model.Plugins
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Plugin load status.
|
||||||
|
/// </summary>
|
||||||
|
public enum PluginStatus
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// This plugin requires a restart in order for it to load. This is a memory only status.
|
||||||
|
/// The actual status of the plugin after reload is present in the manifest.
|
||||||
|
/// eg. A disabled plugin will still be active until the next restart, and so will have a memory status of Restart,
|
||||||
|
/// but a disk manifest status of Disabled.
|
||||||
|
/// </summary>
|
||||||
|
Restart = 1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This plugin is currently running.
|
||||||
|
/// </summary>
|
||||||
|
Active = 0,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This plugin has been marked as disabled.
|
||||||
|
/// </summary>
|
||||||
|
Disabled = -1,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This plugin does not meet the TargetAbi requirements.
|
||||||
|
/// </summary>
|
||||||
|
NotSupported = -2,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This plugin caused an error when instantiated. (Either DI loop, or exception)
|
||||||
|
/// </summary>
|
||||||
|
Malfunctioned = -3,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// This plugin has been superceded by another version.
|
||||||
|
/// </summary>
|
||||||
|
Superceded = -4,
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// An attempt to remove this plugin from disk will happen at every restart.
|
||||||
|
/// It will not be loaded, if unable to do so.
|
||||||
|
/// </summary>
|
||||||
|
Deleted = -5
|
||||||
|
}
|
||||||
|
}
|
@ -1,76 +1,79 @@
|
|||||||
#nullable disable
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using System.Text.Json.Serialization;
|
||||||
|
using SysVersion = System.Version;
|
||||||
|
|
||||||
namespace MediaBrowser.Model.Updates
|
namespace MediaBrowser.Model.Updates
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Class PackageVersionInfo.
|
/// Defines the <see cref="VersionInfo"/> class.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class VersionInfo
|
public class VersionInfo
|
||||||
{
|
{
|
||||||
private Version _version;
|
private SysVersion? _version;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the version.
|
/// Gets or sets the version.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The version.</value>
|
/// <value>The version.</value>
|
||||||
public string version
|
[JsonPropertyName("version")]
|
||||||
|
public string Version
|
||||||
{
|
{
|
||||||
get
|
get => _version == null ? string.Empty : _version.ToString();
|
||||||
{
|
|
||||||
return _version == null ? string.Empty : _version.ToString();
|
|
||||||
}
|
|
||||||
|
|
||||||
set
|
set => _version = SysVersion.Parse(value);
|
||||||
{
|
|
||||||
_version = Version.Parse(value);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the version as a <see cref="Version"/>.
|
/// Gets the version as a <see cref="SysVersion"/>.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public Version VersionNumber => _version;
|
public SysVersion VersionNumber => _version ?? new SysVersion(0, 0, 0);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the changelog for this version.
|
/// Gets or sets the changelog for this version.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The changelog.</value>
|
/// <value>The changelog.</value>
|
||||||
public string changelog { get; set; }
|
[JsonPropertyName("changelog")]
|
||||||
|
public string? Changelog { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the ABI that this version was built against.
|
/// Gets or sets the ABI that this version was built against.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The target ABI version.</value>
|
/// <value>The target ABI version.</value>
|
||||||
public string targetAbi { get; set; }
|
[JsonPropertyName("targetAbi")]
|
||||||
|
public string? TargetAbi { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the source URL.
|
/// Gets or sets the source URL.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The source URL.</value>
|
/// <value>The source URL.</value>
|
||||||
public string sourceUrl { get; set; }
|
[JsonPropertyName("sourceUrl")]
|
||||||
|
public string? SourceUrl { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a checksum for the binary.
|
/// Gets or sets a checksum for the binary.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The checksum.</value>
|
/// <value>The checksum.</value>
|
||||||
public string checksum { get; set; }
|
[JsonPropertyName("checksum")]
|
||||||
|
public string? Checksum { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets a timestamp of when the binary was built.
|
/// Gets or sets a timestamp of when the binary was built.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <value>The timestamp.</value>
|
/// <value>The timestamp.</value>
|
||||||
public string timestamp { get; set; }
|
[JsonPropertyName("timestamp")]
|
||||||
|
public string? Timestamp { get; set; }
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the repository name.
|
/// Gets or sets the repository name.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string repositoryName { get; set; }
|
[JsonPropertyName("repositoryName")]
|
||||||
|
public string RepositoryName { get; set; } = string.Empty;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the repository url.
|
/// Gets or sets the repository url.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public string repositoryUrl { get; set; }
|
[JsonPropertyName("repositoryUrl")]
|
||||||
|
public string RepositoryUrl { get; set; } = string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in new issue