OVRBundleTool.cs
15.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
#if UNITY_EDITOR_WIN && UNITY_ANDROID
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.IO;
using UnityEngine;
using UnityEditor;
public class OVRBundleTool : EditorWindow
{
private static List<EditorSceneInfo> buildableScenes;
private static Vector2 debugLogScroll = new Vector2(0, 0);
private static bool invalidBuildableScene;
private static string toolLog;
private static bool useOptionalTransitionApkPackage;
private static GUIStyle logBoxStyle;
private static Vector2 logBoxSize;
private static float logBoxSpacing = 30.0f;
private bool forceRestart = false;
private bool showBundleManagement = false;
private bool showOther = false;
// Needed to ensure that APK checking does happen during editor start up, but will still happen when the window is opened/updated
private static bool panelInitialized = false;
private enum ApkStatus
{
UNKNOWN,
OK,
NOT_INSTALLED,
DEVICE_NOT_CONNECTED,
};
public enum SceneBundleStatus
{
[Description("Unknown")]
UNKNOWN,
[Description("Queued")]
QUEUED,
[Description("Building")]
BUILDING,
[Description("Done")]
DONE,
[Description("Transferring")]
TRANSFERRING,
[Description("Deployed")]
DEPLOYED,
};
public class EditorSceneInfo
{
public string scenePath;
public string sceneName;
public SceneBundleStatus buildStatus;
public EditorSceneInfo(string path, string name)
{
scenePath = path;
sceneName = name;
buildStatus = SceneBundleStatus.UNKNOWN;
}
}
private enum GuiAction
{
None,
OpenBuildSettingsWindow,
BuildAndDeployScenes,
BuildAndDeployApp,
ClearDeviceBundles,
ClearLocalBundles,
LaunchApp,
UninstallApk,
ClearLog,
}
private GuiAction action = GuiAction.None;
private static ApkStatus currentApkStatus;
[MenuItem("Oculus/OVR Build/OVR Scene Quick Preview %l", false, 10)]
static void Init()
{
currentApkStatus = ApkStatus.UNKNOWN;
EditorWindow.GetWindow(typeof(OVRBundleTool));
invalidBuildableScene = false;
InitializePanel();
OVRPlugin.SetDeveloperMode(OVRPlugin.Bool.True);
OVRPlugin.SendEvent("oculus_bundle_tool", "show_window");
}
public void OnEnable()
{
InitializePanel();
}
public static void InitializePanel()
{
panelInitialized = true;
GetScenesFromBuildSettings();
EditorBuildSettings.sceneListChanged += GetScenesFromBuildSettings;
}
private void OnGUI()
{
this.titleContent.text = "OVR Scene Quick Preview";
if (panelInitialized)
{
CheckForTransitionAPK();
panelInitialized = false;
}
if (logBoxStyle == null)
{
logBoxStyle = new GUIStyle();
logBoxStyle.margin.left = 5;
logBoxStyle.wordWrap = true;
logBoxStyle.normal.textColor = logBoxStyle.focused.textColor = EditorStyles.label.normal.textColor;
logBoxStyle.richText = true;
}
GUILayout.Space(10.0f);
GUILayout.Label("Scenes", EditorStyles.boldLabel);
GUIContent buildSettingsBtnTxt = new GUIContent("Open Build Settings");
if (buildableScenes == null || buildableScenes.Count == 0)
{
string sceneErrorMessage;
if (invalidBuildableScene)
{
sceneErrorMessage = "Invalid scene selection. \nPlease remove OVRTransitionScene in the project's build settings.";
}
else
{
sceneErrorMessage = "No scenes detected. \nTo get started, add scenes in the project's build settings.";
}
GUILayout.Label(sceneErrorMessage);
var buildSettingBtnRt = GUILayoutUtility.GetRect(buildSettingsBtnTxt, GUI.skin.button, GUILayout.Width(150));
if (GUI.Button(buildSettingBtnRt, buildSettingsBtnTxt))
{
action = GuiAction.OpenBuildSettingsWindow;
}
}
else
{
foreach (EditorSceneInfo scene in buildableScenes)
{
EditorGUILayout.BeginHorizontal();
{
EditorGUILayout.LabelField(scene.sceneName, GUILayout.ExpandWidth(true));
GUILayout.FlexibleSpace();
if (scene.buildStatus != SceneBundleStatus.UNKNOWN)
{
string status = GetEnumDescription(scene.buildStatus);
EditorGUILayout.LabelField(status, GUILayout.Width(70));
}
}
EditorGUILayout.EndHorizontal();
}
EditorGUILayout.BeginHorizontal();
{
GUIContent sceneBtnTxt = new GUIContent("Build and Deploy Scene(s)");
var sceneBtnRt = GUILayoutUtility.GetRect(sceneBtnTxt, GUI.skin.button, GUILayout.Width(200));
if (GUI.Button(sceneBtnRt, sceneBtnTxt))
{
action = GuiAction.BuildAndDeployScenes;
}
GUIContent forceRestartLabel = new GUIContent("Force Restart [?]", "Relaunch the application after scene bundles are finished deploying.");
forceRestart = GUILayout.Toggle(forceRestart, forceRestartLabel, GUILayout.ExpandWidth(true));
}
EditorGUILayout.EndHorizontal();
}
GUILayout.Space(10.0f);
GUIContent transitionContent = new GUIContent("Transition APK [?]", "Build and deploy an APK that will transition into the scene you are working on. This enables fast iteration on a specific scene.");
GUILayout.Label(transitionContent, EditorStyles.boldLabel);
EditorGUILayout.BeginHorizontal();
{
GUIStyle statusStyle = EditorStyles.label;
statusStyle.richText = true;
GUILayout.Label("Status: ", statusStyle, GUILayout.ExpandWidth(false));
string statusMesssage;
switch (currentApkStatus)
{
case ApkStatus.OK:
statusMesssage = "<color=green>APK installed. Ready to build and deploy scenes.</color>";
break;
case ApkStatus.NOT_INSTALLED:
statusMesssage = "<color=red>APK not installed. Press build and deploy to install the transition APK.</color>";
break;
case ApkStatus.DEVICE_NOT_CONNECTED:
statusMesssage = "<color=red>Device not connected via ADB. Please connect device and allow debugging.</color>";
break;
case ApkStatus.UNKNOWN:
default:
statusMesssage = "<color=red>Failed to get APK status!</color>";
break;
}
GUILayout.Label(statusMesssage, statusStyle, GUILayout.ExpandWidth(true));
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
{
GUIContent btnTxt = new GUIContent("Build and Deploy App");
var rt = GUILayoutUtility.GetRect(btnTxt, GUI.skin.button, GUILayout.Width(200));
if (GUI.Button(rt, btnTxt))
{
action = GuiAction.BuildAndDeployApp;
}
}
EditorGUILayout.EndHorizontal();
GUILayout.Space(10.0f);
GUILayout.Label("Utilities", EditorStyles.boldLabel);
showBundleManagement = EditorGUILayout.Foldout(showBundleManagement, "Bundle Management");
if (showBundleManagement)
{
EditorGUILayout.BeginHorizontal();
{
GUIContent clearDeviceBundlesTxt = new GUIContent("Delete Device Bundles");
var clearDeviceBundlesBtnRt = GUILayoutUtility.GetRect(clearDeviceBundlesTxt, GUI.skin.button, GUILayout.ExpandWidth(true));
if (GUI.Button(clearDeviceBundlesBtnRt, clearDeviceBundlesTxt))
{
action = GuiAction.ClearDeviceBundles;
}
GUIContent clearLocalBundlesTxt = new GUIContent("Delete Local Bundles");
var clearLocalBundlesBtnRt = GUILayoutUtility.GetRect(clearLocalBundlesTxt, GUI.skin.button, GUILayout.ExpandWidth(true));
if (GUI.Button(clearLocalBundlesBtnRt, clearLocalBundlesTxt))
{
action = GuiAction.ClearLocalBundles;
}
}
EditorGUILayout.EndHorizontal();
}
showOther = EditorGUILayout.Foldout(showOther, "Other");
if (showOther)
{
EditorGUILayout.BeginHorizontal();
{
GUIContent useOptionalTransitionPackageLabel = new GUIContent("Use optional APK package name [?]",
"This allows both full build APK and transition APK to be installed on device. However, platform services like Entitlement check may fail.");
EditorGUILayout.LabelField(useOptionalTransitionPackageLabel, GUILayout.ExpandWidth(false));
bool newToggleValue = EditorGUILayout.Toggle(useOptionalTransitionApkPackage);
if (newToggleValue != useOptionalTransitionApkPackage)
{
useOptionalTransitionApkPackage = newToggleValue;
// Update transition APK status after changing package name option
CheckForTransitionAPK();
}
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
{
GUIContent launchBtnTxt = new GUIContent("Launch App");
var launchBtnRt = GUILayoutUtility.GetRect(launchBtnTxt, GUI.skin.button, GUILayout.ExpandWidth(true));
if (GUI.Button(launchBtnRt, launchBtnTxt))
{
action = GuiAction.LaunchApp;
}
var buildSettingBtnRt = GUILayoutUtility.GetRect(buildSettingsBtnTxt, GUI.skin.button, GUILayout.ExpandWidth(true));
if (GUI.Button(buildSettingBtnRt, buildSettingsBtnTxt))
{
action = GuiAction.OpenBuildSettingsWindow;
}
GUIContent uninstallTxt = new GUIContent("Uninstall APK");
var uninstallBtnRt = GUILayoutUtility.GetRect(uninstallTxt, GUI.skin.button, GUILayout.ExpandWidth(true));
if (GUI.Button(uninstallBtnRt, uninstallTxt))
{
action = GuiAction.UninstallApk;
}
GUIContent clearLogTxt = new GUIContent("Clear Log");
var clearLogBtnRt = GUILayoutUtility.GetRect(clearLogTxt, GUI.skin.button, GUILayout.ExpandWidth(true));
if (GUI.Button(clearLogBtnRt, clearLogTxt))
{
action = GuiAction.ClearLog;
}
}
EditorGUILayout.EndHorizontal();
}
GUILayout.Space(10.0f);
GUILayout.Label("Log", EditorStyles.boldLabel);
if (!string.IsNullOrEmpty(toolLog))
{
debugLogScroll = EditorGUILayout.BeginScrollView(debugLogScroll, GUILayout.ExpandHeight(true));
EditorGUILayout.SelectableLabel(toolLog, logBoxStyle, GUILayout.Height(logBoxSize.y + logBoxSpacing));
EditorGUILayout.EndScrollView();
}
}
private void Update()
{
switch (action)
{
case GuiAction.OpenBuildSettingsWindow:
OpenBuildSettingsWindow();
break;
case GuiAction.BuildAndDeployScenes:
// Check the latest transition apk status
CheckForTransitionAPK();
// Show a dialog to prompt for building and deploying transition APK
if (currentApkStatus != ApkStatus.OK &&
EditorUtility.DisplayDialog("Build and Deploy OVR Transition APK?",
"OVR Transition APK status not ready, it is required to load your scene bundle for quick preview.",
"Yes",
"No"))
{
PrintLog("Building OVR Transition APK");
OVRBundleManager.BuildDeployTransitionAPK(useOptionalTransitionApkPackage);
CheckForTransitionAPK();
}
for (int i = 0; i < buildableScenes.Count; i++)
{
buildableScenes[i].buildStatus = SceneBundleStatus.QUEUED;
}
OVRBundleManager.BuildDeployScenes(buildableScenes, forceRestart);
break;
case GuiAction.BuildAndDeployApp:
OVRBundleManager.BuildDeployTransitionAPK(useOptionalTransitionApkPackage);
CheckForTransitionAPK();
break;
case GuiAction.ClearDeviceBundles:
OVRBundleManager.DeleteRemoteAssetBundles();
break;
case GuiAction.ClearLocalBundles:
OVRBundleManager.DeleteLocalAssetBundles();
break;
case GuiAction.LaunchApp:
OVRBundleManager.LaunchApplication();
break;
case GuiAction.UninstallApk:
OVRBundleManager.UninstallAPK();
CheckForTransitionAPK();
break;
case GuiAction.ClearLog:
PrintLog("", true);
break;
default:
break;
}
action = GuiAction.None;
}
private static void OpenBuildSettingsWindow()
{
EditorWindow.GetWindow(System.Type.GetType("UnityEditor.BuildPlayerWindow,UnityEditor"));
}
public static void UpdateSceneBuildStatus(SceneBundleStatus status, int index = -1)
{
if (index >= 0 && index < buildableScenes.Count)
{
buildableScenes[index].buildStatus = status;
}
else
{
// Update status for all scenes
for (int i = 0; i < buildableScenes.Count; i++)
{
buildableScenes[i].buildStatus = status;
}
}
}
private static void GetScenesFromBuildSettings()
{
invalidBuildableScene = false;
buildableScenes = new List<EditorSceneInfo>();
for (int i = 0; i < EditorBuildSettings.scenes.Length; i++)
{
EditorBuildSettingsScene scene = EditorBuildSettings.scenes[i];
if (scene.enabled)
{
if (Path.GetFileNameWithoutExtension(scene.path) != "OVRTransitionScene")
{
EditorSceneInfo sceneInfo = new EditorSceneInfo(scene.path, Path.GetFileNameWithoutExtension(scene.path));
buildableScenes.Add(sceneInfo);
}
else
{
buildableScenes = null;
invalidBuildableScene = true;
return;
}
}
}
}
private static void CheckForTransitionAPK()
{
OVRADBTool adbTool = new OVRADBTool(OVRConfig.Instance.GetAndroidSDKPath());
if (adbTool.isReady)
{
string matchedPackageList, error;
var transitionPackageName = PlayerSettings.GetApplicationIdentifier(BuildTargetGroup.Android);
if (useOptionalTransitionApkPackage)
{
transitionPackageName += ".transition";
}
string[] packageCheckCommand = new string[] { "-d shell pm list package", transitionPackageName };
if (adbTool.RunCommand(packageCheckCommand, null, out matchedPackageList, out error) == 0)
{
if (string.IsNullOrEmpty(matchedPackageList))
{
currentApkStatus = ApkStatus.NOT_INSTALLED;
}
else
{
// adb "list package" command returns all package names that contains the given query package name
// Need to check if the transition package name is matched exactly
if (matchedPackageList.Contains("package:" + transitionPackageName + "\r\n"))
{
if (useOptionalTransitionApkPackage)
{
// If optional package name is used, it is deterministic that the transition apk is installed
currentApkStatus = ApkStatus.OK;
}
else
{
// get package info to check for TRANSITION_APK_VERSION_NAME
string[] dumpPackageInfoCommand = new string[] { "-d shell dumpsys package", transitionPackageName };
string packageInfo;
if (adbTool.RunCommand(dumpPackageInfoCommand, null, out packageInfo, out error) == 0 &&
!string.IsNullOrEmpty(packageInfo) &&
packageInfo.Contains(OVRBundleManager.TRANSITION_APK_VERSION_NAME))
{
// Matched package name found, and the package info contains TRANSITION_APK_VERSION_NAME
currentApkStatus = ApkStatus.OK;
}
else
{
currentApkStatus = ApkStatus.NOT_INSTALLED;
}
}
}
else
{
// No matached package name returned
currentApkStatus = ApkStatus.NOT_INSTALLED;
}
}
}
else if (error.Contains("no devices found"))
{
currentApkStatus = ApkStatus.DEVICE_NOT_CONNECTED;
}
else
{
currentApkStatus = ApkStatus.UNKNOWN;
}
}
}
public static void PrintLog(string message, bool clear = false)
{
if (clear)
{
toolLog = message;
}
else
{
toolLog += message + "\n";
}
GUIContent logContent = new GUIContent(toolLog);
logBoxSize = logBoxStyle.CalcSize(logContent);
debugLogScroll.y = logBoxSize.y + logBoxSpacing;
}
public static void PrintError(string error = "")
{
if(!string.IsNullOrEmpty(error))
{
toolLog += "<color=red>Failed!\n</color>" + error + "\n";
}
else
{
toolLog += "<color=red>Failed! Check Unity log for more details.\n</color>";
}
}
public static void PrintWarning(string warning)
{
toolLog += "<color=yellow>Warning!\n" + warning + "</color>\n";
}
public static void PrintSuccess()
{
toolLog += "<color=green>Success!</color>\n";
}
public static string GetEnumDescription(Enum eEnum)
{
Type enumType = eEnum.GetType();
MemberInfo[] memberInfo = enumType.GetMember(eEnum.ToString());
if (memberInfo != null && memberInfo.Length > 0)
{
var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
if (attrs != null && attrs.Length > 0)
{
return ((DescriptionAttribute)attrs[0]).Description;
}
}
return eEnum.ToString();
}
public static bool GetUseOptionalTransitionApkPackage()
{
return useOptionalTransitionApkPackage;
}
}
#endif