-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompiler.cs
More file actions
724 lines (623 loc) · 24.2 KB
/
Compiler.cs
File metadata and controls
724 lines (623 loc) · 24.2 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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
using System.Diagnostics;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Emit;
using Terraria;
using TShockAPI;
namespace AutoCompile;
#region 编译结果类
public class CompResult
{
public bool Ok;
public string Msg;
public object Data;
private CompResult(bool ok, string msg, object data = null)
{
Ok = ok;
Msg = msg;
Data = data;
}
public static CompResult Success(string msg = "完成", object data = null)
=> new CompResult(true, msg, data);
public static CompResult Fail(string msg)
=> new CompResult(false, msg);
}
#endregion
public class Compiler
{
public static readonly object LockObj = new();
// 静态构造函数,只会执行一次
static Compiler()
{
try
{
// 修复中文编码用的
Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
}
catch (Exception ex)
{
TShock.Log.ConsoleWarn($"【自动编译】 注册编码提供程序失败: {ex.Message}");
}
}
#region 编译主方法
public static CompResult CompAll(string path = "")
{
try
{
var fullPath = string.IsNullOrEmpty(path)
? Path.Combine(Configuration.Paths, "源码")
: Path.GetFullPath(Path.Combine(Configuration.Paths, path));
var safeResult = Utils.CheckFileSize(fullPath);
if (!safeResult.Ok)
return safeResult;
// 创建目录
if (!Directory.Exists(fullPath))
{
Directory.CreateDirectory(fullPath);
return CompResult.Fail("创建目录完成,请放入.cs文件");
}
// 查找文件
var searchOpt = AutoCompile.Config.IncludeSub
? SearchOption.AllDirectories
: SearchOption.TopDirectoryOnly;
var csFiles = Directory.GetFiles(fullPath, "*.cs", searchOpt);
if (csFiles.Length == 0)
return CompResult.Fail("未找到.cs文件");
// 编译
var sw = Stopwatch.StartNew();
CompResult result;
lock (LockObj)
{
result = BuildAll(csFiles);
}
// 停止计时
sw.Stop();
if (result.Ok)
{
var msg = $"已经编译{csFiles.Length}个cs文件 用时:{sw.ElapsedMilliseconds}ms";
if (result.Data is List<string> files)
{
msg += $" 生成{files.Count}个DLL";
}
return CompResult.Success(msg, result.Data);
}
return result;
}
catch (Exception ex)
{
return CompResult.Fail($"编译异常: {ex.Message}");
}
}
#endregion
#region 构建逻辑(核心方法)
private static CompResult BuildAll(string[] files)
{
// 使用局部变量,让它们尽早离开作用域
List<SyntaxTree>? trees = null;
List<string>? skp = null;
List<string>? err = null;
List<MetadataReference> rfs = null;
try
{
Utils.CleanOutFiles(); // 清理旧文件
skp = new List<string>();
err = new List<string>();
trees = new List<SyntaxTree>();
TShock.Log.ConsoleInfo("【自动编译】 开始添加引用...");
rfs = GetMetaRefs(true);
if (rfs.Count == 0) return CompResult.Fail("无有效引用");
TShock.Log.ConsoleInfo($"【自动编译】 已加载 {rfs.Count} 个引用");
int total = files.Length;
int proc = 0;
TShock.Log.ConsoleInfo($"【自动编译】 开始处理 {total} 个源文件...");
// 遍历所有文件
foreach (var f in files)
{
proc++;
try
{
var fi = new FileInfo(f);
if (fi.Length == 0)
{
skp.Add($"{Path.GetFileName(f)} (空)");
continue;
}
var code = Utils.ReadAndFixFile(f);
code = RemoveUsings(code);
if (string.IsNullOrWhiteSpace(code))
{
skp.Add($"{Path.GetFileName(f)} (空白)");
continue;
}
if (!Utils.IsValidCSharpCode(code))
{
skp.Add($"{Path.GetFileName(f)} (无效)");
continue;
}
var uc = AddUsings(code);
// 解析语法树
var tree = CSharpSyntaxTree.ParseText(
text: uc,
options: CSharpParseOptions.Default.WithLanguageVersion(Utils.GetLangVer()),
path: f,
encoding: Encoding.UTF8
);
trees.Add(tree);
// 显示处理进度(每10%显示一次,或者每个文件都显示)
double tage = (double)proc / total * 100;
// 显示进度条
DisplayProgress("解析源码", proc, total, tage);
}
catch (Exception ex)
{
err.Add($"{Path.GetFileName(f)}: {ex.Message}");
TShock.Log.ConsoleError($"【自动编译】 解析 {Path.GetFileName(f)} 失败: {ex.Message}");
// 出错时也显示进度
double tage = (double)proc / total * 100;
DisplayProgress("解析源码", proc, total, tage);
}
}
// 记录跳过的文件
if (skp.Count > 0 || err.Count > 0)
{
LogsMag.LogSkip(skp, err);
}
// 无有效文件
if (trees.Count == 0)
{
var msg = "无有效.cs文件";
if (skp.Count + err.Count > 0)
{
msg += $",跳过了{skp.Count + err.Count}个文件";
}
return CompResult.Fail(msg);
}
TShock.Log.ConsoleInfo($"【自动编译】 解析完成,共 {trees.Count} 个有效文件");
TShock.Log.ConsoleInfo("【自动编译】 开始编译生成DLL...");
// 获取插件名称
var pName = Utils.GetPluginName(trees);
if (string.IsNullOrEmpty(pName)) pName = "MyPlugin";
var outDir = Path.Combine(Configuration.Paths, "编译输出");
var dllName = $"{Utils.CleanName(pName)}.dll";
var dllPath = Path.Combine(outDir, dllName);
var pdbPath = Path.ChangeExtension(dllPath, ".pdb");
// 显示编译进度
TShock.Log.ConsoleInfo($"【自动编译】 正在编译: {pName}");
EmitResult er = CreateComp(trees, rfs, pName, dllPath, pdbPath);
// 编译失败处理
if (!er.Success)
{
// 返回错误信息
return ErrorMess(pName, er);
}
LogsMag.LogCompile(pName, dllPath, pdbPath);
Utils.ClearLogs(); // 成功后清理日志
// 显示成功信息
TShock.Log.ConsoleInfo($"【自动编译】 编译完成: {pName}");
TShock.Log.ConsoleInfo($"【自动编译】 DLL路径: {dllPath}");
return CompResult.Success("编译完成", new List<string> { dllPath });
}
catch (OutOfMemoryException)
{
return CompResult.Fail("内存不足");
}
catch (Exception ex)
{
return CompResult.Fail($"构建失败: {ex.Message}");
}
finally
{
ClearMem(trees, skp, err, rfs); // 清理内存
ClearMetaRefs(); // 清理编译元数据缓存
}
}
#endregion
#region 显示进度条
public static void DisplayProgress(string stage, int curr, int total, double tage)
{
// 每10%显示一次,或者每个文件都显示(根据总数决定)
bool Display = false;
if (total <= 10)
{
// 文件少时每个都显示
Display = true;
}
else if (total <= 50)
{
// 每处理10%显示一次
int step = Math.Max(1, total / 10);
Display = curr % step == 0 || curr == total;
}
else
{
// 文件多时每处理5%显示一次
int step = Math.Max(1, total / 20);
Display = curr % step == 0 || curr == total;
}
if (Display)
{
// 进度条长度
int barWidth = 20;
int progWidth = (int)(barWidth * tage / 100);
string progBar = new string('█', progWidth) +
new string('░', barWidth - progWidth);
// 在同一行显示进度
Console.Write($"\r【自动编译】 {stage}: [{progBar}] {tage:F1}% ({curr}/{total})");
// 如果是最后一个文件,换行
if (curr == total)
{
Console.WriteLine();
}
}
}
#endregion
#region 为代码添加默认 using
public static string AddUsings(string code)
{
if (string.IsNullOrWhiteSpace(code))
return code;
// 从配置中获取默认 using 指令
var defList = AutoCompile.Config.Usings;
// 从配置获取并格式化
var fmtUsgs = Utils.FmtUsings(defList);
if (string.IsNullOrEmpty(fmtUsgs))
return code;
// 检查代码中是否已经有这些 using(避免重复)
var existing = Utils.GetExistUsings(code);
// 过滤掉已经存在的 using
var ToAdd = Utils.FilterUsings(fmtUsgs, existing);
if (string.IsNullOrEmpty(ToAdd))
return code;
// 总是添加到文件最开头
return ToAdd + code;
}
#endregion
#region 移除指定Using语句
public static string RemoveUsings(string code)
{
var rm = AutoCompile.Config.RemoveUsings;
if (rm == null || rm.Count == 0) return code;
// 简单的移除逻辑:直接替换为空
foreach (var to in rm)
{
if (string.IsNullOrWhiteSpace(to))
continue;
// 移除带using关键字的完整语句
string pattern1 = @"^\s*using\s+" + Regex.Escape(to.Trim()) + @"\s*;\s*\r?\n";
code = Regex.Replace(code, pattern1, "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
// 移除不带using关键字的命名空间(可能在已有的using语句中)
string pattern = @"^\s*" + Regex.Escape(to.Trim()) + @"\s*\r?\n";
code = Regex.Replace(code, pattern, "", RegexOptions.Multiline);
// 移除文件最后一行的情况
pattern = @"^\s*" + Regex.Escape(to.Trim()) + @"\s*$";
code = Regex.Replace(code, pattern, "", RegexOptions.Multiline | RegexOptions.IgnoreCase);
}
return code;
}
#endregion
#region 添加系统运行时程序集
public static void AddSystemReferences(HashSet<string> refs)
{
try
{
// 获取.NET运行时的系统程序集目录
var runtime = Path.GetDirectoryName(typeof(object).Assembly.Location);
if (!string.IsNullOrEmpty(runtime))
{
var Asse = AutoCompile.Config.SystemAsse;
foreach (var ass in Asse)
{
var file = Path.Combine(runtime, ass);
if (File.Exists(file) && !refs.Contains(file))
{
refs.Add(file);
}
else
{
TShock.Log.ConsoleError($"【自动编译】 文件不存在 {file} ");
}
}
}
}
catch (Exception ex)
{
TShock.Log.ConsoleError($"【自动编译】 添加系统引用失败: {ex.Message}");
}
}
#endregion
#region 添加TS程序集引用
public static void AddTShockReferences(HashSet<string> refs, bool dll)
{
try
{
var dir = Path.Combine(Configuration.Paths, "程序集");
// 1. 首先添加插件指定“程序集”文件夹中的所有DLL文件
if (Directory.Exists(dir))
{
var dllFiles = Directory.GetFiles(dir, "*.dll", SearchOption.AllDirectories);
foreach (var dllPath in dllFiles)
{
// 确保文件存在且不是重复的
if (File.Exists(dllPath) && !refs.Contains(dllPath))
{
// 跳过可能损坏或无法加载的DLL
if (Utils.IsValidDll(dllPath))
{
refs.Add(dllPath);
}
else
{
TShock.Log.ConsoleWarn($"【自动编译】 跳过无效的程序集: {Path.GetFileName(dllPath)}");
}
}
}
}
// 2.添加 ServerPlugins 文件夹所有DLL(仅当前文件夹,不扫描子文件夹)
var PluginsDir = Path.Combine(typeof(TShock).Assembly.Location, "ServerPlugins");
if (dll)
{
// 如果是编译插件,只添加 TShockAPI.dll(避免文件夹里存在相同插件导致引用错乱)
var path2 = Path.Combine(PluginsDir, "TShockAPI.dll");
if (File.Exists(path2) && !refs.Contains(path2))
{
refs.Add(path2);
}
}
else
{
// 否则编译的是C#脚本,添加所有DLL(方便编写时引用插件本身)
var dllFiles2 = Directory.GetFiles(PluginsDir, "*.dll", SearchOption.TopDirectoryOnly);
foreach (var path2 in dllFiles2)
{
if (File.Exists(path2) && !refs.Contains(path2))
{
refs.Add(path2);
}
}
}
// 3.添加TS运行核心文件(从bin目录)
var OT = new[]
{
"OTAPI.dll",
"OTAPI.Runtime.dll",
"HttpServer.dll",
"ModFramework.dll",
"TerrariaServer.dll"
};
foreach (var f in OT)
{
var binDir = Path.Combine(typeof(TShock).Assembly.Location, "bin");
var path3 = Path.Combine(binDir, f);
if (File.Exists(path3) && !refs.Contains(path3))
{
refs.Add(path3);
}
}
}
catch (Exception ex)
{
TShock.Log.ConsoleError($"【自动编译】 扫描目录失败: {ex.Message}");
}
}
#endregion
#region 创建编译
private static EmitResult CreateComp(List<SyntaxTree>? trees,
List<MetadataReference> rfs,
string pluginName, string dllPath, string pdbPath)
{
try
{
// 创建编译
var comp = CSharpCompilation.Create(
Utils.CleanName(pluginName),
trees,
rfs,
new CSharpCompilationOptions(
OutputKind.DynamicallyLinkedLibrary,
optimizationLevel: OptimizationLevel.Release,
warningLevel: 0,
assemblyIdentityComparer: AssemblyIdentityComparer.Default,
allowUnsafe: true,
platform: Platform.AnyCpu,
checkOverflow: false,
concurrentBuild: true
));
// 添加目标框架特性
string fw = @"[assembly: System.Runtime.Versioning.TargetFramework("".NET6.0"", FrameworkDisplayName = "".NET 6.0"")]";
var fwTree = CSharpSyntaxTree.ParseText(fw,
options: CSharpParseOptions.Default.WithLanguageVersion(Utils.GetLangVer()),
encoding: Encoding.UTF8);
comp = comp.AddSyntaxTrees(fwTree);
using (var dStream = File.Create(dllPath))
using (var pStream = File.Create(pdbPath))
{
EmitResult er = comp.Emit(dStream, pStream);
return er;
}
}
catch (Exception ex)
{
TShock.Log.ConsoleError($"【自动编译】 编译异常: {ex.Message}");
return null;
}
}
#endregion
#region 错误处理
public static CompResult ErrorMess(string pluginName, EmitResult er)
{
try
{
var sb = new StringBuilder();
sb.AppendLine($"\n❌ 编译失败 [{pluginName}]");
sb.AppendLine("-".PadRight(40, '-'));
// 获取错误
var errs = er.Diagnostics
.Where(d => d.Severity == DiagnosticSeverity.Error)
.ToList();
// 按文件分组显示错误
var ByFile = errs
.GroupBy(err => Utils.GetFileName(err))
.OrderBy(g => g.Key)
.ToList();
sb.AppendLine($" 发现 {errs.Count} 个错误,分布在 {ByFile.Count} 个文件中:");
// 只显示文件名和错误数量
foreach (var group in ByFile)
{
var name = group.Key;
var count = group.Count();
sb.AppendLine($" 📁 {name} - {count}个错误");
}
// 记录到控制台
TShock.Log.ConsoleError(sb.ToString());
// 记录到日志文件
LogsMag.LogErrFile(pluginName, errs);
return CompResult.Fail("编译失败");
}
catch (Exception ex)
{
TShock.Log.ConsoleError($"❌ 编译失败 [{pluginName}]");
TShock.Log.ConsoleError($"错误异常: {ex.Message}");
return CompResult.Fail("编译失败");
}
}
#endregion
#region 脚本编译错误处理
public static CompResult ErrorScript(string scriptName, List<Diagnostic> errors)
{
try
{
var sb = new StringBuilder();
sb.AppendLine($"\n❌ 脚本编译失败 [{scriptName}]");
sb.AppendLine("-".PadRight(40, '-'));
var errs = errors.Where(d => d.Severity == DiagnosticSeverity.Error).ToList();
// 按错误类型分组显示
var ByFile = errs
.GroupBy(err => Utils.GetFileName(err))
.OrderBy(g => g.Key)
.ToList();
sb.AppendLine($" 发现 {errs.Count} 个错误,分布在 {ByFile.Count} 个文件中:");
foreach (var group in ByFile)
{
var name = group.Key;
var count = group.Count();
sb.AppendLine($" 📁 {name} - {count}个错误");
}
TShock.Log.ConsoleError(sb.ToString()); // 记录到控制台
LogsMag.LogErrFile(scriptName, errs); // 记录到日志文件
return CompResult.Fail($"脚本编译失败,共{errs.Count}个错误,请查看《自动编译》-《编译日志》");
}
catch (Exception ex)
{
TShock.Log.ConsoleError($"❌ 脚本编译失败 [{scriptName}]");
TShock.Log.ConsoleError($"错误异常: {ex.Message}");
return CompResult.Fail("脚本编译失败");
}
}
#endregion
#region 结束编译清理内存
private static void ClearMem(List<SyntaxTree>? trees, List<string>? skp, List<string>? err, List<MetadataReference>? rfs)
{
try
{
// 1.清理集合,让它们可以被GC
trees?.Clear();
skp?.Clear();
err?.Clear();
// 2. 释放 MetadataReference
if (rfs != null)
{
foreach (var rf in rfs)
{
if (rf is IDisposable disposable)
disposable.Dispose();
}
rfs.Clear();
}
// 2. 分步GC
long before = GC.GetTotalMemory(false);
// 清理第0代和第1代
GC.Collect(0, GCCollectionMode.Forced);
GC.Collect(1, GCCollectionMode.Forced);
// 等待一会儿
Thread.Sleep(50);
// 清理第2代(完整GC)
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
long after = GC.GetTotalMemory(true);
long freed = before - after;
if (freed > 1024 * 1024)
{
TShock.Log.ConsoleInfo($"【内存清理】 释放了 {freed / 1024 / 1024:F2} MB");
}
}
catch (Exception ex)
{
TShock.Log.ConsoleWarn($"【自动编译】 内存清理异常: {ex.Message}");
}
}
#endregion
#region 获取元数据引用
private static List<MetadataReference> metaRefs; // 缓存元数据引用
public static List<MetadataReference> GetMetaRefs(bool dll = false)
{
lock (LockObj)
{
if (metaRefs == null)
{
var refs = new HashSet<string>();
AddTShockReferences(refs, dll);
AddSystemReferences(refs);
var abRefs = new List<string>();
foreach (var r in refs)
{
try
{
var Paths = Path.GetFullPath(r);
if (File.Exists(Paths))
{
abRefs.Add(Paths);
}
}
catch (Exception ex)
{
TShock.Log.ConsoleWarn($"无法将路径转换为绝对路径,跳过: {r}, 错误: {ex.Message}");
}
}
metaRefs = abRefs.Select(r => (MetadataReference)MetadataReference.CreateFromFile(r)).ToList();
}
return metaRefs;
}
}
#endregion
#region 清除元数据引用缓存
public static void ClearMetaRefs()
{
lock (LockObj)
{
if (metaRefs != null)
{
// 显式释放每个MetadataReference
foreach (var metaRef in metaRefs)
{
// MetadataReference没有Dispose,但可清除引用链
// 对于非托管资源,确保释放
if (metaRef is IDisposable disposable)
disposable.Dispose();
}
metaRefs.Clear();
metaRefs = null;
}
// 分代清理策略
GC.Collect(0, GCCollectionMode.Forced);
Thread.Sleep(10);
GC.Collect(1, GCCollectionMode.Forced);
Thread.Sleep(10);
GC.Collect(2, GCCollectionMode.Forced, true);
GC.WaitForPendingFinalizers();
}
}
#endregion
}