forked from MattRix/UnityDecompiled
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAssemblyHelper.cs
More file actions
393 lines (393 loc) · 12.5 KB
/
Copy pathAssemblyHelper.cs
File metadata and controls
393 lines (393 loc) · 12.5 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
using Mono.Cecil;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using UnityEditor.Modules;
using UnityEditor.Scripting.Compilers;
using UnityEditorInternal;
using UnityEngine;
namespace UnityEditor
{
internal class AssemblyHelper
{
private const int kDefaultDepth = 10;
public static bool IsWindowsRuntimeAssembly(string assemblyPath)
{
bool isWindowsRuntime;
using (FileStream fileStream = File.Open(assemblyPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(fileStream);
isWindowsRuntime = assemblyDefinition.Name.IsWindowsRuntime;
}
return isWindowsRuntime;
}
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;
for (int i = 0; i < array.Length; i++)
{
Assembly assembly = array[i];
try
{
if (assembly.Location.Contains(s))
{
return assembly;
}
}
catch (NotSupportedException)
{
}
}
return null;
}
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)
{
if (cache.ContainsKey(path))
{
return cache[path];
}
AssemblyDefinition assemblyDefinition = AssemblyDefinition.ReadAssembly(path);
cache[path] = assemblyDefinition;
return assemblyDefinition;
}
private static bool IgnoreAssembly(string assemblyPath, BuildTarget target)
{
if (target != BuildTarget.MetroPlayer)
{
if (target == BuildTarget.WP8Player)
{
if (assemblyPath.IndexOf("mscorlib.dll") != -1 || assemblyPath.IndexOf("System.") != -1 || assemblyPath.IndexOf("Windows.dll") != -1 || assemblyPath.IndexOf("Microsoft.") != -1 || assemblyPath.IndexOf("platform.dll") != -1)
{
return true;
}
}
}
else
{
if (assemblyPath.IndexOf("mscorlib.dll") != -1 || assemblyPath.IndexOf("System.") != -1 || assemblyPath.IndexOf("Windows.dll") != -1 || assemblyPath.IndexOf("WinRTLegacy.dll") != -1 || assemblyPath.IndexOf("platform.dll") != -1)
{
return true;
}
}
return AssemblyHelper.IsInternalAssembly(assemblyPath);
}
private static void AddReferencedAssembliesRecurse(string assemblyPath, List<string> alreadyFoundAssemblies, string[] allAssemblyPaths, string[] foldersToSearch, Dictionary<string, AssemblyDefinition> cache, BuildTarget target)
{
if (AssemblyHelper.IgnoreAssembly(assemblyPath, target))
{
return;
}
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)
{
return;
}
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>();
foreach (AssemblyNameReference referencedAssembly in assemblyDefinitionCached.MainModule.AssemblyReferences)
{
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 == string.Empty)
{
bool flag = false;
string[] array = new string[]
{
".dll",
".winmd"
};
string extension;
for (int j = 0; j < array.Length; j++)
{
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)
{
for (int i = 0; i < allAssemblyPaths.Length; i++)
{
AssemblyDefinition assemblyDefinitionCached = AssemblyHelper.GetAssemblyDefinitionCached(allAssemblyPaths[i], cache);
if (assemblyDefinitionCached.MainModule.Assembly.Name.Name == name)
{
return allAssemblyPaths[i];
}
}
for (int j = 0; j < foldersToSearch.Length; j++)
{
string path = foldersToSearch[j];
string text = Path.Combine(path, name + ".dll");
if (File.Exists(text))
{
return text;
}
}
return string.Empty;
}
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)
{
if (type == null)
{
return false;
}
if (type.FullName == "System.Object")
{
return false;
}
Assembly assembly2 = null;
if (type.Scope.Name == "UnityEngine")
{
assembly2 = typeof(MonoBehaviour).Assembly;
}
else
{
if (type.Scope.Name == "UnityEditor")
{
assembly2 = typeof(EditorWindow).Assembly;
}
}
if (assembly2 != null)
{
Type type2 = assembly2.GetType(type.FullName);
if (type2 == typeof(MonoBehaviour) || type2.IsSubclassOf(typeof(MonoBehaviour)))
{
return true;
}
if (type2 == typeof(ScriptableObject) || type2.IsSubclassOf(typeof(ScriptableObject)))
{
return true;
}
}
TypeDefinition typeDefinition = null;
try
{
typeDefinition = type.Resolve();
}
catch (AssemblyResolutionException)
{
}
return typeDefinition != null && AssemblyHelper.IsTypeMonoBehaviourOrScriptableObject(assembly, typeDefinition.BaseType);
}
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;
if (AssemblyHelper.IsTypeMonoBehaviourOrScriptableObject(assemblyDefinition, baseType))
{
list.Add(current2.Name);
list2.Add(current2.Namespace);
}
}
}
classNamesArray = list.ToArray();
classNameSpacesArray = list2.ToArray();
}
public static AssemblyTypeInfoGenerator.ClassInfo[] ExtractAssemblyTypeInfo(BuildTarget targetPlatform, bool isEditor, string assemblyPathName, string[] searchDirs)
{
if (CSharpLanguage.GetUseMicrosoftCSharpCompiler(targetPlatform, isEditor, assemblyPathName))
{
WSASDK wSASDK = EditorUserBuildSettings.wsaSDK;
if (wSASDK == WSASDK.UniversalSDK81)
{
wSASDK = WSASDK.SDK81;
}
string nETCoreFrameworkReferencesDirectory = MicrosoftCSharpCompiler.GetNETCoreFrameworkReferencesDirectory(wSASDK);
searchDirs = new List<string>(searchDirs)
{
nETCoreFrameworkReferencesDirectory
}.ToArray();
}
AssemblyTypeInfoGenerator.ClassInfo[] result;
try
{
AssemblyTypeInfoGenerator assemblyTypeInfoGenerator = new AssemblyTypeInfoGenerator(assemblyPathName, searchDirs);
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)
{
if (assembly == null)
{
return new Type[0];
}
Type[] result;
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__Iterator1<T> <FindImplementors>c__Iterator = new AssemblyHelper.<FindImplementors>c__Iterator1<T>();
<FindImplementors>c__Iterator.assembly = assembly;
<FindImplementors>c__Iterator.<$>assembly = assembly;
AssemblyHelper.<FindImplementors>c__Iterator1<T> expr_15 = <FindImplementors>c__Iterator;
expr_15.$PC = -2;
return expr_15;
}
public static bool IsManagedAssembly(string file)
{
return InternalEditorUtility.DetectDotNetDll(file);
}
public static bool IsInternalAssembly(string file)
{
return ModuleManager.IsRegisteredModule(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>();
if (maxDepth == 0)
{
return list;
}
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)
{
}
return list;
}
}
}