mirror of
https://github.com/Flow-Launcher/Flow.Launcher.git
synced 2026-03-11 08:54:32 +00:00
Merge pull request #367 from taooceros/AvoidDuplicateAssemblyLoad
avoid duplicate assembly load
This commit is contained in:
commit
0683640cfe
1 changed files with 21 additions and 13 deletions
|
|
@ -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<Assembly> loadedAssembly;
|
||||
|
||||
static PluginAssemblyLoader()
|
||||
{
|
||||
loadedAssembly = new List<Assembly>(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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue