1
0
mirror of synced 2024-11-30 18:24:39 +01:00
Switch-Toolbox/Switch_Toolbox_Library/Plugin/PluginLoader.cs
José Guadalupe Nava Zavala dce06db362 Fix build problem
Fix trying to access field in a string.
Changed Toolbox references to automatically build dependencies (Toolbox_Library, File_Format_Library) to resepective output folder
2020-05-02 03:27:08 -05:00

111 lines
3.8 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Reflection;
using PluginContracts;
namespace Toolbox.Library
{
public class PluginLoader
{
public Dictionary<string, IPlugin> _Plugins;
public PluginLoader()
{
}
public static ICollection<IPlugin> LoadPlugins()
{
string path = Runtime.ExecutableDir + "/Lib/Plugins";
string[] dllFileNames = null;
if (Directory.Exists(path))
{
dllFileNames = Directory.GetFiles(path, "*.Plg.dll");
}
if (dllFileNames == null)
{
System.Windows.Forms.MessageBox.Show($"Could not find any plugins in {path}", "",
System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
return new List<IPlugin>();
}
ICollection<Assembly> assemblies = new List<Assembly>(dllFileNames.Length);
foreach (string dllFile in dllFileNames)
{
AssemblyName an = AssemblyName.GetAssemblyName(dllFile);
Assembly assembly = Assembly.Load(an);
assemblies.Add(assembly);
}
Type pluginType = typeof(IPlugin);
ICollection<Type> pluginTypes = new List<Type>();
foreach (Assembly assembly in assemblies)
{
if (assembly != null)
{
try
{
Type[] types = assembly.GetTypes();
foreach (Type type in types)
{
if (type.IsInterface || type.IsAbstract)
{
continue;
}
else
{
if (type.GetInterface(pluginType.FullName) != null)
{
pluginTypes.Add(type);
}
}
}
}
catch (ReflectionTypeLoadException ex)
{
StringBuilder sb = new StringBuilder();
foreach (Exception exSub in ex.LoaderExceptions)
{
sb.AppendLine(exSub.Message);
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
if (exFileNotFound != null)
{
if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
{
sb.AppendLine("Fusion Log:");
sb.AppendLine(exFileNotFound.FusionLog);
}
}
sb.AppendLine();
}
string errorMessage = sb.ToString();
throw new Exception(errorMessage);
}
}
}
ICollection<IPlugin> plugins = new List<IPlugin>(pluginTypes.Count);
foreach (Type type in pluginTypes)
{
try
{
IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
plugins.Add(plugin);
}
catch (Exception ex)
{
Console.Error.WriteLine("Couldn't Load {0} Reason:{1}", type, ex.Message);
}
}
assemblies.Clear();
pluginTypes.Clear();
return plugins;
}
}
}