WitSettingsUtility.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) Meta Platforms, Inc. and affiliates.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the license found in the
  6. * LICENSE file in the root directory of this source tree.
  7. */
  8. #if UNITY_EDITOR
  9. using System;
  10. using System.IO;
  11. using System.Collections.Generic;
  12. using UnityEngine;
  13. namespace Meta.WitAi
  14. {
  15. public static class WitSettingsUtility
  16. {
  17. #region SHARED
  18. // Whether settings have been loaded or not
  19. public static bool IsLoaded { get; private set; }
  20. // Current settings
  21. public static WitSettings Settings
  22. {
  23. get
  24. {
  25. if (_settings.configSettings == null)
  26. {
  27. LoadSettings();
  28. }
  29. return _settings;
  30. }
  31. }
  32. private static WitSettings _settings;
  33. // Settings save path
  34. private const string SETTINGS_PATH = "ProjectSettings/wit.config";
  35. // Server token dictionary path
  36. private static string GetSettingsFilePath() => Application.dataPath.Replace("Assets", SETTINGS_PATH).Replace("\\", "/");
  37. // Load Settings
  38. public static void LoadSettings()
  39. {
  40. // Ignore
  41. if (IsLoaded)
  42. {
  43. return;
  44. }
  45. // Loaded
  46. IsLoaded = true;
  47. // Get file path
  48. string settingsFilePath = GetSettingsFilePath();
  49. if (string.IsNullOrEmpty(settingsFilePath))
  50. {
  51. return;
  52. }
  53. if (!File.Exists(settingsFilePath))
  54. {
  55. VLog.W($"Wit Settings Utility - Generating new settings file\nPath{settingsFilePath}");
  56. _settings = new WitSettings();
  57. return;
  58. }
  59. // Read file
  60. string settingsContents = string.Empty;
  61. try
  62. {
  63. settingsContents = File.ReadAllText(settingsFilePath);
  64. }
  65. // Catch error
  66. catch (Exception e)
  67. {
  68. VLog.E($"Wit Settings Utility - Failed to load settings file\nPath{settingsFilePath}\nError: {e}");
  69. _settings = new WitSettings();
  70. return;
  71. }
  72. // Decode file
  73. try
  74. {
  75. _settings = JsonUtility.FromJson<WitSettings>(settingsContents);
  76. }
  77. // Catch error
  78. catch (Exception e)
  79. {
  80. VLog.E($"Wit Settings Utility - Failed to decode settings file\nPath{settingsFilePath}\nError: {e}");
  81. _settings = new WitSettings();
  82. }
  83. }
  84. // Save Settings
  85. public static void SaveSettings()
  86. {
  87. // Get path
  88. string settingsFilePath = GetSettingsFilePath();
  89. // Encode file
  90. string settingsContents = string.Empty;
  91. try
  92. {
  93. settingsContents = JsonUtility.ToJson(_settings);
  94. }
  95. // Catch error
  96. catch (Exception e)
  97. {
  98. VLog.E($"Wit Settings Utility - Failed to encode settings file\nPath{settingsFilePath}\nError: {e}");
  99. return;
  100. }
  101. // Write file
  102. try
  103. {
  104. File.WriteAllText(settingsFilePath, settingsContents);
  105. }
  106. // Catch error
  107. catch (Exception e)
  108. {
  109. VLog.E($"Wit Settings Utility - Failed to save settings file\nPath{settingsFilePath}\nError: {e}");
  110. }
  111. }
  112. #endregion
  113. #region TOKENS
  114. // Get index for app id
  115. private static int GetConfigIndexWithAppID(string appID) => Settings.configSettings == null ? -1 : Array.FindIndex(Settings.configSettings, (c) => string.Equals(appID, c.appID));
  116. // Get index for server token
  117. private static int GetConfigIndexWithServerToken(string serverToken) => Settings.configSettings == null ? -1 : Array.FindIndex(Settings.configSettings, (c) => string.Equals(serverToken, c.serverToken));
  118. // Get server token
  119. public static string GetServerToken(string appID, string defaultServerToken = "")
  120. {
  121. // Invalid
  122. if (string.IsNullOrEmpty(appID))
  123. {
  124. return string.Empty;
  125. }
  126. // Add if missing
  127. int index = GetConfigIndexWithAppID(appID);
  128. if (index == -1)
  129. {
  130. AddNewConfigSetting(appID, defaultServerToken);
  131. index = _settings.configSettings.Length - 1;
  132. }
  133. // Success
  134. return Settings.configSettings[index].serverToken;
  135. }
  136. // Get app id from server token
  137. public static string GetServerTokenAppID(string serverToken, string defaultAppID = "")
  138. {
  139. // Invalid
  140. if (string.IsNullOrEmpty(serverToken))
  141. {
  142. return string.Empty;
  143. }
  144. // Add if missing
  145. int index = GetConfigIndexWithServerToken(serverToken);
  146. if (index == -1)
  147. {
  148. AddNewConfigSetting(defaultAppID, serverToken);
  149. index = _settings.configSettings.Length - 1;
  150. }
  151. // Success
  152. return Settings.configSettings[index].appID;
  153. }
  154. // Add setting
  155. private static void AddNewConfigSetting(string newAppID, string newServerToken)
  156. {
  157. // Generate config
  158. WitConfigSettings config = new WitConfigSettings();
  159. config.appID = newAppID;
  160. config.serverToken = newServerToken;
  161. // Add config
  162. List<WitConfigSettings> all = new List<WitConfigSettings>();
  163. if (_settings.configSettings != null)
  164. {
  165. all.AddRange(_settings.configSettings);
  166. }
  167. all.Add(config);
  168. _settings.configSettings = all.ToArray();
  169. // Save settings
  170. SaveSettings();
  171. }
  172. // Set server token
  173. public static void SetServerToken(string appID, string newServerToken)
  174. {
  175. // Invalid
  176. if (string.IsNullOrEmpty(appID))
  177. {
  178. return;
  179. }
  180. // Add if missing
  181. int index = GetConfigIndexWithAppID(appID);
  182. if (index == -1)
  183. {
  184. AddNewConfigSetting(appID, newServerToken);
  185. }
  186. // If token changed, adjust
  187. else if (!string.Equals(newServerToken, _settings.configSettings[index].serverToken))
  188. {
  189. WitConfigSettings config = _settings.configSettings[index];
  190. config.serverToken = newServerToken;
  191. _settings.configSettings[index] = config;
  192. SaveSettings();
  193. }
  194. }
  195. #endregion
  196. }
  197. }
  198. #endif