-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScriptGlobals.DebugHelpers.cs
179 lines (160 loc) · 6.07 KB
/
ScriptGlobals.DebugHelpers.cs
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
using System;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
namespace Int19h.Bannerlord.CSharp.Scripting {
partial class ScriptGlobals {
private static Func<string> _defaultToString = new object().ToString;
public static string Dump(object o) {
if (o == null) {
return "null";
}
var output = new StringBuilder();
bool HasDefaultToString(object value) {
Func<string> toString = value.ToString;
return toString.Method == _defaultToString.Method;
}
void AppendType(Type type) {
var typeArgs = type.GetGenericArguments();
if (!typeArgs.Any()) {
output.Append(type.Name);
return;
}
var typeName = type.Name;
var backtick = typeName.LastIndexOf('`');
if (backtick >= 0) {
typeName = typeName.Remove(backtick);
}
output.Append(typeName);
output.Append('<');
foreach (var typeArg in typeArgs) {
AppendType(typeArg);
}
output.Append('>');
}
void AppendCast(object value) {
output.Append("(");
AppendType(value.GetType());
output.Append(") ");
}
void AppendStringLiteral(string s) {
output.Append("'");
foreach (var c in s) {
if (c == '\n') {
output.Append("\\n");
} else if (c == '"' || c == '\'' || c == ';' || c < 32 || c > 127) {
output.Append($"\\u{(ushort)c:X04}");
} else {
output.Append(c);
}
}
output.Append("'");
}
void AppendValue(Func<object> getter) {
object value;
try {
value = getter();
} catch (Exception ex) {
output.Append($"<error: {ex.Message}>");
return;
}
if (value is null) {
output.Append("null");
} else if (value is sbyte or short or int or long or byte or ushort or uint or ulong or float or double or decimal) {
output.Append(value);
} else if (value is bool b) {
output.Append(b ? "true" : "false");
} else if (value is char c) {
AppendStringLiteral(new string(c, 1));
output.Append("[0]");
} else if (value is string s) {
AppendStringLiteral(s);
} else if (value is IEnumerable en) {
int? count = null;
if (en is ICollection coll) {
try {
count = coll.Count;
} catch (Exception) {
}
}
AppendCast(value);
if (count is int n) {
output.Append($"[{n} items]");
} else {
output.Append("[...]");
}
} else {
string? repr = null;
if (!HasDefaultToString(value)) {
try {
repr = value.ToString();
} catch (Exception) {
}
}
AppendCast(value);
if (repr != null) {
AppendStringLiteral(repr);
} else {
output.Append("...");
}
}
}
var type = o.GetType();
output.Append(": ");
AppendType(type);
output.AppendLine(" = {");
var props = type.GetProperties().OrderBy(prop => prop.Name);
foreach (var prop in props) {
if (prop.GetIndexParameters().Length != 0) {
continue;
}
output.Append(" ");
output.Append(prop.Name);
output.Append(": ");
AppendType(prop.PropertyType);
output.Append(" = ");
AppendValue(() => prop.GetValue(o));
output.AppendLine(",");
}
if (o is IEnumerable items and not string) {
int i = 0;
foreach (var item in items) {
output.Append($" [{i}] = ");
AppendValue(() => item);
output.AppendLine(",");
if (++i >= 1000) {
if (o is ICollection coll) {
int remaining = coll.Count - i;
if (remaining > 0) {
output.AppendLine($" ... ({remaining} more) ...");
}
} else {
output.AppendLine(" ...");
}
break;
}
}
}
output.Append("}");
return output.ToString();
}
public static void Edit(params object[] objects) {
var uiThread = new Thread(() => {
var grid = new PropertyGrid {
Dock = DockStyle.Fill,
SelectedObjects = objects,
};
var form = new Form {
Controls = { grid },
Text = objects.Length == 1 ? $"{objects[0]}" : $"{objects.Length} objects",
};
Application.Run(form);
});
uiThread.SetApartmentState(ApartmentState.STA);
uiThread.Start();
uiThread.Join();
}
}
}