mirror of
https://github.com/kwsch/PKHeX
synced 2024-11-23 12:33:06 +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()
|
||||
{
|
||||
#if !MERGED // merged should load dlls from within too, folder is no longer required
|
||||
if (!Directory.Exists(PluginPath))
|
||||
return;
|
||||
#endif
|
||||
Plugins.AddRange(PluginLoader.LoadPlugins<IPlugin>(PluginPath));
|
||||
foreach (var p in Plugins.OrderBy(z => z.Priority))
|
||||
p.Initialize(C_SAV, PKME_Tabs, menuStrip1);
|
||||
|
|
|
@ -15,38 +15,52 @@ namespace PKHeX.WinForms
|
|||
var pluginTypes = GetPluginsOfType<T>(assemblies);
|
||||
return LoadPlugins<T>(pluginTypes);
|
||||
}
|
||||
|
||||
private static IEnumerable<T> LoadPlugins<T>(IEnumerable<Type> pluginTypes)
|
||||
{
|
||||
return pluginTypes.Select(type => (T)Activator.CreateInstance(type));
|
||||
}
|
||||
|
||||
private static IEnumerable<Assembly> GetAssemblies(IEnumerable<string> dllFileNames)
|
||||
{
|
||||
#if UNSAFEDLL
|
||||
return dllFileNames.Select(Assembly.UnsafeLoadFrom);
|
||||
var assemblies = dllFileNames.Select(Assembly.UnsafeLoadFrom);
|
||||
#else
|
||||
return dllFileNames.Select(Assembly.LoadFrom);
|
||||
var assemblies = dllFileNames.Select(Assembly.LoadFrom);
|
||||
#endif
|
||||
#if MERGED
|
||||
assemblies = assemblies.Concat(new[] { Assembly.GetExecutingAssembly() }); // load merged too
|
||||
#endif
|
||||
return assemblies;
|
||||
}
|
||||
|
||||
private static IEnumerable<Type> GetPluginsOfType<T>(IEnumerable<Assembly> assemblies)
|
||||
{
|
||||
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)
|
||||
{
|
||||
Type[] types; try { types = z.GetTypes(); }
|
||||
try
|
||||
{
|
||||
var types = z.GetTypes();
|
||||
return types.Where(type => IsTypePlugin(type, pluginType));
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message);
|
||||
continue;
|
||||
System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message);
|
||||
return Enumerable.Empty<Type>();
|
||||
}
|
||||
foreach (Type type in types)
|
||||
}
|
||||
|
||||
private static bool IsTypePlugin(Type type, Type pluginType)
|
||||
{
|
||||
if (type.IsInterface || type.IsAbstract)
|
||||
continue;
|
||||
return false;
|
||||
if (type.GetInterface(pluginType.FullName) == null)
|
||||
continue;
|
||||
yield return type;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue