using Rhea.Common;
|
using System.Collections.Generic;
|
using System.Reflection;
|
using Tiger.Model.Minsun;
|
using Tiger.Model;
|
using System.Linq;
|
using Autofac;
|
using System.IO;
|
using System.Text.RegularExpressions;
|
using System;
|
using System.Threading;
|
using System.Runtime.Loader;
|
using Tiger.IBusiness;
|
|
namespace Tiger.Api
|
{
|
public class PluginManager
|
{
|
public static List<Plugin> Plugins { get; set; } = new();
|
|
public static List<Assembly> Load(string path, params string[] patterns)
|
{
|
List<Assembly> assemblies = new();
|
foreach (var file in Directory.EnumerateFiles(path, "*.dll", SearchOption.AllDirectories).Where(filename => patterns.Any(pattern => Regex.IsMatch(filename, pattern))))
|
{
|
var plugin = new Plugin(file);
|
assemblies.Add(plugin.Assembly);
|
Plugins.Add(plugin);
|
}
|
return assemblies;
|
}
|
|
}//endClass
|
|
public class Plugin
|
{
|
public Plugin(string path)
|
{
|
Load(path);
|
}
|
|
#region Variables
|
|
#endregion
|
|
#region Propertys
|
public string Id { get; set; } = Guid.NewGuid().ToString("N");
|
public string Name { get; set; }
|
public string FullPath { get; set; }
|
public bool IsActive { get; set; }
|
public AssemblyLoadContext ALC { get; set; }
|
public Assembly Assembly { get; set; }
|
#endregion
|
|
#region Functions
|
/// <summary>
|
/// 加载
|
/// </summary>
|
private void Load(string fullPath)
|
{
|
FullPath = fullPath;
|
//var file = new FileInfo(path);
|
Name = Path.GetFileNameWithoutExtension(FullPath);
|
|
//ALC = new AssemblyLoadContext(Name, true);
|
//Assembly = ALC.LoadFromAssemblyPath(Path);
|
//ALC.Unloading += context => {
|
// IsActive = false;
|
// Logger.Default.Info($"卸载{context.Name}程序集:" + string.Join(',', context.Assemblies.Select(x => x.FullName)));
|
//};
|
ALC = AssemblyLoadContext.Default;
|
Assembly = ALC.LoadFromAssemblyPath(FullPath);
|
//Assembly.LoadFrom(path);
|
|
IsActive = true;
|
}
|
|
/// <summary>
|
/// 卸载
|
/// </summary>
|
public void Unload()
|
{
|
ALC.Unload();
|
}
|
|
/// <summary>
|
/// 重新加载
|
/// </summary>
|
public void Reload()
|
{
|
|
}
|
|
#endregion
|
}//endClass
|
}
|