forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUWPReferences.cs
More file actions
308 lines (258 loc) · 10.9 KB
/
Copy pathUWPReferences.cs
File metadata and controls
308 lines (258 loc) · 10.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
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
// this file is used by both Editor and AssemblyConverter
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using Microsoft.Win32;
namespace UnityEditor.Scripting.Compilers
{
internal struct UWPExtensionSDK
{
public readonly string Name;
public readonly string Version;
public readonly string ManifestPath;
public UWPExtensionSDK(string name, string version, string manifestPath)
{
Name = name;
Version = version;
ManifestPath = manifestPath;
}
}
internal class UWPSDK
{
public readonly Version Version;
public readonly Version MinVSVersion;
public UWPSDK(Version version, Version minVSVersion)
{
Version = version;
MinVSVersion = minVSVersion;
}
}
internal static class UWPReferences
{
private sealed class UWPExtension
{
public string Name { get; private set; }
public string[] References { get; private set; }
public UWPExtension(string manifest, string windowsKitsFolder, string sdkVersion)
{
var document = XDocument.Load(manifest);
var fileListElement = document.Element("FileList");
if (fileListElement.Attribute("TargetPlatform").Value != "UAP")
throw new Exception(string.Format("Invalid extension manifest at \"{0}\".", manifest));
Name = fileListElement.Attribute("DisplayName").Value;
var containedApiContractsElement = fileListElement.Element("ContainedApiContracts");
References = GetReferences(windowsKitsFolder, sdkVersion, containedApiContractsElement);
}
}
public static string[] GetReferences(Version sdkVersion)
{
var folder = GetWindowsKit10();
if (string.IsNullOrEmpty(folder))
return new string[0];
var version = SdkVersionToString(sdkVersion);
var references = new HashSet<string>(StringComparer.InvariantCultureIgnoreCase);
var windowsWinMd = CombinePaths(folder, "UnionMetadata", version, "Facade", "Windows.winmd");
if (!File.Exists(windowsWinMd))
windowsWinMd = CombinePaths(folder, "UnionMetadata", "Facade", "Windows.winmd");
references.Add(windowsWinMd);
foreach (var reference in GetPlatform(folder, version))
{
references.Add(reference);
}
foreach (var extension in GetExtensions(folder, version))
{
foreach (var reference in extension.References)
{
references.Add(reference);
}
}
return references.ToArray();
}
public static IEnumerable<UWPExtensionSDK> GetExtensionSDKs(Version sdkVersion)
{
var windowsKit10Directory = GetWindowsKit10();
if (string.IsNullOrEmpty(windowsKit10Directory))
return new UWPExtensionSDK[0];
return GetExtensionSDKs(windowsKit10Directory, SdkVersionToString(sdkVersion));
}
static string SdkVersionToString(Version version)
{
var sdkVersion = version.ToString();
if (version.Minor == -1)
sdkVersion += ".0";
if (version.Build == -1)
sdkVersion += ".0";
if (version.Revision == -1)
sdkVersion += ".0";
return sdkVersion;
}
public static IEnumerable<UWPSDK> GetInstalledSDKs()
{
var windowsKit10Directory = GetWindowsKit10();
if (string.IsNullOrEmpty(windowsKit10Directory))
return Enumerable.Empty<UWPSDK>();
var platformsUAP = CombinePaths(windowsKit10Directory, "Platforms", "UAP");
if (!Directory.Exists(platformsUAP))
return Enumerable.Empty<UWPSDK>();
var allSDKs = new List<UWPSDK>();
var filesUnderPlatformsUAP = Directory.GetFiles(platformsUAP, "*", SearchOption.AllDirectories);
var allPlatformXmlFiles = filesUnderPlatformsUAP.Where(f => string.Equals("Platform.xml", Path.GetFileName(f), StringComparison.OrdinalIgnoreCase));
foreach (var platformXmlFile in allPlatformXmlFiles)
{
XDocument xDocument;
try
{
xDocument = XDocument.Load(platformXmlFile);
}
catch
{
continue;
}
foreach (var element in xDocument.Elements("ApplicationPlatform"))
{
Version version;
if (FindVersionInNode(element, out version))
{
var minVSVersionString = element.Elements("MinimumVisualStudioVersion").Select(e => e.Value).FirstOrDefault();
allSDKs.Add(new UWPSDK(version, TryParseVersion(minVSVersionString)));
}
}
}
return allSDKs;
}
// No Version.TryParse in .NET 3.5 :(
private static Version TryParseVersion(string s)
{
if (!string.IsNullOrEmpty(s))
{
try
{
return new Version(s);
}
catch
{
}
}
return null;
}
private static bool FindVersionInNode(XElement node, out Version version)
{
for (var attribute = node.FirstAttribute; attribute != null; attribute = attribute.NextAttribute)
{
if (string.Equals(attribute.Name.LocalName, "version", StringComparison.OrdinalIgnoreCase))
{
version = TryParseVersion(attribute.Value);
if (version != null)
{
return true;
}
}
}
version = null;
return false;
}
private static string[] GetPlatform(string folder, string version)
{
var platformXml = CombinePaths(folder, @"Platforms\UAP", version, "Platform.xml");
if (!File.Exists(platformXml))
return new string[0];
var document = XDocument.Load(platformXml);
var applicationPlatformElement = document.Element("ApplicationPlatform");
if (applicationPlatformElement.Attribute("name").Value != "UAP")
throw new Exception(string.Format("Invalid platform manifest at \"{0}\".", platformXml));
var containedApiContractsElement = applicationPlatformElement.Element("ContainedApiContracts");
return GetReferences(folder, version, containedApiContractsElement);
}
private static string CombinePaths(params string[] paths)
{
return UnityEditor.Utils.Paths.Combine(paths);
}
private static IEnumerable<UWPExtensionSDK> GetExtensionSDKs(string sdkFolder, string sdkVersion)
{
var extensions = new List<UWPExtensionSDK>();
var extensionsFolder = Path.Combine(sdkFolder, "Extension SDKs");
if (!Directory.Exists(extensionsFolder))
return new UWPExtensionSDK[0];
foreach (var extensionFolder in Directory.GetDirectories(extensionsFolder))
{
var manifest = CombinePaths(extensionFolder, sdkVersion, "SDKManifest.xml");
var extensionName = Path.GetFileName(extensionFolder);
if (File.Exists(manifest))
{
extensions.Add(new UWPExtensionSDK(extensionName, sdkVersion, manifest));
continue;
}
if (extensionName == "XboxLive")
{
// Workaround for XboxLive SDK bug: currently, its version is always 1.0. Microsoft said they'll fix it in the future.
manifest = CombinePaths(extensionFolder, "1.0", "SDKManifest.xml");
if (File.Exists(manifest))
{
extensions.Add(new UWPExtensionSDK(extensionName, "1.0", manifest));
continue;
}
}
}
return extensions;
}
private static UWPExtension[] GetExtensions(string windowsKitsFolder, string version)
{
var extensions = new List<UWPExtension>();
foreach (var extensionSDK in GetExtensionSDKs(windowsKitsFolder, version))
{
try
{
var extension = new UWPExtension(extensionSDK.ManifestPath, windowsKitsFolder, version);
extensions.Add(extension);
}
catch
{
// ignore exceptions
}
}
return extensions.ToArray();
}
private static string[] GetReferences(string windowsKitsFolder, string sdkVersion, XElement containedApiContractsElement)
{
var references = new List<string>();
foreach (var apiContractElement in containedApiContractsElement.Elements("ApiContract"))
{
var name = apiContractElement.Attribute("name").Value;
var version = apiContractElement.Attribute("version").Value;
var reference = CombinePaths(windowsKitsFolder, "References", sdkVersion, name, version, name + ".winmd");
if (!File.Exists(reference))
{
reference = CombinePaths(windowsKitsFolder, "References", name, version, name + ".winmd");
if (!File.Exists(reference))
continue;
}
references.Add(reference);
}
return references.ToArray();
}
private static string GetWindowsKit10()
{
var programFilesX86 =
Environment.GetEnvironmentVariable("ProgramFiles(x86)")
;
var folder = Path.Combine(programFilesX86, @"Windows Kits\10\");
try
{
const string keyName = @"SOFTWARE\Microsoft\Microsoft SDKs\Windows\v10.0";
folder = UnityEditorInternal.RegistryUtil.GetRegistryStringValue(keyName, "InstallationFolder", folder, UnityEditorInternal.RegistryView._32);
}
catch
{
// ignore exceptions
}
if (!Directory.Exists(folder))
return string.Empty;
return folder;
}
}
}