using System;
using System.Reflection;
using System.Runtime.Loader;

namespace PSCHelpdesk.Plugins;

internal class PluginLoadContext : AssemblyLoadContext
{
    private readonly AssemblyDependencyResolver resolver;

    public PluginLoadContext(string pluginPath)
    {
        this.resolver = new AssemblyDependencyResolver(pluginPath);
    }

    /// <inheritdoc/>
    protected override Assembly Load(AssemblyName assemblyName)
    {
        string assemblyPath = this.resolver.ResolveAssemblyToPath(assemblyName);
        if (assemblyPath != null)
        {
            return this.LoadFromAssemblyPath(assemblyPath);
        }

        return null;
    }

    /// <inheritdoc/>
    protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
    {
        string libraryPath = this.resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
        if (libraryPath != null)
        {
            return this.LoadUnmanagedDllFromPath(libraryPath);
        }

        return IntPtr.Zero;
    }
}