source: branches/eraser6/pluginsRewrite/Eraser.Plugins/PluginInfo.cs @ 2293

Revision 2293, 4.6 KB checked in by lowjoel, 3 years ago (diff)

Rename the PluginInstance? class to be the PluginInfo? class.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26
27using System.Reflection;
28using System.Runtime.InteropServices;
29using System.Security.Cryptography;
30using System.Security.Cryptography.X509Certificates;
31using System.IO;
32
33using Eraser.Util;
34using Eraser.Util.ExtensionMethods;
35
36namespace Eraser.Plugins
37{
38    /// <summary>
39    /// Structure holding the instance values of the plugin like handle and path.
40    /// </summary>
41    public class PluginInfo
42    {
43        /// <summary>
44        /// Constructor
45        /// </summary>
46        /// <param name="assembly">The assembly representing this plugin.</param>
47        /// <param name="path">The path to the ass</param>
48        /// <param name="plugin"></param>
49        internal PluginInfo(Assembly assembly, IPlugin plugin)
50        {
51            Assembly = assembly;
52            Plugin = plugin;
53
54            //Verify the certificate in the assembly.
55            if (Security.VerifyAuthenticode(assembly.Location))
56            {
57                X509Certificate2 cert = new X509Certificate2(
58                    X509Certificate.CreateFromSignedFile(assembly.Location));
59                AssemblyAuthenticode = cert;
60            }
61        }
62
63        /// <summary>
64        /// Executes the plugin's initialisation routine.
65        /// </summary>
66        /// <param name="host">The host for the plugin</param>
67        internal void Load(Host host)
68        {
69            Assembly = Assembly.Load(Assembly.GetName());
70
71            try
72            {
73                //Iterate over every exported type, checking for the IPlugin implementation
74                Type typePlugin = Assembly.GetExportedTypes().First(
75                    type => type.GetInterface("Eraser.Manager.Plugin.IPlugin", true) != null);
76                if (typePlugin == null)
77                    throw new FileLoadException(S._("Could not load the plugin."),
78                        Assembly.Location);
79
80                //Initialize the plugin
81                Plugin = (IPlugin)Activator.CreateInstance(Assembly.GetType(typePlugin.ToString()));
82                Plugin.Initialize(host);
83            }
84            catch (System.Security.SecurityException e)
85            {
86                throw new FileLoadException(S._("Could not load the plugin."),
87                    Assembly.Location, e);
88            }
89        }
90
91        /// <summary>
92        /// Gets the Assembly this plugin instance came from.
93        /// </summary>
94        public Assembly Assembly
95        {
96            get
97            {
98                return assembly;
99            }
100            private set
101            {
102                assembly = value;
103
104                AssemblyInfo info = new AssemblyInfo();
105                info.Version = assembly.GetFileVersion();
106                IList<CustomAttributeData> attributes = CustomAttributeData.GetCustomAttributes(assembly);
107                foreach (CustomAttributeData attr in attributes)
108                    if (attr.Constructor.DeclaringType == typeof(GuidAttribute))
109                        info.Guid = new Guid((string)attr.ConstructorArguments[0].Value);
110                    else if (attr.Constructor.DeclaringType == typeof(AssemblyCompanyAttribute))
111                        info.Author = (string)attr.ConstructorArguments[0].Value;
112                    else if (attr.Constructor.DeclaringType == typeof(LoadingPolicyAttribute))
113                    {
114                        LoadingPolicy = (LoadingPolicy)attr.ConstructorArguments[0].Value;
115                        if (LoadingPolicy == LoadingPolicy.Core)
116                            LoadingPolicy = LoadingPolicy.None;
117                    }
118
119                this.AssemblyInfo = info;
120            }
121        }
122
123        /// <summary>
124        /// Gets the attributes of the assembly, loading from reflection-only sources.
125        /// </summary>
126        public AssemblyInfo AssemblyInfo { get; private set; }
127
128        /// <summary>
129        /// The Authenticode signature used for signing the assembly.
130        /// </summary>
131        public X509Certificate2 AssemblyAuthenticode { get; private set; }
132
133        /// <summary>
134        /// Gets whether the plugin is required for the functioning of Eraser (and
135        /// therefore cannot be disabled.)
136        /// </summary>
137        public LoadingPolicy LoadingPolicy { get; private set; }
138
139        /// <summary>
140        /// Gets the IPlugin interface which the plugin exposed. This may be null
141        /// if the plugin was not loaded.
142        /// </summary>
143        public IPlugin Plugin { get; private set; }
144
145        /// <summary>
146        /// Gets whether this particular plugin is currently loaded in memory.
147        /// </summary>
148        public bool Loaded
149        {
150            get { return Plugin != null; }
151        }
152
153        private Assembly assembly;
154    }
155}
Note: See TracBrowser for help on using the repository browser.