feat(core): add PluginLoader with collectible ALC
This commit is contained in:
parent
9dfbc05052
commit
a7f0732026
8 changed files with 226 additions and 0 deletions
30
src/AcDream.Core/Plugins/PluginAssemblyLoadContext.cs
Normal file
30
src/AcDream.Core/Plugins/PluginAssemblyLoadContext.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace AcDream.Core.Plugins;
|
||||
|
||||
/// <summary>
|
||||
/// Collectible ALC for a single plugin. Resolves assemblies from the plugin's
|
||||
/// own directory EXCEPT for AcDream.Plugin.Abstractions, which must come from
|
||||
/// the default (host) context so type identity for IAcDreamPlugin is preserved.
|
||||
/// </summary>
|
||||
internal sealed class PluginAssemblyLoadContext : AssemblyLoadContext
|
||||
{
|
||||
private readonly AssemblyDependencyResolver _resolver;
|
||||
|
||||
public PluginAssemblyLoadContext(string pluginDirectory, string pluginEntryPath)
|
||||
: base(name: pluginDirectory, isCollectible: true)
|
||||
{
|
||||
_resolver = new AssemblyDependencyResolver(pluginEntryPath);
|
||||
}
|
||||
|
||||
protected override Assembly? Load(AssemblyName assemblyName)
|
||||
{
|
||||
// Share the abstractions assembly with the host — do NOT reload it in the plugin ALC
|
||||
if (assemblyName.Name == "AcDream.Plugin.Abstractions")
|
||||
return null;
|
||||
|
||||
var path = _resolver.ResolveAssemblyToPath(assemblyName);
|
||||
return path is null ? null : LoadFromAssemblyPath(path);
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue