LocalPlugin.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Globalization;
  4. namespace MediaBrowser.Common.Plugins
  5. {
  6. /// <summary>
  7. /// Local plugin struct.
  8. /// </summary>
  9. public readonly struct LocalPlugin : IEquatable<LocalPlugin>
  10. {
  11. /// <summary>
  12. /// Initializes a new instance of the <see cref="LocalPlugin"/> struct.
  13. /// </summary>
  14. /// <param name="id">The plugin id.</param>
  15. /// <param name="name">The plugin name.</param>
  16. /// <param name="version">The plugin version.</param>
  17. /// <param name="path">The plugin path.</param>
  18. public LocalPlugin(Guid id, string name, Version version, string path)
  19. {
  20. Id = id;
  21. Name = name;
  22. Version = version;
  23. Path = path;
  24. DllFiles = new List<string>();
  25. }
  26. /// <summary>
  27. /// Gets the plugin id.
  28. /// </summary>
  29. public Guid Id { get; }
  30. /// <summary>
  31. /// Gets the plugin name.
  32. /// </summary>
  33. public string Name { get; }
  34. /// <summary>
  35. /// Gets the plugin version.
  36. /// </summary>
  37. public Version Version { get; }
  38. /// <summary>
  39. /// Gets the plugin path.
  40. /// </summary>
  41. public string Path { get; }
  42. /// <summary>
  43. /// Gets the list of dll files for this plugin.
  44. /// </summary>
  45. public List<string> DllFiles { get; }
  46. /// <summary>
  47. /// == operator.
  48. /// </summary>
  49. /// <param name="left">Left item.</param>
  50. /// <param name="right">Right item.</param>
  51. /// <returns>Comparison result.</returns>
  52. public static bool operator ==(LocalPlugin left, LocalPlugin right)
  53. {
  54. return left.Equals(right);
  55. }
  56. /// <summary>
  57. /// != operator.
  58. /// </summary>
  59. /// <param name="left">Left item.</param>
  60. /// <param name="right">Right item.</param>
  61. /// <returns>Comparison result.</returns>
  62. public static bool operator !=(LocalPlugin left, LocalPlugin right)
  63. {
  64. return !(left == right);
  65. }
  66. /// <summary>
  67. /// Compare two <see cref="LocalPlugin"/>.
  68. /// </summary>
  69. /// <param name="a">The first item.</param>
  70. /// <param name="b">The second item.</param>
  71. /// <returns>Comparison result.</returns>
  72. public static int Compare(LocalPlugin a, LocalPlugin b)
  73. {
  74. var compare = string.Compare(a.Name, b.Name, true, CultureInfo.InvariantCulture);
  75. // Id is not equal but name is.
  76. if (a.Id != b.Id && compare == 0)
  77. {
  78. compare = a.Id.CompareTo(b.Id);
  79. }
  80. return compare == 0 ? a.Version.CompareTo(b.Version) : compare;
  81. }
  82. /// <inheritdoc />
  83. public override bool Equals(object obj)
  84. {
  85. return obj is LocalPlugin other && this.Equals(other);
  86. }
  87. /// <inheritdoc />
  88. public override int GetHashCode()
  89. {
  90. return Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
  91. }
  92. /// <inheritdoc />
  93. public bool Equals(LocalPlugin other)
  94. {
  95. return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase)
  96. && Id.Equals(other.Id);
  97. }
  98. }
  99. }