2018-11-12 01:48:33 +01:00
|
|
|
|
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 Switch_Toolbox.Library
|
|
|
|
|
{
|
|
|
|
|
public class PluginLoader
|
|
|
|
|
{
|
|
|
|
|
public Dictionary<string, IPlugin> _Plugins;
|
|
|
|
|
|
|
|
|
|
public PluginLoader()
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
public static ICollection<IPlugin> LoadPlugins()
|
|
|
|
|
{
|
|
|
|
|
string path = "Lib/Plugins";
|
|
|
|
|
|
|
|
|
|
string[] dllFileNames = null;
|
|
|
|
|
if (Directory.Exists(path))
|
|
|
|
|
{
|
|
|
|
|
dllFileNames = Directory.GetFiles(path, "*.Plg.dll");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2018-11-17 23:33:00 +01:00
|
|
|
|
try
|
2018-11-12 01:48:33 +01:00
|
|
|
|
{
|
2018-11-17 23:33:00 +01:00
|
|
|
|
Type[] types = assembly.GetTypes();
|
|
|
|
|
foreach (Type type in types)
|
2018-11-12 01:48:33 +01:00
|
|
|
|
{
|
2018-11-17 23:33:00 +01:00
|
|
|
|
if (type.IsInterface || type.IsAbstract)
|
|
|
|
|
{
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (type.GetInterface(pluginType.FullName) != null)
|
|
|
|
|
{
|
|
|
|
|
pluginTypes.Add(type);
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-11-12 01:48:33 +01:00
|
|
|
|
}
|
2018-11-17 23:33:00 +01:00
|
|
|
|
}
|
|
|
|
|
catch (ReflectionTypeLoadException ex)
|
|
|
|
|
{
|
|
|
|
|
StringBuilder sb = new StringBuilder();
|
|
|
|
|
foreach (Exception exSub in ex.LoaderExceptions)
|
2018-11-12 01:48:33 +01:00
|
|
|
|
{
|
2018-11-17 23:33:00 +01:00
|
|
|
|
sb.AppendLine(exSub.Message);
|
|
|
|
|
FileNotFoundException exFileNotFound = exSub as FileNotFoundException;
|
|
|
|
|
if (exFileNotFound != null)
|
2018-11-12 01:48:33 +01:00
|
|
|
|
{
|
2018-11-17 23:33:00 +01:00
|
|
|
|
if (!string.IsNullOrEmpty(exFileNotFound.FusionLog))
|
|
|
|
|
{
|
|
|
|
|
sb.AppendLine("Fusion Log:");
|
|
|
|
|
sb.AppendLine(exFileNotFound.FusionLog);
|
|
|
|
|
}
|
2018-11-12 01:48:33 +01:00
|
|
|
|
}
|
2018-11-17 23:33:00 +01:00
|
|
|
|
sb.AppendLine();
|
2018-11-12 01:48:33 +01:00
|
|
|
|
}
|
2018-11-17 23:33:00 +01:00
|
|
|
|
string errorMessage = sb.ToString();
|
|
|
|
|
throw new Exception(errorMessage);
|
2018-11-12 01:48:33 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ICollection<IPlugin> plugins = new List<IPlugin>(pluginTypes.Count);
|
|
|
|
|
foreach (Type type in pluginTypes)
|
|
|
|
|
{
|
|
|
|
|
IPlugin plugin = (IPlugin)Activator.CreateInstance(type);
|
|
|
|
|
plugins.Add(plugin);
|
|
|
|
|
}
|
|
|
|
|
assemblies.Clear();
|
|
|
|
|
pluginTypes.Clear();
|
|
|
|
|
|
|
|
|
|
return plugins;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|