-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallstackFilterTool.cs
More file actions
360 lines (302 loc) · 12.7 KB
/
CallstackFilterTool.cs
File metadata and controls
360 lines (302 loc) · 12.7 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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text.RegularExpressions;
using UnityEditor;
using UnityEngine;
namespace CallstackFilter
{
[Serializable]
internal class CallstackFilterEntry
{
public string typeName;
public string methodName;
public bool filterAllMembers;
public bool enabled = true;
}
[Serializable]
internal class CallstackFilterConfig
{
public List<CallstackFilterEntry> entries = new List<CallstackFilterEntry>();
}
[InitializeOnLoad]
internal static class CallstackFilterRuntime
{
static readonly Type s_ConsoleWindowType =
typeof(EditorWindow).Assembly.GetType("UnityEditor.ConsoleWindow");
static readonly FieldInfo s_MethodsField = s_ConsoleWindowType?.GetField(
"s_MethodsToHideInCallstack", BindingFlags.Static | BindingFlags.NonPublic);
static readonly FieldInfo s_RegexField = s_ConsoleWindowType?.GetField(
"s_GenericMethodSignatureRegex", BindingFlags.Static | BindingFlags.NonPublic);
static string ConfigPath => Path.Combine(Application.dataPath, "..", "ProjectSettings", "CallstackFilters.json");
static CallstackFilterConfig s_Config;
static List<MethodInfo> s_LastKnownMethods;
static CallstackFilterRuntime()
{
EditorApplication.delayCall += ApplyFilters;
EditorApplication.update += OnUpdate;
}
static void OnUpdate()
{
if (s_ConsoleWindowType == null || s_MethodsField == null)
return;
var current = s_MethodsField.GetValue(null) as List<MethodInfo>;
if (current != s_LastKnownMethods)
ApplyFilters();
}
internal static CallstackFilterConfig GetConfig()
{
if (s_Config == null)
s_Config = LoadConfig();
return s_Config;
}
internal static void SaveAndApply()
{
SaveConfig(s_Config);
ApplyFilters();
}
static CallstackFilterConfig LoadConfig()
{
try
{
if (File.Exists(ConfigPath))
return JsonUtility.FromJson<CallstackFilterConfig>(File.ReadAllText(ConfigPath))
?? new CallstackFilterConfig();
}
catch (Exception e)
{
Debug.LogError($"[CallstackFilter] Failed to load config: {e.Message}");
}
return new CallstackFilterConfig();
}
static void SaveConfig(CallstackFilterConfig config)
{
try
{
File.WriteAllText(ConfigPath, JsonUtility.ToJson(config, true));
}
catch (Exception e)
{
Debug.LogError($"[CallstackFilter] Failed to save config: {e.Message}");
}
}
static void ApplyFilters()
{
if (s_ConsoleWindowType == null || s_MethodsField == null || s_RegexField == null)
return;
var config = GetConfig();
if (config.entries.Count == 0) return;
var methods = s_MethodsField.GetValue(null) as List<MethodInfo>;
var regexDict = s_RegexField.GetValue(null) as Dictionary<MethodInfo, Regex>;
if (methods == null)
{
methods = new List<MethodInfo>();
s_MethodsField.SetValue(null, methods);
}
if (regexDict == null)
{
regexDict = new Dictionary<MethodInfo, Regex>();
s_RegexField.SetValue(null, regexDict);
}
foreach (var entry in config.entries)
{
if (!entry.enabled || string.IsNullOrEmpty(entry.typeName))
continue;
if (!entry.filterAllMembers && string.IsNullOrEmpty(entry.methodName))
continue;
var resolved = entry.filterAllMembers
? ResolveAllMethods(entry.typeName)
: ResolveMethodInfos(entry.typeName, entry.methodName);
if (resolved == null) continue;
foreach (var method in resolved)
{
if (methods.Contains(method))
continue;
methods.Add(method);
if (method.IsGenericMethod)
{
var regex = BuildGenericRegex(method);
if (regex != null)
regexDict.TryAdd(method, regex);
}
}
}
s_LastKnownMethods = methods;
}
static Type FindType(string typeName)
{
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
{
var type = asm.GetType(typeName, false);
if (type != null) return type;
}
return null;
}
static MethodInfo[] ResolveAllMethods(string typeName)
{
var targetType = FindType(typeName);
if (targetType == null) return null;
return targetType.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly)
.ToArray();
}
static MethodInfo[] ResolveMethodInfos(string typeName, string methodName)
{
var targetType = FindType(typeName);
if (targetType == null) return null;
return targetType.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly)
.Where(m => m.Name == methodName)
.ToArray();
}
internal static string ValidateEntry(string typeName, string methodName, bool filterAll)
{
var targetType = FindType(typeName);
if (targetType == null)
return $"Type '{typeName}' not found in any loaded assembly.";
if (filterAll)
{
var all = targetType.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly);
if (all.Length == 0)
return $"Type '{typeName}' has no methods.";
return null;
}
var methods = targetType.GetMethods(
BindingFlags.Public | BindingFlags.NonPublic |
BindingFlags.Static | BindingFlags.Instance |
BindingFlags.DeclaredOnly)
.Where(m => m.Name == methodName)
.ToArray();
if (methods.Length == 0)
return $"Method '{methodName}' not found on type '{typeName}'.";
return null;
}
static Regex BuildGenericRegex(MethodInfo method)
{
var classType = method.DeclaringType;
if (classType == null) return null;
var ns = classType.Namespace;
var escapedNs = string.IsNullOrEmpty(ns) ? "" : Regex.Escape(ns) + @"\.";
var escapedClassName = Regex.Escape(classType.Name);
var escapedMethodName = Regex.Escape(method.Name);
var pattern = escapedNs + escapedClassName + @"(?:\[.*?\])?\:(" +
escapedMethodName + @")([^\(]*)\s*\(([^\)]*)\)";
return new Regex(pattern, RegexOptions.Compiled);
}
}
internal class CallstackFilterSettingsProvider : SettingsProvider
{
string m_NewTypeName = "";
string m_NewMethodName = "";
bool m_NewFilterAll;
Vector2 m_ScrollPos;
CallstackFilterSettingsProvider()
: base("Preferences/Callstack Filter", SettingsScope.User) { }
[SettingsProvider]
static SettingsProvider Create() => new CallstackFilterSettingsProvider();
public override void OnGUI(string searchContext)
{
var config = CallstackFilterRuntime.GetConfig();
EditorGUILayout.Space();
EditorGUILayout.LabelField("Custom Callstack Filters", EditorStyles.boldLabel);
EditorGUILayout.HelpBox(
"Methods added here will be hidden from Console callstacks.\n" +
"Requires Console > Strip Logging Callstack to be enabled.\n" +
"Persisted in ProjectSettings/CallstackFilters.json.",
MessageType.Info);
EditorGUILayout.Space();
// --- Existing entries ---
m_ScrollPos = EditorGUILayout.BeginScrollView(m_ScrollPos);
int removeIndex = -1;
bool changed = false;
for (int i = 0; i < config.entries.Count; i++)
{
var entry = config.entries[i];
EditorGUILayout.BeginHorizontal();
var newEnabled = EditorGUILayout.Toggle(entry.enabled, GUILayout.Width(20));
if (newEnabled != entry.enabled)
{
entry.enabled = newEnabled;
changed = true;
}
EditorGUILayout.LabelField(entry.typeName, GUILayout.MinWidth(200));
EditorGUILayout.LabelField(entry.filterAllMembers ? "*" : entry.methodName, GUILayout.MinWidth(100));
if (GUILayout.Button("X", GUILayout.Width(24)))
removeIndex = i;
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.EndScrollView();
if (removeIndex >= 0)
{
config.entries.RemoveAt(removeIndex);
changed = true;
}
// --- Add new entry ---
EditorGUILayout.Space();
EditorGUILayout.LabelField("Add New Filter", EditorStyles.boldLabel);
m_NewTypeName = EditorGUILayout.TextField("Full Type Name", m_NewTypeName);
m_NewFilterAll = EditorGUILayout.Toggle("Filter All Members", m_NewFilterAll);
using (new EditorGUI.DisabledScope(m_NewFilterAll))
m_NewMethodName = EditorGUILayout.TextField("Method Name", m_NewFilterAll ? "" : m_NewMethodName);
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("Add", GUILayout.Height(24)))
{
var typeName = m_NewTypeName?.Trim();
var methodName = m_NewFilterAll ? "*" : m_NewMethodName?.Trim();
if (string.IsNullOrEmpty(typeName) || (!m_NewFilterAll && string.IsNullOrEmpty(methodName)))
{
EditorUtility.DisplayDialog("Error", "Type name and method name cannot be empty.", "OK");
}
else
{
var error = CallstackFilterRuntime.ValidateEntry(typeName, m_NewFilterAll ? null : methodName, m_NewFilterAll);
if (error != null)
{
EditorUtility.DisplayDialog("Validation Failed", error, "OK");
}
else
{
bool duplicate = config.entries.Any(e =>
e.typeName == typeName && e.filterAllMembers == m_NewFilterAll
&& (m_NewFilterAll || e.methodName == methodName));
if (duplicate)
{
EditorUtility.DisplayDialog("Duplicate", "This filter already exists.", "OK");
}
else
{
config.entries.Add(new CallstackFilterEntry
{
typeName = typeName,
methodName = m_NewFilterAll ? "" : methodName,
filterAllMembers = m_NewFilterAll,
enabled = true
});
changed = true;
m_NewTypeName = "";
m_NewMethodName = "";
m_NewFilterAll = false;
}
}
}
}
if (GUILayout.Button("Apply Now", GUILayout.Height(24)))
{
CallstackFilterRuntime.SaveAndApply();
Debug.Log("[CallstackFilter] Filters saved and applied.");
}
EditorGUILayout.EndHorizontal();
if (changed)
CallstackFilterRuntime.SaveAndApply();
}
}
}