using System.Collections.Generic; namespace TEngine { internal sealed partial class DebuggerModule : IDebuggerModule { /// /// 调试器窗口组。 /// private sealed class DebuggerWindowGroup : IDebuggerWindowGroup { private readonly List> _debuggerWindows = new(); private int _selectedIndex = 0; private string[] _debuggerWindowNames = null; /// /// 获取调试器窗口数量。 /// public int DebuggerWindowCount { get { return _debuggerWindows.Count; } } /// /// 获取或设置当前选中的调试器窗口索引。 /// public int SelectedIndex { get { return _selectedIndex; } set { _selectedIndex = value; } } /// /// 获取当前选中的调试器窗口。 /// public IDebuggerWindow SelectedWindow { get { if (_selectedIndex >= _debuggerWindows.Count) { return null; } return _debuggerWindows[_selectedIndex].Value; } } /// /// 初始化调试组。 /// /// 初始化调试组参数。 public void Initialize(params object[] args) { } /// /// 关闭调试组。 /// public void Shutdown() { foreach (KeyValuePair debuggerWindow in _debuggerWindows) { debuggerWindow.Value.Shutdown(); } _debuggerWindows.Clear(); } /// /// 进入调试器窗口。 /// public void OnEnter() { SelectedWindow.OnEnter(); } /// /// 离开调试器窗口。 /// public void OnLeave() { SelectedWindow.OnLeave(); } /// /// 调试组轮询。 /// /// 逻辑流逝时间,以秒为单位。 /// 真实流逝时间,以秒为单位。 public void OnUpdate(float elapseSeconds, float realElapseSeconds) { SelectedWindow.OnUpdate(elapseSeconds, realElapseSeconds); } /// /// 调试器窗口绘制。 /// public void OnDraw() { } private void RefreshDebuggerWindowNames() { int index = 0; _debuggerWindowNames = new string[_debuggerWindows.Count]; foreach (KeyValuePair debuggerWindow in _debuggerWindows) { _debuggerWindowNames[index++] = debuggerWindow.Key; } } /// /// 获取调试组的调试器窗口名称集合。 /// public string[] GetDebuggerWindowNames() { return _debuggerWindowNames; } /// /// 获取调试器窗口。 /// /// 调试器窗口路径。 /// 要获取的调试器窗口。 public IDebuggerWindow GetDebuggerWindow(string path) { if (string.IsNullOrEmpty(path)) { return null; } int pos = path.IndexOf('/'); if (pos < 0 || pos >= path.Length - 1) { return InternalGetDebuggerWindow(path); } string debuggerWindowGroupName = path.Substring(0, pos); string leftPath = path.Substring(pos + 1); DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName); if (debuggerWindowGroup == null) { return null; } return debuggerWindowGroup.GetDebuggerWindow(leftPath); } /// /// 选中调试器窗口。 /// /// 调试器窗口路径。 /// 是否成功选中调试器窗口。 public bool SelectDebuggerWindow(string path) { if (string.IsNullOrEmpty(path)) { return false; } int pos = path.IndexOf('/'); if (pos < 0 || pos >= path.Length - 1) { return InternalSelectDebuggerWindow(path); } string debuggerWindowGroupName = path.Substring(0, pos); string leftPath = path.Substring(pos + 1); DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName); if (debuggerWindowGroup == null || !InternalSelectDebuggerWindow(debuggerWindowGroupName)) { return false; } return debuggerWindowGroup.SelectDebuggerWindow(leftPath); } /// /// 注册调试器窗口。 /// /// 调试器窗口路径。 /// 要注册的调试器窗口。 public void RegisterDebuggerWindow(string path, IDebuggerWindow debuggerWindow) { if (string.IsNullOrEmpty(path)) { throw new GameFrameworkException("Path is invalid."); } int pos = path.IndexOf('/'); if (pos < 0 || pos >= path.Length - 1) { if (InternalGetDebuggerWindow(path) != null) { throw new GameFrameworkException("Debugger window has been registered."); } _debuggerWindows.Add(new KeyValuePair(path, debuggerWindow)); RefreshDebuggerWindowNames(); } else { string debuggerWindowGroupName = path.Substring(0, pos); string leftPath = path.Substring(pos + 1); DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName); if (debuggerWindowGroup == null) { if (InternalGetDebuggerWindow(debuggerWindowGroupName) != null) { throw new GameFrameworkException("Debugger window has been registered, can not create debugger window group."); } debuggerWindowGroup = new DebuggerWindowGroup(); _debuggerWindows.Add(new KeyValuePair(debuggerWindowGroupName, debuggerWindowGroup)); RefreshDebuggerWindowNames(); } debuggerWindowGroup.RegisterDebuggerWindow(leftPath, debuggerWindow); } } /// /// 解除注册调试器窗口。 /// /// 调试器窗口路径。 /// 是否解除注册调试器窗口成功。 public bool UnregisterDebuggerWindow(string path) { if (string.IsNullOrEmpty(path)) { return false; } int pos = path.IndexOf('/'); if (pos < 0 || pos >= path.Length - 1) { IDebuggerWindow debuggerWindow = InternalGetDebuggerWindow(path); bool result = _debuggerWindows.Remove(new KeyValuePair(path, debuggerWindow)); debuggerWindow.Shutdown(); RefreshDebuggerWindowNames(); return result; } string debuggerWindowGroupName = path.Substring(0, pos); string leftPath = path.Substring(pos + 1); DebuggerWindowGroup debuggerWindowGroup = (DebuggerWindowGroup)InternalGetDebuggerWindow(debuggerWindowGroupName); if (debuggerWindowGroup == null) { return false; } return debuggerWindowGroup.UnregisterDebuggerWindow(leftPath); } private IDebuggerWindow InternalGetDebuggerWindow(string name) { foreach (KeyValuePair debuggerWindow in _debuggerWindows) { if (debuggerWindow.Key == name) { return debuggerWindow.Value; } } return null; } private bool InternalSelectDebuggerWindow(string name) { for (int i = 0; i < _debuggerWindows.Count; i++) { if (_debuggerWindows[i].Key == name) { _selectedIndex = i; return true; } } return false; } } } }