mirror of https://github.com/lidarr/Lidarr
42 lines
1.1 KiB
C#
42 lines
1.1 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Reflection;
|
|
using System.Runtime.Loader;
|
|
|
|
namespace NzbDrone.Common.Composition
|
|
{
|
|
public class PluginLoadContext : AssemblyLoadContext
|
|
{
|
|
private AssemblyDependencyResolver _resolver;
|
|
|
|
public PluginLoadContext(string pluginPath)
|
|
: base(isCollectible: true)
|
|
{
|
|
_resolver = new AssemblyDependencyResolver(pluginPath);
|
|
}
|
|
|
|
protected override Assembly Load(AssemblyName assemblyName)
|
|
{
|
|
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
|
|
if (assemblyPath != null)
|
|
{
|
|
using var fs = new FileStream(assemblyPath, FileMode.Open, FileAccess.Read);
|
|
return LoadFromStream(fs);
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
|
|
{
|
|
var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
|
|
if (libraryPath != null)
|
|
{
|
|
return LoadUnmanagedDllFromPath(libraryPath);
|
|
}
|
|
|
|
return IntPtr.Zero;
|
|
}
|
|
}
|
|
}
|