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
34
src/AcDream.Core/Plugins/PluginLoader.cs
Normal file
34
src/AcDream.Core/Plugins/PluginLoader.cs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
using System.Reflection;
|
||||
using AcDream.Plugin.Abstractions;
|
||||
|
||||
namespace AcDream.Core.Plugins;
|
||||
|
||||
public static class PluginLoader
|
||||
{
|
||||
public static LoadedPlugin Load(string pluginDirectory, PluginManifest manifest, IPluginHost host)
|
||||
{
|
||||
var dllPath = Path.Combine(pluginDirectory, manifest.EntryDll);
|
||||
if (!File.Exists(dllPath))
|
||||
return new LoadedPlugin(manifest, null, $"entry dll not found: {dllPath}");
|
||||
|
||||
try
|
||||
{
|
||||
var alc = new PluginAssemblyLoadContext(pluginDirectory, dllPath);
|
||||
var asm = alc.LoadFromAssemblyPath(dllPath);
|
||||
|
||||
var pluginType = asm.GetTypes()
|
||||
.FirstOrDefault(t => !t.IsAbstract && typeof(IAcDreamPlugin).IsAssignableFrom(t));
|
||||
|
||||
if (pluginType is null)
|
||||
return new LoadedPlugin(manifest, null, $"no IAcDreamPlugin implementation found in {manifest.EntryDll}");
|
||||
|
||||
var instance = (IAcDreamPlugin)Activator.CreateInstance(pluginType)!;
|
||||
instance.Initialize(host);
|
||||
return new LoadedPlugin(manifest, instance, null);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
return new LoadedPlugin(manifest, null, $"{ex.GetType().Name}: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue