diff --git a/Flow.Launcher.Core/Plugin/PluginAssemblyLoader.cs b/Flow.Launcher.Core/Plugin/PluginAssemblyLoader.cs index 273698b86..f473cbc65 100644 --- a/Flow.Launcher.Core/Plugin/PluginAssemblyLoader.cs +++ b/Flow.Launcher.Core/Plugin/PluginAssemblyLoader.cs @@ -1,5 +1,6 @@ using Flow.Launcher.Infrastructure; using System; +using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; @@ -11,17 +12,23 @@ namespace Flow.Launcher.Core.Plugin { private readonly AssemblyDependencyResolver dependencyResolver; - private readonly AssemblyDependencyResolver referencedPluginPackageDependencyResolver; - private readonly AssemblyName assemblyName; + private static readonly List loadedAssembly; + + static PluginAssemblyLoader() + { + loadedAssembly = new List(AppDomain.CurrentDomain.GetAssemblies()); + AppDomain.CurrentDomain.AssemblyLoad += (sender, args) => + { + loadedAssembly.Add(args.LoadedAssembly); + }; + } + internal PluginAssemblyLoader(string assemblyFilePath) { dependencyResolver = new AssemblyDependencyResolver(assemblyFilePath); assemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(assemblyFilePath)); - - referencedPluginPackageDependencyResolver = - new AssemblyDependencyResolver(Path.Combine(Constant.ProgramDirectory, "Flow.Launcher.Plugin.dll")); } internal Assembly LoadAssemblyAndDependencies() @@ -33,10 +40,10 @@ namespace Flow.Launcher.Core.Plugin { string assemblyPath = dependencyResolver.ResolveAssemblyToPath(assemblyName); - // When resolving dependencies, ignore assembly depenedencies that already exits with Flow.Launcher.Plugin - // Otherwise will get unexpected behaviour with plugins, e.g. JsonIgnore attribute not honored in WebSearch or other plugins - // that use Newtonsoft.Json - if (assemblyPath == null || ExistsInReferencedPluginPackage(assemblyName)) + // When resolving dependencies, ignore assembly depenedencies that already exits with Flow.Launcher + // Otherwise duplicate assembly will be loaded and some weird behavior will occur, such as WinRT.Runtime.dll + // will fail due to loading multiple versions in process, each with their own static instance of registration state + if (assemblyPath == null || ExistsInReferencedPackage(assemblyName)) return null; return LoadFromAssemblyPath(assemblyPath); @@ -45,13 +52,14 @@ namespace Flow.Launcher.Core.Plugin internal Type FromAssemblyGetTypeOfInterface(Assembly assembly, params Type[] types) { var allTypes = assembly.ExportedTypes; - return allTypes.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Intersect(types).Any()); } - internal bool ExistsInReferencedPluginPackage(AssemblyName assemblyName) + internal bool ExistsInReferencedPackage(AssemblyName assemblyName) { - return referencedPluginPackageDependencyResolver.ResolveAssemblyToPath(assemblyName) != null; + if (loadedAssembly.Any(a => a.FullName == assemblyName.FullName)) + return true; + return false; } } -} +} \ No newline at end of file