PKHeX/PKHeX.WinForms/Util/Plugins/PluginLoadResult.cs
Kurt a384c9f666 plugin load: slightly more error tolerance
if costura load errors, don't abort loading plugins
pre-scan for constructors with no-args so createinstance doesn't throw an exception preventing any other plugins from loading
2026-05-09 17:54:34 -05:00

47 lines
1.3 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace PKHeX.WinForms;
/// <summary>
/// Encapsulates the result of loading plugins, including their contexts and assemblies.
/// </summary>
public sealed class PluginLoadResult
{
public List<PluginLoadContext> Contexts { get; } = [];
public List<Assembly> Assemblies { get; } = [];
/// <summary>
/// Returns all loaded assemblies for downstream use.
/// </summary>
public IEnumerable<Assembly> GetAssemblies() => Assemblies;
public void Load(string pluginFile)
{
var context = new PluginLoadContext(pluginFile);
var asm = context.LoadFromAssemblyPath(pluginFile);
Contexts.Add(context);
Assemblies.Add(asm);
}
public void LoadFromAssembly(Assembly getExecutingAssembly)
{
Assemblies.Add(getExecutingAssembly);
}
public bool Unload(string pluginFile)
{
var context = Contexts.FirstOrDefault(z => z.Assemblies.Any(a => a.Location == pluginFile));
if (context is null)
return false;
context.Unload();
Contexts.Remove(context);
Assemblies.RemoveAll(a => a.Location == pluginFile);
GC.Collect();
GC.WaitForPendingFinalizers();
return true;
}
}