This repository has been archived by the owner on Aug 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
APublicizer.cs
247 lines (204 loc) · 8.51 KB
/
APublicizer.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
using Mono.Cecil;
using Mono.Collections.Generic;
using System;
using System.IO;
using System.Linq;
namespace APublicizer
{
public static class APublicizer
{
public const string SUFFIX = "-Publicized";
public static void Main(string[] args)
{
var assemblyPath = Path.GetFullPath(args.Single());
using var publicizer = new Publicizer(assemblyPath);
var result = publicizer.Run();
assemblyPath = InsertSuffix(assemblyPath, SUFFIX);
publicizer.Write(assemblyPath);
PrintResult(assemblyPath, result);
}
private static void PrintResult(string path, Publicizer.PublicizeResult result)
{
Console.WriteLine($"Publicized - {path}");
Console.WriteLine("Publicize result - ");
Console.WriteLine($"\tTypes - {result.Types}");
Console.WriteLine($"\tNestedTypes - {result.NestedTypes}");
Console.WriteLine($"\tEvents - {result.Events}");
Console.WriteLine($"\tFields - {result.Fields}");
Console.WriteLine($"\tMethods - {result.Methods}");
Console.WriteLine("\tProperties -");
Console.WriteLine($"\t\tSetters - {result.Property_Setters}");
Console.WriteLine($"\t\tGetters - {result.Property_Getters}");
Console.WriteLine("Thank me!");
}
private static string InsertSuffix(string path, string suffix)
{
var cleanPath = Path.GetDirectoryName(path);
var filename = Path.GetFileNameWithoutExtension(path);
var extension = Path.GetExtension(path);
return Path.Combine(cleanPath!, string.Concat(filename, suffix, extension));
}
public static ReaderParameters GetReaderParameters() => new(ReadingMode.Immediate)
{
InMemory = true
};
}
public sealed class Publicizer : IDisposable
{
public sealed class PublicizeResult
{
public uint Types;
public uint NestedTypes;
public uint Events;
public uint Fields;
public uint Methods;
public uint Property_Setters;
public uint Property_Getters;
public void BumpTypes() => ++Types;
public void BumpNestedTypes() => ++NestedTypes;
public void BumpEvents() => ++Events;
public void BumpFields() => ++Fields;
public void BumpMethods() => ++Methods;
public void BumpPropertySetters() => ++Property_Setters;
public void BumpPropertyGetters() => ++Property_Getters;
}
private readonly AssemblyDefinition _assembly;
private Collection<TypeDefinition>? _types;
public Publicizer(string path)
{
var rParams = APublicizer.GetReaderParameters();
var resolver = new DefaultAssemblyResolver();
resolver.AddSearchDirectory(Path.GetDirectoryName(path));
rParams.AssemblyResolver = resolver;
_assembly = AssemblyDefinition.ReadAssembly(path, rParams);
}
public PublicizeResult Run()
{
var result = new PublicizeResult();
DoPublicize(result);
return result;
}
public void Write(string path) => _assembly.Write(path);
private void DoPublicize(PublicizeResult value)
{
DoPublicizeTypes(value);
DoPublicizeNestedTypes(value);
DoPublicizeFields(value);
// Publicize events before publicizing methods cuz events are literally two methods & one field
DoFixupEvents(value);
// Publicize properties before publicizing methods cuz setters/getters are methods
DoPublicizePropertySetters(value);
DoPublicizePropertyGetters(value);
DoPublicizeMethods(value);
}
#region Utility
private Collection<TypeDefinition> GetTypes()
{
if (_types is not null)
return _types;
var coll = new Collection<TypeDefinition>();
void AddTypes(Collection<TypeDefinition> definitions)
{
for (var z = 0; z < definitions.Count; z++)
{
var definition = definitions[z];
coll.Add(definition);
AddTypes(definition.NestedTypes);
}
}
AddTypes(_assembly.MainModule.Types);
return _types = coll;
}
private void Processor<T>(Collection<T> values, Predicate<T> filter, Action<T> processor, Action bump)
{
for (var z = 0; z < values.Count; z++)
{
var d = values[z];
if (filter(d))
{
processor(d);
bump();
}
}
}
private void ArrayProcessor<T, R>(Collection<T> values, Func<T, Collection<R>> getter, Action<Collection<R>> processor)
{
for (var z = 0; z < values.Count; z++)
{
var d = values[z];
var coll = getter(d);
processor(coll);
}
}
#endregion
#region Do
private void DoPublicizeTypes(PublicizeResult value) =>
Processor<TypeDefinition>(GetTypes(),
(t) => !t.IsNested && !t.IsPublic,
(t) => t.IsPublic = true,
value.BumpTypes);
private void DoPublicizeNestedTypes(PublicizeResult value) =>
Processor<TypeDefinition>(GetTypes(),
(nt) => nt.IsNested && !nt.IsNestedPublic,
(nt) => nt.IsNestedPublic = true,
value.BumpNestedTypes);
private void DoPublicizeFields(PublicizeResult value) =>
ArrayProcessor<TypeDefinition, FieldDefinition>(GetTypes(),
(t) => t.Fields,
(fs) => Processor<FieldDefinition>(fs,
(f) => !f.IsPublic,
(f) => f.IsPublic = true,
value.BumpFields));
private void DoPublicizeMethods(PublicizeResult value) =>
ArrayProcessor<TypeDefinition, MethodDefinition>(GetTypes(),
(t) => t.Methods,
(ms) => Processor<MethodDefinition>(ms,
(m) => !m.IsPublic,
(m) => m.IsPublic = true,
value.BumpMethods));
private void DoPublicizePropertySetters(PublicizeResult value) =>
ArrayProcessor<TypeDefinition, PropertyDefinition>(GetTypes(),
(t) => t.Properties,
(ps) => Processor<PropertyDefinition>(ps,
(p) => !p.SetMethod?.IsPublic ?? false,
(p) => p.SetMethod.IsPublic = true,
value.BumpPropertySetters));
private void DoPublicizePropertyGetters(PublicizeResult value) =>
ArrayProcessor<TypeDefinition, PropertyDefinition>(GetTypes(),
(t) => t.Properties,
(ps) => Processor<PropertyDefinition>(ps,
(p) => !p.GetMethod?.IsPublic ?? false,
(p) => p.GetMethod.IsPublic = true,
value.BumpPropertyGetters));
private void DoFixupEvents(PublicizeResult value)
{
bool FilterEvent(EventDefinition e)
{
// Sometimes, for some reason, events have the same name as their backing fields.
// If both are public, neither can be accessed (name conflict).
var backing = e.DeclaringType.Fields.SingleOrDefault(f => f.Name == e.Name);
if (backing != null)
{
backing.IsPrivate = true;
value.Fields--;
}
return e.AddMethod.IsPrivate || e.RemoveMethod.IsPrivate || (e.InvokeMethod?.IsPrivate ?? false);
}
void ProcessEvent(EventDefinition e)
{
e.AddMethod.IsPublic = true;
e.RemoveMethod.IsPublic = true;
if (e.InvokeMethod != null)
e.InvokeMethod.IsPublic = true;
}
ArrayProcessor<TypeDefinition, EventDefinition>(GetTypes(),
(t) => t.Events,
(es) => Processor<EventDefinition>(es,
FilterEvent,
ProcessEvent,
value.BumpEvents));
}
#endregion
public void Dispose() => _assembly.Dispose();
}
}