-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFormatStructurePrinter.cs
601 lines (515 loc) · 26.2 KB
/
FormatStructurePrinter.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
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
using EditClipboardContents;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using static EditClipboardContents.ClipboardFormats;
// Disable IDE warnings that showed up after going from C# 7 to C# 9
#pragma warning disable IDE0079 // Disable message about unnecessary suppression
#pragma warning disable IDE1006 // Disable messages about capitalization of control names
#pragma warning disable IDE0063 // Disable messages about Using expression simplification
#pragma warning disable IDE0090 // Disable messages about New expression simplification
#pragma warning disable IDE0028,IDE0300,IDE0305 // Disable message about collection initialization
#pragma warning disable IDE0074 // Disable message about compound assignment for checking if null
#pragma warning disable IDE0066 // Disable message about switch case expression
#pragma warning disable IDE0090 // Disable messages about New expression simplification
// Nullable reference types
#nullable enable
namespace EditClipboardContents
{
public static class FormatStructurePrinter
{
public static string rtfHeader = @"{\rtf1\utf8\viewkind4\uc1\pard ";
public static string GetDataStringForTextbox(string formatName, ClipboardItem? fullItem, bool plaintext = false)
{
string displayText;
if (fullItem != null && fullItem.ClipDataObject != null)
{
displayText = fullItem.ClipDataObject.GetCacheStructObjectDisplayInfo();
}
else
{
displayText = CreateDataString(formatName, fullItem);
if (plaintext == true )
displayText = RemoveRtfFormatting(displayText);
return displayText;
}
// Create the data string if it doesn't exist then put it into the cache
if (string.IsNullOrEmpty(displayText))
{
displayText = CreateDataString(formatName, fullItem);
fullItem.ClipDataObject.SetCacheStructObjectDisplayInfo(displayText);
}
if (plaintext == true)
{
displayText = RemoveRtfFormatting(displayText);
}
return displayText;
}
private static string RemoveRtfFormatting(string inputString)
{
string displayText = inputString;
// Check if the rtf header is even there
var headerMatch = System.Text.RegularExpressions.Regex.Match(displayText, Regex.Escape(rtfHeader));
if ( headerMatch.Success )
{
// First remove the RTF header
displayText = System.Text.RegularExpressions.Regex.Replace(displayText, Regex.Escape(rtfHeader), "");
// Remove RTF formatting tags
// First remove assuming there is a space after start tags and before end tags, then remove regardless of space
// Need to remove the end tags first because they have the 0, and if we remove the start tags then it will leave the 0
displayText = displayText
// End Tags:
.Replace(@" \b0", "")
.Replace(@"\b0", "")
.Replace(@" \ul0", "")
.Replace(@"\ul0", "")
.Replace(@" \i0", "")
.Replace(@"\i0", "")
// Start Tags:
.Replace(@"\ul ", "")
.Replace(@"\ul", "")
.Replace(@"\i ", "")
.Replace(@"\i", "")
.Replace(@"\b ", "")
.Replace(@"\b", "")
// Other Tags:
.Replace(@"\line ", "\n")
.Replace(@"\line", "\n");
// Remove the last curly brace if it's there. But be sure to only remove 1
if ( displayText.TrimEnd().EndsWith("}") )
{
displayText = displayText.TrimEnd().Substring(0, displayText.TrimEnd().Length - 1);
}
}
return displayText;
}
public static string CreateDataString(string formatName, ClipboardItem? fullItem)
{
bool anyFormatInfoAvailable = false;
FormatAnalysis? analysis = fullItem?.FormatAnalysis;
string indent = " ";
string originalIndent = indent; // Save the original indent for later, otherwise it will keep doubling in recursive functions
StringBuilder dataInfoString = new StringBuilder();
dataInfoString.AppendLine(@$"\b Format: \b0 {formatName}");
if (FormatInfoHardcoded.ShellFormatNameMap.TryGetValue(formatName, out string? shellFormatName))
{
dataInfoString.AppendLine(@$"{indent}\b Shell Format Definition Name: \b0 {shellFormatName}");
anyFormatInfoAvailable = true;
if (FormatInfoHardcoded.ShellDefinitionDocLink.TryGetValue(shellFormatName, out string? shellDocURL))
{
dataInfoString.AppendLine(@$"{indent}\b Shell Format Info: \b0 {shellDocURL}");
}
}
// Check for any hard coded format
if (FormatInfoHardcoded.FormatDescriptions.TryGetValue(formatName, out string formatDescription))
{
dataInfoString.AppendLine(@$"\b Description: \b0 {formatDescription}");
anyFormatInfoAvailable = true;
}
else if (analysis?.KnownFileExtension != null)
{
dataInfoString.AppendLine(@$"\b File Type Extension: \b0 {analysis.KnownFileExtension}");
anyFormatInfoAvailable = true;
}
// Otherwise check for any info from prior format analysis in SetDataInfo()
else if (analysis?.PossibleFileExtensions != null && analysis.PossibleFileExtensions.Count > 0)
{
// If Both description and extensions are available, add a header
if (analysis.FileTypeDescription != null && analysis.PossibleFileExtensions.Count > 0)
{
anyFormatInfoAvailable = true;
dataInfoString.AppendLine(@$"\line\b Found Likely File Type: \b0");
dataInfoString.AppendLine(@$"\b {indent}File Extension(s): \b0 {string.Join(", ", analysis.PossibleFileExtensions)}");
dataInfoString.AppendLine(@$"\b {indent}Description: \b0 {analysis.FileTypeDescription}");
}
// If both aren't available it means it was a mime lookup so there won't be a description
else
{
dataInfoString.AppendLine(@$"\b Possible File Extensions: \b0 {string.Join(", ", analysis.PossibleFileExtensions)}");
}
anyFormatInfoAvailable = true;
}
// Add URL Link if it exists by dictionary lookup
if (FormatInfoHardcoded.FormatDocsLinks.TryGetValue(formatName, out string docURL))
{
dataInfoString.AppendLine(@$"\b Details: \b0 " + docURL);
anyFormatInfoAvailable = true;
}
if (fullItem?.DataInfoList.Count > 0 && !string.IsNullOrEmpty(fullItem.DataInfoList[0]))
{
dataInfoString.AppendLine(@$"\line \b Data Info: \b0");
// Add each selectedItem in DataInfoList to the result indented
foreach (string dataInfoItem in fullItem.DataInfoList)
{
// Replace newlines with newline plus same indent
dataInfoString.AppendLine($"{indent}{dataInfoItem}".Replace("\n", @$"\line {indent}"));
}
anyFormatInfoAvailable = true;
}
// If there's no full item or object data, we'll still check if there is any data info
if (fullItem == null || (fullItem.ClipDataObject == null && fullItem.ClipEnumObject == null))
{
if (!anyFormatInfoAvailable)
{
return $"{indent}Unknown format: {formatName}";
}
}
// ----------------- If there is a full item and object data -----------------
StringBuilder structInfoString = new StringBuilder();
if (fullItem?.ClipDataObject != null)
{
// Documentation links for the struct and its members
Dictionary<string, string> structDocs = FormatInfoHardcoded.GetDocumentationUrls_ForEntireObject(fullItem.ClipDataObject);
if (structDocs.Count > 0)
{
dataInfoString.AppendLine(@$"\line\b Struct Documentation: \b0");
foreach (var doc in structDocs)
{
dataInfoString.AppendLine($"{indent}{doc.Key}: {doc.Value}");
}
}
structInfoString.AppendLine(@$"\line\b Struct Info: \b0");
RecursivePrintClipDataObject(fullItem.ClipDataObject, indent);
}
else if (fullItem?.ClipEnumObject != null)
{
// Documentation links for the enum. In this case there will be only one
var enumTypeStructName = fullItem.ClipEnumObject.GetType().GetEnumStructName();
if (enumTypeStructName != null && enumTypeStructName != "")
{
if (FormatInfoHardcoded.StructDocsLinks.ContainsKey(enumTypeStructName))
{
string structDocsURL = FormatInfoHardcoded.StructDocsLinks[enumTypeStructName];
dataInfoString.AppendLine(@$"\line\b Struct Documentation: \b0");
dataInfoString.AppendLine($"{indent}{enumTypeStructName}: {structDocsURL}");
}
}
// Print the enum values
Dictionary<string, string> flagsDict = fullItem.ClipEnumObject.GetFlagDescriptionDictionary();
if (flagsDict.Count > 0)
{
structInfoString.AppendLine(@$"\line\b Active Enum Values/Flags: \b0");
foreach (var flag in flagsDict)
{
if (!string.IsNullOrWhiteSpace(flag.Value))
{
structInfoString.AppendLine($"{indent}{flag.Key}: {flag.Value}");
}
else
{
structInfoString.AppendLine($"{indent}{flag.Key}");
}
}
}
}
// Final result
StringBuilder alignedStructInfo = TabAligner(structInfoString);
// Add together datainfostring and alignedstructinfo
StringBuilder finalResult = new StringBuilder();
finalResult.Append(rtfHeader); // RTF header
finalResult.Append(dataInfoString.ToString());
finalResult.Append(alignedStructInfo.ToString());
//finalResult.AppendLine("}"); // RTF ending
string finalString = finalResult.ToString().Replace("\r\n", @"\line ").Replace("\n", @"\line ");
finalString += "}";
return finalString;
// -------------------- LOCAL FUNCTIONS --------------------
void RecursivePrintClipDataObject(IClipboardFormat? obj, string indent, int depth = 0)
{
if (obj == null)
{
structInfoString.AppendLine($"{indent}Max depth reached or object is null");
return;
}
if (depth > 100)
{
structInfoString.AppendLine($"{indent}Max depth of 100 reached");
return;
}
var replacements = obj.DataDisplayReplacements();
foreach (var (propertyName, _, propertyType, arraySize) in obj.EnumerateProperties(getValues: false))
{
// Print replacement data if it is given
if (replacements.TryGetValue(propertyName, out string replacementValue))
{
structInfoString.AppendLine($"{indent}{propertyName}: {replacementValue}");
continue;
}
// All of these first ones are for nested objects and collections and just recurse without printing the property name (except enums)
if (typeof(IClipboardFormat).IsAssignableFrom(propertyType))
{
var nestedObj = obj.GetType().GetProperty(propertyName).GetValue(obj) as IClipboardFormat;
structInfoString.AppendLine($"{indent}{propertyName}:"); // Header label
RecursivePrintClipDataObject(nestedObj, indent + originalIndent, depth + 1);
}
else if (typeof(IEnumerable).IsAssignableFrom(propertyType) && propertyType != typeof(string)) // List
{
var nestedObj = obj.GetType().GetProperty(propertyName).GetValue(obj);
structInfoString.AppendLine($"{indent}{propertyName}:"); // Header label
RecursivePrintCollection(nestedObj, indent + originalIndent, depth: depth+1);
}
else if (propertyType.IsEnum)
{
structInfoString.AppendLine($"{indent}{propertyName}: {obj.GetType().GetProperty(propertyName).GetValue(obj)}");
}
else if (propertyType.IsArray) // Array
{
Array? array = obj.GetType().GetProperty(propertyName).GetValue(obj) as Array;
if (array != null && array.Length > 0 )
{
RecursivePrintArray(array, indent + originalIndent, depth: depth + 1);
}
}
// This finally ends up printing the property name and value for non-nested objects
else
{
// For non-collection types, we might still want to get the value
var value = obj.GetType().GetProperty(propertyName).GetValue(obj);
string valueToDisplay = GetValueString(value);
structInfoString.AppendLine($"{indent}{propertyName}: {valueToDisplay}");
}
}
}
void RecursivePrintCollection(object obj, string indent, int depth)
{
if (obj is IEnumerable enumerable && obj is not string) // not a string
{
int index = 1;
foreach (var item in enumerable)
{
if (item is IClipboardFormat formatObject)
{
structInfoString.AppendLine($"{indent}{index}:");
RecursivePrintClipDataObject(formatObject, indent + originalIndent, depth: depth + 1);
}
else if(item is IEnumerable nestedEnumerable)
{
RecursivePrintCollection(nestedEnumerable, indent + originalIndent, depth: depth + 1);
}
else if (item is Array nestedArray)
{
RecursivePrintArray(nestedArray, indent + originalIndent, depth: depth + 1);
}
else
{
structInfoString.AppendLine($"{indent}{item}");
}
structInfoString.AppendLine("");
index++;
}
}
}
void RecursivePrintArray(Array array, string indent, int depth)
{
for (int i = 0; i < array.Length; i++)
{
object item = array.GetValue(i);
if (item is IClipboardFormat formatObject)
{
structInfoString.AppendLine($"{indent}{i}:");
RecursivePrintClipDataObject(formatObject, indent + originalIndent, depth: depth + 1);
}
else if (item is IEnumerable nestedEnumerable)
{
RecursivePrintCollection(nestedEnumerable, indent + originalIndent, depth: depth + 1);
}
else if (item is Array nestedArray)
{
RecursivePrintArray(nestedArray, indent + originalIndent, depth: depth + 1);
}
else
{
structInfoString.AppendLine($"{indent}{item}");
}
structInfoString.AppendLine("");
}
}
} // ----------------- END OF CreateDataString -----------------
private static string GetValueString(object value, bool hexOnly = false, bool decimalOnly = false)
{
if (value == null)
return "null";
Type valueType = value.GetType();
// For pointers never print the decimal version
if (valueType == typeof(IntPtr) || valueType == typeof(UIntPtr))
{
return Utils.AutoHexString(value, truncate: true);
}
// For nested structs, we'll return a placeholder
if (valueType.IsValueType && !valueType.IsPrimitive && valueType != typeof(IntPtr))
{
return $"[{valueType.Name}]";
}
if (hexOnly)
{
if (valueType == typeof(int) || valueType == typeof(long) || valueType == typeof(short) || valueType == typeof(byte))
{
return string.Format("0x{0:X}", value); // Hexadecimal (0x1234ABCD)
}
else
{
// Return empty string or handle other types as needed
return "";
}
}
else if (decimalOnly)
{
return value.ToString();
}
// Default to both
string finalString = value.ToString();
string hexString = Utils.AsHexString(value);
if (!string.IsNullOrEmpty(hexString))
{
finalString = $"{finalString}\t({hexString})";
}
return finalString;
}
//// Attemps to add additional tabs where necessary so items at the same level are aligned
//private static StringBuilder TabAligner(StringBuilder inputString)
//{
// // For the inputted string builder, we can get the length of the text between the starting whitespace and the first tab which comes before the hex value we want to align
// // We'll do this for each line until the number of preceding spaces changes, then we know we're at a new level
// // When we have all the items in a group, we'll find the longest length and add tabs to the other lines such that the hex values all aign with that longest value's tabbed hex value
//}
private static StringBuilder TabAligner(StringBuilder inputString)
{
// Replace tabs with single spaces and trim endspace
string input = inputString.ToString().Replace("\t", " ");
input = input.TrimEnd();
// Split input into lines
var lines = input.Split(new[] { Environment.NewLine }, StringSplitOptions.None);
// List to store groups
var groups = new List<Group>();
int? currentIndentLevel = null;
Group? currentGroup = null;
foreach (var line in lines)
{
int leadingSpacesCount = line.TakeWhile(char.IsWhiteSpace).Count();
string leadingSpaces = line.Substring(0, leadingSpacesCount);
// Check if indentation level changes
if (currentIndentLevel != leadingSpacesCount)
{
currentIndentLevel = leadingSpacesCount;
currentGroup = new Group
{
Lines = new List<string>(),
IndentLevel = leadingSpacesCount,
MaxPropertyNameLength = 0,
MaxDecimalValueLength = 0
};
groups.Add(currentGroup);
}
currentGroup?.Lines.Add(line);
}
// Now, for each group, find max lengths
foreach (var group in groups)
{
foreach (var line in group.Lines)
{
string trimmedLine = line.TrimStart();
// Skip empty lines
if (string.IsNullOrWhiteSpace(trimmedLine))
continue;
int colonIndex = trimmedLine.IndexOf(':');
if (colonIndex == -1)
{
// Line doesn't have a colon, skip it
continue;
}
// Parse property name and rest of line
string propertyName = trimmedLine.Substring(0, colonIndex);
string rest = trimmedLine.Substring(colonIndex + 1).Trim();
// For value, split on '(' to separate decimal value and hex value
string decimalValue = rest;
int parenIndex = rest.IndexOf('(');
// If there's no hex value, we don't care about the length of the decimal value, so only do this if there is a hex value
if (parenIndex != -1)
{
decimalValue = rest.Substring(0, parenIndex).Trim();
if (decimalValue.Length > group.MaxDecimalValueLength)
{
group.MaxDecimalValueLength = decimalValue.Length;
}
}
// Update max lengths
if (propertyName.Length > group.MaxPropertyNameLength)
{
group.MaxPropertyNameLength = propertyName.Length;
}
}
}
// Now, build the output
var outputStringBuilder = new StringBuilder();
foreach (var group in groups)
{
foreach (var line in group.Lines)
{
string trimmedLine = line.TrimStart();
string leadingSpaces = line.Substring(0, line.Length - trimmedLine.Length);
// Skip empty lines
if (string.IsNullOrWhiteSpace(trimmedLine))
{
outputStringBuilder.AppendLine(line);
continue;
}
int colonIndex = trimmedLine.IndexOf(':');
if (colonIndex == -1)
{
// Line doesn't have a colon, output as is
//outputStringBuilder.AppendLine($@" \ul {line} \ul0 ");
outputStringBuilder.AppendLine(line);
continue;
}
// Parse property name and rest of line
string propertyName = trimmedLine.Substring(0, colonIndex);
string rest = trimmedLine.Substring(colonIndex + 1).Trim();
// For value, split on '(' to separate decimal value and hex value
string decimalValue = rest;
string hexValue = "";
int parenIndex = rest.IndexOf('(');
if (parenIndex != -1)
{
decimalValue = rest.Substring(0, parenIndex).Trim();
hexValue = rest.Substring(parenIndex).Trim();
}
// Format the line. Underline headers and bold the property name
string formattedLine;
if (string.IsNullOrEmpty(decimalValue) && string.IsNullOrEmpty(hexValue))
{
// Ensures the padding doesn't get underlined also
formattedLine = $@"{leadingSpaces}\b\ul {propertyName}\ul0\b0{new string(' ', group.MaxPropertyNameLength - propertyName.Length)}:";
}
// Where only one is present, don't add padding
else if (string.IsNullOrEmpty(hexValue)) // Only decimal value present
{
formattedLine = @$"{leadingSpaces}\b {propertyName.PadRight(group.MaxPropertyNameLength)} \b0: {decimalValue}";
}
else if (string.IsNullOrEmpty(decimalValue)) // Only hex value present
{
formattedLine = @$"{leadingSpaces}\b {propertyName.PadRight(group.MaxPropertyNameLength)} \b0: {hexValue.Trim('(').Trim(')')}";
}
// Both are present. Print the hex with italics
else
{
formattedLine = @$"{leadingSpaces}\b {propertyName.PadRight(group.MaxPropertyNameLength)} \b0: {decimalValue.PadRight(group.MaxDecimalValueLength)} \i{hexValue}\i0";
}
outputStringBuilder.AppendLine(formattedLine);
}
}
return outputStringBuilder;
}
class Group
{
public int IndentLevel { get; set; }
public List<string> Lines { get; set; } = new List<string>();
public int MaxPropertyNameLength { get; set; }
public int MaxDecimalValueLength { get; set; }
}
} // ----------------- END OF FormatStructurePrinter -----------------
}