aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Plugins/BasePlugin.cs
blob: e228ae7ec549c326c32313fe536742c38394c8b3 (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
using System;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Model.Plugins;
using MediaBrowser.Model.Serialization;

namespace MediaBrowser.Common.Plugins
{
    /// <summary>
    /// Provides a common base class for all plugins.
    /// </summary>
    public abstract class BasePlugin : IPlugin, IPluginAssembly
    {
        /// <summary>
        /// Gets the name of the plugin.
        /// </summary>
        /// <value>The name.</value>
        public abstract string Name { get; }

        /// <summary>
        /// Gets the description.
        /// </summary>
        /// <value>The description.</value>
        public virtual string Description => string.Empty;

        /// <summary>
        /// Gets the unique id.
        /// </summary>
        /// <value>The unique id.</value>
        public virtual Guid Id { get; private set; }

        /// <summary>
        /// Gets the plugin version.
        /// </summary>
        /// <value>The version.</value>
        public Version Version { get; private set; }

        /// <summary>
        /// Gets the path to the assembly file.
        /// </summary>
        /// <value>The assembly file path.</value>
        public string AssemblyFilePath { get; private set; }

        /// <summary>
        /// Gets the full path to the data folder, where the plugin can store any miscellaneous files needed.
        /// </summary>
        /// <value>The data folder path.</value>
        public string DataFolderPath { get; private set; }

        /// <summary>
        /// Gets a value indicating whether the plugin can be uninstalled.
        /// </summary>
        public bool CanUninstall => !Path.GetDirectoryName(AssemblyFilePath)
            .Equals(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), StringComparison.InvariantCulture);

        /// <summary>
        /// Gets the plugin info.
        /// </summary>
        /// <returns>PluginInfo.</returns>
        public virtual PluginInfo GetPluginInfo()
        {
            var info = new PluginInfo(
                Name,
                Version,
                Description,
                Id,
                CanUninstall);

            return info;
        }

        /// <summary>
        /// Called just before the plugin is uninstalled from the server.
        /// </summary>
        public virtual void OnUninstalling()
        {
        }

        /// <inheritdoc />
        public void SetAttributes(string assemblyFilePath, string dataFolderPath, Version assemblyVersion)
        {
            AssemblyFilePath = assemblyFilePath;
            DataFolderPath = dataFolderPath;
            Version = assemblyVersion;
        }

        /// <inheritdoc />
        public void SetId(Guid assemblyId)
        {
            Id = assemblyId;
        }
    }
}