2023-04-25 12:04:08 +00:00
|
|
|
|
using System;
|
2020-09-16 11:12:43 +00:00
|
|
|
|
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;
|
2020-09-16 11:12:43 +00:00
|
|
|
|
|
2022-03-01 21:14:05 +00:00
|
|
|
|
private readonly AssemblyName assemblyName;
|
2021-02-24 04:44:42 +00:00
|
|
|
|
|
2020-09-16 11:12:43 +00:00
|
|
|
|
internal PluginAssemblyLoader(string assemblyFilePath)
|
|
|
|
|
|
{
|
2022-03-01 21:14:05 +00:00
|
|
|
|
dependencyResolver = new AssemblyDependencyResolver(assemblyFilePath);
|
|
|
|
|
|
assemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(assemblyFilePath));
|
2020-09-16 11:12:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal Assembly LoadAssemblyAndDependencies()
|
|
|
|
|
|
{
|
2022-03-01 21:14:05 +00:00
|
|
|
|
return LoadFromAssemblyName(assemblyName);
|
2020-09-16 11:12:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Assembly Load(AssemblyName assemblyName)
|
|
|
|
|
|
{
|
2022-03-01 21:14:05 +00:00
|
|
|
|
string assemblyPath = dependencyResolver.ResolveAssemblyToPath(assemblyName);
|
2020-09-16 11:12:43 +00:00
|
|
|
|
|
2021-02-24 10:49:01 +00:00
|
|
|
|
// 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);
|
2021-01-02 09:25:13 +00:00
|
|
|
|
|
2022-02-25 20:23:40 +00:00
|
|
|
|
return existAssembly ?? (assemblyPath == null ? null : LoadFromAssemblyPath(assemblyPath));
|
2020-09-16 11:12:43 +00:00
|
|
|
|
}
|
2023-09-11 14:12:41 +00:00
|
|
|
|
|
|
|
|
|
|
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
|
|
|
|
|
|
{
|
|
|
|
|
|
var path = dependencyResolver.ResolveUnmanagedDllToPath(unmanagedDllName);
|
|
|
|
|
|
if (!string.IsNullOrEmpty(path))
|
|
|
|
|
|
{
|
|
|
|
|
|
return LoadUnmanagedDllFromPath(path);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return IntPtr.Zero;
|
|
|
|
|
|
}
|
2020-09-16 11:12:43 +00:00
|
|
|
|
|
2021-03-23 09:25:46 +00:00
|
|
|
|
internal Type FromAssemblyGetTypeOfInterface(Assembly assembly, Type type)
|
2020-09-16 11:12:43 +00:00
|
|
|
|
{
|
|
|
|
|
|
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));
|
2020-09-16 11:12:43 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2023-09-11 14:12:41 +00:00
|
|
|
|
}
|