LocalPlugin.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using MediaBrowser.Model.Plugins;
  4. namespace MediaBrowser.Common.Plugins
  5. {
  6. /// <summary>
  7. /// Local plugin class.
  8. /// </summary>
  9. public class LocalPlugin : IEquatable<LocalPlugin>
  10. {
  11. private readonly bool _supported;
  12. private Version? _version;
  13. /// <summary>
  14. /// Initializes a new instance of the <see cref="LocalPlugin"/> class.
  15. /// </summary>
  16. /// <param name="path">The plugin path.</param>
  17. /// <param name="isSupported"><b>True</b> if Jellyfin supports this version of the plugin.</param>
  18. /// <param name="manifest">The manifest record for this plugin, or null if one does not exist.</param>
  19. public LocalPlugin(string path, bool isSupported, PluginManifest manifest)
  20. {
  21. Path = path;
  22. DllFiles = Array.Empty<string>();
  23. _supported = isSupported;
  24. Manifest = manifest;
  25. }
  26. /// <summary>
  27. /// Gets the plugin id.
  28. /// </summary>
  29. public Guid Id => Manifest.Id;
  30. /// <summary>
  31. /// Gets the plugin name.
  32. /// </summary>
  33. public string Name => Manifest.Name;
  34. /// <summary>
  35. /// Gets the plugin version.
  36. /// </summary>
  37. public Version Version
  38. {
  39. get
  40. {
  41. if (_version is null)
  42. {
  43. _version = Version.Parse(Manifest.Version);
  44. }
  45. return _version;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets the plugin path.
  50. /// </summary>
  51. public string Path { get; }
  52. /// <summary>
  53. /// Gets or sets the list of dll files for this plugin.
  54. /// </summary>
  55. public IReadOnlyList<string> DllFiles { get; set; }
  56. /// <summary>
  57. /// Gets or sets the instance of this plugin.
  58. /// </summary>
  59. public IPlugin? Instance { get; set; }
  60. /// <summary>
  61. /// Gets a value indicating whether Jellyfin supports this version of the plugin, and it's enabled.
  62. /// </summary>
  63. public bool IsEnabledAndSupported => _supported && Manifest.Status >= PluginStatus.Active;
  64. /// <summary>
  65. /// Gets a value indicating whether the plugin has a manifest.
  66. /// </summary>
  67. public PluginManifest Manifest { get; }
  68. /// <summary>
  69. /// Compare two <see cref="LocalPlugin"/>.
  70. /// </summary>
  71. /// <param name="a">The first item.</param>
  72. /// <param name="b">The second item.</param>
  73. /// <returns>Comparison result.</returns>
  74. public static int Compare(LocalPlugin a, LocalPlugin b)
  75. {
  76. if (a is null || b is null)
  77. {
  78. throw new ArgumentNullException(a is null ? nameof(a) : nameof(b));
  79. }
  80. var compare = string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
  81. // Id is not equal but name is.
  82. if (!a.Id.Equals(b.Id) && compare == 0)
  83. {
  84. compare = a.Id.CompareTo(b.Id);
  85. }
  86. return compare == 0 ? a.Version.CompareTo(b.Version) : compare;
  87. }
  88. /// <summary>
  89. /// Returns the plugin information.
  90. /// </summary>
  91. /// <returns>A <see cref="PluginInfo"/> instance containing the information.</returns>
  92. public PluginInfo GetPluginInfo()
  93. {
  94. var inst = Instance?.GetPluginInfo() ?? new PluginInfo(Manifest.Name, Version, Manifest.Description, Manifest.Id, true);
  95. inst.Status = Manifest.Status;
  96. inst.HasImage = !string.IsNullOrEmpty(Manifest.ImagePath);
  97. return inst;
  98. }
  99. /// <inheritdoc />
  100. public override bool Equals(object? obj)
  101. {
  102. return obj is LocalPlugin other && this.Equals(other);
  103. }
  104. /// <inheritdoc />
  105. public override int GetHashCode()
  106. {
  107. return Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
  108. }
  109. /// <inheritdoc />
  110. public bool Equals(LocalPlugin? other)
  111. {
  112. if (other is null)
  113. {
  114. return false;
  115. }
  116. return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase) && Id.Equals(other.Id) && Version.Equals(other.Version);
  117. }
  118. }
  119. }