2020-09-21 05:44:14 +00:00
|
|
|
|
using Flow.Launcher.Infrastructure;
|
|
|
|
|
|
using System;
|
2021-04-12 10:41:30 +00:00
|
|
|
|
using System.Collections.Concurrent;
|
2021-02-24 12:36:32 +00:00
|
|
|
|
using System.Collections.Generic;
|
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
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly AssemblyDependencyResolver dependencyResolver;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly AssemblyName assemblyName;
|
|
|
|
|
|
|
2021-04-12 11:34:58 +00:00
|
|
|
|
private static readonly ConcurrentDictionary<string, byte> loadedAssembly;
|
2021-02-24 12:36:32 +00:00
|
|
|
|
|
2021-02-24 04:44:42 +00:00
|
|
|
|
static PluginAssemblyLoader()
|
|
|
|
|
|
{
|
2021-04-12 10:41:30 +00:00
|
|
|
|
var currentAssemblies = AppDomain.CurrentDomain.GetAssemblies();
|
2021-04-12 11:34:58 +00:00
|
|
|
|
loadedAssembly = new ConcurrentDictionary<string, byte>(
|
|
|
|
|
|
currentAssemblies.Select(x => new KeyValuePair<string, byte>(x.FullName, default)));
|
2021-04-12 10:41:30 +00:00
|
|
|
|
|
2021-02-24 12:36:32 +00:00
|
|
|
|
AppDomain.CurrentDomain.AssemblyLoad += (sender, args) =>
|
|
|
|
|
|
{
|
2021-04-12 11:34:58 +00:00
|
|
|
|
loadedAssembly[args.LoadedAssembly.FullName] = default;
|
2021-02-24 12:36:32 +00:00
|
|
|
|
};
|
2021-02-24 04:44:42 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-09-16 11:12:43 +00:00
|
|
|
|
internal PluginAssemblyLoader(string assemblyFilePath)
|
|
|
|
|
|
{
|
|
|
|
|
|
dependencyResolver = new AssemblyDependencyResolver(assemblyFilePath);
|
|
|
|
|
|
assemblyName = new AssemblyName(Path.GetFileNameWithoutExtension(assemblyFilePath));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal Assembly LoadAssemblyAndDependencies()
|
|
|
|
|
|
{
|
|
|
|
|
|
return LoadFromAssemblyName(assemblyName);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
protected override Assembly Load(AssemblyName assemblyName)
|
|
|
|
|
|
{
|
|
|
|
|
|
string assemblyPath = dependencyResolver.ResolveAssemblyToPath(assemblyName);
|
|
|
|
|
|
|
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
|
2021-02-24 04:46:51 +00:00
|
|
|
|
if (assemblyPath == null || ExistsInReferencedPackage(assemblyName))
|
2020-09-21 05:44:14 +00:00
|
|
|
|
return null;
|
2021-01-02 09:25:13 +00:00
|
|
|
|
|
2020-09-21 05:44:14 +00:00
|
|
|
|
return LoadFromAssemblyPath(assemblyPath);
|
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
|
|
|
|
}
|
2020-09-21 05:44:14 +00:00
|
|
|
|
|
2021-02-24 04:46:51 +00:00
|
|
|
|
internal bool ExistsInReferencedPackage(AssemblyName assemblyName)
|
2020-09-21 05:44:14 +00:00
|
|
|
|
{
|
2021-04-12 10:41:30 +00:00
|
|
|
|
return loadedAssembly.ContainsKey(assemblyName.FullName);
|
2020-09-21 05:44:14 +00:00
|
|
|
|
}
|
2020-09-16 11:12:43 +00:00
|
|
|
|
}
|
2021-02-24 10:51:44 +00:00
|
|
|
|
}
|