DebuggerManager.DebuggerWindowGroup.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. //------------------------------------------------------------
  2. // Game Framework
  3. // Copyright © 2013-2021 loyalsoft. All rights reserved.
  4. // Homepage: http://www.game7000.com/
  5. // Feedback: http://www.game7000.com/
  6. //------------------------------------------------------------
  7. using System.Collections.Generic;
  8. namespace GameFramework.Debugger
  9. {
  10. internal sealed partial class DebuggerManager : GameFrameworkModule, IDebuggerManager
  11. {
  12. /// <summary>
  13. /// 调试器窗口组。
  14. /// </summary>
  15. private sealed class DebuggerWindowGroup : IDebuggerWindowGroup
  16. {
  17. private readonly List<KeyValuePair<string, IDebuggerWindow>> m_DebuggerWindows;
  18. private int m_SelectedIndex;
  19. private string[] m_DebuggerWindowNames;
  20. public DebuggerWindowGroup()
  21. {
  22. m_DebuggerWindows = new List<KeyValuePair<string, IDebuggerWindow>>();
  23. m_SelectedIndex = 0;
  24. m_DebuggerWindowNames = null;
  25. }
  26. /// <summary>
  27. /// 获取调试器窗口数量。
  28. /// </summary>
  29. public int DebuggerWindowCount
  30. {
  31. get
  32. {
  33. return m_DebuggerWindows.Count;
  34. }
  35. }
  36. /// <summary>
  37. /// 获取或设置当前选中的调试器窗口索引。
  38. /// </summary>
  39. public int SelectedIndex
  40. {
  41. get
  42. {
  43. return m_SelectedIndex;
  44. }
  45. set
  46. {
  47. m_SelectedIndex = value;
  48. }
  49. }
  50. /// <summary>
  51. /// 获取当前选中的调试器窗口。
  52. /// </summary>
  53. public IDebuggerWindow SelectedWindow
  54. {
  55. get
  56. {
  57. if (m_SelectedIndex >= m_DebuggerWindows.Count)
  58. {
  59. return null;
  60. }
  61. return m_DebuggerWindows[m_SelectedIndex].Value;
  62. }
  63. }
  64. /// <summary>
  65. /// 初始化调试组。
  66. /// </summary>
  67. /// <param name="args">初始化调试组参数。</param>
  68. public void Initialize(params object[] args)
  69. {
  70. }
  71. /// <summary>
  72. /// 关闭调试组。
  73. /// </summary>
  74. public void Shutdown()
  75. {
  76. foreach (KeyValuePair<string, IDebuggerWindow> debuggerWindow in m_DebuggerWindows)
  77. {
  78. debuggerWindow.Value.Shutdown();
  79. }
  80. m_DebuggerWindows.Clear();
  81. }
  82. /// <summary>
  83. /// 进入调试器窗口。
  84. /// </summary>
  85. public void OnEnter()
  86. {
  87. SelectedWindow.OnEnter();
  88. }
  89. /// <summary>
  90. /// 离开调试器窗口。
  91. /// </summary>
  92. public void OnLeave()
  93. {
  94. SelectedWindow.OnLeave();
  95. }
  96. /// <summary>
  97. /// 调试组轮询。
  98. /// </summary>
  99. /// <param name="elapseSeconds">逻辑流逝时间,以秒为单位。</param>
  100. /// <param name="realElapseSeconds">真实流逝时间,以秒为单位。</param>
  101. public void OnUpdate(float elapseSeconds, float realElapseSeconds)
  102. {
  103. SelectedWindow.OnUpdate(elapseSeconds, realElapseSeconds);
  104. }
  105. /// <summary>
  106. /// 调试器窗口绘制。
  107. /// </summary>
  108. public void OnDraw()
  109. {
  110. }
  111. private void RefreshDebuggerWindowNames()
  112. {
  113. int index = 0;
  114. m_DebuggerWindowNames = new string[m_DebuggerWindows.Count];
  115. foreach (KeyValuePair<string, IDebuggerWindow> debuggerWindow in m_DebuggerWindows)
  116. {
  117. m_DebuggerWindowNames[index++] = debuggerWindow.Key;
  118. }
  119. }
  120. /// <summary>
  121. /// 获取调试组的调试器窗口名称集合。
  122. /// </summary>
  123. public string[] GetDebuggerWindowNames()
  124. {
  125. return m_DebuggerWindowNames;
  126. }
  127. /// <summary>
  128. /// 获取调试器窗口。
  129. /// </summary>
  130. /// <param name="path">调试器窗口路径。</param>
  131. /// <returns>要获取的调试器窗口。</returns>
  132. public IDebuggerWindow GetDebuggerWindow(string path)
  133. {
  134. if (string.IsNullOrEmpty(path))
  135. {
  136. return null;
  137. }
  138. int pos = path.IndexOf('/');
  139. if (pos < 0 || pos >= path.Length - 1)
  140. {
  141. return InternalGetDebuggerWindow(path);
  142. }
  143. string debuggerWindowGroupName = path.Substring(0, pos);
  144. string leftPath = path.Substring(pos + 1);
  145. DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName);
  146. if (debuggerWindowGroup == null)
  147. {
  148. return null;
  149. }
  150. return debuggerWindowGroup.GetDebuggerWindow(leftPath);
  151. }
  152. /// <summary>
  153. /// 选中调试器窗口。
  154. /// </summary>
  155. /// <param name="path">调试器窗口路径。</param>
  156. /// <returns>是否成功选中调试器窗口。</returns>
  157. public bool SelectDebuggerWindow(string path)
  158. {
  159. if (string.IsNullOrEmpty(path))
  160. {
  161. return false;
  162. }
  163. int pos = path.IndexOf('/');
  164. if (pos < 0 || pos >= path.Length - 1)
  165. {
  166. return InternalSelectDebuggerWindow(path);
  167. }
  168. string debuggerWindowGroupName = path.Substring(0, pos);
  169. string leftPath = path.Substring(pos + 1);
  170. DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName);
  171. if (debuggerWindowGroup == null || !InternalSelectDebuggerWindow(debuggerWindowGroupName))
  172. {
  173. return false;
  174. }
  175. return debuggerWindowGroup.SelectDebuggerWindow(leftPath);
  176. }
  177. /// <summary>
  178. /// 注册调试器窗口。
  179. /// </summary>
  180. /// <param name="path">调试器窗口路径。</param>
  181. /// <param name="debuggerWindow">要注册的调试器窗口。</param>
  182. public void RegisterDebuggerWindow(string path, IDebuggerWindow debuggerWindow)
  183. {
  184. if (string.IsNullOrEmpty(path))
  185. {
  186. throw new GameFrameworkException("Path is invalid.");
  187. }
  188. int pos = path.IndexOf('/');
  189. if (pos < 0 || pos >= path.Length - 1)
  190. {
  191. if (InternalGetDebuggerWindow(path) != null)
  192. {
  193. throw new GameFrameworkException("Debugger window has been registered.");
  194. }
  195. m_DebuggerWindows.Add(new KeyValuePair<string, IDebuggerWindow>(path, debuggerWindow));
  196. RefreshDebuggerWindowNames();
  197. }
  198. else
  199. {
  200. string debuggerWindowGroupName = path.Substring(0, pos);
  201. string leftPath = path.Substring(pos + 1);
  202. DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName);
  203. if (debuggerWindowGroup == null)
  204. {
  205. if (InternalGetDebuggerWindow(debuggerWindowGroupName) != null)
  206. {
  207. throw new GameFrameworkException("Debugger window has been registered, can not create debugger window group.");
  208. }
  209. debuggerWindowGroup = new DebuggerWindowGroup();
  210. m_DebuggerWindows.Add(new KeyValuePair<string, IDebuggerWindow>(debuggerWindowGroupName, debuggerWindowGroup));
  211. RefreshDebuggerWindowNames();
  212. }
  213. debuggerWindowGroup.RegisterDebuggerWindow(leftPath, debuggerWindow);
  214. }
  215. }
  216. /// <summary>
  217. /// 解除注册调试器窗口。
  218. /// </summary>
  219. /// <param name="path">调试器窗口路径。</param>
  220. /// <returns>是否解除注册调试器窗口成功。</returns>
  221. public bool UnregisterDebuggerWindow(string path)
  222. {
  223. if (string.IsNullOrEmpty(path))
  224. {
  225. return false;
  226. }
  227. int pos = path.IndexOf('/');
  228. if (pos < 0 || pos >= path.Length - 1)
  229. {
  230. IDebuggerWindow debuggerWindow = InternalGetDebuggerWindow(path);
  231. bool result = m_DebuggerWindows.Remove(new KeyValuePair<string, IDebuggerWindow>(path, debuggerWindow));
  232. debuggerWindow.Shutdown();
  233. RefreshDebuggerWindowNames();
  234. return result;
  235. }
  236. string debuggerWindowGroupName = path.Substring(0, pos);
  237. string leftPath = path.Substring(pos + 1);
  238. DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName);
  239. if (debuggerWindowGroup == null)
  240. {
  241. return false;
  242. }
  243. return debuggerWindowGroup.UnregisterDebuggerWindow(leftPath);
  244. }
  245. private IDebuggerWindow InternalGetDebuggerWindow(string name)
  246. {
  247. foreach (KeyValuePair<string, IDebuggerWindow> debuggerWindow in m_DebuggerWindows)
  248. {
  249. if (debuggerWindow.Key == name)
  250. {
  251. return debuggerWindow.Value;
  252. }
  253. }
  254. return null;
  255. }
  256. private bool InternalSelectDebuggerWindow(string name)
  257. {
  258. for (int i = 0; i < m_DebuggerWindows.Count; i++)
  259. {
  260. if (m_DebuggerWindows[i].Key == name)
  261. {
  262. m_SelectedIndex = i;
  263. return true;
  264. }
  265. }
  266. return false;
  267. }
  268. }
  269. }
  270. }