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