using System;
using System.Collections.Generic;
using MediaBrowser.Model.Plugins;
namespace MediaBrowser.Common.Plugins
{
///
/// Local plugin class.
///
public class LocalPlugin : IEquatable
{
private readonly bool _supported;
private Version? _version;
///
/// Initializes a new instance of the class.
///
/// The plugin path.
/// True if Jellyfin supports this version of the plugin.
/// The manifest record for this plugin, or null if one does not exist.
public LocalPlugin(string path, bool isSupported, PluginManifest manifest)
{
Path = path;
DllFiles = Array.Empty();
_supported = isSupported;
Manifest = manifest;
}
///
/// Gets the plugin id.
///
public Guid Id => Manifest.Id;
///
/// Gets the plugin name.
///
public string Name => Manifest.Name;
///
/// Gets the plugin version.
///
public Version Version
{
get
{
if (_version is null)
{
_version = Version.Parse(Manifest.Version);
}
return _version;
}
}
///
/// Gets the plugin path.
///
public string Path { get; }
///
/// Gets or sets the list of dll files for this plugin.
///
public IReadOnlyList DllFiles { get; set; }
///
/// Gets or sets the instance of this plugin.
///
public IPlugin? Instance { get; set; }
///
/// Gets a value indicating whether Jellyfin supports this version of the plugin, and it's enabled.
///
public bool IsEnabledAndSupported => _supported && Manifest.Status >= PluginStatus.Active;
///
/// Gets a value indicating whether the plugin has a manifest.
///
public PluginManifest Manifest { get; }
///
/// Compare two .
///
/// The first item.
/// The second item.
/// Comparison result.
public static int Compare(LocalPlugin a, LocalPlugin b)
{
if (a is null || b is null)
{
throw new ArgumentNullException(a is 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;
}
///
/// Returns the plugin information.
///
/// A instance containing the information.
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;
}
///
public override bool Equals(object? obj)
{
return obj is LocalPlugin other && this.Equals(other);
}
///
public override int GetHashCode()
{
return Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
}
///
public bool Equals(LocalPlugin? other)
{
if (other is null)
{
return false;
}
return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && Id.Equals(other.Id) && Version.Equals(other.Version);
}
}
}