aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.Common/Plugins/BasePlugin.cs
blob: fc05cbc9a21323b10ff26fe5072fc1a8f0f6d0d6 (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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using MediaBrowser.Common.Json;

namespace MediaBrowser.Common.Plugins
{
    public abstract class BasePlugin<TConfigurationType> : IPlugin
        where TConfigurationType : BasePluginConfiguration, new()
    {
        public string Path { get; set; }
        public TConfigurationType Configuration { get; private set; }

        private string ConfigurationPath
        {
            get
            {
                return System.IO.Path.Combine(Path, "config.js");
            }
        }
        
        public void Init()
        {
            Configuration = GetConfiguration();

            if (Configuration.Enabled)
            {
                InitInternal();
            }
        }

        protected abstract void InitInternal();

        private TConfigurationType GetConfiguration()
        {
            if (!File.Exists(ConfigurationPath))
            {
                return new TConfigurationType();
            }

            return JsonSerializer.Deserialize<TConfigurationType>(ConfigurationPath);
        }
    }

    public interface IPlugin
    {
        string Path { get; set; }

        void Init();
    }
}