using System; using System.Collections.Generic; using global::System.IO; namespace Pilz.Reflection.PluginSystem { public class PluginManager { /// /// The name of the type where to search for Methods when loading a new Plugin. /// /// public string EntryTypeName { get; set; } = "Plugin"; /// /// If true, all MainMethods of a Plugin will be called as soon as a Plugin is loaded. /// /// public bool AutoCallMainFunctions { get; set; } = true; /// /// A collection of all loaded Plugins. /// /// public Dictionary Plugins { get; private set; } = new Dictionary(); /// /// Loads Plugins that can be found at the given path and adds them to the Plugins-List. /// /// The path where to search for Plugins to load. public IEnumerable LoadPlugins(string pluginPath) { return LoadPlugins(pluginPath, true); } /// /// Loads Plugins that can be found at the given path and adds them to the Plugins-List. /// /// The path where to search for Plugins to load. /// If true, the Plugins will be added to Plugins-List after loading. public IEnumerable LoadPlugins(string pluginPath, bool addToList) { return LoadPlugins(pluginPath, addToList, AutoCallMainFunctions, EntryTypeName); } /// /// Loads Plugins that can be found at the given path and adds them to the Plugins-List. /// /// The path where to search for Plugins to load. /// If true, the Plugins will be added to Plugins-List after loading. /// If true, all MainMethods of a Plugin will be called as soon as a Plugin is loaded. /// The name of the type where to search for Methods when loading a new Plugin. public IEnumerable LoadPlugins(string pluginPath, bool addToList, bool autoCallMainFunction, string entryTypeName) { var loaded = new List(); foreach (string f in Directory.GetFiles(pluginPath, "*.dll", SearchOption.AllDirectories)) { var p = LoadPlugin(f, addToList); if (p is object) { loaded.Add(p); } } return loaded; } /// /// Loads a Plugin and adds it to the Plugins-List. /// /// The path to the plugin to load. /// If true, the Plugin will be added to Plugins-List after loading. public Plugin LoadPlugin(string filePath, bool addToList) { return LoadPlugin(filePath, addToList, AutoCallMainFunctions, EntryTypeName); } /// /// Loads a Plugin and adds it to the Plugins-List. /// /// The path to the plugin to load. /// If true, the Plugin will be added to Plugins-List after loading. /// If true, all MainMethods of a Plugin will be called as soon as a Plugin is loaded. /// The name of the type where to search for Methods when loading a new Plugin. public Plugin LoadPlugin(string filePath, bool addToList, bool autoCallMainFunction, string entryTypeName) { try { var plugin = new Plugin(filePath, autoCallMainFunction, entryTypeName); if (addToList) Plugins.Add(filePath, plugin); return plugin; } catch (Exception ex) { return null; } } /// /// Loads a Plugin and adds it to the Plugins-List. /// /// The path to the plugin to load. public Plugin LoadPlugin(string filePath) { return LoadPlugin(filePath, true); } /// /// Get all PluginFunctions that have one of the given function codes. /// /// /// public IEnumerable GetFunctions(params string[] funcCodes) { var list = new List(); foreach (var kvp in Plugins) list.AddRange(kvp.Value.GetFunctions(funcCodes)); return list; } /// /// Get the first PluginFunction that have the one of the given function codes. /// /// public PluginFunction GetFunction(params string[] funcCodes) { PluginFunction f = null; foreach (var kvp in Plugins) { if (f is null) { foreach (PluginFunction func in kvp.Value.GetFunctions(funcCodes)) { if (f is null) { f = func; } } } } return f; } } }