diff --git a/PKHeX.WinForms/MainWindow/Main.cs b/PKHeX.WinForms/MainWindow/Main.cs index 40c02a980..33eb865bf 100644 --- a/PKHeX.WinForms/MainWindow/Main.cs +++ b/PKHeX.WinForms/MainWindow/Main.cs @@ -259,8 +259,10 @@ private void FormLoadConfig(out bool BAKprompt, out bool showChangelog) } 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(PluginPath)); foreach (var p in Plugins.OrderBy(z => z.Priority)) p.Initialize(C_SAV, PKME_Tabs, menuStrip1); diff --git a/PKHeX.WinForms/MainWindow/PluginLoader.cs b/PKHeX.WinForms/MainWindow/PluginLoader.cs index 15da10e3c..3e46a01a7 100644 --- a/PKHeX.WinForms/MainWindow/PluginLoader.cs +++ b/PKHeX.WinForms/MainWindow/PluginLoader.cs @@ -15,38 +15,52 @@ public static IEnumerable LoadPlugins(string pluginPath) var pluginTypes = GetPluginsOfType(assemblies); return LoadPlugins(pluginTypes); } + private static IEnumerable LoadPlugins(IEnumerable pluginTypes) { return pluginTypes.Select(type => (T)Activator.CreateInstance(type)); } + private static IEnumerable GetAssemblies(IEnumerable 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 GetPluginsOfType(IEnumerable 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 GetPluginTypes(Assembly z, Type pluginType) + { + try { - Type[] types; try { types = z.GetTypes(); } - catch (Exception ex) - { - 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; - } + var types = z.GetTypes(); + return types.Where(type => IsTypePlugin(type, pluginType)); } + catch (Exception ex) + { + System.Diagnostics.Debug.WriteLine($"Unable to load plugin [{pluginType.Name}]: {z.FullName}", ex.Message); + return Enumerable.Empty(); + } + } + + 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; } } }