ToluaInjectionBlackListPanel.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. using System;
  2. using System.Xml;
  3. using System.IO;
  4. using System.Linq;
  5. using UnityEngine;
  6. using UnityEditor;
  7. using System.Reflection;
  8. using UnityEditorInternal;
  9. using System.Collections.Generic;
  10. using System.Text.RegularExpressions;
  11. public class InjectionBlackListGenerator : EditorWindow
  12. {
  13. public static string blackListFilePath = CustomSettings.injectionFilesPath + "InjectionBlackList.txt";
  14. public static Action onBlackListGenerated;
  15. static string pathsInfoSavedPath;
  16. static HashSet<string> specifiedDropType = new HashSet<string>
  17. {
  18. };
  19. const string nestRegex = @" (?<=
  20. (?<!/{2,}.*) #Ignore Comment
  21. )
  22. (
  23. (?<=class\s+) #Capture class name
  24. (\b(\w+)\b)
  25. |
  26. (?<=struct\s+) #Capture struct name
  27. (\b(\w+)\b)
  28. |
  29. (?<=namespace\s+) #Capture namespace name
  30. ((\b(\w+)\b\.?){1,})
  31. )";/// match class or struct
  32. const string namespaceRegex = @"(?<=
  33. (?<=
  34. (?<!/{2,}.*) #Ignore Comment
  35. )
  36. namespace\s+
  37. )";/// match namespace Name
  38. List<string> paths;
  39. ReorderableList pathUIList;
  40. Vector2 scrollPosition = Vector2.zero;
  41. HashSet<string> blackList = new HashSet<string>();
  42. Dictionary<string, System.Action<HashSet<string>, string>> assetExtentions = new Dictionary<string, System.Action<HashSet<string>, string>>
  43. {
  44. { "*.cs", SearchTypeInCSFile },
  45. { "*.dll", SearchTypeInAssembly },
  46. };
  47. #if ENABLE_LUA_INJECTION
  48. [MenuItem("Lua/Generate LuaInjection BlackList")]
  49. #endif
  50. public static void Open()
  51. {
  52. GetWindow<InjectionBlackListGenerator>("LuaInjection", true);
  53. }
  54. void OnListHeaderGUI(Rect rect)
  55. {
  56. EditorGUI.LabelField(rect, "Scripts Path");
  57. }
  58. void OnListElementGUI(Rect rect, int index, bool isactive, bool isfocused)
  59. {
  60. const float GAP = 5;
  61. string info = paths[index];
  62. rect.y++;
  63. Rect r = rect;
  64. r.width = 16;
  65. r.height = 18;
  66. r.xMin = r.xMax + GAP;
  67. r.xMax = rect.xMax - 50 - GAP;
  68. GUI.enabled = false;
  69. info = GUI.TextField(r, info);
  70. GUI.enabled = true;
  71. r.xMin = r.xMax + GAP;
  72. r.width = 50;
  73. if (GUI.Button(r, "Select"))
  74. {
  75. var path = SelectFolder();
  76. if (path != null)
  77. {
  78. paths[index] = path;
  79. }
  80. }
  81. }
  82. string SelectFolder()
  83. {
  84. string dataPath = Application.dataPath;
  85. string selectedPath = EditorUtility.OpenFolderPanel("Path", dataPath, "");
  86. if (!string.IsNullOrEmpty(selectedPath))
  87. {
  88. return GetRelativePath(selectedPath);
  89. }
  90. return null;
  91. }
  92. string GetRelativePath(string absolutePath)
  93. {
  94. if (absolutePath.StartsWith(Application.dataPath))
  95. {
  96. return "Assets/" + absolutePath.Substring(Application.dataPath.Length + 1);
  97. }
  98. else
  99. {
  100. ShowNotification(new GUIContent("不能在Assets目录之外!"));
  101. }
  102. return null;
  103. }
  104. void AddNewPath()
  105. {
  106. string path = SelectFolder();
  107. if (!string.IsNullOrEmpty(path))
  108. {
  109. paths.Add(path);
  110. }
  111. }
  112. void InitFilterListDrawer()
  113. {
  114. if (pathUIList != null)
  115. {
  116. return;
  117. }
  118. pathUIList = new ReorderableList(paths, typeof(string));
  119. pathUIList.drawElementCallback = OnListElementGUI;
  120. pathUIList.drawHeaderCallback = OnListHeaderGUI;
  121. pathUIList.draggable = true;
  122. pathUIList.elementHeight = 22;
  123. pathUIList.onAddCallback = (list) => AddNewPath();
  124. }
  125. void OnGUI()
  126. {
  127. InitPathsInfo();
  128. InitFilterListDrawer();
  129. bool execGenerate = false;
  130. GUILayout.BeginHorizontal(EditorStyles.toolbar);
  131. {
  132. if (GUILayout.Button("Add", EditorStyles.toolbarButton))
  133. {
  134. AddNewPath();
  135. }
  136. if (GUILayout.Button("Save", EditorStyles.toolbarButton))
  137. {
  138. SavePathsInfo();
  139. }
  140. GUILayout.FlexibleSpace();
  141. if (GUILayout.Button("Generate", EditorStyles.toolbarButton))
  142. {
  143. execGenerate = true;
  144. }
  145. }
  146. GUILayout.EndHorizontal();
  147. GUILayout.BeginVertical();
  148. {
  149. scrollPosition = GUILayout.BeginScrollView(scrollPosition);
  150. {
  151. pathUIList.DoLayoutList();
  152. }
  153. GUILayout.EndScrollView();
  154. }
  155. GUILayout.EndVertical();
  156. if (execGenerate)
  157. {
  158. Generate();
  159. Close();
  160. }
  161. }
  162. void Generate()
  163. {
  164. if (paths.Count == 0)
  165. {
  166. EditorUtility.DisplayDialog("提示", "没有选中任何可跳过的路径", "确定");
  167. return;
  168. }
  169. blackList.Clear();
  170. foreach (var path in paths)
  171. {
  172. foreach (var extention in assetExtentions)
  173. {
  174. var files = from fileName in Directory.GetFiles(path, extention.Key, SearchOption.AllDirectories)
  175. let validFullFileName = fileName.Replace("\\", "/")
  176. let bEditorScriptFile = validFullFileName.Contains("/Editor/")
  177. //let bToluaGeneratedFile = validFullFileName.Contains("/GenerateWrapFiles/")
  178. where !bEditorScriptFile /*&& !bToluaGeneratedFile*/
  179. select validFullFileName;
  180. int index = 0;
  181. foreach (var fileFullPath in files)
  182. {
  183. EditorUtility.DisplayProgressBar("Searching", fileFullPath, (float)index / files.Count());
  184. extention.Value(blackList, fileFullPath);
  185. ++index;
  186. }
  187. EditorUtility.ClearProgressBar();
  188. }
  189. }
  190. blackList.UnionWith(specifiedDropType);
  191. SaveBlackList();
  192. SavePathsInfo();
  193. if (onBlackListGenerated != null)
  194. {
  195. onBlackListGenerated();
  196. }
  197. onBlackListGenerated = null;
  198. Debug.Log("BlackList Generated!!!");
  199. }
  200. static void SearchTypeInCSFile(HashSet<string> typeSet, string fileFullPath)
  201. {
  202. var fileContent = File.ReadAllText(fileFullPath);
  203. if (string.IsNullOrEmpty(fileContent))
  204. {
  205. return;
  206. }
  207. int nestCount = 0;
  208. int lastTypeMatchedIndex = 0;
  209. Stack<int> nestIndexStack = new Stack<int>();
  210. Stack<string> nestStack = new Stack<string>();
  211. var matchResult = Regex.Match(fileContent, nestRegex, RegexOptions.IgnorePatternWhitespace);
  212. while (matchResult.Success)
  213. {
  214. string typeName = matchResult.Value;
  215. if (string.IsNullOrEmpty(typeName))
  216. {
  217. matchResult = matchResult.NextMatch();
  218. continue;
  219. }
  220. lastTypeMatchedIndex = nestIndexStack.Count > 0 ? nestIndexStack.Peek() : 0;
  221. string matchSubString = fileContent.Substring(lastTypeMatchedIndex, matchResult.Index - lastTypeMatchedIndex);
  222. var beginBraceMatchResult = Regex.Matches(matchSubString, "(?<!/{2,}.*){");
  223. var endBraceMatchResult = Regex.Matches(matchSubString, "(?<!/{2,}.*)}");
  224. int compareTag = beginBraceMatchResult.Count - endBraceMatchResult.Count;
  225. bool bNamespaceMatched = Regex.IsMatch(matchSubString, namespaceRegex, RegexOptions.IgnorePatternWhitespace);
  226. nestIndexStack.Push(matchResult.Index);
  227. if (compareTag == 0)
  228. {
  229. nestStack.Clear();
  230. nestCount = 0;
  231. }
  232. else if (compareTag >= nestCount)
  233. {
  234. if (compareTag == nestCount)
  235. {
  236. nestStack.Pop();
  237. }
  238. if (compareTag > nestCount)
  239. {
  240. nestCount++;
  241. }
  242. nestIndexStack.Pop();
  243. }
  244. else if (compareTag < nestCount && compareTag > 0)
  245. {
  246. int searchedStackCount = nestStack.Count - compareTag;
  247. for (int i = 0; i < searchedStackCount; ++i)
  248. {
  249. nestStack.Pop();
  250. }
  251. nestCount = nestStack.Count;
  252. if (nestCount > 0)
  253. {
  254. nestIndexStack.Pop();
  255. }
  256. }
  257. string prefix = "";
  258. foreach (var name in nestStack)
  259. {
  260. prefix = name + prefix;
  261. }
  262. string typeFullName = prefix + typeName;
  263. if (!typeSet.Contains(typeFullName) && !bNamespaceMatched)
  264. {
  265. typeSet.Add(typeFullName);
  266. }
  267. string nestTag = (bNamespaceMatched ? "." : "+");
  268. nestStack.Push(typeName + nestTag);
  269. matchResult = matchResult.NextMatch();
  270. }
  271. }
  272. static void SearchTypeInAssembly(HashSet<string> typeSet, string assemblyPath)
  273. {
  274. Assembly assembly = null;
  275. try
  276. {
  277. assembly = Assembly.LoadFile(assemblyPath);
  278. }
  279. catch { }
  280. if (assembly == null)
  281. {
  282. return;
  283. }
  284. try
  285. {
  286. foreach (Type t in assembly.GetTypes())
  287. {
  288. bool bNotPrimitiveType = t.IsClass || (t.IsValueType && !t.IsPrimitive && !t.IsEnum);
  289. bool bCustomType = bNotPrimitiveType && !t.FullName.Contains("<");
  290. if (bCustomType && !typeSet.Contains(t.FullName) && !t.ContainsGenericParameters)
  291. {
  292. typeSet.Add(t.FullName);
  293. }
  294. }
  295. }
  296. catch (Exception e)
  297. {
  298. Debug.LogError(e.ToString());
  299. }
  300. }
  301. void InitPathsInfo()
  302. {
  303. if (pathsInfoSavedPath == null)
  304. {
  305. pathsInfoSavedPath = CustomSettings.injectionFilesPath + "LuaInjectionSkipPaths.txt";
  306. }
  307. if (paths != null)
  308. {
  309. return;
  310. }
  311. if (File.Exists(pathsInfoSavedPath))
  312. {
  313. paths = new List<string>(File.ReadAllLines(pathsInfoSavedPath));
  314. }
  315. else
  316. {
  317. paths = new List<string>();
  318. string toluaPath = GetRelativePath(CustomSettings.injectionFilesPath.Substring(0, CustomSettings.injectionFilesPath.Length - "Injection/".Length));
  319. paths.Add(toluaPath + "Core/");
  320. paths.Add(toluaPath + "Injection/");
  321. paths.Add(toluaPath + "Misc/");
  322. paths.Add(toluaPath + "Injection/");
  323. paths.Add(toluaPath + "Misc/");
  324. paths.Add(Application.dataPath + "/Plugins/");
  325. paths.Add(CustomSettings.toluaBaseType);
  326. paths.Add(GetRelativePath(CustomSettings.saveDir));
  327. }
  328. }
  329. void SavePathsInfo()
  330. {
  331. File.WriteAllLines(pathsInfoSavedPath, paths.ToArray());
  332. AssetDatabase.Refresh();
  333. }
  334. void SaveBlackList()
  335. {
  336. try
  337. {
  338. if (File.Exists(blackListFilePath))
  339. {
  340. File.Delete(blackListFilePath);
  341. }
  342. File.WriteAllLines(blackListFilePath, blackList.ToArray());
  343. }
  344. catch (Exception e)
  345. {
  346. Debug.LogError(e.ToString());
  347. }
  348. AssetDatabase.Refresh();
  349. }
  350. }