~quequotion/glippy/glippy

« back to all changes in this revision

Viewing changes to src/core/Plugins.cs

  • Committer: Que Quotion
  • Date: 2018-07-01 13:34:36 UTC
  • Revision ID: quequotion@bugmenot.com-20180701133436-usy073goo274fh6a
Glippy: Simple, powerful clipboard manager

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Glippy
 
3
 * Copyright © 2010, 2011, 2012 Wojciech Kowalczyk
 
4
 * The program is distributed under the terms of the GNU General Public License Version 3.
 
5
 * See LICENCE for details.
 
6
 */
 
7
 
 
8
using System;
 
9
using System.Collections.Generic;
 
10
using System.Reflection;
 
11
using Glippy.Core.Api;
 
12
 
 
13
namespace Glippy.Core
 
14
{
 
15
        /// <summary>
 
16
        /// Plugins management.
 
17
        /// </summary>
 
18
        public static class Plugins
 
19
        {
 
20
                /// <summary>
 
21
                /// Loads plugin.
 
22
                /// </summary>
 
23
                /// <param name="dllName">Name of library which contains plugin.</param>
 
24
                /// <returns>Loaded plugin instance or null.</returns>
 
25
                public static IBase LoadPlugin(string dllName)
 
26
                {
 
27
                        IBase plugin = null;                    
 
28
                        Assembly asm = Assembly.LoadFile((dllName.Contains(AppDomain.CurrentDomain.BaseDirectory) ? string.Empty : AppDomain.CurrentDomain.BaseDirectory) + dllName + (dllName.EndsWith(".dll") ? string.Empty : ".dll"));
 
29
                        
 
30
                        foreach (Type t in asm.GetExportedTypes())
 
31
                        {
 
32
                                if (t.GetInterface(typeof(IBase).Name) != null)
 
33
                                        plugin = Activator.CreateInstance(t) as IBase;                                  
 
34
                        }
 
35
                        
 
36
                        return plugin;
 
37
                }
 
38
                
 
39
                /// <summary>
 
40
                /// Loads all plugins.
 
41
                /// </summary>
 
42
                public static List<IBase> LoadAllPlugins()
 
43
                {
 
44
                        List<IBase> plugins = new List<IBase>();
 
45
                        
 
46
                        foreach (string dll in System.IO.Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory, "*.dll"))
 
47
                        {       
 
48
                                try
 
49
                                {
 
50
                                        IBase plugin = LoadPlugin(dll);
 
51
                                        
 
52
                                        if (plugin != null)
 
53
                                                plugins.Add(plugin);
 
54
                                }
 
55
                                catch { }
 
56
                        }
 
57
                        
 
58
                        return plugins;
 
59
                }
 
60
        }
 
61
}