Flow.Launcher/Flow.Launcher.Core/Plugin/PluginAssemblyLoader.cs

47 lines
1.8 KiB
C#
Raw Permalink Normal View History

using Flow.Launcher.Infrastructure;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Loader;
namespace Flow.Launcher.Core.Plugin
{
internal class PluginAssemblyLoader : AssemblyLoadContext
{
2022-03-01 21:14:05 +00:00
private readonly AssemblyDependencyResolver dependencyResolver;
2022-03-01 21:14:05 +00:00
private readonly AssemblyName assemblyName;
internal PluginAssemblyLoader(string assemblyFilePath)
{
2022-03-01 21:14:05 +00:00
dependencyResolver = new AssemblyDependencyResolver(assemblyFilePath);
assemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(assemblyFilePath));
}
internal Assembly LoadAssemblyAndDependencies()
{
2022-03-01 21:14:05 +00:00
return LoadFromAssemblyName(assemblyName);
}
protected override Assembly Load(AssemblyName assemblyName)
{
2022-03-01 21:14:05 +00:00
string assemblyPath = dependencyResolver.ResolveAssemblyToPath(assemblyName);
// When resolving dependencies, ignore assembly depenedencies that already exits with Flow.Launcher
2021-02-24 10:57:18 +00:00
// Otherwise duplicate assembly will be loaded and some weird behavior will occur, such as WinRT.Runtime.dll
2021-02-24 11:01:11 +00:00
// will fail due to loading multiple versions in process, each with their own static instance of registration state
2022-02-25 20:23:40 +00:00
var existAssembly = Default.Assemblies.FirstOrDefault(x => x.FullName == assemblyName.FullName);
2022-02-25 20:23:40 +00:00
return existAssembly ?? (assemblyPath == null ? null : LoadFromAssemblyPath(assemblyPath));
}
2021-03-23 09:25:46 +00:00
internal Type FromAssemblyGetTypeOfInterface(Assembly assembly, Type type)
{
var allTypes = assembly.ExportedTypes;
2021-03-23 09:25:46 +00:00
return allTypes.First(o => o.IsClass && !o.IsAbstract && o.GetInterfaces().Any(t => t == type));
}
}
2021-02-24 10:51:44 +00:00
}