-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomExportBlock.cs
418 lines (370 loc) · 13.1 KB
/
CustomExportBlock.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
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
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using UnityEditor;
using UnityEngine;
namespace SLua
{
/// <summary>
/// 根据类型反射查询构造函数的编辑器
/// </summary>
public class CustomExportBlockEditor : EditorWindow
{
[MenuItem("SLua/Custom/Custom导出白名单过滤")]
static public void ShowWnd()
{
CustomExportBlockEditor wnd = EditorWindow.GetWindow<CustomExportBlockEditor>("CustomExportBlockEditor");
wnd.Show();
}
private void OnEnable()
{
CustomExportBlockTool.GetInstance().LoadJson();
}
Vector2 scrollPosition = Vector2.zero;
Vector2 blockPosition = Vector2.zero;
class ShowInfo
{
public Type classType;
public ConstructorInfo constructorInfo;
public MethodInfo methodInfo;
public void OnGUI()
{
GUILayout.BeginHorizontal();
if(constructorInfo != null )
{
renderMethod(constructorInfo);
}
else if (methodInfo != null )
{
renderMethod(methodInfo);
}
GUILayout.EndHorizontal();
}
private void renderMethod(MethodBase methodBase)
{
ParameterInfo[] infos = methodBase.GetParameters();
if (infos.Length > 0)
{
GUILayout.BeginHorizontal();
foreach (var pInfo in infos)
{
EditorGUILayout.SelectableLabel(pInfo.ParameterType.Name);
}
GUILayout.EndHorizontal();
}
else
{
EditorGUILayout.SelectableLabel("无参数");
}
bool isBlock = CustomExportBlockTool.GetInstance().IsValidMethodInfo(classType.FullName, methodBase);
string desc = "屏蔽导出";
if (isBlock )
{
desc = "取消屏蔽";
}
if (GUILayout.Button(desc))
{
if ( isBlock )
{
CustomExportBlockTool.GetInstance().AddBlockClass(classType, methodBase);
}
else
{
CustomExportBlockTool.GetInstance().RemoveBlockClass(classType, methodBase);
}
}
}
}
List<ShowInfo> paramList = new List<ShowInfo>();
private void FillConstructorData(string classWholeName)
{
paramList.Clear();
Type tt = Type.GetType(classWholeName);
var r = tt;
ConstructorInfo[] constructInfoList = r.GetConstructors(BindingFlags.Instance | BindingFlags.Public);
foreach (var constructorInfo in constructInfoList)
{
ShowInfo showInfo = new ShowInfo();
showInfo.constructorInfo = constructorInfo;
paramList.Add(showInfo);
}
}
private void FillMethodData(string classWholeName)
{
paramList.Clear();
Type tt = Type.GetType(classWholeName);
var r = tt;
MethodInfo[] methodInfoList = r.GetMethods();
foreach (var methodInfo in methodInfoList)
{
ShowInfo showInfo = new ShowInfo();
showInfo.methodInfo = methodInfo;
paramList.Add(showInfo);
}
}
string m_uiValueClassName;
void OnGUI()
{
GUILayout.BeginVertical();
{
GUILayout.BeginHorizontal();
{
GUILayout.Label("请输入类名:",GUILayout.Width(80));
m_uiValueClassName = GUILayout.TextField(m_uiValueClassName, GUILayout.Width(350));
}
GUILayout.EndHorizontal();
blockPosition = EditorGUILayout.BeginScrollView(blockPosition, GUILayout.Width(Screen.width), GUILayout.ExpandWidth(true));
Dictionary<string, CustomExportBlock> blockMap = CustomExportBlockTool.GetInstance().BlockMap;
foreach( var pair in blockMap)
{
if (GUILayout.Button(pair.Value.className))
{
m_uiValueClassName = pair.Value.className;
}
}
EditorGUILayout.EndScrollView();
GUILayout.BeginHorizontal();
{
if (GUILayout.Button("查看构造函数"))
{
FillConstructorData(m_uiValueClassName);
}
if (GUILayout.Button("查看成员方法"))
{
FillMethodData(m_uiValueClassName);
}
}
GUILayout.EndHorizontal();
scrollPosition = EditorGUILayout.BeginScrollView(scrollPosition, GUILayout.Width(Screen.width), GUILayout.ExpandHeight(true));
foreach (var info in paramList)
{
info.OnGUI();
}
EditorGUILayout.EndScrollView();
}
GUILayout.EndVertical();
}
}
/// <summary>
/// 白名单json对应的类
/// </summary>
[System.Serializable]
public class CustomExportBlock
{
[System.Serializable]
public class MethodParams
{
public List<string> param = new List<string>();
}
[System.Serializable]
public class BlockMethod
{
public string methodName;
public List<MethodParams> methodParams;
}
public string className;
public List<BlockMethod> method;
public Dictionary<string, BlockMethod> methodMap;
}
[System.Serializable]
public class CustomExportBlocks
{
public List<CustomExportBlock> allClass;
}
public class CustomExportBlockTool
{
static private CustomExportBlockTool instance;
static public CustomExportBlockTool GetInstance()
{
if (instance == null)
{
instance = new CustomExportBlockTool();
}
return instance;
}
/// <summary>
/// Json读出的白名单数据
/// </summary>
CustomExportBlocks datas;
/// <summary>
/// 类名白名单Map
/// </summary>
Dictionary<string, CustomExportBlock> blockMap;
public Dictionary<string, CustomExportBlock> BlockMap
{
get
{
return blockMap;
}
}
public void LoadJson()
{
if (blockMap != null )
{
blockMap.Clear();
}
else
{
blockMap = new Dictionary<string, CustomExportBlock>();
}
string jsonStr = File.ReadAllText(Application.dataPath + "/Slua/Editor/CustomExportBlockWhiteList.json");
datas = JsonUtility.FromJson<CustomExportBlocks>(jsonStr);
foreach ( var block in datas.allClass)
{
blockMap[block.className] = block;
if (block.methodMap != null)
{
block.methodMap.Clear();
}
else
{
block.methodMap = new Dictionary<string, CustomExportBlock.BlockMethod>();
}
foreach (CustomExportBlock.BlockMethod method in block.method)
{
block.methodMap[method.methodName] = method;
}
}
}
public void AddBlockClass(Type type, MethodBase info)
{
CustomExportBlock block;
if (!blockMap.TryGetValue(type.FullName, out block))
{
block = new CustomExportBlock();
block.className = type.FullName;
blockMap[type.FullName] = block;
}
CustomExportBlock.BlockMethod method;
if (!block.methodMap.TryGetValue(info.Name, out method))
{
method = new CustomExportBlock.BlockMethod();
method.methodName = info.Name;
block.methodMap[info.Name] = method;
}
CustomExportBlock.MethodParams methodParams = new CustomExportBlock.MethodParams();
foreach (ParameterInfo param in info.GetParameters())
{
methodParams.param.Add(param.ParameterType.Name);
}
}
private void RemoveBlockClass(Type type, MethodBase info)
{
CustomExportBlock block;
if (!blockMap.TryGetValue(type.FullName, out block))
{
return;
}
CustomExportBlock.BlockMethod method;
if (!block.methodMap.TryGetValue(info.Name, out method))
{
return;
}
foreach (CustomExportBlock.MethodParams param in method.methodParams)
{
if ( IsSameParameter(param.param, info.GetParameters()) )
{
method.methodParams.Remove(param);
break;
}
}
}
/// <summary>
/// 过滤成员函数
/// </summary>
/// <param name="className">类名</param>
/// <param name="cInfo">反射得到的构造函数信息</param>
/// <returns></returns>
public bool IsValidConstructorInfo(string className, ConstructorInfo cInfo)
{
CustomExportBlock block = IsValidClass(className);
return IsValidMethod(block, cInfo);
}
/// <summary>
/// 过滤员方法
/// </summary>
/// <param name="className"></param>
/// <param name="info"></param>
/// <returns></returns>
public bool IsValidMethodInfo(string className, MethodBase info)
{
CustomExportBlock block = IsValidClass(className);
return IsValidMethod(block, info);
}
/// <summary>
/// 需要检查的类名
/// </summary>
/// <param name="className">类名</param>
/// <returns></returns>
public CustomExportBlock IsValidClass(string className)
{
if (blockMap == null )
{
return null;
}
CustomExportBlock block;
blockMap.TryGetValue(className, out block);
return block;
}
public bool IsValidMethod(CustomExportBlock block, MethodBase methodBase)
{
bool ret = true;
if (block == null || methodBase == null)
{
return ret;
}
ParameterInfo[] pi = methodBase.GetParameters();
CustomExportBlock.BlockMethod method;
if (block.methodMap.TryGetValue(methodBase.Name, out method))
{
ret = isSameMethodParams(method.methodParams, pi);
}
return ret;
}
public bool isSameMethodParams(List<CustomExportBlock.MethodParams> sourceMathodParams, ParameterInfo[] pi)
{
bool ret = true;
foreach (CustomExportBlock.MethodParams param in sourceMathodParams)
{
ret = ret && IsSameParameter(param.param, pi);
if (!ret)
{
break;
}
}
return ret;
}
/// <summary>
/// true不同方法参数,false相同方法参数
/// </summary>
/// <param name="param"></param>
/// <param name="info"></param>
/// <returns></returns>
public bool IsSameParameter(List<string> param, ParameterInfo[] info)
{
bool ret = true;
int fitCount = 0;
if (info.Length == param.Count)
{
for (int i = 0; i < info.Length; i++)
{
if (info[i].ParameterType.Name.Equals(param[i]))
{
fitCount++;
}
}
}
else
{
fitCount = -1;
}
if (fitCount == info.Length)
{
ret = false;
}
return ret;
}
}
}