-
Notifications
You must be signed in to change notification settings - Fork 0
/
WinFormsScraper.cs
171 lines (140 loc) · 4.69 KB
/
WinFormsScraper.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
using System.Reflection;
using System.Collections.Immutable;
using System.Diagnostics;
using Config = WinFormsScraper.WinFormsScraperConfig;
namespace WinFormsScraper;
/// <summary>
/// Deals with scraping property data from Forms
/// </summary>
public static class WinFormsScraper
{
/// <summary>
/// Scrapes all forms based on a certain hashset
/// </summary>
/// <param name="hashset"></param>
public static void Scrape(HashSet<string> hashset)
{
Config.ScrapeFilter = hashset;
Scrape(CUSTOM, Assembly.GetCallingAssembly());
}
/// <summary>
/// Scrapes every form in the assembly
/// </summary>
/// <param name="assembly">The assembly to check</param>
/// <param name="scrapeType">What properties of control to print out</param>
public static void Scrape(ScrapeType? scrapeType = null, Assembly? assembly = null)
{
scrapeType ??= Config.Type;
assembly ??= Assembly.GetCallingAssembly();
// Ensure the file exists
if(!File.Exists(Config.Path))
{
File.Create(Config.Path);
}
// Clear the file
File.WriteAllText(Config.Path, string.Empty);
// Create the streamwriter
using StreamWriter sw = new StreamWriter(Config.Path, true);
// Check for null
if(sw is null)
{
return;
}
// Get all types
Type[] types = assembly.GetTypes();
// Filter for forms
Type form = typeof(Form);
List<Type> forms = types.Where(x => x.IsSubclassOf(form) || x == form).ToList();
foreach(Type type in forms)
{
// Create the instance of the form
var instance = Activator.CreateInstance(type);
// The name of the form
sw!.WriteLine($"--- {type.Name} ---");
// Extra line for clarity
sw!.WriteLine();
// Find the control property
var controlProp = type.GetProperty("Controls");
// Check if the form has the control property ( logically should always )
if(controlProp is null)
{
continue;
}
// Take the controls from the property;
Control.ControlCollection? controls = controlProp.GetValue(instance) as Control.ControlCollection;
// Make sure the collection exists
if(controls is null)
{
continue;
}
// Check what Information should be printed
ImmutableHashSet<string> filter = scrapeType switch
{
ALL => ScrapeSets.All,
IMPORTANT => ScrapeSets.Important,
SIZE_AND_LOCATION => ScrapeSets.SizeLocation,
CUSTOM => ScrapeSets.Custom,
_ => ImmutableHashSet<string>.Empty
};
if(filter is null)
{
continue;
}
// Iterate through all of the controls
foreach (Control control in controls)
{
sw!.WriteLine($"{control.Name}:");
// Write all the properties
foreach(var str in GetInformation(control, filter))
{
sw.WriteLine(str);
}
// Line for clarity
sw!.WriteLine();
}
sw!.WriteLine();
}
// Collect all that garbage from potentially large amount of objects created
GC.Collect();
}
private static IEnumerable<string> GetInformation<T>(T control, ImmutableHashSet<string> filter) where T : Control
{
var props = control.GetType().GetProperties();
foreach (var prop in props)
{
if (filter is null || !filter.Contains(prop.Name))
{
continue;
}
string str;
try
{
// Grab the value of the property
str = $"\t\t{prop.Name}: {PrettyPrint(prop.GetMethod?.Invoke(control, new object[] { }))}";
}
catch (Exception)
{
// Some weird property values may encounter this exception
// In that case, just ignore it
continue;
}
yield return str;
}
}
private static string? PrettyPrint(object? obj)
{
if(obj is Font font)
{
return $"{font.Name} - {font.Size}px";
}
if(obj is Point point)
{
return $"[{point.X}, {point.Y}]";
}
if(obj is Size size)
{
return $"[{size.Width}, {size.Height}]";
}
return (string?)obj;
}
}