forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadingAssemblyDefinition.cs
More file actions
378 lines (318 loc) · 16.9 KB
/
Copy pathLoadingAssemblyDefinition.cs
File metadata and controls
378 lines (318 loc) · 16.9 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
// 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.IO;
using System.Linq;
using UnityEditor.Compilation;
namespace UnityEditor.Scripting.ScriptCompilation
{
interface ILoadingAssemblyDefinition
{
CustomScriptAssembly[] CustomScriptAssemblies { get; }
Exception[] Exceptions { get; }
List<CustomScriptAssemblyReference> CustomScriptAssemblyReferences { get; }
void SetAllCustomScriptAssemblyJsonContents(string[] paths, string[] contents, string[] guids);
void SetAllCustomScriptAssemblyReferenceJsonsContents(string[] paths, string[] contents);
void ClearCustomScriptAssemblies();
void Refresh(
ICompilationSetupErrorsTracker trackerBase,
bool skipCustomScriptAssemblyGraphValidation,
string projectDirectory);
}
class LoadingAssemblyDefinition : ILoadingAssemblyDefinition
{
ICompilationSetupErrorsTracker m_CompilationSetupErrorsTracker;
bool m_SkipCustomScriptAssemblyGraphValidation;
string m_ProjectDirectory;
public CustomScriptAssembly[] CustomScriptAssemblies { get; private set; } = new CustomScriptAssembly[0];
public List<CustomScriptAssemblyReference> CustomScriptAssemblyReferences { get; private set; } = new List<CustomScriptAssemblyReference>();
public Exception[] Exceptions { get; private set; }
public void Refresh(
ICompilationSetupErrorsTracker trackerBase,
bool skipCustomScriptAssemblyGraphValidation,
string projectDirectory)
{
m_CompilationSetupErrorsTracker = trackerBase;
m_SkipCustomScriptAssemblyGraphValidation = skipCustomScriptAssemblyGraphValidation;
m_ProjectDirectory = projectDirectory;
}
public void SetAllCustomScriptAssemblyJsonContents(string[] paths, string[] contents, string[] guids)
{
var assemblies = new List<CustomScriptAssembly>();
var assemblyLowercaseNamesLookup = new Dictionary<string, CustomScriptAssembly>();
var exceptions = new List<Exception>();
var guidsToAssemblies = new Dictionary<string, CustomScriptAssembly>();
HashSet<string> predefinedAssemblyNames = null;
// To check if a path prefix is already being used we use a Dictionary where the key is the prefix and the value is the file path.
var prefixToFilePathLookup = CustomScriptAssemblyReferences.ToDictionary(x => x.PathPrefix,
x => new List<string> {x.FilePath}, StringComparer.OrdinalIgnoreCase);
m_CompilationSetupErrorsTracker.ClearCompilationSetupErrors(CompilationSetupErrors.LoadError);
// Load first to setup guidsToAssemblies dictionary and convert guids to assembly names
// before checking for assembly reference errors, so errors emit assembly names instead of guids.
for (var i = 0; i < paths.Length; ++i)
{
var path = paths[i];
var guid = guids[i];
string lowerCaseName = null;
CustomScriptAssembly loadedCustomScriptAssembly = null;
try
{
var fullPath = AssetPath.IsPathRooted(path)
? AssetPath.GetFullPath(path)
: AssetPath.Combine(m_ProjectDirectory, path);
loadedCustomScriptAssembly =
contents != null
? LoadCustomScriptAssemblyFromJson(fullPath, contents[i], guid)
: LoadCustomScriptAssemblyFromJsonPath(fullPath, guid);
loadedCustomScriptAssembly.References = loadedCustomScriptAssembly.References ?? new string[0];
lowerCaseName = Utility.FastToLower(loadedCustomScriptAssembly.Name);
guidsToAssemblies[Utility.FastToLower(guid)] = loadedCustomScriptAssembly;
if (!m_SkipCustomScriptAssemblyGraphValidation)
{
if (predefinedAssemblyNames == null)
{
predefinedAssemblyNames = new HashSet<string>(EditorBuildRules.PredefinedTargetAssemblyNames);
var netfw = MonoLibraryHelpers
.GetSystemLibraryReferences(ApiCompatibilityLevel.NET_Unity_4_8).Select(Path.GetFileNameWithoutExtension);
var netstandard21 = MonoLibraryHelpers
.GetSystemLibraryReferences(ApiCompatibilityLevel.NET_Standard).Select(Path.GetFileNameWithoutExtension);
predefinedAssemblyNames.UnionWith(netfw);
predefinedAssemblyNames.UnionWith(netstandard21);
}
CheckPredefinedAssemblyNames(
predefinedAssemblyNames,
assemblyLowercaseNamesLookup,
lowerCaseName,
prefixToFilePathLookup,
ref loadedCustomScriptAssembly);
}
}
catch (Exception e)
{
m_CompilationSetupErrorsTracker.SetCompilationSetupErrors(CompilationSetupErrors.LoadError);
exceptions.Add(e);
}
if (loadedCustomScriptAssembly == null || m_SkipCustomScriptAssemblyGraphValidation && assemblyLowercaseNamesLookup.ContainsKey(lowerCaseName))
{
continue;
}
loadedCustomScriptAssembly.References = loadedCustomScriptAssembly.References ?? new string[0];
assemblyLowercaseNamesLookup[lowerCaseName] = loadedCustomScriptAssembly;
assemblies.Add(loadedCustomScriptAssembly);
if (!prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssembly.PathPrefix, out var duplicateFilePaths))
{
duplicateFilePaths = new List<string>();
prefixToFilePathLookup[loadedCustomScriptAssembly.PathPrefix] = duplicateFilePaths;
}
duplicateFilePaths.Add(loadedCustomScriptAssembly.FilePath);
}
ConvertGUIDReferencesToAssemblyNames(assemblies, guidsToAssemblies);
if (!m_SkipCustomScriptAssemblyGraphValidation)
{
CheckForReferenceErrors(assemblies, exceptions);
}
CustomScriptAssemblies = assemblies.ToArray();
Exceptions = exceptions.ToArray();
}
void CheckForReferenceErrors(List<CustomScriptAssembly> assemblies, List<Exception> exceptions)
{
foreach (var loadedCustomScriptAssembly in assemblies)
{
try
{
var references = loadedCustomScriptAssembly.References.Where(r => !string.IsNullOrEmpty(r))
.ToArray();
if (references.Length == references.Distinct().Count())
{
continue;
}
var duplicateRefs = references.GroupBy(r => r).SelectMany(g => g.Skip(1)).ToArray();
var duplicateRefsString = string.Join(",", duplicateRefs);
throw new AssemblyDefinitionException(
$"Assembly has duplicate references: {duplicateRefsString}",
loadedCustomScriptAssembly.FilePath);
}
catch (Exception e)
{
m_CompilationSetupErrorsTracker.SetCompilationSetupErrors(CompilationSetupErrors.LoadError);
exceptions.Add(e);
}
}
}
static void ConvertGUIDReferencesToAssemblyNames(List<CustomScriptAssembly> assemblies, Dictionary<string, CustomScriptAssembly> guidsToAssemblies)
{
foreach (var assembly in assemblies)
{
for (int i = 0; i < assembly.References.Length; ++i)
{
var reference = assembly.References[i];
if (!GUIDReference.IsGUIDReference(reference))
{
continue;
}
var guid = Utility.FastToLower(GUIDReference.GUIDReferenceToGUID(reference));
reference = guidsToAssemblies.TryGetValue(guid, out var referenceAssembly)
? referenceAssembly.Name
: string.Empty;
assembly.References[i] = reference;
}
}
}
public void SetAllCustomScriptAssemblyReferenceJsonsContents(string[] paths, string[] contents)
{
var assemblyRefs = new List<CustomScriptAssemblyReference>();
var exceptions = new List<Exception>();
// We only construct this lookup if it is required, which is when we are using guids instead of assembly names.
Dictionary<string, CustomScriptAssembly> guidsToAssemblies = null;
// To check if a path prefix is already being used we use a Dictionary where the key is the prefix and the value is the file path.
var prefixToFilePathLookup = m_SkipCustomScriptAssemblyGraphValidation ?
null :
CustomScriptAssemblies.GroupBy(x => x.PathPrefix).ToDictionary(x => x.First().PathPrefix, x => new List<string>() { x.First().FilePath }, StringComparer.OrdinalIgnoreCase);
for (var i = 0; i < paths.Length; ++i)
{
var path = paths[i];
CustomScriptAssemblyReference loadedCustomScriptAssemblyReference = null;
try
{
var fullPath = AssetPath.IsPathRooted(path) ? AssetPath.GetFullPath(path) : AssetPath.Combine(m_ProjectDirectory, path);
if (contents != null)
{
var jsonContents = contents[i];
loadedCustomScriptAssemblyReference = LoadCustomScriptAssemblyReferenceFromJson(fullPath, jsonContents);
}
else
{
loadedCustomScriptAssemblyReference = LoadCustomScriptAssemblyReferenceFromJsonPath(fullPath);
}
if (!m_SkipCustomScriptAssemblyGraphValidation)
{
// Check both asmdef and asmref files.
if (prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssemblyReference.PathPrefix, out var duplicateFilePaths))
{
var filePaths = new List<string> {loadedCustomScriptAssemblyReference.FilePath};
filePaths.AddRange(duplicateFilePaths);
throw new AssemblyDefinitionException(
$"Folder '{loadedCustomScriptAssemblyReference.PathPrefix}' contains multiple assembly definition files", filePaths.ToArray());
}
}
// Convert GUID references to assembly names
if (GUIDReference.IsGUIDReference(loadedCustomScriptAssemblyReference.Reference))
{
// Generate the guid to assembly lookup?
guidsToAssemblies = guidsToAssemblies ?? CustomScriptAssemblies.ToDictionary(x => x.GUID);
var guid = Utility.FastToLower(GUIDReference.GUIDReferenceToGUID(loadedCustomScriptAssemblyReference.Reference));
if (guidsToAssemblies.TryGetValue(guid, out var foundAssembly))
{
loadedCustomScriptAssemblyReference.Reference = foundAssembly.Name;
}
}
}
catch (Exception e)
{
m_CompilationSetupErrorsTracker.SetCompilationSetupErrors(CompilationSetupErrors.LoadError);
exceptions.Add(e);
}
if (loadedCustomScriptAssemblyReference != null)
{
assemblyRefs.Add(loadedCustomScriptAssemblyReference);
if (m_SkipCustomScriptAssemblyGraphValidation)
{
continue;
}
if (!prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssemblyReference.PathPrefix, out var duplicateFilePaths))
{
duplicateFilePaths = new List<string>();
prefixToFilePathLookup[loadedCustomScriptAssemblyReference.PathPrefix] = duplicateFilePaths;
}
duplicateFilePaths.Add(loadedCustomScriptAssemblyReference.FilePath);
}
}
CustomScriptAssemblyReferences = assemblyRefs;
Exceptions = exceptions.ToArray();
}
public void ClearCustomScriptAssemblies()
{
CustomScriptAssemblies = new CustomScriptAssembly[0];
CustomScriptAssemblyReferences.Clear();
}
static void CheckPredefinedAssemblyNames(
HashSet<string> predefinedAssemblyNames,
Dictionary<string, CustomScriptAssembly> assemblyLowercaseNamesLookup,
string lowerCaseName,
Dictionary<string, List<string>> prefixToFilePathLookup,
ref CustomScriptAssembly loadedCustomScriptAssembly)
{
if (predefinedAssemblyNames.Contains(loadedCustomScriptAssembly.Name))
{
throw new AssemblyDefinitionException(
$"Assembly cannot be have reserved name '{loadedCustomScriptAssembly.Name}'",
loadedCustomScriptAssembly.FilePath);
}
if (assemblyLowercaseNamesLookup.TryGetValue(lowerCaseName, out var duplicate))
{
var filePaths = new[]
{
loadedCustomScriptAssembly.FilePath,
duplicate.FilePath
};
var errorMsg = $"Assembly with name '{loadedCustomScriptAssembly.Name}' already exists";
loadedCustomScriptAssembly = null; // Set to null to prevent it being added.
throw new AssemblyDefinitionException(errorMsg, filePaths);
}
// Check both asmdef and asmref files.
if (prefixToFilePathLookup.TryGetValue(loadedCustomScriptAssembly.PathPrefix,
out var duplicateFilePaths))
{
var filePaths = new List<string> {loadedCustomScriptAssembly.FilePath};
filePaths.AddRange(duplicateFilePaths);
throw new AssemblyDefinitionException(
$"Folder '{loadedCustomScriptAssembly.PathPrefix}' contains multiple assembly definition files",
filePaths.ToArray());
}
}
public static CustomScriptAssembly LoadCustomScriptAssemblyFromJson(string path, string json, string guid)
{
try
{
var customScriptAssemblyData = CustomScriptAssemblyData.FromJson(json);
return CustomScriptAssembly.FromCustomScriptAssemblyData(path, guid, customScriptAssemblyData);
}
catch (Exception e)
{
throw new AssemblyDefinitionException(e.Message, path);
}
}
public static CustomScriptAssembly LoadCustomScriptAssemblyFromJsonPath(string path, string guid)
{
var json = Utility.ReadTextAsset(path);
try
{
var customScriptAssemblyData = CustomScriptAssemblyData.FromJson(json);
return CustomScriptAssembly.FromCustomScriptAssemblyData(path, guid, customScriptAssemblyData);
}
catch (Exception e)
{
throw new AssemblyDefinitionException(e.Message, path);
}
}
static CustomScriptAssemblyReference LoadCustomScriptAssemblyReferenceFromJsonPath(string path)
{
var json = Utility.ReadTextAsset(path);
return LoadCustomScriptAssemblyReferenceFromJson(path, json);
}
static CustomScriptAssemblyReference LoadCustomScriptAssemblyReferenceFromJson(string path, string json)
{
try
{
var customScriptAssemblyRefData = CustomScriptAssemblyReferenceData.FromJson(json);
return CustomScriptAssemblyReference.FromCustomScriptAssemblyReferenceData(path, customScriptAssemblyRefData);
}
catch (Exception e)
{
throw new AssemblyDefinitionException(e.Message, path);
}
}
}
}