LuaDiffScrollView.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #if (UNITY_5 || UNITY_2017_1_OR_NEWER)
  2. namespace MikuLuaProfiler
  3. {
  4. using System;
  5. using System.Collections.Generic;
  6. using System.IO;
  7. using System.Text;
  8. using UnityEditor;
  9. using UnityEngine;
  10. public class LuaDiffScrollView
  11. {
  12. private bool m_isStaticRecord = false;
  13. private bool m_isRecord = false;
  14. private string m_staticDateTime = "";
  15. private string m_dateTime = "";
  16. LuaDiffInfo luaDiff;
  17. List<string> m_detailList;
  18. private Vector2 scrollPositionAdd;
  19. private Vector2 scrollPositionRm;
  20. private Vector2 scrollPositionNull;
  21. private Vector2 scrollPositionDetail;
  22. public void DoRefScroll()
  23. {
  24. if (m_isStaticRecord)
  25. {
  26. EditorGUILayout.LabelField(m_staticDateTime);
  27. }
  28. if (m_isRecord)
  29. {
  30. EditorGUILayout.LabelField(m_dateTime);
  31. }
  32. if (luaDiff == null) return;
  33. GUILayout.BeginHorizontal();
  34. #region value
  35. GUILayout.BeginVertical();
  36. scrollPositionAdd = GUILayout.BeginScrollView(scrollPositionAdd, EditorStyles.helpBox, GUILayout.Height(100));
  37. int drawCount = 0;
  38. foreach (var item in luaDiff.addRef)
  39. {
  40. string typeStr = item.Key.ToString();
  41. bool needBreak = false;
  42. foreach (var v in item.Value)
  43. {
  44. DrawItemInfo(v, typeStr, luaDiff.addDetail);
  45. drawCount++;
  46. if (drawCount > 200)
  47. {
  48. GUILayout.Label("more please show log", GUILayout.Width(400));
  49. needBreak = true;
  50. break;
  51. }
  52. }
  53. if (needBreak) break;
  54. }
  55. GUILayout.EndScrollView();
  56. GUILayout.BeginHorizontal();
  57. EditorGUILayout.LabelField("add values count:" + drawCount, GUILayout.Width(150));
  58. if (GUILayout.Button("ShowLog", GUILayout.Width(100)))
  59. {
  60. ShowLog("add", luaDiff.addRef, luaDiff.addDetail);
  61. }
  62. GUILayout.EndHorizontal();
  63. scrollPositionRm = GUILayout.BeginScrollView(scrollPositionRm, EditorStyles.helpBox, GUILayout.Height(100));
  64. drawCount = 0;
  65. foreach (var item in luaDiff.rmRef)
  66. {
  67. string typeStr = item.Key.ToString();
  68. bool needBreak = false;
  69. foreach (var v in item.Value)
  70. {
  71. DrawItemInfo(v, typeStr, luaDiff.rmDetail);
  72. drawCount++;
  73. if (drawCount > 200)
  74. {
  75. GUILayout.Label("more please show log", GUILayout.Width(400));
  76. needBreak = true;
  77. break;
  78. }
  79. }
  80. if (needBreak) break;
  81. }
  82. GUILayout.EndScrollView();
  83. GUILayout.BeginHorizontal();
  84. EditorGUILayout.LabelField("remain values count:" + drawCount, GUILayout.Width(150));
  85. if (GUILayout.Button("ShowLog", GUILayout.Width(100)))
  86. {
  87. ShowLog("remain", luaDiff.rmRef, luaDiff.rmDetail);
  88. }
  89. GUILayout.EndHorizontal();
  90. scrollPositionNull = GUILayout.BeginScrollView(scrollPositionNull, EditorStyles.helpBox, GUILayout.Height(100));
  91. drawCount = 0;
  92. foreach (var item in luaDiff.nullRef)
  93. {
  94. string typeStr = item.Key.ToString();
  95. bool needBreak = false;
  96. foreach (var v in item.Value)
  97. {
  98. DrawItemInfo(v, typeStr, luaDiff.nullDetail);
  99. drawCount++;
  100. if (drawCount > 200)
  101. {
  102. GUILayout.Label("more please show log", GUILayout.Width(400));
  103. needBreak = true;
  104. break;
  105. }
  106. }
  107. if (needBreak) break;
  108. }
  109. GUILayout.EndScrollView();
  110. GUILayout.BeginHorizontal();
  111. EditorGUILayout.LabelField("Destory null values count:" + drawCount, GUILayout.Width(200));
  112. if (GUILayout.Button("ShowLog", GUILayout.Width(100)))
  113. {
  114. ShowLog("null", luaDiff.nullRef, luaDiff.nullDetail);
  115. }
  116. GUILayout.EndHorizontal();
  117. GUILayout.EndVertical();
  118. #endregion
  119. #region detail
  120. scrollPositionDetail = GUILayout.BeginScrollView(scrollPositionDetail, EditorStyles.helpBox, GUILayout.Width(250));
  121. if (m_detailList != null)
  122. {
  123. foreach (var item in m_detailList)
  124. {
  125. GUILayout.Label(item);
  126. }
  127. }
  128. GUILayout.EndScrollView();
  129. #endregion
  130. GUILayout.EndHorizontal();
  131. }
  132. public void ShowLog(string fileName, Dictionary<LuaTypes, HashSet<string>> refDict, Dictionary<string, List<string>> detailDict)
  133. {
  134. StringBuilder sb = new StringBuilder();
  135. foreach (var item in refDict)
  136. {
  137. string typeStr = item.Key.ToString();
  138. List<string> list = null;
  139. foreach (var v in item.Value)
  140. {
  141. string key = v;
  142. if (detailDict.TryGetValue(key, out list))
  143. {
  144. sb.AppendLine("type:" + typeStr);
  145. for (int i = 0, imax = list.Count; i < imax; i++)
  146. {
  147. if (i == imax - 1)
  148. {
  149. sb.AppendLine(" └─ref" + (i + 1) + " url:" + list[i]);
  150. }
  151. else
  152. {
  153. sb.AppendLine(" ├─ref" + (i + 1) + " url:" + list[i]);
  154. }
  155. }
  156. }
  157. }
  158. }
  159. if (!Directory.Exists(Application.dataPath.Replace("Assets", "Diff")))
  160. {
  161. Directory.CreateDirectory(Application.dataPath.Replace("Assets", "Diff"));
  162. }
  163. string path = Application.dataPath.Replace("Assets", "Diff/" + fileName + ".txt");
  164. File.WriteAllText(path, sb.ToString());
  165. System.Diagnostics.Process.Start("explorer.exe", Application.dataPath.Replace("Assets", "Diff").Replace("/", "\\"));
  166. }
  167. public void DrawItemInfo(string key, string value, Dictionary<string, List<string>> dict)
  168. {
  169. GUILayout.BeginHorizontal();
  170. List<string> list = null;
  171. dict.TryGetValue(key, out list);
  172. int count = list == null ? 0 : list.Count;
  173. GUILayout.Label(string.Format("Key:{0}", key), GUILayout.Width(400));
  174. GUILayout.Label(string.Format("Value:{0}", value));
  175. GUILayout.Label(string.Format("Ref Count:{0}", count));
  176. if (GUILayout.Button("detail", GUILayout.Width(100)))
  177. {
  178. m_detailList = list == null ? m_detailList : list;
  179. StringBuilder sb = new StringBuilder();
  180. foreach (var item in m_detailList)
  181. {
  182. sb.AppendLine(item);
  183. }
  184. Debug.Log(sb.ToString());
  185. }
  186. GUILayout.EndHorizontal();
  187. }
  188. public void Clear()
  189. {
  190. m_isRecord = false;
  191. m_isStaticRecord = false;
  192. luaDiff = null;
  193. m_dateTime = "";
  194. m_staticDateTime = "";
  195. m_detailList = null;
  196. LuaHook.ClearStaticRecord();
  197. LuaHook.ClearRecord();
  198. }
  199. public void MarkIsStaticRecord()
  200. {
  201. m_staticDateTime = "static record:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  202. m_isStaticRecord = true;
  203. }
  204. public void MarkIsRecord()
  205. {
  206. m_dateTime = "record" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
  207. m_isRecord = true;
  208. }
  209. public void DelDiffInfo(LuaDiffInfo info)
  210. {
  211. luaDiff = info;
  212. }
  213. }
  214. }
  215. #endif