forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssemblyGraphBuilder.cs
More file actions
244 lines (211 loc) · 9.95 KB
/
Copy pathAssemblyGraphBuilder.cs
File metadata and controls
244 lines (211 loc) · 9.95 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
// 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;
namespace UnityEditor.Scripting.ScriptCompilation
{
internal interface IAssemblyGraphBuilder
{
Dictionary<CustomScriptAssembly, List<string>> Match(
IReadOnlyCollection<string> sources);
}
internal class AssemblyGraphBuilder : IAssemblyGraphBuilder
{
private readonly string _projectPath;
private readonly CustomScriptAssembly _globalAssemblyDefinition;
private readonly CustomScriptAssembly _editorAssebmlyDefinition;
private readonly CustomScriptAssembly _globalFirstpassAssemblyDefinition;
private readonly CustomScriptAssembly _editorFirstpassAssemblyDefinition;
static readonly string _guidFormat = "N";
private readonly CustomScriptAssemblyReference[] _globalFirstpassAssemblyReferences;
private PathMultidimensionalDivisionTree<CustomScriptAssembly> _npp =
new PathMultidimensionalDivisionTree<CustomScriptAssembly>();
public AssemblyGraphBuilder(string projectPath)
{
_projectPath = projectPath;
_globalAssemblyDefinition = new CustomScriptAssembly
{
Name = "Assembly-CSharp",
PathPrefix = projectPath,
FilePath = Path.Combine(projectPath, "main.asmdef"),
GUID = CreateNewUnityGuid(),
IsPredefined = true,
};
_editorAssebmlyDefinition = new CustomScriptAssembly
{
Name = "Assembly-CSharp-Editor",
PathPrefix = Path.Combine(projectPath, "Editor"),
FilePath = Path.Combine(projectPath, "Editor/main-editor.asmdef"),
GUID = CreateNewUnityGuid(),
IsPredefined = true,
};
_globalFirstpassAssemblyDefinition = new CustomScriptAssembly
{
Name = "Assembly-CSharp-firstpass",
PathPrefix = Path.Combine(projectPath, "Plugins"),
FilePath = Path.Combine(projectPath, "Plugins/firstpass.asmdef"),
GUID = CreateNewUnityGuid(),
IsPredefined = true,
};
_editorFirstpassAssemblyDefinition = new CustomScriptAssembly
{
Name = "Assembly-CSharp-Editor-firstpass",
PathPrefix = Path.Combine(projectPath, "Plugins/Editor"),
FilePath = Path.Combine(projectPath, "Plugins/Editor/firstpass-editor.asmdef"),
GUID = CreateNewUnityGuid(),
IsPredefined = true,
};
_globalFirstpassAssemblyReferences = new[]
{
CustomScriptAssemblyReference.FromPathAndReference(
Path.Combine(projectPath, "standard assets/standard assets.asmref"),
_globalFirstpassAssemblyDefinition.Name),
CustomScriptAssemblyReference.FromPathAndReference(
Path.Combine(projectPath, "pro standard assets/pro standard assets.asmref"),
_globalFirstpassAssemblyDefinition.Name),
CustomScriptAssemblyReference.FromPathAndReference(
Path.Combine(projectPath, "iphone standard assets/iphone standard assets.asmref"),
_globalFirstpassAssemblyDefinition.Name),
};
}
public void Initialize(IReadOnlyCollection<CustomScriptAssembly> assemblies,
IReadOnlyCollection<CustomScriptAssemblyReference> customScriptAssemblyReferences)
{
var assemblyByNameLookup = assemblies.ToDictionary(x => x.Name, x => x);
var assemblyByGuidLookup = assemblies.ToDictionary(x => x.GUID, x => x);
bool rootOverridden = assemblies.Any(x => AssetPath.ComparePaths(x.PathPrefix, _projectPath));
if (!rootOverridden)
{
_npp.Insert(_globalAssemblyDefinition.PathPrefix, _globalAssemblyDefinition);
_npp.Insert(_editorAssebmlyDefinition.PathPrefix, _editorAssebmlyDefinition);
_npp.Insert(_globalFirstpassAssemblyDefinition.PathPrefix, _globalFirstpassAssemblyDefinition);
_npp.Insert(_editorFirstpassAssemblyDefinition.PathPrefix, _editorFirstpassAssemblyDefinition);
}
foreach (var assemblyDef in assemblies)
{
_npp.Insert(assemblyDef.PathPrefix, assemblyDef);
}
if (!rootOverridden)
{
foreach (var globalFirstpassAssemblyReference in _globalFirstpassAssemblyReferences)
{
_npp.Insert(globalFirstpassAssemblyReference.PathPrefix, _globalFirstpassAssemblyDefinition);
}
}
foreach (var assemblyReference in customScriptAssemblyReferences)
{
CustomScriptAssembly foundAssemblyDef = null;
var foundAssemblyDefinition = GUIDReference.IsGUIDReference(assemblyReference.Reference)
? assemblyByGuidLookup.TryGetValue(GUIDReference.GUIDReferenceToGUID(assemblyReference.Reference),
out foundAssemblyDef)
: assemblyByNameLookup.TryGetValue(assemblyReference.Reference, out foundAssemblyDef);
if (foundAssemblyDefinition)
{
_npp.Insert(assemblyReference.PathPrefix, foundAssemblyDef);
}
else
{
Console.WriteLine(
$"Assembly reference {assemblyReference.FilePath} has no target assembly definition");
}
}
}
public Dictionary<CustomScriptAssembly, List<string>> Match(
IReadOnlyCollection<string> sources)
{
var assemblyNameSources = new Dictionary<CustomScriptAssembly, List<string>>();
foreach (var source in sources)
{
var sourceSpan = source.AsSpan();
var currentMatchingAssemblyDefinition = _npp.MatchClosest(sourceSpan, out var matchedBy);
currentMatchingAssemblyDefinition =
CheckAndUpdateEditorSpecialFolder(currentMatchingAssemblyDefinition, sourceSpan, matchedBy);
if (currentMatchingAssemblyDefinition == null)
{
Console.WriteLine(
$"Script '{source}' will not be compiled because it exists outside the Assets folder and does not to belong to any assembly definition file.");
continue;
}
if (!assemblyNameSources.TryGetValue(currentMatchingAssemblyDefinition, out var sourceList))
{
sourceList = new List<string>();
assemblyNameSources[currentMatchingAssemblyDefinition] = sourceList;
}
sourceList.Add(source);
}
return assemblyNameSources;
}
internal static string CreateNewUnityGuid()
{
return Guid.NewGuid().ToString(_guidFormat);
}
internal static ReadOnlySpan<char> GetRelativePathFromAsmdefOrAsmref(CustomScriptAssembly currentMatchingAssemblyDefinition, ReadOnlySpan<char> sourceSpan, string matchedBy)
{
if(currentMatchingAssemblyDefinition == null)
{
return sourceSpan;
}
return sourceSpan[matchedBy.Length..];
}
private CustomScriptAssembly CheckAndUpdateEditorSpecialFolder(
CustomScriptAssembly currentMatchingAssemblyDefinition, ReadOnlySpan<char> sourceSpan, string matchedBy)
{
var relativeSourceSpan = GetRelativePathFromAsmdefOrAsmref(currentMatchingAssemblyDefinition, sourceSpan, matchedBy);
if (HasEditorSpecialFolder(relativeSourceSpan))
{
if (currentMatchingAssemblyDefinition == null ||
currentMatchingAssemblyDefinition == _globalAssemblyDefinition)
{
currentMatchingAssemblyDefinition = _editorAssebmlyDefinition;
}
else if (currentMatchingAssemblyDefinition == _globalFirstpassAssemblyDefinition)
{
currentMatchingAssemblyDefinition = _editorFirstpassAssemblyDefinition;
}
}
return currentMatchingAssemblyDefinition;
}
internal static bool HasEditorSpecialFolder(ReadOnlySpan<char> remainingPath)
{
const string editorLower = "editor";
const string editorUpper = "EDITOR";
if (remainingPath.Length < editorLower.Length)
{
return false;
}
int matchOffset = 0;
for (int i = 0; i < remainingPath.Length; i++)
{
if (editorLower[matchOffset] == remainingPath[i] || editorUpper[matchOffset] == remainingPath[i])
{
matchOffset++;
if (matchOffset < editorLower.Length)
{
continue;
}
else if (i >= remainingPath.Length || Utility.IsPathSeparator(remainingPath[i + 1]))
{
return true;
}
}
// forward to next path separator or end
for (; i < remainingPath.Length; i++)
{
if (Utility.IsPathSeparator(remainingPath[i]))
{
break;
}
}
matchOffset = 0;
if (remainingPath.Length - i < editorLower.Length)
{
return false;
}
}
return matchOffset == editorLower.Length;
}
}
}