-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptManager.cs
More file actions
404 lines (384 loc) · 14.5 KB
/
ScriptManager.cs
File metadata and controls
404 lines (384 loc) · 14.5 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
using CSScriptLib;
using NekoBot.Exceptions;
using NekoBot.Interfaces;
using NekoBot.Types;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using Telegram.Bot;
using Telegram.Bot.Types;
using Telegram.Bot.Types.Enums;
using File = System.IO.File;
using Message = NekoBot.Types.Message;
namespace NekoBot
{
public class Script<T>
{
public T? Instance { get; init; }
public Exception? Exception { get; init; }
}
public static partial class ScriptManager
{
public static bool IsCompiling { get; private set; } = false;
static List<IExtension> loadedScripts = new();
static IEvaluator evaluator = CSScript.RoslynEvaluator.Clone();
static void LoadAssembly()
{
var libPath = Path.Combine(Config.ScriptPath, "Library");
if (Directory.Exists(libPath))
{
foreach (var depend in Core.Config.Assembly)
{
var path = Path.Combine(libPath, depend);
if (!File.Exists(path))
{
Core.Debug(DebugType.Warning, $"Assembly \"{depend}\" not found");
continue;
}
try
{
evaluator.ReferenceAssembly(Assembly.LoadFrom(path));
Core.Debug(DebugType.Info, $"Loaded assembly: {path}");
}
catch(Exception e)
{
Core.Debug(DebugType.Error, $"Loading assembly failure: {e}");
}
}
}
}
public static void Init()
{
evaluator.Reset();
if (!Directory.Exists(Config.ScriptPath))
return;
try
{
LoadAssembly();
var scripts = GetScripts();
var loader = new ScriptLoader(scripts.Select(x => x.Info).ToList());
var loadOrder = loader.GetLoadOrder();
loadOrder.Reverse();
foreach (var name in loadOrder)
{
var obj = scripts.Find(x => x.Info.Name == name);
AddExtension(obj);
}
}
catch (Exception e)
{
Core.Debug(DebugType.Error, $"Loading script failure:\n{e}");
}
}
public static void Save()
{
foreach (var obj in loadedScripts)
obj.Save();
}
/// <summary>
/// 重新加载所有Script
/// </summary>
/// <param name="userMsg"></param>
public static async void Reload(Message userMsg)
{
IsCompiling = true;
var msg = (await userMsg.Reply("Reloading Script..."))!;
try
{
List<IExtension> newScripts = GetScripts(s => msg.Edit(s));
var oldScripts = new List<IExtension>(loadedScripts);
var loader = new ScriptLoader(newScripts.Select(x => x.Info).ToList());
var loadOrder = loader.GetLoadOrder();
loadOrder.Reverse();
foreach (var old in oldScripts)
RemoveExtension(old);
foreach (var newScript in loadOrder)
AddExtension(newScripts.Find(x => x.Info.Name == newScript));
UpdateCommand();
var scripts = string.Join("\n- ", GetLoadedScript());
var _ =
$"""
Scripts have been loaded:
- {scripts}
""";
await msg.Edit(
$"""
```python
{Extension.StringHandle(_)}
```
"""
,ParseMode.MarkdownV2);
GC.Collect();
}
catch(Exception e)
{
await msg.Edit("Reload script failure");
Core.Debug(DebugType.Error, $"Reload script failure:\n{e}");
}
IsCompiling = false;
}
public static void MessageHandle(in ITelegramBotClient client,Update update)
{
var type = update.Type;
var handlerName = $"{type}Handler";
var index = loadedScripts.FindIndex(x => x.Info.Name == handlerName);
if (index != -1)
{
var ext = loadedScripts[index];
if (ext is IHandler handler)
{
try
{
var foo = handler.Handle(client, loadedScripts, update);
if (foo is not null)
foo();
}
catch(Exception e)
{
Core.Debug(DebugType.Error, $"Module inner exception:\n{e}");
}
}
else
Core.Debug(DebugType.Warning, $"Unknown handler module \"{ext.Info.Name}\",maybe you didn't inherit and implement IHandler?");
}
else
Core.Debug(DebugType.Warning, $"No handler found for handling message type \"{type}\",this message will not be handled");
}
/// <summary>
/// 更新指定Script
/// </summary>
/// <param name="ext"></param>
public static void UpdateScript(IExtension ext)
{
IsCompiling = true;
var loadedExt = GetExtension(ext.Info.Name);
if(loadedExt is not null)
RemoveExtension(loadedExt);
AddExtension(ext);
GC.Collect();
IsCompiling = false;
}
/// <summary>
/// 执行传入的C#代码,并返回String
/// </summary>
/// <param name="code"></param>
/// <returns></returns>
public static string? EvalCode(string code)
{
try
{
var eval = CSScript.RoslynEvaluator.Clone().Reset(false);
return ((object)eval.Eval(code))?.ToString();
}
catch(Exception e)
{
return e.ToString();
}
}
/// <summary>
/// 编译指定的C#文件
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="filePath"></param>
/// <returns>第一个类的实例</returns>
public static Script<T> CompileScript<T>(string filePath) where T:class
{
try
{
return new Script<T>()
{
Instance = CSScript.Evaluator.LoadFile<T>(filePath),
Exception = null
};
}
catch(Exception e)
{
return new Script<T>()
{
Instance = null,
Exception = e
};
}
}
/// <summary>
/// 更新Bot的Command列表
/// </summary>
public static async void UpdateCommand()
{
var result = loadedScripts.SelectMany(x => x.Info.Commands);
Core.BotCommands = result.ToArray();
await Core.GetClient().SetMyCommandsAsync(result);
Core.Debug(DebugType.Info,"Bot commands has been updated");
}
}
public static partial class ScriptManager
{
/// <summary>
/// 根据Name获取Extension
/// </summary>
/// <param name="extName"></param>
/// <returns>IExtension的实例,不存在则返回null</returns>
public static IExtension? GetExtension(string extName) => loadedScripts.Find(x => x.Info.Name == extName);
/// <summary>
/// 加载并初始化该Extension
/// </summary>
/// <param name="extName"></param>
public static void AddExtension(string extName) => AddExtension(GetExtension(extName));
/// <summary>
/// 加载并初始化该Extension
/// </summary>
/// <param name="ext"></param>
public static void AddExtension(IExtension? ext)
{
if (ext is null) return;
bool isConflict = false;
foreach (var item in ext.Info.Commands)
{
var loadedCmds = loadedScripts.SelectMany(x => x.Info.Commands);
if (loadedCmds.Any( x => x.Command == item.Command))
{
Core.Debug(DebugType.Warning, $"Module \"{ext.Info.Name}(v{ext.Info.Version})\" command list conflicts with had been loaded module: \"{item.Command}\"");
isConflict = true;
}
}
if (isConflict)
return;
loadedScripts.Add(ext);
ext.Init();
GC.Collect(2, GCCollectionMode.Forced);
Core.Debug(DebugType.Info, $"Loaded script : {ext.Info.Name}");
}
/// <summary>
/// 卸载该Extension
/// </summary>
/// <param name="extName"></param>
public static void RemoveExtension(string extName) => RemoveExtension(GetExtension(extName));
/// <summary>
/// 卸载该Extension
/// </summary>
/// <param name="ext"></param>
public static void RemoveExtension(IExtension? ext)
{
if (ext is null || !loadedScripts.Contains(ext)) return;
var name = ext.Info.Name;
loadedScripts.Remove(ext);
if(ext is IDestroyable _ext)
_ext.Destroy();
GC.Collect();
Core.Debug(DebugType.Info, $"Unloaded script : {name}");
}
/// <summary>
/// 获取已加载Script的Name
/// </summary>
/// <returns></returns>
public static string[] GetLoadedScript() => loadedScripts.Select(x => $"{x.Info.Name}(v{x.Info.Version})").ToArray();
static List<IExtension> GetScripts() => GetScripts(s => { });
public static FileInfo[] GetFiles(string path)
{
List<FileInfo> files = new();
Stack<string> dirs = new();
dirs.Push(path);
while(dirs.Count > 0)
{
var dirPath = dirs.Pop();
files.AddRange(Directory.GetFiles(dirPath).Select(x => new FileInfo(x)));
foreach (var dir in Directory.GetDirectories(dirPath))
dirs.Push(dir);
}
return files.ToArray();
}
public static string? GetScriptPath(string extName)
{
return GetFiles(Config.ScriptPath).Where(x => x.Extension is ".csx" or ".cs" && x.Name == $"{extName}.csx")
.Select(x => x.FullName)
.FirstOrDefault();
}
static List<IExtension> GetScripts(Action<string> step)
{
var scriptPaths = GetFiles(Config.ScriptPath).Where(x => x.Extension is ".csx" or ".cs")
.Select(x => x.FullName)
.ToArray();
List<IExtension> uninitObjs = new();
foreach (var path in scriptPaths)
{
try
{
step($"Compiling \"{new FileInfo(path).Name}\"...");
var obj = evaluator.LoadFile<IExtension>(path);
var info = obj.Info;
var conflictObj = uninitObjs.Find(x => x.Info.Name == info.Name);
if (conflictObj is not null)
{
if (conflictObj.Info.Version < info.Version)
{
uninitObjs.Remove(conflictObj);
Core.Debug(DebugType.Info, $"Conflicting scripts, removing: {conflictObj.Info.Name}(v{conflictObj.Info.Version})");
}
else
continue;
}
uninitObjs.Add(obj);
Core.Debug(DebugType.Info, $"Compiled script: {info.Name}(v{info.Version})");
}
catch (Exception e)
{
var name = new FileInfo(path).Name;
Core.Debug(DebugType.Error, $"Compiling script failure ({name}):\n{e}");
step($"Compiling script failure ({name})");
}
}
return uninitObjs;
}
/// <summary>
/// 获取主Assembly的版本号
/// </summary>
/// <returns></returns>
public static System.Version? GetVersion() => Assembly.GetExecutingAssembly().GetName().Version;
static string GetRandomStr() => Convert.ToBase64String(SHA512.HashData(Guid.NewGuid().ToByteArray()));
}
public class ScriptLoader
{
Dictionary<string, ExtensionInfo> scriptInfos;
HashSet<string> visited = new();
HashSet<string> visiting = new();
Stack<string> sortedScripts = new();
public ScriptLoader(List<ExtensionInfo> scriptsList)
{
scriptInfos = scriptsList.ToDictionary(x => x.Name);
}
public List<string> GetLoadOrder()
{
foreach (var scriptInfo in scriptInfos.Values)
{
if (!visited.Contains(scriptInfo.Name))
{
if (!Sort(scriptInfo))
throw new InvalidOperationException("Circular dependency detected");
}
}
return sortedScripts.ToList();
}
bool Sort(ExtensionInfo info)
{
if (visited.Contains(info.Name))
return true;
if (visiting.Contains(info.Name))
return false;
visiting.Add(info.Name);
foreach (var depend in info.Dependencies)
{
if (!scriptInfos.ContainsKey(depend.Name))
throw new DependNotFoundException($"Script \"{info.Name}\" depends on \"{depend.Name}\",but \"{depend.Name}\" is not found");
if (!Sort(scriptInfos[depend.Name]))
return false;
}
visiting.Remove(info.Name);
visited.Add(info.Name);
sortedScripts.Push(info.Name);
return true;
}
}
}