LocalPlugin.cs 4.3 KB

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