forked from Caraxi/SimpleTweaksPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTPDebug.cs
More file actions
238 lines (189 loc) · 6.86 KB
/
STPDebug.cs
File metadata and controls
238 lines (189 loc) · 6.86 KB
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Dalamud.Bindings.ImGui;
using Dalamud.Game.Command;
using Dalamud.Interface.Colors;
using Dalamud.Interface.Windowing;
using Dalamud.Plugin;
using STPDebug.Debugging;
using STPDebug.Utility;
namespace STPDebug;
public sealed class STPDebug : IDalamudPlugin
{
public string Name => "STPDebug";
public static STPDebug Plugin { get; private set; } = null!;
public STPDebugConfig PluginConfig { get; }
public DebugWindow DebugWindow { get; } = new();
public WindowSystem WindowSystem { get; } = new("STPDebug");
internal bool ShowErrorWindow { get; private set; }
internal List<CaughtError> ErrorList { get; } = [];
public STPDebug(IDalamudPluginInterface pluginInterface)
{
Plugin = this;
pluginInterface.Create<Service>();
pluginInterface.Create<SimpleLog>();
pluginInterface.Create<Common>();
PluginConfig = Service.PluginInterface.GetPluginConfig() as STPDebugConfig ?? new();
Task.Run(() => Service.Framework.RunOnFrameworkThread(Initialize));
}
public void Dispose()
{
SimpleLog.Debug("开始释放调试器插件");
PluginConfig.Save();
Service.PluginInterface.UiBuilder.Draw -= BuildUI;
Service.PluginInterface.UiBuilder.OpenConfigUi -= OnOpenConfig;
RemoveCommands();
DebugManager.Dispose();
foreach (var hook in Common.HookList.Where(hook => !hook.IsDisposed))
{
if (hook.IsEnabled) hook.Disable();
hook.Dispose();
}
Common.HookList.Clear();
Common.Shutdown();
TooltipManager.Destroy();
SimpleEvent.Destroy();
Service.Dispose();
}
private void Initialize()
{
UiHelper.Setup(Service.SigScanner);
DebugManager.SetPlugin(this);
Common.Setup();
WindowSystem.AddWindow(DebugWindow);
Service.PluginInterface.UiBuilder.Draw += BuildUI;
Service.PluginInterface.UiBuilder.OpenConfigUi += OnOpenConfig;
SetupCommands();
}
private void SetupCommands()
{
Service.Commands.AddHandler
(
"/stpdb",
new CommandInfo(OnCommand)
{
HelpMessage = "Open STPDebug debug window",
ShowInHelp = true
}
);
}
private static void RemoveCommands() =>
Service.Commands.RemoveHandler("/stpdb");
private static void OnOpenConfig() => OpenDebugWindow();
private static void OnCommand(string command, string arguments) => OpenDebugWindow();
private static void OpenDebugWindow() => Plugin.DebugWindow.UnCollapseOrToggle();
private void BuildUI()
{
foreach (var error in ErrorList.Where(error => error.IsNew))
{
error.IsNew = false;
ShowErrorWindow = true;
}
WindowSystem.Draw();
if (ShowErrorWindow) DrawErrorWindow();
if (Service.PluginInterface.IsDevMenuOpen)
{
if (ImGui.BeginMainMenuBar())
{
if (ImGui.MenuItem("STPDebug")) OpenDebugWindow();
ImGui.EndMainMenuBar();
}
}
}
private void DrawErrorWindow()
{
if (ErrorList.Count == 0)
{
ShowErrorWindow = false;
return;
}
var isOpen = true;
if (ImGui.Begin($"{Name}:错误", ref isOpen, ImGuiWindowFlags.NoCollapse | ImGuiWindowFlags.AlwaysAutoResize))
{
for (var index = 0; index < ErrorList.Count && index < 5; index++)
{
var error = ErrorList[index];
ImGui.Text("调试器内部发生异常:");
if (!string.IsNullOrWhiteSpace(error.Message)) ImGui.TextWrapped(error.Message);
ImGui.TextWrapped($"{error.Exception}");
if (ImGui.Button($"关闭此项###closeError_{index}")) error.Closed = true;
if (error.Count > 1)
{
ImGui.SameLine();
ImGui.Text($"已重复出现 {error.Count} 次。");
}
ImGui.Separator();
}
if (ErrorList.Count > 5) ImGui.TextColored(ImGuiColors.DalamudRed, $"还有 {ErrorList.Count - 5} 条额外错误未展开。");
}
ImGui.End();
ErrorList.RemoveAll(error => error.Closed);
if (!isOpen)
{
ErrorList.Clear();
ShowErrorWindow = false;
}
}
#if DEBUG
public void Error
(
Exception exception,
string message = "",
[CallerFilePath] string callerFilePath = "",
[CallerLineNumber] int callerLineNumber =
0,
[CallerMemberName] string callerMemberName = ""
)
{
if (string.IsNullOrWhiteSpace(message))
{
SimpleLog.Error("调试器内部发生异常。", callerFilePath, callerMemberName, callerLineNumber);
}
else
{
SimpleLog.Error($"调试器内部发生异常:{message}", callerFilePath, callerMemberName, callerLineNumber);
}
SimpleLog.Error($"{exception}", callerFilePath, callerMemberName, callerLineNumber);
AddError(exception, message, allowContinue: false);
}
#else
public void Error(Exception exception, string message = "")
{
if (string.IsNullOrWhiteSpace(message)) SimpleLog.Error("调试器内部发生异常。");
else SimpleLog.Error($"调试器内部发生异常:{message}");
SimpleLog.Error($"{exception}");
AddError(exception, message, false);
}
#endif
private void AddError(Exception exception, string message, bool allowContinue)
{
var error = new CaughtError
{
Exception = exception,
IsNew = !allowContinue,
Message = message
};
var index = ErrorList.IndexOf(error);
if (index >= 0)
{
ErrorList[index].Count++;
ErrorList[index].IsNew |= error.IsNew;
return;
}
ErrorList.Insert(0, error);
if (ErrorList.Count > 50) ErrorList.RemoveRange(50, ErrorList.Count - 50);
}
internal sealed class CaughtError
{
public Exception Exception { get; init; } = null!;
public bool IsNew { get; set; } = true;
public bool Closed { get; set; }
public string Message { get; init; } = string.Empty;
public ulong Count { get; set; } = 1;
public override bool Equals(object? obj) =>
obj is CaughtError other && other.Message == Message && $"{other.Exception}" == $"{Exception}";
public override int GetHashCode() => HashCode.Combine(Message, $"{Exception}");
}
}