三国卡牌客户端基础资源仓库
lcy
2026-04-28 bcb2550e54cbbefcfdaa55bc74d2e2b24cfe4d8f
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
using UnityEngine;
using UnityEditor;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
 
/// <summary>
/// 事件订阅分析工具 - 检查整个项目中所有的事件订阅和取消订阅是否匹配
/// </summary>
public class EventSubscriptionAnalyzer : EditorWindow
{
    [MenuItem("Tools/事件订阅匹配检查")]
    public static void ShowWindow()
    {
        GetWindow<EventSubscriptionAnalyzer>("Event Sub Analyzer");
    }
 
    private Vector2 scrollPosition;
    private string analysisResult = "";
    private List<SubscriptionIssue> issues = new List<SubscriptionIssue>();
 
    private class SubscriptionIssue
    {
        public string FilePath;
        public string FileName;
        public int LineNumber;
        public string EventType;
        public string ClassName;
        public string MethodName;
        public IssueType Type;
        public string Description;
 
        public enum IssueType
        {
            MissingUnsubscribe,      // 缺少取消订阅
            MultipleSubscribe,       // 重复订阅
            SubscribeBeforeUnsubscribe, // 订阅在取消之前(正常情况)
            PotentialMemoryLeak,     // 潜在内存泄漏
            Warning                  // 警告
        }
 
        public override string ToString()
        {
            return $"[{Type}] {ClassName}.{MethodName} in {Path.GetFileName(FilePath)}:{LineNumber} - {Description}";
        }
    }
 
    void OnGUI()
    {
        GUILayout.Label("Event Subscription Analyzer", EditorStyles.boldLabel);
        
        if (GUILayout.Button("Analyze Project"))
        {
            AnalyzeProject();
        }
 
        GUILayout.Space(10);
 
        if (issues.Count > 0)
        {
            GUILayout.Label($"Found {issues.Count} Issues:", EditorStyles.boldLabel);
            
            scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition);
            
            foreach (var issue in issues)
            {
                Color color = GetIssueColor(issue.Type);
                GUI.backgroundColor = color;
                
                GUILayout.BeginVertical(EditorStyles.helpBox);
                
                string icon = GetIssueIcon(issue.Type);
                GUILayout.Label($"{icon} {issue.Type}", EditorStyles.boldLabel);
                GUILayout.Label($"File: {issue.FileName}:{issue.LineNumber}");
                GUILayout.Label($"Class: {issue.ClassName}");
                GUILayout.Label($"Event: {issue.EventType}");
                GUILayout.Label($"Method: {issue.MethodName}");
                GUILayout.Label($"Description: {issue.Description}");
                
                if (GUILayout.Button("Open File"))
                {
                    UnityEditorInternal.InternalEditorUtility.OpenFileAtLineExternal(
                        issue.FilePath, issue.LineNumber);
                }
                
                GUILayout.EndVertical();
                
                GUI.backgroundColor = Color.white;
            }
            
            EditorGUILayout.EndScrollView();
        }
        else if (!string.IsNullOrEmpty(analysisResult))
        {
            GUILayout.Label(analysisResult);
        }
    }
 
    private Color GetIssueColor(SubscriptionIssue.IssueType type)
    {
        switch (type)
        {
            case SubscriptionIssue.IssueType.MissingUnsubscribe:
                return new Color(1f, 0.6f, 0.6f); // 红色
            case SubscriptionIssue.IssueType.PotentialMemoryLeak:
                return new Color(1f, 0.8f, 0.6f); // 橙色
            case SubscriptionIssue.IssueType.Warning:
                return new Color(1f, 1f, 0.6f); // 黄色
            default:
                return new Color(0.8f, 1f, 0.8f); // 绿色
        }
    }
 
    private string GetIssueIcon(SubscriptionIssue.IssueType type)
    {
        switch (type)
        {
            case SubscriptionIssue.IssueType.MissingUnsubscribe:
                return "[ERROR]";
            case SubscriptionIssue.IssueType.PotentialMemoryLeak:
                return "[WARN]";
            case SubscriptionIssue.IssueType.Warning:
                return "[WARN]";
            default:
                return "[OK]";
        }
    }
 
    private void AnalyzeProject()
    {
        issues.Clear();
 
        // 只获取 Assets/Scripts 下的 C# 文件
        string scriptsPath = Path.Combine(Application.dataPath, "Scripts");
        if (!Directory.Exists(scriptsPath))
        {
            Debug.LogWarning($"Scripts folder not found at: {scriptsPath}");
            return;
        }
 
        string[] csFiles = Directory.GetFiles(
            scriptsPath,
            "*.cs",
            SearchOption.AllDirectories);
 
        Debug.Log($"Found {csFiles.Length} C# files to analyze in Assets/Scripts");
 
        foreach (string filePath in csFiles)
        {
            // 跳过临时文件和生成的文件
            if (filePath.Contains(".meta") ||
                filePath.Contains("/Temp/") ||
                filePath.Contains("/Library/"))
            {
                continue;
            }
 
            string relativePath = "Assets" + filePath.Replace(Application.dataPath, "");
            relativePath = relativePath.Replace("\\", "/");
 
            AnalyzeFile(filePath, relativePath);
        }
 
        Debug.Log($"Analysis complete. Found {issues.Count} issues.");
        Repaint();
    }
 
    private void AnalyzeFile(string filePath, string relativePath)
    {
        string[] lines = File.ReadAllLines(filePath);
        string className = ExtractClassName(filePath);
        string fileContent = File.ReadAllText(filePath);
 
        // 找到所有方法定义
        var methodRanges = ExtractMethodRanges(lines);
 
        // 动态查找所有事件订阅和取消订阅
        var subscriptions = FindAllEventSubscriptions(lines, methodRanges);
        var unsubscriptions = FindAllEventUnsubscriptions(lines, methodRanges);
 
        // 检查每个订阅是否有对应的取消
        foreach (var sub in subscriptions)
        {
            bool hasUnsubscribe = HasMatchingUnsubscription(sub, unsubscriptions, methodRanges);
 
            if (!hasUnsubscribe)
            {
                // 检查是否有 OnDestroy 方法
                bool hasOnDestroy = methodRanges.Any(m => m.Name == "OnDestroy");
 
                var issue = new SubscriptionIssue
                {
                    FilePath = filePath,
                    FileName = Path.GetFileName(filePath),
                    LineNumber = sub.LineNumber,
                    EventType = sub.EventName,
                    ClassName = className,
                    MethodName = sub.HandlerMethod,
                    Type = hasOnDestroy ?
                        SubscriptionIssue.IssueType.Warning :
                        SubscriptionIssue.IssueType.MissingUnsubscribe,
                    Description = hasOnDestroy ?
                        $"事件 '{sub.EventName}' 已订阅但取消订阅不在 OnDestroy 中,可能导致内存泄漏" :
                        $"事件 '{sub.EventName}' 已订阅但没有找到对应的取消订阅,可能导致内存泄漏"
                };
 
                issues.Add(issue);
            }
        }
    }
 
    private string ExtractClassName(string filePath)
    {
        string content = File.ReadAllText(filePath);
        Match match = Regex.Match(content, @"class\s+(\w+)\s*:");
        return match.Success ? match.Groups[1].Value : Path.GetFileNameWithoutExtension(filePath);
    }
 
    private List<MethodRange> ExtractMethodRanges(string[] lines)
    {
        var methodRanges = new List<MethodRange>();
        int depth = 0;
        MethodRange currentMethod = null;
        bool inMethodBody = false;
 
        for (int i = 0; i < lines.Length; i++)
        {
            string line = lines[i];
            string trimmedLine = line.Trim();
 
            // 检测方法开始
            if (Regex.IsMatch(trimmedLine, @"^\s*(?:[a-zA-Z_]\w*\s+)+(?:[a-zA-Z_]\w*\s+)*(\w+)\s*\(.*\)") ||
                Regex.IsMatch(trimmedLine, @"^\s*(void|int|string|bool|float|double|long|short|byte|char|decimal|var|Task|IEnumerator)\s+\w+\s*\(.*\)"))
            {
                if (currentMethod == null && depth == 0)
                {
                    Match nameMatch = Regex.Match(trimmedLine, @"(\w+)\s*\(");
                    if (nameMatch.Success)
                    {
                        string methodName = nameMatch.Groups[1].Value;
                        string[] keywords = { "void", "int", "string", "bool", "float", "double", "long", "short", "byte", "char", "decimal", "var", "Task", "IEnumerator", "public", "private", "protected", "internal", "static", "virtual", "override", "async", "unsafe" };
                        if (!keywords.Contains(methodName))
                        {
                            currentMethod = new MethodRange
                            {
                                Name = methodName,
                                StartLine = i,
                                EndLine = i
                            };
                            depth = CountBraces(line);
 
                            if (depth > 0)
                            {
                                inMethodBody = true;
                            }
                        }
                    }
                }
            }
 
            if (currentMethod != null)
            {
                int braceChange = CountBraces(line);
                depth += braceChange;
 
                if (inMethodBody && depth <= 0)
                {
                    currentMethod.EndLine = i;
                    methodRanges.Add(currentMethod);
                    currentMethod = null;
                    depth = 0;
                    inMethodBody = false;
                }
                else if (!inMethodBody && depth > 0)
                {
                    inMethodBody = true;
                }
            }
        }
 
        return methodRanges;
    }
 
    private int CountBraces(string line)
    {
        return line.Count(c => c == '{') - line.Count(c => c == '}');
    }
 
    /// <summary>
    /// 动态查找所有事件订阅操作 (+=)
    /// </summary>
    private List<EventSubscription> FindAllEventSubscriptions(string[] lines, List<MethodRange> methodRanges)
    {
        var subscriptions = new List<EventSubscription>();
 
        for (int i = 0; i < lines.Length; i++)
        {
            string line = lines[i];
            string trimmedLine = line.Trim();
 
            // 跳过注释
            if (trimmedLine.StartsWith("//") || trimmedLine.StartsWith("/*") || trimmedLine.StartsWith("*"))
                continue;
 
            // 跳过不包含 += 的行
            if (!line.Contains("+="))
                continue;
 
            // 严格匹配:eventPath += methodName;
            // 要求:+= 左侧必须包含点号(.),右侧必须是标识符,后面紧跟 ; 或 // 或 /*,不能有任何运算符
            var pattern = @"(\w+(?:\.\w+)+)\s*\+=\s*(\w+)\s*([;]|//|/\*)";
 
            Match match = Regex.Match(trimmedLine, pattern);
            if (!match.Success)
            {
                // 不再尝试简单名称匹配(不包含点号的情况),直接跳过
                continue;
            }
 
            if (match.Success)
            {
                string fullEventPath = match.Groups[1].Value;
                string handlerMethod = match.Groups[2].Value;
 
                // 过滤关键字
                if (IsKeyword(handlerMethod))
                    continue;
 
                // 过滤数字开头
                if (Regex.IsMatch(handlerMethod, @"^\d+"))
                    continue;
 
                // 提取事件名
                string eventName = fullEventPath.Contains(".")
                    ? fullEventPath.Substring(fullEventPath.LastIndexOf('.') + 1)
                    : fullEventPath;
 
                MethodRange containingMethod = methodRanges.FirstOrDefault(
                    m => i >= m.StartLine && i <= m.EndLine);
 
                subscriptions.Add(new EventSubscription
                {
                    LineNumber = i + 1,
                    EventName = eventName,
                    FullEventPath = fullEventPath,
                    HandlerMethod = handlerMethod,
                    MethodRange = containingMethod
                });
            }
        }
 
        return subscriptions;
    }
 
    /// <summary>
    /// 动态查找所有事件取消订阅操作 (-=)
    /// </summary>
    private List<EventUnsubscription> FindAllEventUnsubscriptions(string[] lines, List<MethodRange> methodRanges)
    {
        var unsubscriptions = new List<EventUnsubscription>();
 
        for (int i = 0; i < lines.Length; i++)
        {
            string line = lines[i];
            string trimmedLine = line.Trim();
 
            if (trimmedLine.StartsWith("//") || trimmedLine.StartsWith("/*") || trimmedLine.StartsWith("*"))
                continue;
 
            if (!line.Contains("-="))
                continue;
 
            // 严格匹配:eventPath -= methodName;
            // 要求:-= 左侧必须包含点号(.),右侧必须是标识符,后面紧跟 ; 或 // 或 /*
            var pattern = @"(\w+(?:\.\w+)+)\s*-=\s*(\w+)\s*([;]|//|/\*)";
 
            Match match = Regex.Match(trimmedLine, pattern);
            if (!match.Success)
            {
                // 不再尝试简单名称匹配(不包含点号的情况),直接跳过
                continue;
            }
 
            if (match.Success)
            {
                string fullEventPath = match.Groups[1].Value;
                string handlerMethod = match.Groups[2].Value;
 
                if (IsKeyword(handlerMethod))
                    continue;
 
                if (Regex.IsMatch(handlerMethod, @"^\d+"))
                    continue;
 
                string eventName = fullEventPath.Contains(".")
                    ? fullEventPath.Substring(fullEventPath.LastIndexOf('.') + 1)
                    : fullEventPath;
 
                MethodRange containingMethod = methodRanges.FirstOrDefault(
                    m => i >= m.StartLine && i <= m.EndLine);
 
                unsubscriptions.Add(new EventUnsubscription
                {
                    LineNumber = i + 1,
                    EventName = eventName,
                    FullEventPath = fullEventPath,
                    HandlerMethod = handlerMethod,
                    MethodRange = containingMethod
                });
            }
        }
 
        return unsubscriptions;
    }
 
    /// <summary>
    /// 检查是否为关键字
    /// </summary>
    private bool IsKeyword(string word)
    {
        string[] keywords = {
            "null", "true", "false", "default", "new", "typeof", "sizeof",
            "this", "base", "return", "throw", "if", "else", "for", "foreach",
            "while", "do", "switch", "case", "break", "continue", "goto",
            "try", "catch", "finally", "using", "lock", "checked", "unchecked",
            "void", "int", "string", "bool", "float", "double", "long", "short",
            "byte", "char", "decimal", "var", "object", "dynamic"
        };
 
        return keywords.Contains(word);
    }
 
    /// <summary>
    /// 检查订阅是否有对应的取消订阅
    /// </summary>
    private bool HasMatchingUnsubscription(EventSubscription subscription,
        List<EventUnsubscription> unsubscriptions, List<MethodRange> methodRanges)
    {
        // 使用完整的事件路径进行匹配(不仅仅是事件名)
        var matchingUnsubs = unsubscriptions.Where(u =>
            u.FullEventPath == subscription.FullEventPath &&
            u.HandlerMethod == subscription.HandlerMethod).ToList();
 
        // 在同一个方法中查找 -=(必须在订阅之后)
        if (subscription.MethodRange != null)
        {
            var sameMethodUnsubs = matchingUnsubs.Where(u =>
                u.MethodRange != null &&
                u.MethodRange.Name == subscription.MethodRange.Name &&
                u.LineNumber > subscription.LineNumber).ToList();
 
            if (sameMethodUnsubs.Any())
            {
                return true;
            }
        }
 
        // 在其他任意方法中查找匹配的取消订阅
        // 只要有取消订阅存在,就认为已正确处理
        if (matchingUnsubs.Any(u => u.MethodRange != null))
        {
            return true;
        }
 
        return false;
    }
 
    private class MethodRange
    {
        public string Name { get; set; }
        public int StartLine { get; set; }
        public int EndLine { get; set; }
    }
 
    private class EventSubscription
    {
        public int LineNumber { get; set; }
        public string EventName { get; set; }
        public string FullEventPath { get; set; }
        public string HandlerMethod { get; set; }
        public MethodRange MethodRange { get; set; }
    }
 
    private class EventUnsubscription
    {
        public int LineNumber { get; set; }
        public string EventName { get; set; }
        public string FullEventPath { get; set; }
        public string HandlerMethod { get; set; }
        public MethodRange MethodRange { get; set; }
    }
}