jellyfin/MediaBrowser.Common/Plugins/LocalPlugin.cs

140 lines
4.3 KiB
C#
Raw Normal View History

using System;
2020-10-01 01:37:30 +02:00
using System.Collections.Generic;
2020-12-07 00:48:54 +01:00
using MediaBrowser.Model.Plugins;
2020-10-01 01:37:30 +02:00
namespace MediaBrowser.Common.Plugins
{
/// <summary>
/// Local plugin class.
2020-10-01 01:37:30 +02:00
/// </summary>
2020-11-08 17:31:53 +01:00
public class LocalPlugin : IEquatable<LocalPlugin>
2020-10-01 01:37:30 +02:00
{
2020-12-07 00:48:54 +01:00
private readonly bool _supported;
private Version? _version;
2020-10-01 01:37:30 +02:00
/// <summary>
2020-11-08 17:31:53 +01:00
/// Initializes a new instance of the <see cref="LocalPlugin"/> class.
2020-10-01 01:37:30 +02:00
/// </summary>
/// <param name="path">The plugin path.</param>
2020-12-07 00:48:54 +01:00
/// <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)
2020-10-01 01:37:30 +02:00
{
Path = path;
2021-03-09 05:57:38 +01:00
DllFiles = Array.Empty<string>();
2020-12-07 00:48:54 +01:00
_supported = isSupported;
Manifest = manifest;
2020-10-01 01:37:30 +02:00
}
/// <summary>
/// Gets the plugin id.
/// </summary>
2020-12-18 21:37:35 +01:00
public Guid Id => Manifest.Id;
2020-10-01 01:37:30 +02:00
/// <summary>
/// Gets the plugin name.
/// </summary>
2020-12-07 00:48:54 +01:00
public string Name => Manifest.Name;
2020-10-01 01:37:30 +02:00
/// <summary>
/// Gets the plugin version.
/// </summary>
2020-12-07 00:48:54 +01:00
public Version Version
{
get
{
2022-12-05 15:00:20 +01:00
if (_version is null)
2020-12-07 00:48:54 +01:00
{
_version = Version.Parse(Manifest.Version);
}
return _version;
}
}
2020-10-01 01:37:30 +02:00
/// <summary>
/// Gets the plugin path.
/// </summary>
public string Path { get; }
/// <summary>
2021-03-09 05:57:38 +01:00
/// Gets or sets the list of dll files for this plugin.
2020-10-01 01:37:30 +02:00
/// </summary>
2021-03-09 05:57:38 +01:00
public IReadOnlyList<string> DllFiles { get; set; }
2020-10-01 01:37:30 +02:00
/// <summary>
2020-12-07 00:48:54 +01:00
/// Gets or sets the instance of this plugin.
2020-10-01 01:37:30 +02:00
/// </summary>
2020-12-07 00:48:54 +01:00
public IPlugin? Instance { get; set; }
2020-10-01 01:37:30 +02:00
/// <summary>
2020-12-07 00:48:54 +01:00
/// Gets a value indicating whether Jellyfin supports this version of the plugin, and it's enabled.
2020-10-01 01:37:30 +02:00
/// </summary>
2020-12-07 00:48:54 +01:00
public bool IsEnabledAndSupported => _supported && Manifest.Status >= PluginStatus.Active;
/// <summary>
/// Gets a value indicating whether the plugin has a manifest.
/// </summary>
public PluginManifest Manifest { get; }
2020-10-01 01:37:30 +02:00
/// <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)
{
2022-12-05 15:00:20 +01:00
if (a is null || b is null)
2020-12-07 00:48:54 +01:00
{
2022-12-05 15:00:20 +01:00
throw new ArgumentNullException(a is null ? nameof(a) : nameof(b));
2020-12-07 00:48:54 +01:00
}
2020-12-16 20:37:23 +01:00
var compare = string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
2020-10-01 01:37:30 +02:00
// Id is not equal but name is.
2020-12-07 00:48:54 +01:00
if (!a.Id.Equals(b.Id) && compare == 0)
2020-10-01 01:37:30 +02:00
{
compare = a.Id.CompareTo(b.Id);
}
return compare == 0 ? a.Version.CompareTo(b.Version) : compare;
}
2020-12-07 00:48:54 +01:00
/// <summary>
/// Returns the plugin information.
/// </summary>
/// <returns>A <see cref="PluginInfo"/> instance containing the information.</returns>
public PluginInfo GetPluginInfo()
{
2020-12-18 21:37:35 +01:00
var inst = Instance?.GetPluginInfo() ?? new PluginInfo(Manifest.Name, Version, Manifest.Description, Manifest.Id, true);
2020-12-07 00:48:54 +01:00
inst.Status = Manifest.Status;
2020-12-18 21:52:44 +01:00
inst.HasImage = !string.IsNullOrEmpty(Manifest.ImagePath);
2020-12-07 00:48:54 +01:00
return inst;
}
2020-10-01 01:37:30 +02:00
/// <inheritdoc />
2020-12-07 00:48:54 +01:00
public override bool Equals(object? obj)
2020-10-01 01:37:30 +02:00
{
return obj is LocalPlugin other && this.Equals(other);
}
/// <inheritdoc />
public override int GetHashCode()
{
return Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
}
/// <inheritdoc />
2020-12-07 00:48:54 +01:00
public bool Equals(LocalPlugin? other)
2020-10-01 01:37:30 +02:00
{
2022-12-05 15:00:20 +01:00
if (other is null)
{
return false;
}
2020-12-07 00:48:54 +01:00
return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && Id.Equals(other.Id) && Version.Equals(other.Version);
2020-10-01 01:37:30 +02:00
}
}
}