forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyValidation.cs
More file actions
458 lines (371 loc) · 16.5 KB
/
Copy pathAssemblyValidation.cs
File metadata and controls
458 lines (371 loc) · 16.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
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using Mono.Cecil;
using UnityEditor.Scripting.ScriptCompilation;
using UnityEngine.Scripting;
namespace UnityEditor
{
static class AssemblyValidation
{
// Keep in sync with AssemblyValidationFlags in MonoManager.cpp
[Flags]
public enum ErrorFlags
{
None = 0,
ReferenceHasErrors = (1 << 0),
UnresolvableReference = (1 << 1),
IncompatibleWithEditor = (1 << 2),
AsmdefPluginConflict = (1 << 3)
}
[StructLayout(LayoutKind.Sequential)]
public struct Error
{
public ErrorFlags flags;
public string message;
public string assemblyPath;
public void Add(ErrorFlags newFlags, string newMessage)
{
flags |= newFlags;
if (message == null)
{
message = newMessage;
}
else
{
message += string.Format("\n{0}", newMessage);
}
}
public bool HasFlag(ErrorFlags testFlags)
{
return (flags & testFlags) == testFlags;
}
public void ClearFlags(ErrorFlags clearFlags)
{
flags &= ~clearFlags;
}
}
public struct AssemblyAndReferences
{
public int assemblyIndex;
public int[] referenceIndicies;
}
class AssemblyResolver : BaseAssemblyResolver
{
readonly IDictionary<string, AssemblyDefinition> cache;
public AssemblyResolver()
{
cache = new Dictionary<string, AssemblyDefinition>(StringComparer.Ordinal);
}
public override AssemblyDefinition Resolve(AssemblyNameReference name)
{
if (name == null)
throw new ArgumentNullException("name");
AssemblyDefinition assembly;
if (cache.TryGetValue(name.Name, out assembly))
return assembly;
assembly = base.Resolve(name);
cache[name.Name] = assembly;
return assembly;
}
public void RegisterAssembly(AssemblyDefinition assembly)
{
if (assembly == null)
throw new ArgumentNullException("assembly");
var name = assembly.Name.Name;
if (cache.ContainsKey(name))
return;
cache[name] = assembly;
}
protected override void Dispose(bool disposing)
{
foreach (var assembly in cache.Values)
assembly.Dispose();
cache.Clear();
base.Dispose(disposing);
}
}
[RequiredByNativeCode]
public static Error[] ValidateAssemblies(string[] assemblyPaths, bool enableLogging)
{
var searchPaths = AssemblyHelper.GetDefaultAssemblySearchPaths();
var assemblyDefinitions = LoadAssemblyDefinitions(assemblyPaths, searchPaths);
if (enableLogging)
{
// Prints assemblies and their references to the Editor.log
PrintAssemblyDefinitions(assemblyDefinitions);
foreach (var searchPath in searchPaths)
{
Console.WriteLine("[AssemblyValidation] Search Path: '" + searchPath + "'");
}
}
var errors = ValidateAssemblyDefinitions(assemblyPaths,
assemblyDefinitions,
PluginCompatibleWithEditor);
return errors;
}
[RequiredByNativeCode]
public static Error[] ValidateAssemblyDefinitionFiles()
{
var customScriptAssemblies = EditorCompilationInterface.Instance.GetCustomScriptAssemblies();
if (customScriptAssemblies.Length == 0)
return null;
var pluginImporters = PluginImporter.GetAllImporters();
if (pluginImporters == null || pluginImporters.Length == 0)
return null;
var pluginFilenameToAssetPath = new Dictionary<string, string>();
foreach (var pluginImporter in pluginImporters)
{
var pluginAssetPath = pluginImporter.assetPath;
var lowerPluginFilename = AssetPath.GetFileName(pluginAssetPath).ToLower(CultureInfo.InvariantCulture);
pluginFilenameToAssetPath[lowerPluginFilename] = pluginAssetPath;
}
var errors = new List<Error>();
foreach (var customScriptAssembly in customScriptAssemblies)
{
var lowerAsmdefFilename = $"{customScriptAssembly.Name.ToLower(CultureInfo.InvariantCulture)}.dll";
string pluginPath;
if (pluginFilenameToAssetPath.TryGetValue(lowerAsmdefFilename, out pluginPath))
{
var error = new Error()
{
message = $"Plugin '{pluginPath}' has the same filename as Assembly Definition File '{customScriptAssembly.FilePath}'. Rename the assemblies to avoid hard to diagnose issues and crashes.",
flags = ErrorFlags.AsmdefPluginConflict,
assemblyPath = pluginPath
};
errors.Add(error);
}
}
return errors.ToArray();
}
public static bool PluginCompatibleWithEditor(string path)
{
var pluginImporter = AssetImporter.GetAtPath(path) as PluginImporter;
if (pluginImporter == null)
return true;
if (pluginImporter.GetCompatibleWithAnyPlatform())
return true;
return pluginImporter.GetCompatibleWithEditor();
}
public static bool ShouldValidateReferences(string path)
{
var pluginImporter = AssetImporter.GetAtPath(path) as PluginImporter;
if (pluginImporter == null)
return true;
return pluginImporter.ValidateReferences;
}
public static void PrintAssemblyDefinitions(AssemblyDefinition[] assemblyDefinitions)
{
foreach (var assemblyDefinition in assemblyDefinitions)
{
Console.WriteLine("[AssemblyValidation] Assembly: " + assemblyDefinition.Name);
var assemblyReferences = GetAssemblyNameReferences(assemblyDefinition);
foreach (var reference in assemblyReferences)
{
Console.WriteLine("[AssemblyValidation] Reference: " + reference);
}
}
}
public static Error[] ValidateAssembliesInternal(string[] assemblyPaths,
string[] searchPaths,
Func<string, bool> compatibleWithEditor)
{
var assemblyDefinitions = LoadAssemblyDefinitions(assemblyPaths, searchPaths);
return ValidateAssemblyDefinitions(assemblyPaths, assemblyDefinitions, compatibleWithEditor);
}
public static Error[] ValidateAssemblyDefinitions(string[] assemblyPaths,
AssemblyDefinition[] assemblyDefinitions,
Func<string, bool> compatibleWithEditor)
{
var errors = new Error[assemblyPaths.Length];
CheckAssemblyReferences(assemblyPaths,
errors,
assemblyDefinitions,
compatibleWithEditor);
return errors;
}
public static AssemblyDefinition[] LoadAssemblyDefinitions(string[] assemblyPaths, string[] searchPaths)
{
var assemblyResolver = new AssemblyResolver();
foreach (var asmpath in searchPaths)
assemblyResolver.AddSearchDirectory(asmpath);
var readerParameters = new ReaderParameters
{
AssemblyResolver = assemblyResolver
};
var assemblyDefinitions = new AssemblyDefinition[assemblyPaths.Length];
for (int i = 0; i < assemblyPaths.Length; ++i)
{
assemblyDefinitions[i] = AssemblyDefinition.ReadAssembly(assemblyPaths[i], readerParameters);
// Cecil tries to resolve references by filename, since Unity force loads
// assemblies, then assembly reference will resolve even if the assembly name
// does not match the assembly filename. So we register all assemblies in
// in the resolver.
assemblyResolver.RegisterAssembly(assemblyDefinitions[i]);
}
return assemblyDefinitions;
}
public static void CheckAssemblyReferences(string[] assemblyPaths,
Error[] errors,
AssemblyDefinition[] assemblyDefinitions,
Func<string, bool> compatibleWithEditor)
{
SetupEditorCompatibility(assemblyPaths, errors, compatibleWithEditor);
var assemblyDefinitionNameToIndex = new Dictionary<string, int>();
var assembliesAndReferencesArray = new AssemblyAndReferences[assemblyPaths.Length];
for (int i = 0; i < assemblyDefinitions.Length; ++i)
{
assemblyDefinitionNameToIndex[assemblyDefinitions[i].Name.Name] = i;
assembliesAndReferencesArray[i] = new AssemblyAndReferences
{
assemblyIndex = i,
referenceIndicies = new int[0]
};
}
for (int i = 0; i < assemblyPaths.Length; ++i)
{
if (errors[i].HasFlag(ErrorFlags.IncompatibleWithEditor))
continue;
var assemblyPath = assemblyPaths[i];
// Check if "Validate References" option is enabled
// in the PluginImporter
if (!ShouldValidateReferences(assemblyPath))
continue;
ResolveAndSetupReferences(i,
errors,
assemblyDefinitions,
assemblyDefinitionNameToIndex,
assembliesAndReferencesArray);
}
// Check assemblies for references to assemblies with errors
int referenceErrorCount;
do
{
referenceErrorCount = 0;
foreach (var assemblyAndReferences in assembliesAndReferencesArray)
{
var assemblyIndex = assemblyAndReferences.assemblyIndex;
foreach (var referenceIndex in assemblyAndReferences.referenceIndicies)
{
var referenceError = errors[referenceIndex];
if (errors[assemblyIndex].flags == ErrorFlags.None &&
referenceError.flags != ErrorFlags.None)
{
if (referenceError.HasFlag(ErrorFlags.IncompatibleWithEditor))
{
errors[assemblyIndex].Add(ErrorFlags.ReferenceHasErrors | ErrorFlags.IncompatibleWithEditor,
string.Format("Reference '{0}' is incompatible with the editor.",
assemblyDefinitions[referenceIndex].Name.Name));
}
else
{
errors[assemblyIndex].Add(ErrorFlags.ReferenceHasErrors | referenceError.flags,
string.Format("Reference has errors '{0}'.",
assemblyDefinitions[referenceIndex].Name.Name));
}
referenceErrorCount++;
}
}
}
}
while (referenceErrorCount > 0);
}
public static void SetupEditorCompatibility(string[] assemblyPaths,
Error[] errors,
Func<string, bool> compatibleWithEditor)
{
for (int i = 0; i < assemblyPaths.Length; ++i)
{
var assemblyPath = assemblyPaths[i];
if (!compatibleWithEditor(assemblyPath))
{
errors[i].Add(ErrorFlags.IncompatibleWithEditor,
"Assembly is incompatible with the editor");
}
}
}
public static void ResolveAndSetupReferences(int index,
Error[] errors,
AssemblyDefinition[] assemblyDefinitions,
Dictionary<string, int> assemblyDefinitionNameToIndex,
AssemblyAndReferences[] assemblyAndReferences)
{
var assemblyDefinition = assemblyDefinitions[index];
var assemblyResolver = assemblyDefinition.MainModule.AssemblyResolver;
var assemblyReferences = GetAssemblyNameReferences(assemblyDefinition);
var referenceIndieces = new List<int>
{
Capacity = assemblyReferences.Length
};
foreach (var reference in assemblyReferences)
{
try
{
var referenceAssemblyDefinition = assemblyResolver.Resolve(reference);
int referenceAssemblyDefinitionIndex;
if (assemblyDefinitionNameToIndex.TryGetValue(referenceAssemblyDefinition.Name.Name,
out referenceAssemblyDefinitionIndex))
{
bool isSigned = IsSigned(reference);
if (isSigned)
{
var definition = assemblyDefinitions[referenceAssemblyDefinitionIndex];
if (definition.Name.Version.ToString() != reference.Version.ToString() && !IsInSameFolder(assemblyDefinition, referenceAssemblyDefinition))
{
errors[index].Add(ErrorFlags.UnresolvableReference,
$"{assemblyDefinition.Name.Name} references strong named {reference.Name} in a different folder, versions has to match. Assembly references: {reference.Version} Found in project: {definition.Name.Version}.");
}
}
referenceIndieces.Add(referenceAssemblyDefinitionIndex);
}
}
catch (AssemblyResolutionException)
{
errors[index].Add(ErrorFlags.UnresolvableReference,
string.Format("Unable to resolve reference '{0}'. Is the assembly missing or incompatible with the current platform?\nReference validation can be disabled in the Plugin Inspector.",
reference.Name));
}
}
assemblyAndReferences[index].referenceIndicies = referenceIndieces.ToArray();
}
private static bool IsInSameFolder(AssemblyDefinition first, AssemblyDefinition second)
{
var firstAssemblyPath = Path.GetDirectoryName(first.MainModule.FileName);
var secondAssemblyPath = Path.GetDirectoryName(second.MainModule.FileName);
return firstAssemblyPath.Equals(secondAssemblyPath, StringComparison.Ordinal);
}
private static bool IsSigned(AssemblyNameReference reference)
{
//Bug in Cecil where HasPublicKey is always false
foreach (var publicTokenByte in reference.PublicKeyToken)
{
if (publicTokenByte != 0)
{
return true;
}
}
return false;
}
public static AssemblyNameReference[] GetAssemblyNameReferences(AssemblyDefinition assemblyDefinition)
{
List<AssemblyNameReference> result = new List<AssemblyNameReference>
{
Capacity = 16
};
foreach (ModuleDefinition module in assemblyDefinition.Modules)
{
var references = module.AssemblyReferences;
foreach (var reference in references)
{
result.Add(reference);
}
}
return result.ToArray();
}
}
}