LocalPlugin.cs 4.4 KB

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