2018-05-13 19:49:26 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace PKHeX.WinForms
|
|
|
|
|
{
|
|
|
|
|
public static class PluginLoader
|
|
|
|
|
{
|
2021-10-06 21:48:17 +00:00
|
|
|
|
public static IEnumerable<T> LoadPlugins<T>(string pluginPath, PluginLoadSetting loadSetting) where T : class
|
2018-05-13 19:49:26 +00:00
|
|
|
|
{
|
2019-02-15 19:46:46 +00:00
|
|
|
|
var dllFileNames = !Directory.Exists(pluginPath)
|
2018-07-24 04:47:31 +00:00
|
|
|
|
? Enumerable.Empty<string>()
|
|
|
|
|
: Directory.EnumerateFiles(pluginPath, "*.dll", SearchOption.AllDirectories);
|
2021-10-06 21:48:17 +00:00
|
|
|
|
var assemblies = GetAssemblies(dllFileNames, loadSetting);
|
2018-05-13 19:49:26 +00:00
|
|
|
|
var pluginTypes = GetPluginsOfType<T>(assemblies);
|
|
|
|
|
return LoadPlugins<T>(pluginTypes);
|
|
|
|
|
}
|
2018-07-24 04:36:26 +00:00
|
|
|
|
|
2020-10-18 18:02:39 +00:00
|
|
|
|
private static IEnumerable<T> LoadPlugins<T>(IEnumerable<Type> pluginTypes) where T : class
|
2018-05-13 19:49:26 +00:00
|
|
|
|
{
|
2020-10-18 18:02:39 +00:00
|
|
|
|
foreach (var t in pluginTypes)
|
|
|
|
|
{
|
2021-08-13 22:37:21 +00:00
|
|
|
|
T? activate;
|
|
|
|
|
try { activate = (T?)Activator.CreateInstance(t); }
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
2021-09-17 00:04:34 +00:00
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{t.Name}]: {t.FullName}");
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(ex.Message);
|
2021-08-13 22:37:21 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2020-10-18 18:02:39 +00:00
|
|
|
|
if (activate != null)
|
|
|
|
|
yield return activate;
|
|
|
|
|
}
|
2018-05-13 19:49:26 +00:00
|
|
|
|
}
|
2018-07-24 04:36:26 +00:00
|
|
|
|
|
2021-10-06 21:48:17 +00:00
|
|
|
|
private static IEnumerable<Assembly> GetAssemblies(IEnumerable<string> dllFileNames, PluginLoadSetting loadSetting)
|
2018-05-13 19:49:26 +00:00
|
|
|
|
{
|
2021-10-06 21:48:17 +00:00
|
|
|
|
var assemblies = dllFileNames.Select(GetPluginLoadMethod(loadSetting));
|
|
|
|
|
if (loadSetting is PluginLoadSetting.LoadFromMerged or PluginLoadSetting.LoadFileMerged or PluginLoadSetting.UnsafeMerged)
|
|
|
|
|
assemblies = assemblies.Concat(new[] { Assembly.GetExecutingAssembly() }); // load merged too
|
2018-07-24 04:36:26 +00:00
|
|
|
|
return assemblies;
|
2018-05-13 19:49:26 +00:00
|
|
|
|
}
|
2018-07-24 04:36:26 +00:00
|
|
|
|
|
2021-10-06 21:48:17 +00:00
|
|
|
|
private static Func<string, Assembly> GetPluginLoadMethod(PluginLoadSetting pls) => pls switch
|
|
|
|
|
{
|
|
|
|
|
PluginLoadSetting.LoadFrom or PluginLoadSetting.LoadFromMerged => Assembly.LoadFrom,
|
|
|
|
|
PluginLoadSetting.LoadFile or PluginLoadSetting.LoadFileMerged => Assembly.LoadFile,
|
|
|
|
|
PluginLoadSetting.UnsafeLoadFrom or PluginLoadSetting.UnsafeMerged => Assembly.UnsafeLoadFrom,
|
|
|
|
|
_ => throw new NotImplementedException($"PluginLoadSetting: {pls} method not defined."),
|
|
|
|
|
};
|
|
|
|
|
|
2018-05-13 19:49:26 +00:00
|
|
|
|
private static IEnumerable<Type> GetPluginsOfType<T>(IEnumerable<Assembly> assemblies)
|
|
|
|
|
{
|
|
|
|
|
var pluginType = typeof(T);
|
2021-03-14 23:16:55 +00:00
|
|
|
|
return assemblies.SelectMany(z => GetPluginTypes(z, pluginType));
|
2018-07-24 04:36:26 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static IEnumerable<Type> GetPluginTypes(Assembly z, Type pluginType)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
2021-11-27 21:54:38 +00:00
|
|
|
|
// Handle Costura merged plugin dll's; need to Attach for them to correctly retrieve their dependencies.
|
|
|
|
|
var assemblyLoaderType = z.GetType("Costura.AssemblyLoader", false);
|
|
|
|
|
var attachMethod = assemblyLoaderType?.GetMethod("Attach", BindingFlags.Static | BindingFlags.Public);
|
|
|
|
|
attachMethod?.Invoke(null, Array.Empty<object>());
|
|
|
|
|
|
2018-07-24 04:36:26 +00:00
|
|
|
|
var types = z.GetTypes();
|
|
|
|
|
return types.Where(type => IsTypePlugin(type, pluginType));
|
|
|
|
|
}
|
2020-09-19 05:11:13 +00:00
|
|
|
|
// User plugins can be out of date, with mismatching API surfaces.
|
2018-07-24 04:36:26 +00:00
|
|
|
|
catch (Exception ex)
|
2018-05-13 19:49:26 +00:00
|
|
|
|
{
|
2021-09-17 00:04:34 +00:00
|
|
|
|
System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}");
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(ex.Message);
|
|
|
|
|
if (ex is ReflectionTypeLoadException rtle)
|
|
|
|
|
{
|
|
|
|
|
foreach (var le in rtle.LoaderExceptions)
|
|
|
|
|
{
|
|
|
|
|
if (le is not null)
|
|
|
|
|
System.Diagnostics.Debug.WriteLine(le.Message);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Array.Empty<Type>();
|
2018-05-13 19:49:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-24 04:36:26 +00:00
|
|
|
|
|
|
|
|
|
private static bool IsTypePlugin(Type type, Type pluginType)
|
|
|
|
|
{
|
|
|
|
|
if (type.IsInterface || type.IsAbstract)
|
|
|
|
|
return false;
|
2020-10-18 18:02:39 +00:00
|
|
|
|
var name = pluginType.FullName;
|
|
|
|
|
if (name == null)
|
|
|
|
|
return false;
|
|
|
|
|
if (type.GetInterface(name) == null)
|
2018-07-24 04:36:26 +00:00
|
|
|
|
return false;
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2018-05-13 19:49:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|