-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPluginLoader.cs
68 lines (58 loc) · 2.19 KB
/
PluginLoader.cs
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
using EleCho.GoCqHttpSdk;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
namespace MiraiGoC
{
class PluginLoader
{
public static void LoadPlugins(CqWsSession _s)
{
if (!Directory.Exists($"plugins"))
{
Program.Logger.LogWarning("插件目录不存在, 创建中...");
Directory.CreateDirectory($"plugins");
}
DirectoryInfo dir = new DirectoryInfo($"plugins");
var fs = dir.GetFiles();
foreach (FileInfo f in fs)
{
// 如果不是动态链接库, 则跳过这个文件
if (!f.Extension.Equals(".dll", StringComparison.OrdinalIgnoreCase))
continue;
try
{
Program.Logger.LogInfo($"加载插件: {f.Name}");
// 利用反射来加载 C# DLL
var pluginAsm = Assembly.LoadFile(f.FullName);
Type[] types = pluginAsm.GetTypes();
foreach (Type type in types)
{
if (!typeof(CqPostPlugin).IsAssignableFrom(type) || !type.Name.Equals("PluginLoadEnter"))
continue;
try
{
object _obj = Activator.CreateInstance(type) ?? throw new Exception("无法创建类型实例");
MethodInfo mf = type.GetMethod("Load") ?? throw new Exception("无法获取插件加载方法");
// 入口:Load
mf.Invoke(_obj, new CqWsSession[] { _s });
}
catch (Exception ex)
{
Program.Logger.LogError(ex.Message);
}
}
}
catch (Exception ex)
{
Program.Logger.LogError(ex.Message);
}
}
}
}
}