forked from needle-mirror/com.unity.xr.management
-
Notifications
You must be signed in to change notification settings - Fork 1
/
XRGeneralBuildProcessor.cs
268 lines (232 loc) · 10.6 KB
/
XRGeneralBuildProcessor.cs
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using UnityEditor.Android;
using UnityEditor.Build;
using UnityEditor.Build.Reporting;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.XR.Management;
[assembly: InternalsVisibleTo("Unity.XR.Management.EditorTests")]
namespace UnityEditor.XR.Management
{
/// <summary>
/// Small utility class for reading, updating and writing boot config.
/// </summary>
internal class BootConfig
{
public static readonly string kXrBootSettingsKey = "xr-boot-settings";
Dictionary<string, string> bootConfigSettings;
BuildTarget m_target;
string bootConfigPath;
internal BootConfig(BuildTarget target)
{
m_target = target;
}
internal void ReadBootConfig()
{
bootConfigSettings = new Dictionary<string, string>();
string buildTargetName = BuildPipeline.GetBuildTargetName(m_target);
string xrBootSettings = UnityEditor.EditorUserBuildSettings.GetPlatformSettings(buildTargetName, kXrBootSettingsKey);
if (!String.IsNullOrEmpty(xrBootSettings))
{
// boot settings string format
// <boot setting>:<value>[;<boot setting>:<value>]*
var bootSettings = xrBootSettings.Split(';');
foreach (var bootSetting in bootSettings)
{
var setting = bootSetting.Split(':');
if (setting.Length == 2 && !String.IsNullOrEmpty(setting[0]) && !String.IsNullOrEmpty(setting[1]))
{
bootConfigSettings.Add(setting[0], setting[1]);
}
}
}
}
internal void SetValueForKey(string key, string value, bool replace = false)
{
if (bootConfigSettings.ContainsKey(key))
{
bootConfigSettings[key] = value;
}
else
{
bootConfigSettings.Add(key, value);
}
}
internal bool DeleteKey(string key)
{
return bootConfigSettings.Remove(key);
}
internal void WriteBootConfig()
{
// boot settings string format
// <boot setting>:<value>[;<boot setting>:<value>]*
bool firstEntry = true;
var sb = new System.Text.StringBuilder();
foreach (var kvp in bootConfigSettings)
{
if (!firstEntry)
{
sb.Append(";");
}
sb.Append($"{kvp.Key}:{kvp.Value}");
firstEntry = false;
}
string buildTargetName = BuildPipeline.GetBuildTargetName(m_target);
EditorUserBuildSettings.SetPlatformSettings(buildTargetName, kXrBootSettingsKey, sb.ToString());
}
}
class XRGeneralBuildProcessor : IPreprocessBuildWithReport, IPostprocessBuildWithReport
{
public static readonly string kPreInitLibraryKey = "xrsdk-pre-init-library";
class PreInitInfo
{
public PreInitInfo(IXRLoaderPreInit loader, BuildTarget buildTarget, BuildTargetGroup buildTargetGroup)
{
this.loader = loader;
this.buildTarget = buildTarget;
this.buildTargetGroup = buildTargetGroup;
}
public IXRLoaderPreInit loader;
public BuildTarget buildTarget;
public BuildTargetGroup buildTargetGroup;
}
internal static readonly int s_CallbackOrder = 0;
public int callbackOrder
{
get { return s_CallbackOrder; }
}
void CleanOldSettings()
{
BuildHelpers.CleanOldSettings<XRGeneralSettings>();
}
internal static bool TryGetSettingsPerBuildTarget(out XRGeneralSettingsPerBuildTarget buildTargetSettings)
{
// Fix for [1378643](https://fogbugz.unity3d.com/f/cases/1378643/)
// Ensure that if a settings asset exists in the project, it gets processed.
if (!EditorBuildSettings.TryGetConfigObject(XRGeneralSettings.k_SettingsKey, out buildTargetSettings))
{
if (XRGeneralSettingsPerBuildTarget.TryFindSettingsAsset(out buildTargetSettings))
{
// Asset found but not set. Set the configuration object. If it's empty it will get culled.
EditorBuildSettings.AddConfigObject(XRGeneralSettings.k_SettingsKey, buildTargetSettings, true);
}
else
{
// If no asset is found the processor should not run
return false;
}
}
return true;
}
public void OnPreprocessBuild(BuildReport report)
{
OnPreprocessBuildImpl(report.summary.guid, report.summary.platformGroup, report.summary.platform);
}
internal void OnPreprocessBuildImpl(in GUID buildGuid, in BuildTargetGroup targetGroup, in BuildTarget target)
{
// Always remember to cleanup preloaded assets after build to make sure we don't
// dirty later builds with assets that may not be needed or are out of date.
CleanOldSettings();
var buildTargetSettings = XRGeneralSettingsPerBuildTarget.GetOrCreate();
if (!buildTargetSettings)
return;
XRGeneralSettings settings = buildTargetSettings.SettingsForBuildTarget(targetGroup);
if (settings == null)
return;
XRManagerSettings loaderManager = settings.AssignedSettings;
if (loaderManager != null)
{
var loaders = loaderManager.activeLoaders;
XRManagementAnalytics.SendBuildEvent(buildGuid, target, targetGroup, loaders);
// chances are that our devices won't fall back to graphics device types later in the list so it's better to assume the device will be created with the first gfx api in the list.
// furthermore, we have no way to influence falling back to other graphics API types unless we automatically change settings underneath the user which is no good!
GraphicsDeviceType[] deviceTypes = PlayerSettings.GetGraphicsAPIs(target);
if (deviceTypes.Length > 0)
{
VerifyGraphicsAPICompatibility(loaderManager, deviceTypes[0]);
}
else
{
Debug.LogWarning("No Graphics APIs have been configured in Player Settings.");
}
PreInitInfo preInitInfo = null;
if (loaders.Count >= 1)
{
preInitInfo = new PreInitInfo(loaders[0] as IXRLoaderPreInit, target, targetGroup);
}
var loader = preInitInfo?.loader ?? null;
BootConfig bootConfig = new BootConfig(target);
bootConfig.ReadBootConfig();
if (loader != null)
{
string preInitLibraryName = loader.GetPreInitLibraryName(preInitInfo.buildTarget, preInitInfo.buildTargetGroup);
bootConfig.SetValueForKey(kPreInitLibraryKey, preInitLibraryName);
}
else
{
bootConfig.DeleteKey(kPreInitLibraryKey);
}
bootConfig.WriteBootConfig();
}
UnityEngine.Object[] preloadedAssets = PlayerSettings.GetPreloadedAssets();
var settingsIncludedInPreloadedAssets = preloadedAssets.Contains(settings);
// If there are no loaders present in the current manager instance, then the settings will not be included in the current build.
if (!settingsIncludedInPreloadedAssets && loaderManager.activeLoaders.Count > 0)
{
var assets = preloadedAssets.ToList();
assets.Add(settings);
PlayerSettings.SetPreloadedAssets(assets.ToArray());
}
else
{
CleanOldSettings();
}
}
public static void VerifyGraphicsAPICompatibility(XRManagerSettings loaderManager, GraphicsDeviceType selectedDeviceType)
{
HashSet<GraphicsDeviceType> allLoaderGraphicsDeviceTypes = new HashSet<GraphicsDeviceType>();
foreach (var loader in loaderManager.activeLoaders)
{
List<GraphicsDeviceType> supporteDeviceTypes = loader.GetSupportedGraphicsDeviceTypes(true);
// To help with backward compatibility, if we find that any of the compatibility lists are empty we assume that at least one of the loaders does not implement the GetSupportedGraphicsDeviceTypes method
// Therefore we revert to the previous behavior of building the app regardless of gfx api settings.
if (supporteDeviceTypes.Count == 0)
{
allLoaderGraphicsDeviceTypes.Clear();
break;
}
foreach (var supportedGraphicsDeviceType in supporteDeviceTypes)
{
allLoaderGraphicsDeviceTypes.Add(supportedGraphicsDeviceType);
}
}
if (allLoaderGraphicsDeviceTypes.Count > 0 && !allLoaderGraphicsDeviceTypes.Contains(selectedDeviceType))
{
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.AppendFormat(
"The selected graphics API, {0}, is not supported by any of the current loaders. Please change the preferred Graphics API setting in Player Settings.\n",
selectedDeviceType);
foreach (var loader in loaderManager.activeLoaders)
{
stringBuilder.AppendLine(loader.name + " supports:");
foreach (var supportedGraphicsDeviceType in loader.GetSupportedGraphicsDeviceTypes(true))
{
stringBuilder.AppendLine("\t -" + supportedGraphicsDeviceType);
}
}
throw new BuildFailedException(stringBuilder.ToString());
}
}
public void OnPostprocessBuild(BuildReport report)
{
// Always remember to cleanup preloaded assets after build to make sure we don't
// dirty later builds with assets that may not be needed or are out of date.
CleanOldSettings();
}
}
}