mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-26 22:10:21 +00:00
Allow plugins to be activated if merged flag set
reduce some nesting
This commit is contained in:
parent
8cb82560f6
commit
b59ebb3002
2 changed files with 33 additions and 17 deletions
|
@ -259,8 +259,10 @@ namespace PKHeX.WinForms
|
||||||
}
|
}
|
||||||
private void FormLoadPlugins()
|
private void FormLoadPlugins()
|
||||||
{
|
{
|
||||||
|
#if !MERGED // merged should load dlls from within too, folder is no longer required
|
||||||
if (!Directory.Exists(PluginPath))
|
if (!Directory.Exists(PluginPath))
|
||||||
return;
|
return;
|
||||||
|
#endif
|
||||||
Plugins.AddRange(PluginLoader.LoadPlugins<IPlugin>(PluginPath));
|
Plugins.AddRange(PluginLoader.LoadPlugins<IPlugin>(PluginPath));
|
||||||
foreach (var p in Plugins.OrderBy(z => z.Priority))
|
foreach (var p in Plugins.OrderBy(z => z.Priority))
|
||||||
p.Initialize(C_SAV, PKME_Tabs, menuStrip1);
|
p.Initialize(C_SAV, PKME_Tabs, menuStrip1);
|
||||||
|
|
|
@ -15,38 +15,52 @@ namespace PKHeX.WinForms
|
||||||
var pluginTypes = GetPluginsOfType<T>(assemblies);
|
var pluginTypes = GetPluginsOfType<T>(assemblies);
|
||||||
return LoadPlugins<T>(pluginTypes);
|
return LoadPlugins<T>(pluginTypes);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<T> LoadPlugins<T>(IEnumerable<Type> pluginTypes)
|
private static IEnumerable<T> LoadPlugins<T>(IEnumerable<Type> pluginTypes)
|
||||||
{
|
{
|
||||||
return pluginTypes.Select(type => (T)Activator.CreateInstance(type));
|
return pluginTypes.Select(type => (T)Activator.CreateInstance(type));
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<Assembly> GetAssemblies(IEnumerable<string> dllFileNames)
|
private static IEnumerable<Assembly> GetAssemblies(IEnumerable<string> dllFileNames)
|
||||||
{
|
{
|
||||||
#if UNSAFEDLL
|
#if UNSAFEDLL
|
||||||
return dllFileNames.Select(Assembly.UnsafeLoadFrom);
|
var assemblies = dllFileNames.Select(Assembly.UnsafeLoadFrom);
|
||||||
#else
|
#else
|
||||||
return dllFileNames.Select(Assembly.LoadFrom);
|
var assemblies = dllFileNames.Select(Assembly.LoadFrom);
|
||||||
#endif
|
#endif
|
||||||
|
#if MERGED
|
||||||
|
assemblies = assemblies.Concat(new[] { Assembly.GetExecutingAssembly() }); // load merged too
|
||||||
|
#endif
|
||||||
|
return assemblies;
|
||||||
}
|
}
|
||||||
|
|
||||||
private static IEnumerable<Type> GetPluginsOfType<T>(IEnumerable<Assembly> assemblies)
|
private static IEnumerable<Type> GetPluginsOfType<T>(IEnumerable<Assembly> assemblies)
|
||||||
{
|
{
|
||||||
var pluginType = typeof(T);
|
var pluginType = typeof(T);
|
||||||
foreach (var z in assemblies.Where(z => z != null))
|
return assemblies.Where(z => z != null).SelectMany(z => GetPluginTypes(z, pluginType));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IEnumerable<Type> GetPluginTypes(Assembly z, Type pluginType)
|
||||||
|
{
|
||||||
|
try
|
||||||
{
|
{
|
||||||
Type[] types; try { types = z.GetTypes(); }
|
var types = z.GetTypes();
|
||||||
catch (Exception ex)
|
return types.Where(type => IsTypePlugin(type, pluginType));
|
||||||
{
|
|
||||||
Console.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
foreach (Type type in types)
|
|
||||||
{
|
|
||||||
if (type.IsInterface || type.IsAbstract)
|
|
||||||
continue;
|
|
||||||
if (type.GetInterface(pluginType.FullName) == null)
|
|
||||||
continue;
|
|
||||||
yield return type;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message);
|
||||||
|
return Enumerable.Empty<Type>();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static bool IsTypePlugin(Type type, Type pluginType)
|
||||||
|
{
|
||||||
|
if (type.IsInterface || type.IsAbstract)
|
||||||
|
return false;
|
||||||
|
if (type.GetInterface(pluginType.FullName) == null)
|
||||||
|
return false;
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue