服务端的TigerApi 框架,基于.NET6 2024 版本
Rodney Chen
2024-10-24 38bde3e8210c41e9d0d219daba0c075cd676efce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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
}