-
Notifications
You must be signed in to change notification settings - Fork 3
/
FrooxEngineTypeParser.cs
352 lines (295 loc) · 12.8 KB
/
FrooxEngineTypeParser.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
using System.Reflection;
using System.Text;
using System.Xml.Linq;
using Elements.Core;
using FrooxEngine;
using FrooxEngine.ProtoFlux;
internal class Program
{
private static async Task Main(string[] args)
{
const char SEPARATOR = '#';
HashSet<string> processedAssemblies = [];
Dictionary<string, string> failedAssemblies = new Dictionary<string, string>();
StringBuilder componentsString = new();
string GetFrooxEngineDirectory()
{
Console.WriteLine("Getting FrooxEngine directory...");
var csprojPath = Directory
.GetFiles(Directory.GetCurrentDirectory(), "*.csproj", SearchOption.AllDirectories)
.FirstOrDefault();
if (csprojPath == null)
{
Console.WriteLine("No .csproj file found in the current directory.");
throw new Exception("No .csproj file found in the current directory.");
}
Console.WriteLine($"Found .csproj file at {csprojPath}");
var doc = XDocument.Load(csprojPath);
var ns = doc.Root?.GetDefaultNamespace();
if (ns == null)
{
Console.WriteLine("No default namespace found in the .csproj file.");
throw new Exception("No default namespace found in the .csproj file.");
}
Console.WriteLine($"Default namespace: {ns}");
var resonitePath = doc.Descendants(ns + "ResonitePath")
.FirstOrDefault(rp => Directory.Exists(rp.Value))
?.Value;
if (resonitePath == null)
{
Console.WriteLine("ResonitePath property not found or directory does not exist.");
throw new Exception("ResonitePath property not found or directory does not exist.");
}
var hintPaths = doc.Descendants(ns + "HintPath")
.Where(hp => hp.Value.Contains("FrooxEngine.dll"));
if (!hintPaths.Any())
{
Console.WriteLine("FrooxEngine.dll HintPath not found in .csproj file.");
throw new Exception("FrooxEngine.dll HintPath not found in .csproj file.");
}
var hintPath = hintPaths.First();
var expandedHintPath = hintPath.Value.Replace("$(ResonitePath)", resonitePath);
Console.WriteLine($"FrooxEngine.dll HintPath: {expandedHintPath}");
var frooxEngineDllPath = Path.GetFullPath(
Path.Combine(Path.GetDirectoryName(csprojPath) ?? string.Empty, expandedHintPath)
);
Console.WriteLine($"Full path to FrooxEngine.dll: {frooxEngineDllPath}");
var frooxEngineDirectory = Path.GetDirectoryName(frooxEngineDllPath);
if (frooxEngineDirectory == null)
{
Console.WriteLine("Failed to get directory name from FrooxEngine.dll path.");
throw new Exception("Failed to get directory name from FrooxEngine.dll path.");
}
Console.WriteLine($"FrooxEngine directory: {frooxEngineDirectory}");
return frooxEngineDirectory;
}
IEnumerable<Type> GetLoadableTypes(Assembly assembly, HashSet<string> processedAssemblies)
{
List<Type> types = [];
// Console.WriteLine($"Loading types from assembly: {assembly.FullName}");
try
{
var exportedTypes = assembly.GetExportedTypes();
types.AddRange(exportedTypes);
Console.WriteLine($"Loaded {exportedTypes.Length} types from {assembly.FullName}");
}
catch (ReflectionTypeLoadException e)
{
// Exclude types that can't be loaded
var loadableTypes = e.Types.Where(t => t != null).Cast<Type>();
types.AddRange(loadableTypes);
Console.WriteLine(
$"Loaded {loadableTypes.Count()} types from {assembly.FullName} after excluding unloadable types"
);
}
catch (TypeLoadException e)
{
// Skip assembly if a type can't be loaded
Console.WriteLine(
$"Failed to load types from assembly: {assembly.FullName}. Error: {e.Message}"
);
return Enumerable.Empty<Type>();
}
catch (FileNotFoundException e)
{
// Skip assembly if it can't be found
Console.WriteLine($"Failed to load assembly: {assembly.FullName}. Error: {e.Message}");
failedAssemblies.Add(string.IsNullOrEmpty(assembly.FullName) ? "DefaultKey" : assembly.FullName, e.Message);
return Enumerable.Empty<Type>();
}
// This idea didn't work, just caused more problems.
/*
foreach (AssemblyName referencedAssemblyName in assembly.GetReferencedAssemblies())
{
try
{
Assembly referencedAssembly = Assembly.Load(referencedAssemblyName);
var referencedTypes = referencedAssembly.GetExportedTypes();
// Do something with referencedTypes
}
catch (Exception ex)
{
Console.WriteLine($"Failed to load referenced assembly: {referencedAssemblyName.FullName}. Error: {ex.Message}");
failedAssemblies.Add(string.IsNullOrEmpty(referencedAssemblyName.FullName) ? "DefaultKey" : referencedAssemblyName.FullName, e.Message);
}
}
*/
return types;
}
IEnumerable<Assembly> asms = Directory
.GetFiles(GetFrooxEngineDirectory(), "*.dll")
.Where((s) => !s.StartsWith("System"))
.Select(
(s) =>
{
try
{
// This will throw a BadImageFormatException if s is not a valid assembly.
var assemblyName = AssemblyName.GetAssemblyName(s);
return Assembly.LoadFrom(s);
}
catch (BadImageFormatException)
{
Console.WriteLine($"Skipping non-assembly file: {s}");
return null;
}
catch (FileLoadException)
{
Console.WriteLine($"Skipping unloadable assembly: {s}");
return null;
}
}
)
.Where((asm) => asm != null)!;
Console.WriteLine($"Loaded {asms.Count()} assemblies.");
List<Type> allTypes = asms.SelectMany(asm => GetLoadableTypes(asm, processedAssemblies)).ToList();
Console.WriteLine($"Loaded {allTypes.Count} types.");
try
{
WorkerInitializer.Initialize(allTypes, true);
}
catch (Exception ex)
{
Console.WriteLine($"Failed to initialize workers: {ex.Message}");
return;
}
void PrintComp(Type element, StringBuilder builder, int depth, HashSet<string> seenOverloads)
{
if (element == null)
{
Console.WriteLine("Element is null");
return;
}
if (typeof(ProtoFluxNode).IsAssignableFrom(element))
{
Type toPrint = element;
_ = builder.AppendLine(
new string(SEPARATOR, depth + 1)
+ " "
+ toPrint.GetNiceName()
+ SEPARATOR
+ toPrint.FullName
);
return;
}
_ = builder.AppendLine(
new string(SEPARATOR, depth + 1)
+ " "
+ element.GetNiceName()
+ SEPARATOR
+ element.FullName
);
}
void ProcessNode(CategoryNode<Type> node, StringBuilder builder, int depth)
{
if (node == null)
{
Console.WriteLine("Node is null");
return;
}
_ = builder.AppendLine(new string(SEPARATOR, depth) + " " + node.Name);
foreach (CategoryNode<Type>? subdir in node.Subcategories)
{
ProcessNode(subdir, builder, depth + 1);
}
HashSet<string> seenOverloads = [];
foreach (Type element in node.Elements)
{
PrintComp(element, builder, depth, seenOverloads);
}
}
foreach (CategoryNode<Type>? node in WorkerInitializer.ComponentLibrary.Subcategories)
{
if (node.Name == "ProtoFlux")
{
continue;
}
ProcessNode(node, componentsString, 0);
}
string outputFolder = args.Length < 1 ? "./data/" : args[0];
// Get the directory name from the file path
string directoryName = Path.GetDirectoryName(outputFolder)!;
// Check if the directory exists, if not, create it
if (!Directory.Exists(directoryName))
{
_ = Directory.CreateDirectory(directoryName);
}
async Task ProcessCategoryAndWriteToFile(string? categoryName, string fileName)
{
StringBuilder categoryString = new();
HashSet<string> seenOverloads = [];
CategoryNode<Type> categoryPath = WorkerInitializer.ComponentLibrary.GetSubcategory(
categoryName
);
foreach (CategoryNode<Type>? node in categoryPath.Subcategories)
{
if (node.Name != "ProtoFlux")
{
ProcessNode(node, categoryString, 0);
}
}
foreach (Type? node in categoryPath.Elements)
{
PrintComp(node, categoryString, -1, seenOverloads);
}
// Writes our category list to a file.
await File.WriteAllTextAsync(Path.Combine(outputFolder, fileName), categoryString.ToString());
}
await ProcessCategoryAndWriteToFile(null, "ComponentList.txt");
await ProcessCategoryAndWriteToFile("ProtoFlux", "ProtoFluxList.txt");
async Task ProcessAllMethodsAndWriteToFile(string fileName)
{
StringBuilder allMethodsString = new();
Dictionary<string, List<string>> typesToMethods = [];
foreach (CategoryNode<Type>? node in WorkerInitializer.ComponentLibrary.Subcategories)
{
foreach (Type? type in node.Elements)
{
foreach (var method in type.GetMethods())
{
string methodSignature =
$"{method.Name}({string.Join(", ", method.GetParameters().Select(p => p.ParameterType.Name))}){SEPARATOR}{method.ReturnType.Name}";
if (!typesToMethods.ContainsKey(type.Name))
{
typesToMethods[type.Name] = [];
}
typesToMethods[type.Name].Add(methodSignature);
}
}
}
foreach (Type? type in WorkerInitializer.ComponentLibrary.Elements)
{
foreach (var method in type.GetMethods())
{
string methodSignature =
$"{method.Name}({string.Join(", ", method.GetParameters().Select(p => p.ParameterType.Name))}){SEPARATOR}{method.ReturnType.Name}";
if (!typesToMethods.ContainsKey(type.Name))
{
typesToMethods[type.Name] = [];
}
typesToMethods[type.Name].Add(methodSignature);
}
}
// Writes our all methods list to a file.
foreach (var pair in typesToMethods)
{
allMethodsString.AppendLine($"{pair.Key}");
foreach (var methodSignature in pair.Value)
{
allMethodsString.AppendLine($"# {methodSignature}");
}
}
await File.WriteAllTextAsync(Path.Combine(outputFolder, fileName), allMethodsString.ToString());
}
await ProcessAllMethodsAndWriteToFile("MethodsList.txt");
// Output failed assemblies at the end
if (failedAssemblies.Any())
{
Console.WriteLine($"Failed to load the following assemblies: {failedAssemblies.Count}");
foreach (var assembly in failedAssemblies)
{
Console.WriteLine(assembly.Value);
}
}
}
}