[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
[2592] | 24 | using System.Linq;
|
---|
[16859] | 25 | using System.Runtime.CompilerServices;
|
---|
[2] | 26 |
|
---|
[16859] | 27 | [assembly: InternalsVisibleTo("HeuristicLab.PluginInfrastructure.Runner-3.4, PublicKey=0024000004800000940000000602000000240000525341310004000001000100e3d38bc66a0dd8dd36f57285e34632ec04b3049866ab1e64cf230e95ffcbfbb90c437b4d11dfe74ba981f746274290bb03f3e636e139e685b501031dc6e0bc8409153f0c842721eb9e8e2a703c9e4d102283f3ddbdfab4078c08de51869715992a694d2f608d0fa865c9d17c06b8d6a9135004e982fd862cdb2277e4ad15a4a6")]
|
---|
[13389] | 28 | namespace HeuristicLab.PluginInfrastructure {
|
---|
[2] | 29 | /// <summary>
|
---|
| 30 | /// Holds information of loaded plugins that is needed for plugin management.
|
---|
| 31 | /// Used to represent plugins in AppDomains without loading the plugin assemblies.
|
---|
| 32 | /// </summary>
|
---|
| 33 | [Serializable]
|
---|
[2517] | 34 | public sealed class PluginDescription : IPluginDescription {
|
---|
[2504] | 35 | private int nTimesLoaded;
|
---|
| 36 |
|
---|
[2] | 37 | private string name;
|
---|
[1189] | 38 | /// <summary>
|
---|
[2592] | 39 | /// Gets the name of the plugin.
|
---|
[1189] | 40 | /// </summary>
|
---|
[2] | 41 | public string Name {
|
---|
| 42 | get { return name; }
|
---|
[2504] | 43 | internal set { name = value; }
|
---|
[2] | 44 | }
|
---|
[2513] | 45 |
|
---|
| 46 | private string description;
|
---|
| 47 | /// <summary>
|
---|
| 48 | /// Gets or sets the description of the plugin.
|
---|
| 49 | /// </summary>
|
---|
[2922] | 50 | public string Description {
|
---|
[2513] | 51 | get { return description; }
|
---|
[2922] | 52 | internal set { description = value; }
|
---|
[2513] | 53 | }
|
---|
[2] | 54 | private Version version;
|
---|
[1189] | 55 | /// <summary>
|
---|
[2592] | 56 | /// Gets the version of the plugin.
|
---|
[1189] | 57 | /// </summary>
|
---|
[2592] | 58 | public Version Version {
|
---|
[2] | 59 | get { return version; }
|
---|
[2592] | 60 | internal set { version = value; }
|
---|
[2] | 61 | }
|
---|
[2481] | 62 |
|
---|
[2778] | 63 | private string contactName;
|
---|
| 64 | /// <summary>
|
---|
| 65 | /// Gets or sets the name of the contact person for this plugin.
|
---|
| 66 | /// </summary>
|
---|
[2815] | 67 | public string ContactName {
|
---|
[2778] | 68 | get { return contactName; }
|
---|
[2815] | 69 | internal set { contactName = value; }
|
---|
[2778] | 70 | }
|
---|
| 71 |
|
---|
| 72 | private string contactEmail;
|
---|
| 73 | /// <summary>
|
---|
| 74 | /// Gets or sets the e-mail address of the contact person for this plugin.
|
---|
| 75 | /// </summary>
|
---|
[2815] | 76 | public string ContactEmail {
|
---|
[2778] | 77 | get { return contactEmail; }
|
---|
[2815] | 78 | internal set { contactEmail = value; }
|
---|
[2778] | 79 | }
|
---|
[2815] | 80 | private string licenseText;
|
---|
| 81 | /// <summary>
|
---|
| 82 | /// Gets or sets the license text of the plugin.
|
---|
| 83 | /// </summary>
|
---|
| 84 | public string LicenseText {
|
---|
| 85 | get { return licenseText; }
|
---|
| 86 | internal set { licenseText = value; }
|
---|
| 87 | }
|
---|
[2778] | 88 |
|
---|
[2481] | 89 | private PluginState pluginState;
|
---|
| 90 | /// <summary>
|
---|
| 91 | /// Gets or sets the plugin state.
|
---|
| 92 | /// </summary>
|
---|
| 93 | public PluginState PluginState {
|
---|
| 94 | get { return pluginState; }
|
---|
| 95 | }
|
---|
| 96 |
|
---|
[2779] | 97 | private string loadingErrorInformation;
|
---|
| 98 | /// <summary>
|
---|
| 99 | /// Gets the error message why this plugin has been disabled.
|
---|
| 100 | /// </summary>
|
---|
[13338] | 101 | public string LoadingErrorInformation {
|
---|
[2779] | 102 | get {
|
---|
| 103 | return loadingErrorInformation;
|
---|
| 104 | }
|
---|
| 105 | }
|
---|
[2488] | 106 |
|
---|
[2688] | 107 | private List<PluginFile> files = new List<PluginFile>();
|
---|
[2] | 108 | /// <summary>
|
---|
[1189] | 109 | /// Gets the names of all files that belong to this plugin.
|
---|
[2] | 110 | /// These files are deleted when the plugin is removed or updated.
|
---|
| 111 | /// </summary>
|
---|
[2688] | 112 | public IEnumerable<IPluginFile> Files {
|
---|
| 113 | get { return files.Cast<IPluginFile>(); }
|
---|
[2] | 114 | }
|
---|
| 115 |
|
---|
[2688] | 116 | internal void AddFiles(IEnumerable<PluginFile> fileNames) {
|
---|
[2481] | 117 | files.AddRange(fileNames);
|
---|
| 118 | }
|
---|
| 119 |
|
---|
[2504] | 120 | private List<PluginDescription> dependencies = new List<PluginDescription>();
|
---|
[2592] | 121 | internal IEnumerable<PluginDescription> Dependencies {
|
---|
| 122 | get { return dependencies; }
|
---|
| 123 | }
|
---|
[1189] | 124 | /// <summary>
|
---|
| 125 | /// Gets all dependencies of the plugin.
|
---|
| 126 | /// </summary>
|
---|
[2592] | 127 | IEnumerable<IPluginDescription> IPluginDescription.Dependencies {
|
---|
| 128 | get { return dependencies.Cast<IPluginDescription>(); }
|
---|
[2] | 129 | }
|
---|
[2481] | 130 |
|
---|
[2504] | 131 | internal void AddDependency(PluginDescription dependency) {
|
---|
[2481] | 132 | dependencies.Add(dependency);
|
---|
| 133 | }
|
---|
| 134 |
|
---|
[2779] | 135 |
|
---|
[2] | 136 | /// <summary>
|
---|
[2690] | 137 | /// Gets the locations (file names) of the assemblies that belong to this plugin.
|
---|
[2] | 138 | /// </summary>
|
---|
[2690] | 139 | public IEnumerable<string> AssemblyLocations {
|
---|
| 140 | get { return Files.Where(f => f.Type == PluginFileType.Assembly).Select(f => f.Name); }
|
---|
[2] | 141 | }
|
---|
[2481] | 142 |
|
---|
[8193] | 143 | /// <summary>
|
---|
| 144 | /// Gets and sets the list of assembly names for this plugin. Assembly names are only available after the plugin has been loaded.
|
---|
| 145 | /// </summary>
|
---|
| 146 | private List<string> assemblyNames;
|
---|
| 147 | public IEnumerable<string> AssemblyNames {
|
---|
| 148 | get { return assemblyNames; }
|
---|
| 149 | set { this.assemblyNames = new List<string>(value); }
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[2504] | 152 | internal PluginDescription() {
|
---|
[2488] | 153 | pluginState = PluginState.Undefined;
|
---|
| 154 | }
|
---|
| 155 |
|
---|
[2779] | 156 | internal void Disable(string loadingErrorInformation) {
|
---|
[2504] | 157 | if (pluginState != PluginState.Undefined)
|
---|
[5196] | 158 | throw new InvalidOperationException("Cannot disable a plugin in state " + pluginState);
|
---|
[2504] | 159 | pluginState = PluginState.Disabled;
|
---|
[2779] | 160 | this.loadingErrorInformation = loadingErrorInformation;
|
---|
[2504] | 161 | }
|
---|
| 162 |
|
---|
| 163 | internal void Enable() {
|
---|
| 164 | if (pluginState != PluginState.Undefined)
|
---|
[5196] | 165 | throw new InvalidOperationException("Cannot enable a plugin in state " + pluginState);
|
---|
[2504] | 166 | pluginState = PluginState.Enabled;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | internal void Load() {
|
---|
| 170 | if (!(pluginState == PluginState.Enabled || pluginState == PluginState.Loaded))
|
---|
[5196] | 171 | throw new InvalidOperationException("Cannot load a plugin in state " + pluginState);
|
---|
[2504] | 172 | pluginState = PluginState.Loaded;
|
---|
| 173 | nTimesLoaded++;
|
---|
| 174 | }
|
---|
| 175 |
|
---|
| 176 | internal void Unload() {
|
---|
| 177 | if (pluginState != PluginState.Loaded)
|
---|
[5196] | 178 | throw new InvalidOperationException("Cannot unload a plugin in state " + pluginState);
|
---|
[2504] | 179 | nTimesLoaded--;
|
---|
| 180 | if (nTimesLoaded == 0) pluginState = PluginState.Enabled;
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 |
|
---|
[1189] | 184 | /// <summary>
|
---|
| 185 | /// Gets the string representation of the plugin.
|
---|
| 186 | /// </summary>
|
---|
| 187 | /// <returns>The name of the plugin.</returns>
|
---|
[2] | 188 | public override string ToString() {
|
---|
[2922] | 189 | return Name + " " + Version;
|
---|
[2] | 190 | }
|
---|
[2481] | 191 |
|
---|
[2497] | 192 | // equals and hashcode have to be implemented because we want to compare PluginDescriptions from
|
---|
| 193 | // different AppDomains and serialization destroys reference equality
|
---|
| 194 | /// <summary>
|
---|
| 195 | /// Checks whether the given object is equal to the current plugin.
|
---|
| 196 | /// </summary>
|
---|
| 197 | /// <param name="obj">The object to compare.</param>
|
---|
| 198 | /// <returns><c>true</c> if it is equal, <c>false</c> otherwise.</returns>
|
---|
| 199 | public override bool Equals(object obj) {
|
---|
[2504] | 200 | PluginDescription other = obj as PluginDescription;
|
---|
| 201 | if (other == null) return false;
|
---|
[2] | 202 |
|
---|
[2497] | 203 | return other.Name == this.Name && other.Version == this.Version;
|
---|
| 204 | }
|
---|
| 205 | /// <summary>
|
---|
| 206 | /// Gets the hash code of the current plugin.
|
---|
| 207 | /// </summary>
|
---|
| 208 | /// <returns>The hash code of the plugin.</returns>
|
---|
| 209 | public override int GetHashCode() {
|
---|
| 210 | if (version != null) {
|
---|
| 211 | return name.GetHashCode() + version.GetHashCode();
|
---|
| 212 | } else return name.GetHashCode();
|
---|
| 213 | }
|
---|
[2] | 214 | }
|
---|
| 215 | }
|
---|