forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyHelper.cs
More file actions
444 lines (424 loc) · 13.8 KB
/
AssemblyHelper.cs
File metadata and controls
444 lines (424 loc) · 13.8 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
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
using Mono.Cecil;
using Mono.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor.Modules;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor
{
internal class AssemblyHelper
{
private const int kDefaultDepth = 10;
public static void CheckForAssemblyFileNameMismatch(string assemblyPath)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(assemblyPath);
string text = AssemblyHelper.ExtractInternalAssemblyName(assemblyPath);
if (fileNameWithoutExtension != text)
{
UnityEngine.Debug.LogWarning(string.Concat(new string[]
{
"Assembly '",
text,
"' has non matching file name: '",
Path.GetFileName(assemblyPath),
"'. This can cause build issues on some platforms."
}));
}
}
public static string[] GetNamesOfAssembliesLoadedInCurrentDomain()
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
List<string> list = new List<string>();
Assembly[] array = assemblies;
for (int i = 0; i < array.Length; i++)
{
Assembly assembly = array[i];
try
{
list.Add(assembly.Location);
}
catch (NotSupportedException)
{
}
}
return list.ToArray();
}
public static Assembly FindLoadedAssemblyWithName(string s)
{
Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
Assembly[] array = assemblies;
Assembly result;
for (int i = 0; i < array.Length; i++)
{
Assembly assembly = array[i];
try
{
if (assembly.Location.Contains(s))
{
result = assembly;
return result;
}
}
catch (NotSupportedException)
{
}
}
result = null;
return result;
}
public static string ExtractInternalAssemblyName(string path)
{
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path);
return assemblyDefinition.Name.Name;
}
private static AssemblyDefinition GetAssemblyDefinitionCached(string path, Dictionary<string, AssemblyDefinition> cache)
{
AssemblyDefinition result;
if (cache.ContainsKey(path))
{
result = cache[path];
}
else
{
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path);
cache[path] = assemblyDefinition;
result = assemblyDefinition;
}
return result;
}
private static bool IgnoreAssembly(string assemblyPath, BuildTarget target)
{
bool result;
if (target == BuildTarget.WSAPlayer || (target == BuildTarget.XboxOne && PlayerSettings.GetApiCompatibilityLevel(BuildTargetGroup.XboxOne) == ApiCompatibilityLevel.NET_4_6))
{
if (assemblyPath.IndexOf("mscorlib.dll") != -1 || assemblyPath.IndexOf("System.") != -1 || assemblyPath.IndexOf("Windows.dll") != -1 || assemblyPath.IndexOf("Microsoft.") != -1 || assemblyPath.IndexOf("Windows.") != -1 || assemblyPath.IndexOf("WinRTLegacy.dll") != -1 || assemblyPath.IndexOf("platform.dll") != -1)
{
result = true;
return result;
}
}
result = AssemblyHelper.IsInternalAssembly(assemblyPath);
return result;
}
private static void AddReferencedAssembliesRecurse(string assemblyPath, List<string> alreadyFoundAssemblies, string[] allAssemblyPaths, string[] foldersToSearch, Dictionary<string, AssemblyDefinition> cache, BuildTarget target)
{
if (!AssemblyHelper.IgnoreAssembly(assemblyPath, target))
{
AssemblyDefinition assemblyDefinitionCached = AssemblyHelper.GetAssemblyDefinitionCached(assemblyPath, cache);
if (assemblyDefinitionCached == null)
{
throw new ArgumentException("Referenced Assembly " + Path.GetFileName(assemblyPath) + " could not be found!");
}
if (alreadyFoundAssemblies.IndexOf(assemblyPath) == -1)
{
alreadyFoundAssemblies.Add(assemblyPath);
IEnumerable<string> source = (from i in PluginImporter.GetImporters(target).Where(delegate(PluginImporter i)
{
string platformData = i.GetPlatformData(target, "CPU");
return !string.IsNullOrEmpty(platformData) && !string.Equals(platformData, "AnyCPU", StringComparison.InvariantCultureIgnoreCase);
})
select Path.GetFileName(i.assetPath)).Distinct<string>();
using (Collection<AssemblyNameReference>.Enumerator enumerator = assemblyDefinitionCached.MainModule.AssemblyReferences.GetEnumerator())
{
while (enumerator.MoveNext())
{
AssemblyNameReference referencedAssembly = enumerator.Current;
if (!(referencedAssembly.Name == "BridgeInterface"))
{
if (!(referencedAssembly.Name == "WinRTBridge"))
{
if (!(referencedAssembly.Name == "UnityEngineProxy"))
{
if (!AssemblyHelper.IgnoreAssembly(referencedAssembly.Name + ".dll", target))
{
string text = AssemblyHelper.FindAssemblyName(referencedAssembly.FullName, referencedAssembly.Name, allAssemblyPaths, foldersToSearch, cache);
if (text == "")
{
bool flag = false;
string[] array = new string[]
{
".dll",
".winmd"
};
for (int j = 0; j < array.Length; j++)
{
string extension = array[j];
if (source.Any((string p) => string.Equals(p, referencedAssembly.Name + extension, StringComparison.InvariantCultureIgnoreCase)))
{
flag = true;
break;
}
}
if (!flag)
{
throw new ArgumentException(string.Format("The Assembly {0} is referenced by {1} ('{2}'). But the dll is not allowed to be included or could not be found.", referencedAssembly.Name, assemblyDefinitionCached.MainModule.Assembly.Name.Name, assemblyPath));
}
}
else
{
AssemblyHelper.AddReferencedAssembliesRecurse(text, alreadyFoundAssemblies, allAssemblyPaths, foldersToSearch, cache, target);
}
}
}
}
}
}
}
}
}
}
private static string FindAssemblyName(string fullName, string name, string[] allAssemblyPaths, string[] foldersToSearch, Dictionary<string, AssemblyDefinition> cache)
{
string result;
for (int i = 0; i < allAssemblyPaths.Length; i++)
{
AssemblyDefinition assemblyDefinitionCached = AssemblyHelper.GetAssemblyDefinitionCached(allAssemblyPaths[i], cache);
if (assemblyDefinitionCached.MainModule.Assembly.Name.Name == name)
{
result = allAssemblyPaths[i];
return result;
}
}
for (int j = 0; j < foldersToSearch.Length; j++)
{
string path = foldersToSearch[j];
string text = Path.Combine(path, name + ".dll");
if (File.Exists(text))
{
result = text;
return result;
}
}
result = "";
return result;
}
public static string[] FindAssembliesReferencedBy(string[] paths, string[] foldersToSearch, BuildTarget target)
{
List<string> list = new List<string>();
Dictionary<string, AssemblyDefinition> cache = new Dictionary<string, AssemblyDefinition>();
for (int i = 0; i < paths.Length; i++)
{
AssemblyHelper.AddReferencedAssembliesRecurse(paths[i], list, paths, foldersToSearch, cache, target);
}
for (int j = 0; j < paths.Length; j++)
{
list.Remove(paths[j]);
}
return list.ToArray();
}
public static string[] FindAssembliesReferencedBy(string path, string[] foldersToSearch, BuildTarget target)
{
return AssemblyHelper.FindAssembliesReferencedBy(new string[]
{
path
}, foldersToSearch, target);
}
private static bool IsTypeMonoBehaviourOrScriptableObject(AssemblyDefinition assembly, TypeReference type)
{
bool result;
if (type == null)
{
result = false;
}
else if (type.FullName == "System.Object")
{
result = false;
}
else
{
Assembly assembly2 = null;
if (type.Scope.Name == "UnityEngine")
{
assembly2 = typeof(MonoBehaviour).Assembly;
}
else if (type.Scope.Name == "UnityEditor")
{
assembly2 = typeof(EditorWindow).Assembly;
}
else if (type.Scope.Name == "UnityEngine.UI")
{
assembly2 = AssemblyHelper.FindLoadedAssemblyWithName("UnityEngine.UI");
}
if (assembly2 != null)
{
string name = (!type.IsGenericInstance) ? type.FullName : (type.Namespace + "." + type.Name);
Type type2 = assembly2.GetType(name);
if (type2 == typeof(MonoBehaviour) || type2.IsSubclassOf(typeof(MonoBehaviour)))
{
result = true;
return result;
}
if (type2 == typeof(ScriptableObject) || type2.IsSubclassOf(typeof(ScriptableObject)))
{
result = true;
return result;
}
}
TypeDefinition typeDefinition = null;
try
{
typeDefinition = type.Resolve();
}
catch (AssemblyResolutionException)
{
}
result = (typeDefinition != null && AssemblyHelper.IsTypeMonoBehaviourOrScriptableObject(assembly, typeDefinition.BaseType));
}
return result;
}
public static void ExtractAllClassesThatInheritMonoBehaviourAndScriptableObject(string path, out string[] classNamesArray, out string[] classNameSpacesArray)
{
List<string> list = new List<string>();
List<string> list2 = new List<string>();
ReaderParameters readerParameters = new ReaderParameters();
DefaultAssemblyResolver defaultAssemblyResolver = new DefaultAssemblyResolver();
defaultAssemblyResolver.AddSearchDirectory(Path.GetDirectoryName(path));
readerParameters.AssemblyResolver = defaultAssemblyResolver;
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path, readerParameters);
foreach (ModuleDefinition current in assemblyDefinition.Modules)
{
foreach (TypeDefinition current2 in current.Types)
{
TypeReference baseType = current2.BaseType;
try
{
if (AssemblyHelper.IsTypeMonoBehaviourOrScriptableObject(assemblyDefinition, baseType))
{
list.Add(current2.Name);
list2.Add(current2.Namespace);
}
}
catch (Exception)
{
UnityEngine.Debug.LogError(string.Concat(new string[]
{
"Failed to extract ",
current2.FullName,
" class of base type ",
baseType.FullName,
" when inspecting ",
path
}));
}
}
}
classNamesArray = list.ToArray();
classNameSpacesArray = list2.ToArray();
}
public static AssemblyTypeInfoGenerator.ClassInfo[] ExtractAssemblyTypeInfo(BuildTarget targetPlatform, bool isEditor, string assemblyPathName, string[] searchDirs)
{
AssemblyTypeInfoGenerator.ClassInfo[] result;
try
{
string targetStringFromBuildTarget = ModuleManager.GetTargetStringFromBuildTarget(targetPlatform);
ICompilationExtension compilationExtension = ModuleManager.GetCompilationExtension(targetStringFromBuildTarget);
string[] compilerExtraAssemblyPaths = compilationExtension.GetCompilerExtraAssemblyPaths(isEditor, assemblyPathName);
if (compilerExtraAssemblyPaths != null && compilerExtraAssemblyPaths.Length > 0)
{
List<string> list = new List<string>(searchDirs);
list.AddRange(compilerExtraAssemblyPaths);
searchDirs = list.ToArray();
}
IAssemblyResolver assemblyResolver = compilationExtension.GetAssemblyResolver(isEditor, assemblyPathName, searchDirs);
AssemblyTypeInfoGenerator assemblyTypeInfoGenerator;
if (assemblyResolver == null)
{
assemblyTypeInfoGenerator = new AssemblyTypeInfoGenerator(assemblyPathName, searchDirs);
}
else
{
assemblyTypeInfoGenerator = new AssemblyTypeInfoGenerator(assemblyPathName, assemblyResolver);
}
result = assemblyTypeInfoGenerator.GatherClassInfo();
}
catch (Exception ex)
{
throw new Exception(string.Concat(new object[]
{
"ExtractAssemblyTypeInfo: Failed to process ",
assemblyPathName,
", ",
ex
}));
}
return result;
}
internal static Type[] GetTypesFromAssembly(Assembly assembly)
{
Type[] result;
if (assembly == null)
{
result = new Type[0];
}
else
{
try
{
result = assembly.GetTypes();
}
catch (ReflectionTypeLoadException)
{
result = new Type[0];
}
}
return result;
}
[DebuggerHidden]
internal static IEnumerable<T> FindImplementors<T>(Assembly assembly) where T : class
{
AssemblyHelper.<FindImplementors>c__Iterator0<T> <FindImplementors>c__Iterator = new AssemblyHelper.<FindImplementors>c__Iterator0<T>();
<FindImplementors>c__Iterator.assembly = assembly;
AssemblyHelper.<FindImplementors>c__Iterator0<T> expr_0E = <FindImplementors>c__Iterator;
expr_0E.$PC = -2;
return expr_0E;
}
public static bool IsManagedAssembly(string file)
{
DllType dllType = InternalEditorUtility.DetectDotNetDll(file);
return dllType != DllType.Unknown && dllType != DllType.Native;
}
public static bool IsInternalAssembly(string file)
{
return ModuleManager.IsRegisteredModule(file) || ModuleUtils.GetAdditionalReferencesForUserScripts().Any((string p) => p.Equals(file));
}
internal static ICollection<string> FindAssemblies(string basePath)
{
return AssemblyHelper.FindAssemblies(basePath, 10);
}
internal static ICollection<string> FindAssemblies(string basePath, int maxDepth)
{
List<string> list = new List<string>();
ICollection<string> result;
if (maxDepth == 0)
{
result = list;
}
else
{
try
{
DirectoryInfo directoryInfo = new DirectoryInfo(basePath);
list.AddRange(from file in directoryInfo.GetFiles()
where AssemblyHelper.IsManagedAssembly(file.FullName)
select file.FullName);
DirectoryInfo[] directories = directoryInfo.GetDirectories();
for (int i = 0; i < directories.Length; i++)
{
DirectoryInfo directoryInfo2 = directories[i];
list.AddRange(AssemblyHelper.FindAssemblies(directoryInfo2.FullName, maxDepth - 1));
}
}
catch (Exception)
{
}
result = list;
}
return result;
}
}
}