aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Plugins/LocalPlugin.cs
blob: e26631615a3730c70bc1d226b84e7c014e0e8112 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Collections.Generic;
using System.Globalization;

namespace MediaBrowser.Common.Plugins
{
    /// <summary>
    /// Local plugin struct.
    /// </summary>
    public readonly struct LocalPlugin : IEquatable<LocalPlugin>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LocalPlugin"/> struct.
        /// </summary>
        /// <param name="id">The plugin id.</param>
        /// <param name="name">The plugin name.</param>
        /// <param name="version">The plugin version.</param>
        /// <param name="path">The plugin path.</param>
        public LocalPlugin(Guid id, string name, Version version, string path)
        {
            Id = id;
            Name = name;
            Version = version;
            Path = path;
            DllFiles = new List<string>();
        }

        /// <summary>
        /// Gets the plugin id.
        /// </summary>
        public Guid Id { get; }

        /// <summary>
        /// Gets the plugin name.
        /// </summary>
        public string Name { get; }

        /// <summary>
        /// Gets the plugin version.
        /// </summary>
        public Version Version { get; }

        /// <summary>
        /// Gets the plugin path.
        /// </summary>
        public string Path { get; }

        /// <summary>
        /// Gets the list of dll files for this plugin.
        /// </summary>
        public List<string> DllFiles { get; }

        /// <summary>
        /// == operator.
        /// </summary>
        /// <param name="left">Left item.</param>
        /// <param name="right">Right item.</param>
        /// <returns>Comparison result.</returns>
        public static bool operator ==(LocalPlugin left, LocalPlugin right)
        {
            return left.Equals(right);
        }

        /// <summary>
        /// != operator.
        /// </summary>
        /// <param name="left">Left item.</param>
        /// <param name="right">Right item.</param>
        /// <returns>Comparison result.</returns>
        public static bool operator !=(LocalPlugin left, LocalPlugin right)
        {
            return !(left == right);
        }

        /// <summary>
        /// Compare two <see cref="LocalPlugin"/>.
        /// </summary>
        /// <param name="a">The first item.</param>
        /// <param name="b">The second item.</param>
        /// <returns>Comparison result.</returns>
        public static int Compare(LocalPlugin a, LocalPlugin b)
        {
            var compare = string.Compare(a.Name, b.Name, true, CultureInfo.InvariantCulture);

            // Id is not equal but name is.
            if (a.Id != b.Id && compare == 0)
            {
                compare = a.Id.CompareTo(b.Id);
            }

            return compare == 0 ? a.Version.CompareTo(b.Version) : compare;
        }

        /// <inheritdoc />
        public override bool Equals(object obj)
        {
            return obj is LocalPlugin other && this.Equals(other);
        }

        /// <inheritdoc />
        public override int GetHashCode()
        {
            return Name.GetHashCode(StringComparison.OrdinalIgnoreCase);
        }

        /// <inheritdoc />
        public bool Equals(LocalPlugin other)
        {
            return Name.Equals(other.Name, StringComparison.OrdinalIgnoreCase)
                   && Id.Equals(other.Id);
        }
    }
}