-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathRendering.cs
More file actions
267 lines (235 loc) · 9.14 KB
/
Copy pathRendering.cs
File metadata and controls
267 lines (235 loc) · 9.14 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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
using System.Text;
using KismetAnalyzer.Dot;
using static KismetAnalyzer.Dot.Html;
namespace KismetAnalyzer;
public enum ElementKind
{
ExpressionType, // EX_Jump, EX_LocalVariable, etc.
Address, // Numeric jump targets, bytecode addresses
Variable, // Local, instance, default variables
Function, // Function calls, virtual functions
StringLiteral, // Text constants
NumericLiteral, // Integers, floats, vectors
TypeName, // Class names, struct names, property types
Keyword, // case, default, operators
PlainText // Punctuation, separators, spaces
}
public readonly record struct Element(ElementKind Kind, string Value);
public interface ILinesRenderer
{
string Render(IEnumerable<Element> elements);
}
public class PlainTextRenderer : ILinesRenderer
{
public static readonly PlainTextRenderer Instance = new();
public string Render(IEnumerable<Element> elements)
{
return string.Concat(elements.Select(e => e.Value));
}
public static void RenderTo(TextWriter writer, SummaryGenerator.Lines lines)
{
RenderTo(writer, new[] { lines });
}
public static void RenderTo(TextWriter writer, IEnumerable<SummaryGenerator.Lines> linesCollection)
{
foreach (var lines in linesCollection)
{
foreach (var (address, nest, elements) in lines.GetElements())
{
var sb = new StringBuilder();
for (int i = 0; i < elements.Count; i++)
{
var e = elements[i];
// Add indentation after prefix address (or at start if no prefix)
if (i == 1 || (i == 0 && e.Kind != ElementKind.Address))
{
sb.Append("".PadRight(nest * 4));
}
sb.Append(e.Value);
}
writer.WriteLine(sb.ToString());
}
}
}
}
public class AnsiColorRenderer : ILinesRenderer
{
public static readonly AnsiColorRenderer Instance = new();
private static readonly Dictionary<ElementKind, string> Colors = new()
{
[ElementKind.ExpressionType] = "\x1b[36m", // Cyan
[ElementKind.Address] = "\x1b[33m", // Yellow
[ElementKind.Variable] = "\x1b[32m", // Green
[ElementKind.Function] = "\x1b[35m", // Magenta
[ElementKind.StringLiteral] = "\x1b[31m", // Red
[ElementKind.NumericLiteral] = "\x1b[34m", // Blue
[ElementKind.TypeName] = "\x1b[93m", // Bright Yellow
[ElementKind.Keyword] = "\x1b[95m", // Bright Magenta
[ElementKind.PlainText] = "", // No color
};
private const string Reset = "\x1b[0m";
private const string NestedAddressColor = "\x1b[90m"; // Dim gray for nested addresses
public static string GetColor(ElementKind kind) => Colors.GetValueOrDefault(kind, "");
public string Render(IEnumerable<Element> elements)
{
var sb = new StringBuilder();
foreach (var e in elements)
{
var color = Colors.GetValueOrDefault(e.Kind, "");
if (!string.IsNullOrEmpty(color))
{
sb.Append(color);
sb.Append(e.Value);
sb.Append(Reset);
}
else
{
sb.Append(e.Value);
}
}
return sb.ToString();
}
public static void RenderTo(TextWriter writer, SummaryGenerator.Lines lines)
{
RenderTo(writer, new[] { lines });
}
public static void RenderTo(TextWriter writer, IEnumerable<SummaryGenerator.Lines> linesCollection)
{
foreach (var lines in linesCollection)
{
foreach (var (address, nest, elements) in lines.GetElements())
{
var sb = new StringBuilder();
for (int i = 0; i < elements.Count; i++)
{
var e = elements[i];
bool isPrefix = (i == 0 && e.Kind == ElementKind.Address);
// Add indentation after prefix address (or at start if no prefix)
if (i == 1 || (i == 0 && e.Kind != ElementKind.Address))
{
sb.Append("".PadRight(nest * 4));
}
// Prefix address at nest > 0 gets dimmer color
var color = (isPrefix && nest > 0)
? NestedAddressColor
: Colors.GetValueOrDefault(e.Kind, "");
if (!string.IsNullOrEmpty(color))
{
sb.Append(color);
sb.Append(e.Value);
sb.Append(Reset);
}
else
{
sb.Append(e.Value);
}
}
writer.WriteLine(sb.ToString());
}
}
}
}
public class HtmlRenderer : ILinesRenderer
{
public static readonly HtmlRenderer Instance = new();
private static readonly Dictionary<ElementKind, string> CssClasses = new()
{
[ElementKind.ExpressionType] = "expr",
[ElementKind.Address] = "addr",
[ElementKind.Variable] = "var",
[ElementKind.Function] = "func",
[ElementKind.StringLiteral] = "str",
[ElementKind.NumericLiteral] = "num",
[ElementKind.TypeName] = "type",
[ElementKind.Keyword] = "kw",
[ElementKind.PlainText] = "",
};
public string Render(IEnumerable<Element> elements)
{
var sb = new StringBuilder();
foreach (var e in elements)
{
var escaped = System.Net.WebUtility.HtmlEncode(e.Value);
var cssClass = CssClasses.GetValueOrDefault(e.Kind, "");
if (string.IsNullOrEmpty(cssClass))
sb.Append(escaped);
else
sb.Append($"<span class=\"{cssClass}\">{escaped}</span>");
}
return sb.ToString();
}
}
public class DotHtmlLabelRenderer : ILinesRenderer
{
public static readonly DotHtmlLabelRenderer Instance = new();
private static readonly Dictionary<ElementKind, string> Colors = new()
{
[ElementKind.ExpressionType] = "#0055AA", // Bold blue
[ElementKind.Address] = "#555555", // Dark gray
[ElementKind.Variable] = "#006400", // Dark green
[ElementKind.Function] = "#6A0080", // Dark purple
[ElementKind.StringLiteral] = "#A00000", // Dark red
[ElementKind.NumericLiteral] = "#0000CD", // Medium blue
[ElementKind.TypeName] = "#996300", // Dark orange
[ElementKind.Keyword] = "#800080", // Purple
[ElementKind.PlainText] = "", // No color (default black)
};
public static string NestedAddressColor => "#AAAAAA"; // Lighter gray for nested addresses
public static string GetColor(ElementKind kind) => Colors.GetValueOrDefault(kind, "");
public static void RenderTo(HtmlElement container, SummaryGenerator.Lines lines)
{
RenderTo(container, new[] { lines });
}
public static void RenderTo(HtmlElement container, IEnumerable<SummaryGenerator.Lines> linesCollection)
{
bool firstLine = true;
foreach (var lines in linesCollection)
{
foreach (var (address, nest, elements) in lines.GetElements())
{
if (!firstLine) container.Add(Br());
firstLine = false;
for (int i = 0; i < elements.Count; i++)
{
var e = elements[i];
// Add indentation after prefix address (or at start if no prefix)
if (i == 1 || (i == 0 && e.Kind != ElementKind.Address))
{
for (int j = 0; j < nest; j++)
container.Add(Raw(" "));
}
// Prefix address at nest > 0 gets lighter color
var color = (i == 0 && e.Kind == ElementKind.Address && nest > 0)
? NestedAddressColor
: GetColor(e.Kind);
if (!string.IsNullOrEmpty(color))
container.Add(Font(color).Add(e.Value));
else
container.Add(e.Value);
}
}
}
}
public string Render(IEnumerable<Element> elements)
{
var sb = new StringBuilder();
foreach (var e in elements)
{
var escaped = SanitizeForHtml(e.Value);
var color = Colors.GetValueOrDefault(e.Kind, "");
if (!string.IsNullOrEmpty(color))
sb.Append($"<FONT COLOR=\"{color}\">{escaped}</FONT>");
else
sb.Append(escaped);
}
return sb.ToString();
}
public static string SanitizeForHtml(string str)
{
return str
.Replace("&", "&")
.Replace("<", "<")
.Replace(">", ">")
.Replace("\"", """);
}
}