WelcomeWizard.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * Licensed under the Oculus SDK License Agreement (the "License");
  6. * you may not use the Oculus SDK except in compliance with the License,
  7. * which is provided at the time of installation or download, or which
  8. * otherwise accompanies this software in either electronic or hard copy form.
  9. *
  10. * You may obtain a copy of the License at
  11. *
  12. * https://developer.oculus.com/licenses/oculussdk/
  13. *
  14. * Unless required by applicable law or agreed to in writing, the Oculus SDK
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. using System.Collections.Generic;
  21. using Meta.WitAi;
  22. using Meta.WitAi.Data.Configuration;
  23. using Meta.WitAi.Windows;
  24. using Meta.WitAi.Data.Info;
  25. using Oculus.Voice.Utility;
  26. using UnityEngine;
  27. namespace Oculus.Voice.Windows
  28. {
  29. public class WelcomeWizard : WitWelcomeWizard
  30. {
  31. private int witBuiltInIndex;
  32. private string[] builtinAppNames;
  33. protected override Texture2D HeaderIcon => VoiceSDKStyles.MainHeader;
  34. protected override GUIContent Title => VoiceSDKStyles.SetupTitle;
  35. protected override string ContentHeaderLabel => VoiceSDKStyles.Texts.SetupHeaderLabel;
  36. protected override string ContentSubheaderLabel => VoiceSDKStyles.Texts.SetupSubheaderLabel;
  37. protected override string DocsUrl => VoiceSDKStyles.Texts.VoiceDocsUrl;
  38. protected override void OnEnable()
  39. {
  40. WitAuthUtility.tokenValidator = new VoiceSDKTokenValidatorProvider();
  41. base.OnEnable();
  42. witBuiltInIndex = 0;
  43. var names = AppBuiltIns.appNames;
  44. builtinAppNames = new string[names.Length + 1];
  45. builtinAppNames[0] = "Custom App";
  46. for (int i = 0; i < names.Length; i++)
  47. {
  48. builtinAppNames[i + 1] = names[i];
  49. }
  50. }
  51. protected override void LayoutFields()
  52. {
  53. // Prebuilt language app
  54. bool updated = false;
  55. WitEditorUI.LayoutLabel(VoiceSDKStyles.Texts.SetupLanguageLabel);
  56. WitEditorUI.LayoutPopup("", builtinAppNames, ref witBuiltInIndex, ref updated);
  57. if (updated)
  58. {
  59. if (witBuiltInIndex == 0)
  60. {
  61. serverToken = WitAuthUtility.ServerToken;
  62. }
  63. else
  64. {
  65. serverToken = AppBuiltIns.builtInPrefix + builtinAppNames[witBuiltInIndex];
  66. }
  67. }
  68. // Base fields
  69. if (witBuiltInIndex == 0)
  70. {
  71. GUILayout.Space(WitStyles.HeaderPaddingBottom);
  72. base.LayoutFields();
  73. }
  74. }
  75. // Customize configuration if voice app was selected
  76. protected override int CreateConfiguration(string newToken)
  77. {
  78. // Do base for custom app
  79. if (witBuiltInIndex <= 0)
  80. {
  81. return base.CreateConfiguration(newToken);
  82. }
  83. // Get built in app data
  84. string languageName = builtinAppNames[witBuiltInIndex];
  85. Dictionary<string, string> appData = AppBuiltIns.apps[languageName];
  86. // Generate asset using app data
  87. WitConfiguration configuration = ScriptableObject.CreateInstance<WitConfiguration>();
  88. configuration.SetClientAccessToken(appData["clientToken"]);
  89. WitAppInfo application = new WitAppInfo()
  90. {
  91. name = appData["name"],
  92. id = appData["id"],
  93. lang = appData["lang"]
  94. };
  95. configuration.SetApplicationInfo(application);
  96. configuration.name = application.id;
  97. // Save configuration to asset
  98. return WitConfigurationUtility.SaveConfiguration(string.Empty, configuration);
  99. }
  100. }
  101. public class VoiceSDKTokenValidatorProvider : WitAuthUtility.ITokenValidationProvider
  102. {
  103. public bool IsTokenValid(string appId, string token)
  104. {
  105. return IsServerTokenValid(token);
  106. }
  107. public bool IsServerTokenValid(string serverToken)
  108. {
  109. return null != serverToken && (serverToken.Length == 32 || serverToken.StartsWith(AppBuiltIns.builtInPrefix));
  110. }
  111. }
  112. }