| 1 | using System; |
| 2 | using System.Collections; |
| 3 | using System.Collections.Generic; |
| 4 | using System.Diagnostics; |
| 5 | using System.IO; |
| 6 | using System.Text.RegularExpressions; |
| 7 | using System.Threading; |
| 8 | using UnityEditor; |
| 9 | using UnityEngine; |
| 10 | using UnityEngine.Networking; |
| 11 | |
| 12 | namespace Assets.Oculus.VR.Editor |
| 13 | { |
| 14 | public class OVRPlatformTool : EditorWindow |
| 15 | { |
| 16 | public enum TargetPlatform |
| 17 | { |
| 18 | Rift, |
| 19 | OculusGoGearVR, |
| 20 | Quest, |
| 21 | None, |
| 22 | }; |
| 23 | |
| 24 | const string urlPlatformUtil = |
| 25 | "https://www.oculus.com/download_app/?id=1076686279105243"; |
| 26 | |
| 27 | static private Process ovrPlatUtilProcess; |
| 28 | Vector2 commandMenuScroll; |
| 29 | Vector2 debugLogScroll; |
| 30 | |
| 31 | static public string log; |
| 32 | |
| 33 | private static bool activeProcess = false; |
| 34 | private static bool ranSelfUpdate = false; |
| 35 | private static int retryCount = 0; |
| 36 | |
| 37 | private const float buttonPadding = 5.0f; |
| 38 | |
| 39 | private bool showOptionalCommands = false; |
| 40 | private bool show2DCommands = false; |
| 41 | private bool showExpansionFileCommands = false; |
| 42 | private bool showRedistCommands = false; |
| 43 | |
| 44 | private const float INDENT_SPACING = 15f; |
| 45 | private const float SINGLE_LINE_SPACING = 18f; |
| 46 | private const float ASSET_CONFIG_BACKGROUND_PADDING = 10f; |
| 47 | private const float DEFAULT_LABEL_WIDTH = 180f; |
| 48 | private const int MAX_DOWNLOAD_RETRY_COUNT = 2; |
| 49 | |
| 50 | private static GUIStyle boldFoldoutStyle; |
| 51 | |
| 52 | string[] platformOptions = new string[] |
| 53 | { |
| 54 | "Oculus Rift", |
| 55 | "Oculus Go | Gear VR", |
| 56 | "Oculus Quest" |
| 57 | }; |
| 58 | |
| 59 | string[] gamepadOptions = new string[] |
| 60 | { |
| 61 | "Off", |
| 62 | "Twinstick", |
| 63 | "Right D Pad", |
| 64 | "Left D Pad" |
| 65 | }; |
| 66 | |
| 67 | [MenuItem("Oculus/Tools/Oculus Platform Tool")] |
| 68 | static void Init() |
| 69 | { |
| 70 | OVRPlatformTool.log = string.Empty; |
| 71 | // Get existing open window or if none, make a new one: |
| 72 | EditorWindow.GetWindow(typeof(OVRPlatformTool)); |
| 73 | |
| 74 | // Populate initial target platform value based on OVRDeviceSelector |
| 75 | #if UNITY_ANDROID |
| 76 | if (OVRDeviceSelector.isTargetDeviceQuest) |
| 77 | { |
| 78 | OVRPlatformToolSettings.TargetPlatform = TargetPlatform.Quest; |
| 79 | } |
| 80 | else |
| 81 | { |
| 82 | OVRPlatformToolSettings.TargetPlatform = TargetPlatform.OculusGoGearVR; |
| 83 | } |
| 84 | #else |
| 85 | OVRPlatformToolSettings.TargetPlatform = TargetPlatform.Rift; |
| 86 | #endif |
| 87 | EditorUtility.SetDirty(OVRPlatformToolSettings.Instance); |
| 88 | |
| 89 | // Load redist packages by calling list-redists in the CLI |
| 90 | string dataPath = Application.dataPath; |
| 91 | var thread = new Thread(delegate () { |
| 92 | retryCount = 0; |
| 93 | LoadRedistPackages(dataPath); |
| 94 | }); |
| 95 | thread.Start(); |
| 96 | |
| 97 | OVRPlugin.SendEvent("oculus_platform_tool", "show_window"); |
| 98 | } |
| 99 | |
| 100 | void OnGUI() |
| 101 | { |
| 102 | if (boldFoldoutStyle == null) |
| 103 | { |
| 104 | boldFoldoutStyle = new GUIStyle(EditorStyles.foldout); |
| 105 | boldFoldoutStyle.fontStyle = FontStyle.Bold; |
| 106 | } |
| 107 | |
| 108 | EditorGUIUtility.labelWidth = DEFAULT_LABEL_WIDTH; |
| 109 | |
| 110 | GUILayout.Label("OVR Platform Tool", EditorStyles.boldLabel); |
| 111 | this.titleContent.text = "OVR Platform Tool"; |
| 112 | |
| 113 | GUIContent TargetPlatformLabel = new GUIContent("Target Oculus Platform"); |
| 114 | OVRPlatformToolSettings.TargetPlatform = (TargetPlatform)MakePopup(TargetPlatformLabel, (int)OVRPlatformToolSettings.TargetPlatform, platformOptions); |
| 115 | SetOVRProjectConfig(OVRPlatformToolSettings.TargetPlatform); |
| 116 | SetDirtyOnGUIChange(); |
| 117 | |
| 118 | commandMenuScroll = EditorGUILayout.BeginScrollView(commandMenuScroll, GUILayout.Height(Screen.height / 2)); |
| 119 | { |
| 120 | // Add the UI Form |
| 121 | EditorGUI.BeginChangeCheck(); |
| 122 | GUILayout.Space(15.0f); |
| 123 | |
| 124 | // App ID |
| 125 | GUIContent AppIDLabel = new GUIContent("Oculus Application ID [?]: ", |
| 126 | "This AppID will be used when uploading the build."); |
| 127 | OVRPlatformToolSettings.AppID = MakeTextBox(AppIDLabel, OVRPlatformToolSettings.AppID); |
| 128 | |
| 129 | // App Token |
| 130 | GUIContent AppTokenLabel = new GUIContent("Oculus App Token [?]: ", |
| 131 | "You can get your app token from your app's Oculus API Dashboard."); |
| 132 | OVRPlatformToolSettings.AppToken = MakePasswordBox(AppTokenLabel, OVRPlatformToolSettings.AppToken); |
| 133 | |
| 134 | // Release Channel |
| 135 | GUIContent ReleaseChannelLabel = new GUIContent("Release Channel [?]: ", |
| 136 | "Specify the releaes channel of the new build, you can reassign to other channels after upload."); |
| 137 | OVRPlatformToolSettings.ReleaseChannel = MakeTextBox(ReleaseChannelLabel, OVRPlatformToolSettings.ReleaseChannel); |
| 138 | |
| 139 | // Releaes Note |
| 140 | GUIContent ReleaseNoteLabel = new GUIContent("Release Note: "); |
| 141 | OVRPlatformToolSettings.ReleaseNote = MakeTextBox(ReleaseNoteLabel, OVRPlatformToolSettings.ReleaseNote); |
| 142 | |
| 143 | // Platform specific fields |
| 144 | if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.Rift) |
| 145 | { |
| 146 | GUIContent BuildDirLabel = new GUIContent("Rift Build Directory [?]: ", |
| 147 | "The full path to the directory containing your Rift build files."); |
| 148 | OVRPlatformToolSettings.RiftBuildDirectory = MakeFileDirectoryField(BuildDirLabel, OVRPlatformToolSettings.RiftBuildDirectory, |
| 149 | "Choose Rifle Build Directory"); |
| 150 | |
| 151 | GUIContent BuildVersionLabel = new GUIContent("Build Version [?]: ", |
| 152 | "The version number shown to users."); |
| 153 | OVRPlatformToolSettings.RiftBuildVersion = MakeTextBox(BuildVersionLabel, OVRPlatformToolSettings.RiftBuildVersion); |
| 154 | |
| 155 | GUIContent LaunchFileLabel = new GUIContent("Launch File Path [?]: ", |
| 156 | "The full path to the executable that launches your app."); |
| 157 | OVRPlatformToolSettings.RiftLaunchFile = MakeFileDirectoryField(LaunchFileLabel, OVRPlatformToolSettings.RiftLaunchFile, |
| 158 | "Choose Launch File", true, "exe"); |
| 159 | } |
| 160 | else |
| 161 | { |
| 162 | GUIContent ApkPathLabel = new GUIContent("Build APK File Path [?]: ", |
| 163 | "The full path to the APK file."); |
| 164 | OVRPlatformToolSettings.ApkBuildPath = MakeFileDirectoryField(ApkPathLabel, OVRPlatformToolSettings.ApkBuildPath, |
| 165 | "Choose APK File", true, "apk"); |
| 166 | |
| 167 | if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.OculusGoGearVR) |
| 168 | { |
| 169 | // Go and Gear VR specific fields |
| 170 | } |
| 171 | else if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.Quest) |
| 172 | { |
| 173 | // Quest specific fields |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | showOptionalCommands = EditorGUILayout.Foldout(showOptionalCommands, "Optional Commands", boldFoldoutStyle); |
| 178 | if (showOptionalCommands) |
| 179 | { |
| 180 | IncrementIndent(); |
| 181 | |
| 182 | if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.Rift) |
| 183 | { |
| 184 | // Launch Parameters |
| 185 | GUIContent LaunchParamLabel = new GUIContent("Launch Parameters [?]: ", |
| 186 | "Specifies any arguments passed to the launcher."); |
| 187 | OVRPlatformToolSettings.RiftLaunchParams = MakeTextBox(LaunchParamLabel, OVRPlatformToolSettings.RiftLaunchParams); |
| 188 | |
| 189 | GUIContent FirewallExceptionLabel = new GUIContent("Firewall Exception [?]: ", |
| 190 | "Specifies if a Windows Firewall exception is required."); |
| 191 | OVRPlatformToolSettings.RiftFirewallException = MakeToggleBox(FirewallExceptionLabel, OVRPlatformToolSettings.RiftFirewallException); |
| 192 | |
| 193 | GUIContent GamepadEmulationLabel = new GUIContent("Gamepad Emulation [?]: ", |
| 194 | "Specifies the type of gamepad emulation used by the Oculus Touch controllers."); |
| 195 | OVRPlatformToolSettings.RiftGamepadEmulation = (OVRPlatformToolSettings.GamepadType)MakePopup(GamepadEmulationLabel, (int)OVRPlatformToolSettings.RiftGamepadEmulation, gamepadOptions); |
| 196 | |
| 197 | show2DCommands = EditorGUILayout.Foldout(show2DCommands, "2D", boldFoldoutStyle); |
| 198 | if (show2DCommands) |
| 199 | { |
| 200 | IncrementIndent(); |
| 201 | |
| 202 | // 2D Launch File |
| 203 | GUIContent LaunchFile2DLabel = new GUIContent("2D Launch File [?]: ", |
| 204 | "The full path to the executable that launches your app in 2D mode."); |
| 205 | OVRPlatformToolSettings.Rift2DLaunchFile = MakeFileDirectoryField(LaunchFile2DLabel, OVRPlatformToolSettings.Rift2DLaunchFile, |
| 206 | "Choose 2D Launch File", true, "exe"); |
| 207 | |
| 208 | // 2D Launch Parameters |
| 209 | GUIContent LaunchParam2DLabel = new GUIContent("2D Launch Parameters [?]: ", |
| 210 | "Specifies any arguments passed to the launcher in 2D mode."); |
| 211 | OVRPlatformToolSettings.Rift2DLaunchParams = MakeTextBox(LaunchParam2DLabel, OVRPlatformToolSettings.Rift2DLaunchParams); |
| 212 | |
| 213 | DecrementIndent(); |
| 214 | } |
| 215 | |
| 216 | showRedistCommands = EditorGUILayout.Foldout(showRedistCommands, "Redistributable Packages", boldFoldoutStyle); |
| 217 | if (showRedistCommands) |
| 218 | { |
| 219 | IncrementIndent(); |
| 220 | |
| 221 | for (int i = 0; i < OVRPlatformToolSettings.RiftRedistPackages.Count; i++) |
| 222 | { |
| 223 | GUIContent RedistPackageLabel = new GUIContent(OVRPlatformToolSettings.RiftRedistPackages[i].name); |
| 224 | OVRPlatformToolSettings.RiftRedistPackages[i].include = MakeToggleBox(RedistPackageLabel, OVRPlatformToolSettings.RiftRedistPackages[i].include); |
| 225 | } |
| 226 | |
| 227 | DecrementIndent(); |
| 228 | } |
| 229 | |
| 230 | showExpansionFileCommands = EditorGUILayout.Foldout(showExpansionFileCommands, "Expansion Files", boldFoldoutStyle); |
| 231 | if (showExpansionFileCommands) |
| 232 | { |
| 233 | IncrementIndent(); |
| 234 | |
| 235 | // Language Pack Directory |
| 236 | GUIContent LanguagePackLabel = new GUIContent("Language Pack Directory [?]: ", |
| 237 | "The full path to the directory containing the language packs"); |
| 238 | OVRPlatformToolSettings.LanguagePackDirectory = MakeFileDirectoryField(LanguagePackLabel, OVRPlatformToolSettings.LanguagePackDirectory, |
| 239 | "Choose Language Pack Directory"); |
| 240 | } |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.OculusGoGearVR) |
| 245 | { |
| 246 | // Go and Gear VR specific optional fields |
| 247 | } |
| 248 | else if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.Quest) |
| 249 | { |
| 250 | // Quest specific optional fields |
| 251 | } |
| 252 | |
| 253 | showExpansionFileCommands = EditorGUILayout.Foldout(showExpansionFileCommands, "Expansion Files", boldFoldoutStyle); |
| 254 | if (showExpansionFileCommands) |
| 255 | { |
| 256 | IncrementIndent(); |
| 257 | |
| 258 | // OBB File Path |
| 259 | GUIContent ObbPathLabel = new GUIContent("OBB File Path [?]: ", |
| 260 | "The full path to the OBB file."); |
| 261 | OVRPlatformToolSettings.ObbFilePath = MakeFileDirectoryField(ObbPathLabel, OVRPlatformToolSettings.ObbFilePath, |
| 262 | "Choose OBB File", true, "obb"); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | if (showExpansionFileCommands) |
| 267 | { |
| 268 | // Assets Directory |
| 269 | GUIContent AssetsDirLabel = new GUIContent("Assets Directory [?]: ", |
| 270 | "The full path to the directory with DLCs for this build."); |
| 271 | string assetsDirectory = MakeFileDirectoryField(AssetsDirLabel, OVRPlatformToolSettings.AssetsDirectory, |
| 272 | "Choose Assets Directory"); |
| 273 | |
| 274 | if (assetsDirectory != OVRPlatformToolSettings.AssetsDirectory) |
| 275 | { |
| 276 | OVRPlatformToolSettings.AssetsDirectory = assetsDirectory; |
| 277 | OVRPlatformToolSettings.AssetConfigs.Clear(); |
| 278 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.AssetsDirectory)) |
| 279 | { |
| 280 | DirectoryInfo dirInfo = new DirectoryInfo(OVRPlatformToolSettings.AssetsDirectory); |
| 281 | FileInfo[] assetFiles = dirInfo.GetFiles(); |
| 282 | foreach (FileInfo f in assetFiles) |
| 283 | { |
| 284 | OVRPlatformToolSettings.AssetConfigs.Add(new AssetConfig(f.Name)); |
| 285 | } |
| 286 | } |
| 287 | EditorUtility.SetDirty(OVRPlatformToolSettings.Instance); |
| 288 | } |
| 289 | |
| 290 | // Display bordered asset configuration list |
| 291 | GUILayout.Space(3f); |
| 292 | Rect rect = GUILayoutUtility.GetRect(0, GetAssetConfigElementHeight() + (ASSET_CONFIG_BACKGROUND_PADDING * 2), |
| 293 | GUILayout.ExpandWidth(true)); |
| 294 | rect.x += (EditorGUI.indentLevel * INDENT_SPACING + 5); |
| 295 | rect.width -= (EditorGUI.indentLevel * INDENT_SPACING + 10); |
| 296 | DrawAssetConfigList(rect); |
| 297 | |
| 298 | DecrementIndent(); |
| 299 | } |
| 300 | |
| 301 | EditorGUI.indentLevel--; |
| 302 | } |
| 303 | |
| 304 | if (EditorGUI.EndChangeCheck()) |
| 305 | { |
| 306 | EditorUtility.SetDirty(OVRPlatformToolSettings.Instance); |
| 307 | } |
| 308 | } |
| 309 | EditorGUILayout.EndScrollView(); |
| 310 | |
| 311 | GUILayout.Space(SINGLE_LINE_SPACING); |
| 312 | |
| 313 | GUILayout.FlexibleSpace(); |
| 314 | |
| 315 | // Run OVR Lint Option |
| 316 | EditorGUIUtility.labelWidth = DEFAULT_LABEL_WIDTH; |
| 317 | GUIContent RunOvrLintLabel = new GUIContent("Run OVR Lint (Recommended) [?]: ", |
| 318 | "Run OVR Lint tool to ensure project is optimized for performance and meets Oculus packaging requirement for publishing."); |
| 319 | OVRPlatformToolSettings.RunOvrLint = MakeToggleBox(RunOvrLintLabel, OVRPlatformToolSettings.RunOvrLint); |
| 320 | |
| 321 | // Add an Upload button |
| 322 | GUI.enabled = !activeProcess; |
| 323 | GUIContent btnTxt = new GUIContent("Upload"); |
| 324 | var rt = GUILayoutUtility.GetRect(btnTxt, GUI.skin.button, GUILayout.ExpandWidth(false)); |
| 325 | var btnYPos = rt.center.y; |
| 326 | rt.center = new Vector2(EditorGUIUtility.currentViewWidth / 2 - rt.width / 2 - buttonPadding, btnYPos); |
| 327 | if (GUI.Button(rt, btnTxt, GUI.skin.button)) |
| 328 | { |
| 329 | OVRPlugin.SendEvent("oculus_platform_tool", "upload"); |
| 330 | OVRPlatformTool.log = string.Empty; |
| 331 | OnUpload(OVRPlatformToolSettings.TargetPlatform); |
| 332 | } |
| 333 | |
| 334 | // Add a cancel button |
| 335 | GUI.enabled = activeProcess; |
| 336 | btnTxt = new GUIContent("Cancel"); |
| 337 | rt = GUILayoutUtility.GetRect(btnTxt, GUI.skin.button, GUILayout.ExpandWidth(false)); |
| 338 | rt.center = new Vector2(EditorGUIUtility.currentViewWidth / 2 + rt.width / 2 + buttonPadding, btnYPos); |
| 339 | if (GUI.Button(rt, btnTxt, GUI.skin.button)) |
| 340 | { |
| 341 | if (EditorUtility.DisplayDialog("Cancel Upload Process", "Are you sure you want to cancel the upload process?", "Yes", "No")) |
| 342 | { |
| 343 | if (ovrPlatUtilProcess != null) |
| 344 | { |
| 345 | ovrPlatUtilProcess.Kill(); |
| 346 | OVRPlatformTool.log += "Upload process was canceled\n"; |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | |
| 351 | GUI.enabled = true; |
| 352 | GUILayout.FlexibleSpace(); |
| 353 | |
| 354 | GUILayout.Space(SINGLE_LINE_SPACING); |
| 355 | |
| 356 | debugLogScroll = EditorGUILayout.BeginScrollView(debugLogScroll); |
| 357 | GUIStyle logBoxStyle = new GUIStyle(); |
| 358 | logBoxStyle.margin.left = 5; |
| 359 | logBoxStyle.wordWrap = true; |
| 360 | logBoxStyle.normal.textColor = logBoxStyle.focused.textColor = EditorStyles.label.normal.textColor; |
| 361 | EditorGUILayout.SelectableLabel(OVRPlatformTool.log, logBoxStyle, GUILayout.Height(position.height - 30)); |
| 362 | EditorGUILayout.EndScrollView(); |
| 363 | } |
| 364 | |
| 365 | private void SetOVRProjectConfig(TargetPlatform targetPlatform) |
| 366 | { |
| 367 | #if UNITY_ANDROID |
| 368 | |
| 369 | var targetDeviceTypes = new List<OVRProjectConfig.DeviceType>(); |
| 370 | |
| 371 | if (targetPlatform == TargetPlatform.Quest && !OVRDeviceSelector.isTargetDeviceQuest) |
| 372 | { |
| 373 | targetDeviceTypes.Add(OVRProjectConfig.DeviceType.Quest); |
| 374 | } |
| 375 | else if (targetPlatform == TargetPlatform.OculusGoGearVR && !OVRDeviceSelector.isTargetDeviceGearVrOrGo) |
| 376 | { |
| 377 | targetDeviceTypes.Add(OVRProjectConfig.DeviceType.GearVrOrGo); |
| 378 | } |
| 379 | |
| 380 | if (targetDeviceTypes.Count != 0) |
| 381 | { |
| 382 | OVRProjectConfig projectConfig = OVRProjectConfig.GetProjectConfig(); |
| 383 | projectConfig.targetDeviceTypes = targetDeviceTypes; |
| 384 | OVRProjectConfig.CommitProjectConfig(projectConfig); |
| 385 | } |
| 386 | #endif |
| 387 | } |
| 388 | |
| 389 | private void IncrementIndent() |
| 390 | { |
| 391 | EditorGUI.indentLevel++; |
| 392 | EditorGUIUtility.labelWidth = DEFAULT_LABEL_WIDTH - (EditorGUI.indentLevel * INDENT_SPACING); |
| 393 | } |
| 394 | |
| 395 | private void DecrementIndent() |
| 396 | { |
| 397 | EditorGUI.indentLevel--; |
| 398 | EditorGUIUtility.labelWidth = DEFAULT_LABEL_WIDTH - (EditorGUI.indentLevel * INDENT_SPACING); |
| 399 | } |
| 400 | |
| 401 | private void OnUpload(TargetPlatform targetPlatform) |
| 402 | { |
| 403 | OVRPlatformTool.log = string.Empty; |
| 404 | SetDirtyOnGUIChange(); |
| 405 | var lintCount = 0; |
| 406 | if (OVRPlatformToolSettings.RunOvrLint) |
| 407 | { |
| 408 | lintCount = OVRLint.RunCheck(); |
| 409 | } |
| 410 | if (lintCount != 0) |
| 411 | { |
| 412 | OVRPlatformTool.log += lintCount.ToString() + " lint suggestions are found. \n" + |
| 413 | "Please run Oculus\\Tools\\OVR Performance Lint Tool to review and fix lint errors. \n" + |
| 414 | "You can uncheck Run OVR Lint to bypass lint errors. \n"; |
| 415 | OVRPlugin.SendEvent("oculus_platform_tool_lint", lintCount.ToString()); |
| 416 | } |
| 417 | else |
| 418 | { |
| 419 | // Continue uploading process |
| 420 | ExecuteCommand(targetPlatform); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | static void ExecuteCommand(TargetPlatform targetPlatform) |
| 425 | { |
| 426 | string dataPath = Application.dataPath; |
| 427 | |
| 428 | // If we already have a copy of the platform util, check if it needs to be updated |
| 429 | if (!ranSelfUpdate && File.Exists(dataPath + "/Oculus/VR/Editor/Tools/ovr-platform-util.exe")) |
| 430 | { |
| 431 | ranSelfUpdate = true; |
| 432 | activeProcess = true; |
| 433 | var updateThread = new Thread(delegate () { |
| 434 | retryCount = 0; |
| 435 | CheckForUpdate(dataPath); |
| 436 | }); |
| 437 | updateThread.Start(); |
| 438 | } |
| 439 | |
| 440 | var thread = new Thread(delegate () { |
| 441 | // Wait for update process to finish before starting upload process |
| 442 | while (activeProcess) |
| 443 | { |
| 444 | Thread.Sleep(100); |
| 445 | } |
| 446 | retryCount = 0; |
| 447 | Command(targetPlatform, dataPath); |
| 448 | }); |
| 449 | thread.Start(); |
| 450 | } |
| 451 | |
| 452 | private static string CheckForPlatformUtil(string dataPath) |
| 453 | { |
| 454 | string toolDataPath = dataPath + "/Oculus/VR/Editor/Tools"; |
| 455 | if (!Directory.Exists(toolDataPath)) |
| 456 | { |
| 457 | Directory.CreateDirectory(toolDataPath); |
| 458 | } |
| 459 | |
| 460 | string platformUtil = toolDataPath + "/ovr-platform-util.exe"; |
| 461 | if (!System.IO.File.Exists(platformUtil)) |
| 462 | { |
| 463 | OVRPlugin.SendEvent("oculus_platform_tool", "provision_util"); |
| 464 | EditorCoroutine downloadCoroutine = EditorCoroutine.Start(ProvisionPlatformUtil(platformUtil)); |
| 465 | while (!downloadCoroutine.GetCompleted()) { } |
| 466 | } |
| 467 | |
| 468 | return platformUtil; |
| 469 | } |
| 470 | |
| 471 | private static void InitializePlatformUtilProcess(string path, string args) |
| 472 | { |
| 473 | ovrPlatUtilProcess = new Process(); |
| 474 | var processInfo = new ProcessStartInfo(path, args); |
| 475 | |
| 476 | processInfo.CreateNoWindow = true; |
| 477 | processInfo.UseShellExecute = false; |
| 478 | processInfo.RedirectStandardError = true; |
| 479 | processInfo.RedirectStandardOutput = true; |
| 480 | |
| 481 | ovrPlatUtilProcess.StartInfo = processInfo; |
| 482 | ovrPlatUtilProcess.EnableRaisingEvents = true; |
| 483 | } |
| 484 | |
| 485 | static void CheckForUpdate(string dataPath) |
| 486 | { |
| 487 | string platformUtilPath = CheckForPlatformUtil(dataPath); |
| 488 | InitializePlatformUtilProcess(platformUtilPath, "self-update"); |
| 489 | |
| 490 | OVRPlatformTool.log += "Checking for update...\n"; |
| 491 | |
| 492 | ovrPlatUtilProcess.Exited += new EventHandler( |
| 493 | (s, e) => |
| 494 | { |
| 495 | if (File.Exists(dataPath + ".ovr-platform-util.exe")) |
| 496 | { |
| 497 | OVRPlatformTool.log += "Cleaning up...\n"; |
| 498 | while (File.Exists(dataPath + ".ovr-platform-util.exe")) { } |
| 499 | OVRPlatformTool.log += "Finished updating platform utility.\n"; |
| 500 | } |
| 501 | activeProcess = false; |
| 502 | } |
| 503 | ); |
| 504 | |
| 505 | ovrPlatUtilProcess.OutputDataReceived += new DataReceivedEventHandler( |
| 506 | (s, e) => |
| 507 | { |
| 508 | if (e.Data.Length != 0 && !e.Data.Contains("\u001b")) |
| 509 | { |
| 510 | OVRPlatformTool.log += e.Data + "\n"; |
| 511 | } |
| 512 | } |
| 513 | ); |
| 514 | |
| 515 | try |
| 516 | { |
| 517 | ovrPlatUtilProcess.Start(); |
| 518 | ovrPlatUtilProcess.BeginOutputReadLine(); |
| 519 | } |
| 520 | catch |
| 521 | { |
| 522 | if (ThrowPlatformUtilStartupError(platformUtilPath)) |
| 523 | { |
| 524 | CheckForUpdate(dataPath); |
| 525 | } |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | static void LoadRedistPackages(string dataPath) |
| 530 | { |
| 531 | // Check / Download the platform util and call list-redists on it |
| 532 | activeProcess = true; |
| 533 | string platformUtilPath = CheckForPlatformUtil(dataPath); |
| 534 | InitializePlatformUtilProcess(platformUtilPath, "list-redists"); |
| 535 | |
| 536 | OVRPlatformTool.log += "Loading redistributable packages...\n"; |
| 537 | |
| 538 | List<RedistPackage> redistPacks = new List<RedistPackage>(); |
| 539 | |
| 540 | ovrPlatUtilProcess.Exited += new EventHandler( |
| 541 | (s, e) => |
| 542 | { |
| 543 | activeProcess = false; |
| 544 | } |
| 545 | ); |
| 546 | |
| 547 | ovrPlatUtilProcess.OutputDataReceived += new DataReceivedEventHandler( |
| 548 | (s, e) => |
| 549 | { |
| 550 | if (e.Data != null && e.Data.Length != 0 && !e.Data.Contains("\u001b") && !e.Data.Contains("ID")) |
| 551 | { |
| 552 | // Get the name / ID pair from the CLI and create a redist package instance |
| 553 | string[] terms = e.Data.Split('|'); |
| 554 | if (terms.Length == 2) |
| 555 | { |
| 556 | RedistPackage redistPack = new RedistPackage(terms[1], terms[0]); |
| 557 | redistPacks.Add(redistPack); |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | ); |
| 562 | |
| 563 | try |
| 564 | { |
| 565 | ovrPlatUtilProcess.Start(); |
| 566 | ovrPlatUtilProcess.BeginOutputReadLine(); |
| 567 | |
| 568 | ovrPlatUtilProcess.WaitForExit(); |
| 569 | |
| 570 | if (redistPacks.Count != OVRPlatformToolSettings.RiftRedistPackages.Count) |
| 571 | { |
| 572 | OVRPlatformTool.log += "Successfully updated redistributable packages.\n"; |
| 573 | OVRPlatformToolSettings.RiftRedistPackages = redistPacks; |
| 574 | } |
| 575 | else |
| 576 | { |
| 577 | OVRPlatformTool.log += "Redistributable packages up to date.\n"; |
| 578 | } |
| 579 | } |
| 580 | catch |
| 581 | { |
| 582 | if (ThrowPlatformUtilStartupError(platformUtilPath)) |
| 583 | { |
| 584 | LoadRedistPackages(dataPath); |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | static void Command(TargetPlatform targetPlatform, string dataPath) |
| 590 | { |
| 591 | string platformUtilPath = CheckForPlatformUtil(dataPath); |
| 592 | |
| 593 | string args; |
| 594 | if (genUploadCommand(targetPlatform, out args)) |
| 595 | { |
| 596 | activeProcess = true; |
| 597 | InitializePlatformUtilProcess(platformUtilPath, args); |
| 598 | |
| 599 | ovrPlatUtilProcess.Exited += new EventHandler( |
| 600 | (s, e) => |
| 601 | { |
| 602 | activeProcess = false; |
| 603 | } |
| 604 | ); |
| 605 | |
| 606 | ovrPlatUtilProcess.OutputDataReceived += new DataReceivedEventHandler( |
| 607 | (s, e) => |
| 608 | { |
| 609 | if (e.Data.Length != 0 && !e.Data.Contains("\u001b")) |
| 610 | { |
| 611 | OVRPlatformTool.log += e.Data + "\n"; |
| 612 | } |
| 613 | } |
| 614 | ); |
| 615 | ovrPlatUtilProcess.ErrorDataReceived += new DataReceivedEventHandler( |
| 616 | (s, e) => |
| 617 | { |
| 618 | OVRPlatformTool.log += e.Data + "\n"; |
| 619 | } |
| 620 | ); |
| 621 | |
| 622 | try |
| 623 | { |
| 624 | ovrPlatUtilProcess.Start(); |
| 625 | ovrPlatUtilProcess.BeginOutputReadLine(); |
| 626 | ovrPlatUtilProcess.BeginErrorReadLine(); |
| 627 | } |
| 628 | catch |
| 629 | { |
| 630 | if (ThrowPlatformUtilStartupError(platformUtilPath)) |
| 631 | { |
| 632 | Command(targetPlatform, dataPath); |
| 633 | } |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | private static bool genUploadCommand(TargetPlatform targetPlatform, out string command) |
| 639 | { |
| 640 | bool success = true; |
| 641 | command = ""; |
| 642 | |
| 643 | switch (targetPlatform) |
| 644 | { |
| 645 | case TargetPlatform.Rift: |
| 646 | command = "upload-rift-build"; |
| 647 | break; |
| 648 | case TargetPlatform.OculusGoGearVR: |
| 649 | command = "upload-mobile-build"; |
| 650 | break; |
| 651 | case TargetPlatform.Quest: |
| 652 | command = "upload-quest-build"; |
| 653 | break; |
| 654 | default: |
| 655 | OVRPlatformTool.log += "ERROR: Invalid target platform selected"; |
| 656 | success = false; |
| 657 | break; |
| 658 | } |
| 659 | |
| 660 | // Add App ID |
| 661 | ValidateTextField(AppIDFieldValidator, OVRPlatformToolSettings.AppID, "App ID", ref success); |
| 662 | command += " --app-id \"" + OVRPlatformToolSettings.AppID + "\""; |
| 663 | |
| 664 | // Add App Token |
| 665 | ValidateTextField(GenericFieldValidator, OVRPlatformToolSettings.AppToken, "App Token", ref success); |
| 666 | command += " --app-secret \"" + OVRPlatformToolSettings.AppToken + "\""; |
| 667 | |
| 668 | // Add Platform specific fields |
| 669 | if (targetPlatform == TargetPlatform.Rift) |
| 670 | { |
| 671 | // Add Rift Build Directory |
| 672 | ValidateTextField(DirectoryValidator, OVRPlatformToolSettings.RiftBuildDirectory, "Rift Build Directory", ref success); |
| 673 | command += " --build-dir \"" + OVRPlatformToolSettings.RiftBuildDirectory + "\""; |
| 674 | |
| 675 | // Add Rift Launch File |
| 676 | ValidateTextField(FileValidator, OVRPlatformToolSettings.RiftLaunchFile, "Rift Launch File Path", ref success); |
| 677 | command += " --launch-file \"" + OVRPlatformToolSettings.RiftLaunchFile + "\""; |
| 678 | |
| 679 | // Add Rift Build Version |
| 680 | ValidateTextField(GenericFieldValidator, OVRPlatformToolSettings.RiftBuildVersion, "Build Version", ref success); |
| 681 | command += " --version \"" + OVRPlatformToolSettings.RiftBuildVersion + "\""; |
| 682 | |
| 683 | // Add Rift Launch Parameters |
| 684 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.RiftLaunchParams)) |
| 685 | { |
| 686 | ValidateTextField(LaunchParameterValidator, OVRPlatformToolSettings.RiftLaunchParams, "Launch Parameters", ref success); |
| 687 | command += " --launch_params \"" + OVRPlatformToolSettings.RiftLaunchParams + "\""; |
| 688 | } |
| 689 | |
| 690 | // Add 2D Launch File |
| 691 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.Rift2DLaunchFile)) |
| 692 | { |
| 693 | ValidateTextField(FileValidator, OVRPlatformToolSettings.Rift2DLaunchFile, "2D Launch File", ref success); |
| 694 | command += " --launch_file_2d \"" + OVRPlatformToolSettings.Rift2DLaunchFile + "\""; |
| 695 | |
| 696 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.Rift2DLaunchParams)) |
| 697 | { |
| 698 | ValidateTextField(LaunchParameterValidator, OVRPlatformToolSettings.Rift2DLaunchParams, "2D Launch Parameters", ref success); |
| 699 | command += " --launch_params_2d \"" + OVRPlatformToolSettings.Rift2DLaunchParams + "\""; |
| 700 | } |
| 701 | } |
| 702 | |
| 703 | // Add Firewall Exception |
| 704 | if (OVRPlatformToolSettings.RiftFirewallException) |
| 705 | { |
| 706 | command += " --firewall_exceptions true"; |
| 707 | } |
| 708 | |
| 709 | // Add Redistributable Packages |
| 710 | List<string> redistCommandIds = new List<string>(); |
| 711 | for (int i = 0; i < OVRPlatformToolSettings.RiftRedistPackages.Count; i++) |
| 712 | { |
| 713 | if (OVRPlatformToolSettings.RiftRedistPackages[i].include) |
| 714 | { |
| 715 | redistCommandIds.Add(OVRPlatformToolSettings.RiftRedistPackages[i].id); |
| 716 | } |
| 717 | } |
| 718 | if (redistCommandIds.Count > 0) |
| 719 | { |
| 720 | command += " --redistributables \"" + string.Join(",", redistCommandIds.ToArray()) + "\""; |
| 721 | } |
| 722 | |
| 723 | // Add Gamepad Emulation |
| 724 | if (OVRPlatformToolSettings.RiftGamepadEmulation > OVRPlatformToolSettings.GamepadType.OFF && |
| 725 | OVRPlatformToolSettings.RiftGamepadEmulation <= OVRPlatformToolSettings.GamepadType.LEFT_D_PAD) |
| 726 | { |
| 727 | command += " --gamepad-emulation "; |
| 728 | switch (OVRPlatformToolSettings.RiftGamepadEmulation) |
| 729 | { |
| 730 | case OVRPlatformToolSettings.GamepadType.TWINSTICK: command += "TWINSTICK"; break; |
| 731 | case OVRPlatformToolSettings.GamepadType.RIGHT_D_PAD: command += "RIGHT_D_PAD"; break; |
| 732 | case OVRPlatformToolSettings.GamepadType.LEFT_D_PAD: command += "LEFT_D_PAD"; break; |
| 733 | default: command += "OFF"; break; |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | // Add Rift Language Pack Directory |
| 738 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.LanguagePackDirectory)) |
| 739 | { |
| 740 | ValidateTextField(DirectoryValidator, OVRPlatformToolSettings.LanguagePackDirectory, "Language Pack Directory", ref success); |
| 741 | command += " --language_packs_dir \"" + OVRPlatformToolSettings.LanguagePackDirectory + "\""; |
| 742 | } |
| 743 | } |
| 744 | else |
| 745 | { |
| 746 | // Add APK Build Path |
| 747 | ValidateTextField(FileValidator, OVRPlatformToolSettings.ApkBuildPath, "APK Build Path", ref success); |
| 748 | command += " --apk \"" + OVRPlatformToolSettings.ApkBuildPath + "\""; |
| 749 | |
| 750 | // Add OBB File Path |
| 751 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.ObbFilePath)) |
| 752 | { |
| 753 | ValidateTextField(FileValidator, OVRPlatformToolSettings.ObbFilePath, "OBB File Path", ref success); |
| 754 | command += " --obb \"" + OVRPlatformToolSettings.ObbFilePath + "\""; |
| 755 | } |
| 756 | |
| 757 | if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.OculusGoGearVR) |
| 758 | { |
| 759 | // Go and Gear VR specific commands |
| 760 | } |
| 761 | else if (OVRPlatformToolSettings.TargetPlatform == TargetPlatform.Quest) |
| 762 | { |
| 763 | // Quest specific commands |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | // Add Assets Directory |
| 768 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.AssetsDirectory)) |
| 769 | { |
| 770 | ValidateTextField(DirectoryValidator, OVRPlatformToolSettings.AssetsDirectory, "Assets Directory", ref success); |
| 771 | command += " --assets-dir \"" + OVRPlatformToolSettings.AssetsDirectory + "\""; |
| 772 | |
| 773 | // Add Asset Configurations |
| 774 | if (OVRPlatformToolSettings.AssetConfigs.Count > 0) |
| 775 | { |
| 776 | List<string> assetConfigs = new List<string>(); |
| 777 | for (int i = 0; i < OVRPlatformToolSettings.AssetConfigs.Count; i++) |
| 778 | { |
| 779 | List<string> configParameters = new List<string>(); |
| 780 | AssetConfig config = OVRPlatformToolSettings.AssetConfigs[i]; |
| 781 | |
| 782 | if (config.required) |
| 783 | { |
| 784 | configParameters.Add("\\\"required\\\":true"); |
| 785 | } |
| 786 | if (config.type > AssetConfig.AssetType.DEFAULT) |
| 787 | { |
| 788 | string typeCommand = "\\\"type\\\":"; |
| 789 | switch (config.type) |
| 790 | { |
| 791 | case AssetConfig.AssetType.LANGUAGE_PACK: |
| 792 | configParameters.Add(typeCommand + "\\\"LANGUAGE_PACK\\\""); |
| 793 | break; |
| 794 | case AssetConfig.AssetType.STORE: |
| 795 | configParameters.Add(typeCommand + "\\\"STORE\\\""); |
| 796 | break; |
| 797 | default: |
| 798 | configParameters.Add(typeCommand + "\\\"DEFAULT\\\""); |
| 799 | break; |
| 800 | } |
| 801 | } |
| 802 | if (!string.IsNullOrEmpty(config.sku)) |
| 803 | { |
| 804 | configParameters.Add("\\\"sku\\\":\\\"" + config.sku + "\\\""); |
| 805 | } |
| 806 | |
| 807 | if (configParameters.Count > 0) |
| 808 | { |
| 809 | string configString = "\\\"" + config.name + "\\\":{" + string.Join(",", configParameters.ToArray()) + "}"; |
| 810 | assetConfigs.Add(configString); |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | if (assetConfigs.Count > 0) |
| 815 | { |
| 816 | command += " --asset_files_config {" + string.Join(",", assetConfigs.ToArray()) + "}"; |
| 817 | } |
| 818 | } |
| 819 | } |
| 820 | |
| 821 | // Add Release Channel |
| 822 | ValidateTextField(GenericFieldValidator, OVRPlatformToolSettings.ReleaseChannel, "Release Channel", ref success); |
| 823 | command += " --channel \"" + OVRPlatformToolSettings.ReleaseChannel + "\""; |
| 824 | |
| 825 | // Add Notes |
| 826 | if (!string.IsNullOrEmpty(OVRPlatformToolSettings.ReleaseNote)) |
| 827 | { |
| 828 | string sanatizedReleaseNote = OVRPlatformToolSettings.ReleaseNote; |
| 829 | sanatizedReleaseNote = sanatizedReleaseNote.Replace("\"", "\"\""); |
| 830 | command += " --notes \"" + sanatizedReleaseNote + "\""; |
| 831 | } |
| 832 | |
| 833 | return success; |
| 834 | } |
| 835 | |
| 836 | // Private delegate for text field validation functions |
| 837 | private delegate TSuccess FieldValidatorDelegate<in TText, TError, out TSuccess>(TText text, ref TError error); |
| 838 | |
| 839 | // Validate the text using a given field validator function. An error message will be printed if validation fails. Success will ONLY be modified to false if validation fails. |
| 840 | static void ValidateTextField(FieldValidatorDelegate<string, string, bool> fieldValidator, string fieldText, string fieldName, ref bool success) |
| 841 | { |
| 842 | string error = ""; |
| 843 | if (!fieldValidator(fieldText, ref error)) |
| 844 | { |
| 845 | OVRPlatformTool.log += "ERROR: Please verify that the " + fieldName + " is correct. "; |
| 846 | OVRPlatformTool.log += string.IsNullOrEmpty(error) ? "\n" : error + "\n"; |
| 847 | success = false; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | // Checks if the text is null or empty |
| 852 | static bool GenericFieldValidator(string fieldText, ref string error) |
| 853 | { |
| 854 | if (string.IsNullOrEmpty(fieldText)) |
| 855 | { |
| 856 | error = "The field is empty."; |
| 857 | return false; |
| 858 | } |
| 859 | return true; |
| 860 | } |
| 861 | |
| 862 | // Checks if the App ID contains only numbers |
| 863 | static bool AppIDFieldValidator(string fieldText, ref string error) |
| 864 | { |
| 865 | if (string.IsNullOrEmpty(fieldText)) |
| 866 | { |
| 867 | error = "The field is empty."; |
| 868 | return false; |
| 869 | } |
| 870 | else if (!Regex.IsMatch(OVRPlatformToolSettings.AppID, "^[0-9]+$")) |
| 871 | { |
| 872 | error = "The field contains invalid characters."; |
| 873 | return false; |
| 874 | } |
| 875 | return true; |
| 876 | } |
| 877 | |
| 878 | // Check that the directory exists |
| 879 | static bool DirectoryValidator(string path, ref string error) |
| 880 | { |
| 881 | if (!Directory.Exists(path)) |
| 882 | { |
| 883 | error = "The directory does not exist."; |
| 884 | return false; |
| 885 | } |
| 886 | return true; |
| 887 | } |
| 888 | |
| 889 | // Check that the file exists |
| 890 | static bool FileValidator(string path, ref string error) |
| 891 | { |
| 892 | if (!File.Exists(path)) |
| 893 | { |
| 894 | error = "The file does not exist."; |
| 895 | return false; |
| 896 | } |
| 897 | return true; |
| 898 | } |
| 899 | |
| 900 | // Check if the launch parameter string contains illegal characters |
| 901 | static bool LaunchParameterValidator(string fieldText, ref string error) |
| 902 | { |
| 903 | if (fieldText.Contains("\"")) |
| 904 | { |
| 905 | error = "The field contains illegal characters."; |
| 906 | return false; |
| 907 | } |
| 908 | return true; |
| 909 | } |
| 910 | |
| 911 | void OnInspectorUpdate() |
| 912 | { |
| 913 | Repaint(); |
| 914 | } |
| 915 | |
| 916 | private static bool ThrowPlatformUtilStartupError(string utilPath) |
| 917 | { |
| 918 | if (retryCount < MAX_DOWNLOAD_RETRY_COUNT) |
| 919 | { |
| 920 | retryCount++; |
| 921 | OVRPlatformTool.log += "There was a problem starting Oculus Platform Util. Restarting provision process...\n"; |
| 922 | File.Delete(utilPath); |
| 923 | return true; |
| 924 | } |
| 925 | else |
| 926 | { |
| 927 | OVRPlatformTool.log += "OVR Platform Tool had a problem with downloading a valid executable after several trys. Please reopen the tool to try again.\n"; |
| 928 | return false; |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | private string MakeTextBox(GUIContent label, string variable) |
| 933 | { |
| 934 | string result = string.Empty; |
| 935 | |
| 936 | EditorGUILayout.BeginHorizontal(); |
| 937 | EditorGUILayout.LabelField(label, GUILayout.ExpandWidth(false)); |
| 938 | result = EditorGUILayout.TextField(variable); |
| 939 | EditorGUILayout.EndHorizontal(); |
| 940 | |
| 941 | return result; |
| 942 | } |
| 943 | |
| 944 | private string MakePasswordBox(GUIContent label, string variable) |
| 945 | { |
| 946 | string result = string.Empty; |
| 947 | |
| 948 | EditorGUILayout.BeginHorizontal(); |
| 949 | EditorGUILayout.LabelField(label, GUILayout.ExpandWidth(false)); |
| 950 | result = EditorGUILayout.PasswordField(variable); |
| 951 | EditorGUILayout.EndHorizontal(); |
| 952 | |
| 953 | return result; |
| 954 | } |
| 955 | |
| 956 | private bool MakeToggleBox(GUIContent label, bool variable) |
| 957 | { |
| 958 | bool result = false; |
| 959 | |
| 960 | EditorGUILayout.BeginHorizontal(); |
| 961 | EditorGUILayout.LabelField(label, GUILayout.ExpandWidth(false)); |
| 962 | result = EditorGUILayout.Toggle(variable); |
| 963 | EditorGUILayout.EndHorizontal(); |
| 964 | |
| 965 | return result; |
| 966 | } |
| 967 | |
| 968 | private int MakePopup(GUIContent label, int variable, string[] options) |
| 969 | { |
| 970 | int result = 0; |
| 971 | |
| 972 | EditorGUILayout.BeginHorizontal(); |
| 973 | EditorGUILayout.LabelField(label, GUILayout.ExpandWidth(false)); |
| 974 | result = EditorGUILayout.Popup(variable, options); |
| 975 | EditorGUILayout.EndHorizontal(); |
| 976 | |
| 977 | return result; |
| 978 | } |
| 979 | |
| 980 | private string MakeFileDirectoryField(GUIContent label, string variable, string title, bool isFile = false, string extension = "") |
| 981 | { |
| 982 | EditorGUILayout.BeginHorizontal(); |
| 983 | EditorGUILayout.LabelField(label, GUILayout.ExpandWidth(false)); |
| 984 | |
| 985 | Rect rect = GUILayoutUtility.GetRect(0, SINGLE_LINE_SPACING, GUILayout.ExpandWidth(true)); |
| 986 | EditorGUI.SelectableLabel(rect, variable); |
| 987 | |
| 988 | // Create X button if there is a valid path in the field |
| 989 | string result = variable; |
| 990 | if (!string.IsNullOrEmpty(variable)) |
| 991 | { |
| 992 | Color defaultColor = GUI.backgroundColor; |
| 993 | GUI.backgroundColor = new Color(.9f, 0.5f, 0.5f); |
| 994 | rect = GUILayoutUtility.GetRect(SINGLE_LINE_SPACING, SINGLE_LINE_SPACING, GUILayout.ExpandWidth(false)); |
| 995 | if (GUI.Button(rect, "X")) |
| 996 | { |
| 997 | result = string.Empty; |
| 998 | } |
| 999 | GUI.backgroundColor = defaultColor; |
| 1000 | } |
| 1001 | |
| 1002 | // Create the Choose button to initiate the file explorer |
| 1003 | rect = GUILayoutUtility.GetRect(75f, SINGLE_LINE_SPACING, GUILayout.ExpandWidth(false)); |
| 1004 | if (GUI.Button(rect, "Choose ...")) |
| 1005 | { |
| 1006 | string newPath = string.Empty; |
| 1007 | string path = string.IsNullOrEmpty(variable) ? Application.dataPath : variable; |
| 1008 | if (isFile) |
| 1009 | { |
| 1010 | newPath = EditorUtility.OpenFilePanel(title, path, extension); |
| 1011 | } |
| 1012 | else |
| 1013 | { |
| 1014 | newPath = EditorUtility.OpenFolderPanel(title, path, string.Empty); |
| 1015 | } |
| 1016 | if (newPath.Length > 0) |
| 1017 | { |
| 1018 | result = newPath; |
| 1019 | } |
| 1020 | } |
| 1021 | GUILayout.Space(5f); |
| 1022 | EditorGUILayout.EndHorizontal(); |
| 1023 | |
| 1024 | // If the path has changed, deselect the selectable field so that it can update. |
| 1025 | if (result != variable) |
| 1026 | { |
| 1027 | GUIUtility.hotControl = 0; |
| 1028 | GUIUtility.keyboardControl = 0; |
| 1029 | } |
| 1030 | |
| 1031 | return result; |
| 1032 | } |
| 1033 | |
| 1034 | private static void SetDirtyOnGUIChange() |
| 1035 | { |
| 1036 | if (GUI.changed) |
| 1037 | { |
| 1038 | EditorUtility.SetDirty(OVRPlatformToolSettings.Instance); |
| 1039 | GUI.changed = false; |
| 1040 | } |
| 1041 | } |
| 1042 | |
| 1043 | private static IEnumerator ProvisionPlatformUtil(string dataPath) |
| 1044 | { |
| 1045 | #if UNITY_2019_1_OR_NEWER |
| 1046 | var webRequest = new UnityWebRequest(urlPlatformUtil, UnityWebRequest.kHttpVerbGET); |
| 1047 | string path = dataPath; |
| 1048 | webRequest.downloadHandler = new DownloadHandlerFile(path); |
| 1049 | // WWW request timeout in seconds |
| 1050 | webRequest.timeout = 60; |
| 1051 | UnityWebRequestAsyncOperation webOp = webRequest.SendWebRequest(); |
| 1052 | while (!webOp.isDone) { } |
| 1053 | if (webRequest.isNetworkError || webRequest.isHttpError) |
| 1054 | { |
| 1055 | var networkErrorMsg = "Failed to provision Oculus Platform Util\n"; |
| 1056 | UnityEngine.Debug.LogError(networkErrorMsg); |
| 1057 | OVRPlatformTool.log += networkErrorMsg; |
| 1058 | } |
| 1059 | else |
| 1060 | { |
| 1061 | OVRPlatformTool.log += "Completed Provisioning Oculus Platform Util\n"; |
| 1062 | } |
| 1063 | SetDirtyOnGUIChange(); |
| 1064 | yield return webOp; |
| 1065 | #else |
| 1066 | using (WWW www = new WWW(urlPlatformUtil)) |
| 1067 | { |
| 1068 | UnityEngine.Debug.Log("Started Provisioning Oculus Platform Util"); |
| 1069 | float timer = 0; |
| 1070 | float timeOut = 60; |
| 1071 | yield return www; |
| 1072 | while (!www.isDone && timer < timeOut) |
| 1073 | { |
| 1074 | timer += Time.deltaTime; |
| 1075 | if (www.error != null) |
| 1076 | { |
| 1077 | UnityEngine.Debug.Log("Download error: " + www.error); |
| 1078 | break; |
| 1079 | } |
| 1080 | OVRPlatformTool.log = string.Format("Downloading.. {0:P1}", www.progress); |
| 1081 | SetDirtyOnGUIChange(); |
| 1082 | yield return new WaitForSeconds(1f); |
| 1083 | } |
| 1084 | if (www.isDone) |
| 1085 | { |
| 1086 | System.IO.File.WriteAllBytes(dataPath, www.bytes); |
| 1087 | OVRPlatformTool.log = "Completed Provisioning Oculus Platform Util\n"; |
| 1088 | SetDirtyOnGUIChange(); |
| 1089 | } |
| 1090 | } |
| 1091 | #endif |
| 1092 | } |
| 1093 | |
| 1094 | private static void DrawAssetConfigList(Rect rect) |
| 1095 | { |
| 1096 | DrawAssetConfigHeader(rect); |
| 1097 | DrawAssetConfigBackground(rect); |
| 1098 | DrawAssetConfigElement(rect); |
| 1099 | } |
| 1100 | |
| 1101 | private static void DrawAssetConfigElement(Rect rect) |
| 1102 | { |
| 1103 | Rect elementRect = new Rect(rect.x, rect.y + SINGLE_LINE_SPACING + ASSET_CONFIG_BACKGROUND_PADDING / 2, |
| 1104 | rect.width, SINGLE_LINE_SPACING); |
| 1105 | if (OVRPlatformToolSettings.AssetConfigs.Count > 0) |
| 1106 | { |
| 1107 | for (int i = 0; i < OVRPlatformToolSettings.AssetConfigs.Count; i++) |
| 1108 | { |
| 1109 | AssetConfig config = OVRPlatformToolSettings.AssetConfigs[i]; |
| 1110 | GUIContent fieldLabel; |
| 1111 | |
| 1112 | config.SetFoldoutState(EditorGUI.Foldout(elementRect, config.GetFoldoutState(), config.name, boldFoldoutStyle)); |
| 1113 | if (config.GetFoldoutState()) |
| 1114 | { |
| 1115 | Rect attributeRect = new Rect(elementRect.x + INDENT_SPACING, elementRect.y + SINGLE_LINE_SPACING, |
| 1116 | elementRect.width - INDENT_SPACING - 3f, SINGLE_LINE_SPACING); |
| 1117 | // Extra asset config params are disabled for now until CLI supports them |
| 1118 | #if !DISABLE_EXTRA_ASSET_CONFIG |
| 1119 | fieldLabel = new GUIContent("Required Asset [?]", "Whether or not this asset file is required for the app to run."); |
| 1120 | config.required = EditorGUI.Toggle(attributeRect, fieldLabel, config.required); |
| 1121 | |
| 1122 | attributeRect.y += SINGLE_LINE_SPACING; |
| 1123 | fieldLabel = new GUIContent("Asset Type [?]", "The asset file type."); |
| 1124 | config.type = (AssetConfig.AssetType)EditorGUI.EnumPopup(attributeRect, fieldLabel, config.type); |
| 1125 | |
| 1126 | attributeRect.y += SINGLE_LINE_SPACING; |
| 1127 | #endif |
| 1128 | fieldLabel = new GUIContent("Asset SKU [?]", "The Oculus store SKU for this asset file."); |
| 1129 | config.sku = EditorGUI.TextField(attributeRect, fieldLabel, config.sku); |
| 1130 | |
| 1131 | elementRect.y = attributeRect.y; |
| 1132 | } |
| 1133 | elementRect.y += SINGLE_LINE_SPACING; |
| 1134 | } |
| 1135 | } |
| 1136 | else |
| 1137 | { |
| 1138 | EditorGUI.LabelField(elementRect, "No asset files found. Choose a valid assets directory."); |
| 1139 | } |
| 1140 | } |
| 1141 | |
| 1142 | private static float GetAssetConfigElementHeight() |
| 1143 | { |
| 1144 | float totalHeight = 0f; |
| 1145 | if (OVRPlatformToolSettings.AssetConfigs.Count > 0) |
| 1146 | { |
| 1147 | for (int i = 0; i < OVRPlatformToolSettings.AssetConfigs.Count; i++) |
| 1148 | { |
| 1149 | AssetConfig config = OVRPlatformToolSettings.AssetConfigs[i]; |
| 1150 | #if !DISABLE_EXTRA_ASSET_CONFIG |
| 1151 | totalHeight += config.GetFoldoutState() ? SINGLE_LINE_SPACING * 4 : SINGLE_LINE_SPACING; |
| 1152 | #else |
| 1153 | totalHeight += config.GetFoldoutState() ? SINGLE_LINE_SPACING * 2 : SINGLE_LINE_SPACING; |
| 1154 | #endif |
| 1155 | } |
| 1156 | } |
| 1157 | else |
| 1158 | { |
| 1159 | totalHeight += SINGLE_LINE_SPACING; |
| 1160 | } |
| 1161 | return totalHeight + ASSET_CONFIG_BACKGROUND_PADDING; |
| 1162 | } |
| 1163 | |
| 1164 | private static void DrawAssetConfigHeader(Rect rect) |
| 1165 | { |
| 1166 | Rect headerRect = new Rect(rect.x, rect.y, rect.width, SINGLE_LINE_SPACING); |
| 1167 | EditorGUI.DrawRect(headerRect, EditorGUIUtility.isProSkin ? new Color(0.37f, 0.37f, 0.37f) : new Color(0.55f, 0.55f, 0.55f)); |
| 1168 | EditorGUI.LabelField(rect, "Asset File Configuration"); |
| 1169 | } |
| 1170 | |
| 1171 | private static void DrawAssetConfigBackground(Rect rect) |
| 1172 | { |
| 1173 | Rect backgroundRect = new Rect(rect.x, rect.y + SINGLE_LINE_SPACING, rect.width, GetAssetConfigElementHeight()); |
| 1174 | EditorGUI.DrawRect(backgroundRect, EditorGUIUtility.isProSkin ? new Color(0.3f, 0.3f, 0.3f) : new Color(0.63f, 0.63f, 0.63f)); |
| 1175 | } |
| 1176 | |
| 1177 | class GUIHelper |
| 1178 | { |
| 1179 | public delegate void Worker(); |
| 1180 | |
| 1181 | static void InOut(Worker begin, Worker body, Worker end) |
| 1182 | { |
| 1183 | try |
| 1184 | { |
| 1185 | begin(); |
| 1186 | body(); |
| 1187 | } |
| 1188 | finally |
| 1189 | { |
| 1190 | end(); |
| 1191 | } |
| 1192 | } |
| 1193 | |
| 1194 | public static void HInset(int pixels, Worker worker) |
| 1195 | { |
| 1196 | InOut( |
| 1197 | () => { |
| 1198 | GUILayout.BeginHorizontal(); |
| 1199 | GUILayout.Space(pixels); |
| 1200 | GUILayout.BeginVertical(); |
| 1201 | }, |
| 1202 | worker, |
| 1203 | () => { |
| 1204 | GUILayout.EndVertical(); |
| 1205 | GUILayout.EndHorizontal(); |
| 1206 | } |
| 1207 | ); |
| 1208 | } |
| 1209 | |
| 1210 | public delegate T ControlWorker<T>(); |
| 1211 | public static T MakeControlWithLabel<T>(GUIContent label, ControlWorker<T> worker) |
| 1212 | { |
| 1213 | EditorGUILayout.BeginHorizontal(); |
| 1214 | EditorGUILayout.LabelField(label); |
| 1215 | |
| 1216 | var result = worker(); |
| 1217 | |
| 1218 | EditorGUILayout.EndHorizontal(); |
| 1219 | return result; |
| 1220 | } |
| 1221 | } |
| 1222 | |
| 1223 | public class EditorCoroutine |
| 1224 | { |
| 1225 | public static EditorCoroutine Start(IEnumerator routine) |
| 1226 | { |
| 1227 | EditorCoroutine coroutine = new EditorCoroutine(routine); |
| 1228 | coroutine.Start(); |
| 1229 | return coroutine; |
| 1230 | } |
| 1231 | |
| 1232 | readonly IEnumerator routine; |
| 1233 | bool completed; |
| 1234 | EditorCoroutine(IEnumerator _routine) |
| 1235 | { |
| 1236 | routine = _routine; |
| 1237 | completed = false; |
| 1238 | } |
| 1239 | |
| 1240 | void Start() |
| 1241 | { |
| 1242 | EditorApplication.update += Update; |
| 1243 | } |
| 1244 | public void Stop() |
| 1245 | { |
| 1246 | EditorApplication.update -= Update; |
| 1247 | completed = true; |
| 1248 | } |
| 1249 | |
| 1250 | public bool GetCompleted() |
| 1251 | { |
| 1252 | return completed; |
| 1253 | } |
| 1254 | |
| 1255 | void Update() |
| 1256 | { |
| 1257 | if (!routine.MoveNext()) |
| 1258 | { |
| 1259 | Stop(); |
| 1260 | } |
| 1261 | } |
| 1262 | } |
| 1263 | } |
| 1264 | } |