SteamVR_Input_ActionFile.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. using UnityEngine;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using Valve.Newtonsoft.Json;
  7. using System.IO;
  8. namespace Valve.VR
  9. {
  10. [System.Serializable]
  11. public class SteamVR_Input_ActionFile
  12. {
  13. public List<SteamVR_Input_ActionFile_Action> actions = new List<SteamVR_Input_ActionFile_Action>();
  14. public List<SteamVR_Input_ActionFile_ActionSet> action_sets = new List<SteamVR_Input_ActionFile_ActionSet>();
  15. public List<SteamVR_Input_ActionFile_DefaultBinding> default_bindings = new List<SteamVR_Input_ActionFile_DefaultBinding>();
  16. public List<Dictionary<string, string>> localization = new List<Dictionary<string, string>>();
  17. [JsonIgnore]
  18. public List<SteamVR_Input_ActionFile_LocalizationItem> localizationHelperList = new List<SteamVR_Input_ActionFile_LocalizationItem>();
  19. public void InitializeHelperLists()
  20. {
  21. foreach (var actionset in action_sets)
  22. {
  23. actionset.actionsInList = new List<SteamVR_Input_ActionFile_Action>(actions.Where(action => action.path.StartsWith(actionset.name) && SteamVR_Input_ActionFile_ActionTypes.listIn.Contains(action.type)));
  24. actionset.actionsOutList = new List<SteamVR_Input_ActionFile_Action>(actions.Where(action => action.path.StartsWith(actionset.name) && SteamVR_Input_ActionFile_ActionTypes.listOut.Contains(action.type)));
  25. actionset.actionsList = new List<SteamVR_Input_ActionFile_Action>(actions.Where(action => action.path.StartsWith(actionset.name)));
  26. }
  27. foreach (var item in localization)
  28. {
  29. localizationHelperList.Add(new SteamVR_Input_ActionFile_LocalizationItem(item));
  30. }
  31. }
  32. public void SaveHelperLists()
  33. {
  34. //fix actions list
  35. foreach (var actionset in action_sets)
  36. {
  37. actionset.actionsList.Clear();
  38. actionset.actionsList.AddRange(actionset.actionsInList);
  39. actionset.actionsList.AddRange(actionset.actionsOutList);
  40. }
  41. actions.Clear();
  42. foreach (var actionset in action_sets)
  43. {
  44. actions.AddRange(actionset.actionsInList);
  45. actions.AddRange(actionset.actionsOutList);
  46. }
  47. localization.Clear();
  48. foreach (var item in localizationHelperList)
  49. {
  50. Dictionary<string, string> localizationItem = new Dictionary<string, string>();
  51. localizationItem.Add(SteamVR_Input_ActionFile_LocalizationItem.languageTagKeyName, item.language);
  52. foreach (var itemItem in item.items)
  53. {
  54. localizationItem.Add(itemItem.Key, itemItem.Value);
  55. }
  56. localization.Add(localizationItem);
  57. }
  58. }
  59. public static string GetShortName(string name)
  60. {
  61. string fullName = name;
  62. int lastSlash = fullName.LastIndexOf('/');
  63. if (lastSlash != -1)
  64. {
  65. if (lastSlash == fullName.Length - 1)
  66. {
  67. fullName = fullName.Remove(lastSlash);
  68. lastSlash = fullName.LastIndexOf('/');
  69. if (lastSlash == -1)
  70. {
  71. return GetCodeFriendlyName(fullName);
  72. }
  73. }
  74. return GetCodeFriendlyName(fullName.Substring(lastSlash + 1));
  75. }
  76. return GetCodeFriendlyName(fullName);
  77. }
  78. public static string GetCodeFriendlyName(string name)
  79. {
  80. name = name.Replace('/', '_').Replace(' ', '_');
  81. if (char.IsLetter(name[0]) == false)
  82. name = "_" + name;
  83. for (int charIndex = 0; charIndex < name.Length; charIndex++)
  84. {
  85. if (char.IsLetterOrDigit(name[charIndex]) == false && name[charIndex] != '_')
  86. {
  87. name = name.Remove(charIndex, 1);
  88. name = name.Insert(charIndex, "_");
  89. }
  90. }
  91. return name;
  92. }
  93. public string[] GetFilesToCopy(bool throwErrors = false)
  94. {
  95. List<string> files = new List<string>();
  96. FileInfo actionFileInfo = new FileInfo(SteamVR_Input.actionsFilePath);
  97. string path = actionFileInfo.Directory.FullName;
  98. files.Add(SteamVR_Input.actionsFilePath);
  99. foreach (var binding in default_bindings)
  100. {
  101. string bindingPath = Path.Combine(path, binding.binding_url);
  102. if (File.Exists(bindingPath))
  103. files.Add(bindingPath);
  104. else
  105. {
  106. if (throwErrors)
  107. {
  108. Debug.LogError("<b>[SteamVR]</b> Could not bind binding file specified by the actions.json manifest: " + bindingPath);
  109. }
  110. }
  111. }
  112. return files.ToArray();
  113. }
  114. public void CopyFilesToPath(string toPath, bool overwrite)
  115. {
  116. string[] files = SteamVR_Input.actionFile.GetFilesToCopy();
  117. foreach (string file in files)
  118. {
  119. FileInfo bindingInfo = new FileInfo(file);
  120. string newFilePath = Path.Combine(toPath, bindingInfo.Name);
  121. bool exists = false;
  122. if (File.Exists(newFilePath))
  123. exists = true;
  124. if (exists)
  125. {
  126. if (overwrite)
  127. {
  128. FileInfo existingFile = new FileInfo(newFilePath);
  129. existingFile.IsReadOnly = false;
  130. existingFile.Delete();
  131. File.Copy(file, newFilePath);
  132. RemoveAppKey(newFilePath);
  133. Debug.Log("<b>[SteamVR]</b> Copied (overwrote) SteamVR Input file at path: " + newFilePath);
  134. }
  135. else
  136. {
  137. Debug.Log("<b>[SteamVR]</b> Skipped writing existing file at path: " + newFilePath);
  138. }
  139. }
  140. else
  141. {
  142. File.Copy(file, newFilePath);
  143. RemoveAppKey(newFilePath);
  144. Debug.Log("<b>[SteamVR]</b> Copied SteamVR Input file to folder: " + newFilePath);
  145. }
  146. }
  147. }
  148. public bool IsInStreamingAssets()
  149. {
  150. return SteamVR_Input.actionsFilePath.Contains("StreamingAssets");
  151. }
  152. private const string findString_appKeyStart = "\"app_key\"";
  153. private const string findString_appKeyEnd = "\",";
  154. private static void RemoveAppKey(string newFilePath)
  155. {
  156. if (File.Exists(newFilePath))
  157. {
  158. string jsonText = System.IO.File.ReadAllText(newFilePath);
  159. string findString = "\"app_key\"";
  160. int stringStart = jsonText.IndexOf(findString);
  161. if (stringStart == -1)
  162. return; //no app key
  163. int stringEnd = jsonText.IndexOf("\",", stringStart);
  164. if (stringEnd == -1)
  165. return; //no end?
  166. stringEnd += findString_appKeyEnd.Length;
  167. int stringLength = stringEnd - stringStart;
  168. string newJsonText = jsonText.Remove(stringStart, stringLength);
  169. FileInfo file = new FileInfo(newFilePath);
  170. file.IsReadOnly = false;
  171. File.WriteAllText(newFilePath, newJsonText);
  172. }
  173. }
  174. public void Save(string path)
  175. {
  176. FileInfo existingActionsFile = new FileInfo(path);
  177. if (existingActionsFile.Exists)
  178. {
  179. existingActionsFile.IsReadOnly = false;
  180. }
  181. //SanitizeActionFile(); //todo: shouldn't we be doing this?
  182. string json = JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
  183. File.WriteAllText(path, json);
  184. }
  185. }
  186. public enum SteamVR_Input_ActionFile_DefaultBinding_ControllerTypes
  187. {
  188. vive,
  189. vive_pro,
  190. vive_controller,
  191. generic,
  192. holographic_controller,
  193. oculus_touch,
  194. gamepad,
  195. knuckles,
  196. }
  197. [System.Serializable]
  198. public class SteamVR_Input_ActionFile_DefaultBinding
  199. {
  200. public string controller_type;
  201. public string binding_url;
  202. public SteamVR_Input_ActionFile_DefaultBinding GetCopy()
  203. {
  204. SteamVR_Input_ActionFile_DefaultBinding newDefaultBinding = new SteamVR_Input_ActionFile_DefaultBinding();
  205. newDefaultBinding.controller_type = this.controller_type;
  206. newDefaultBinding.binding_url = this.binding_url;
  207. return newDefaultBinding;
  208. }
  209. }
  210. [System.Serializable]
  211. public class SteamVR_Input_ActionFile_ActionSet
  212. {
  213. [JsonIgnore]
  214. private const string actionSetInstancePrefix = "instance_";
  215. public string name;
  216. public string usage;
  217. [JsonIgnore]
  218. public string codeFriendlyName
  219. {
  220. get
  221. {
  222. return SteamVR_Input_ActionFile.GetCodeFriendlyName(name);
  223. }
  224. }
  225. [JsonIgnore]
  226. public string shortName
  227. {
  228. get
  229. {
  230. int lastIndex = name.LastIndexOf('/');
  231. if (lastIndex == name.Length - 1)
  232. return string.Empty;
  233. return SteamVR_Input_ActionFile.GetShortName(name);
  234. }
  235. }
  236. public void SetNewShortName(string newShortName)
  237. {
  238. name = GetPathFromName(newShortName);
  239. }
  240. public static string CreateNewName()
  241. {
  242. return GetPathFromName("NewSet");
  243. }
  244. private const string nameTemplate = "/actions/{0}";
  245. public static string GetPathFromName(string name)
  246. {
  247. return string.Format(nameTemplate, name);
  248. }
  249. public static SteamVR_Input_ActionFile_ActionSet CreateNew()
  250. {
  251. return new SteamVR_Input_ActionFile_ActionSet() { name = CreateNewName() };
  252. }
  253. public SteamVR_Input_ActionFile_ActionSet GetCopy()
  254. {
  255. SteamVR_Input_ActionFile_ActionSet newSet = new SteamVR_Input_ActionFile_ActionSet();
  256. newSet.name = this.name;
  257. newSet.usage = this.usage;
  258. return newSet;
  259. }
  260. public override bool Equals(object obj)
  261. {
  262. if (obj is SteamVR_Input_ActionFile_ActionSet)
  263. {
  264. SteamVR_Input_ActionFile_ActionSet set = (SteamVR_Input_ActionFile_ActionSet)obj;
  265. if (set == this)
  266. return true;
  267. if (set.name == this.name)
  268. return true;
  269. return false;
  270. }
  271. return base.Equals(obj);
  272. }
  273. public override int GetHashCode()
  274. {
  275. return base.GetHashCode();
  276. }
  277. [JsonIgnore]
  278. public List<SteamVR_Input_ActionFile_Action> actionsInList = new List<SteamVR_Input_ActionFile_Action>();
  279. [JsonIgnore]
  280. public List<SteamVR_Input_ActionFile_Action> actionsOutList = new List<SteamVR_Input_ActionFile_Action>();
  281. [JsonIgnore]
  282. public List<SteamVR_Input_ActionFile_Action> actionsList = new List<SteamVR_Input_ActionFile_Action>();
  283. }
  284. public enum SteamVR_Input_ActionFile_Action_Requirements
  285. {
  286. optional,
  287. suggested,
  288. mandatory,
  289. }
  290. [System.Serializable]
  291. public class SteamVR_Input_ActionFile_Action
  292. {
  293. [JsonIgnore]
  294. private static string[] _requirementValues;
  295. [JsonIgnore]
  296. public static string[] requirementValues
  297. {
  298. get
  299. {
  300. if (_requirementValues == null)
  301. _requirementValues = System.Enum.GetNames(typeof(SteamVR_Input_ActionFile_Action_Requirements));
  302. return _requirementValues;
  303. }
  304. }
  305. public string name;
  306. public string type;
  307. public string scope;
  308. public string skeleton;
  309. public string requirement;
  310. public SteamVR_Input_ActionFile_Action GetCopy()
  311. {
  312. SteamVR_Input_ActionFile_Action newAction = new SteamVR_Input_ActionFile_Action();
  313. newAction.name = this.name;
  314. newAction.type = this.type;
  315. newAction.scope = this.scope;
  316. newAction.skeleton = this.skeleton;
  317. newAction.requirement = this.requirement;
  318. return newAction;
  319. }
  320. [JsonIgnore]
  321. public SteamVR_Input_ActionFile_Action_Requirements requirementEnum
  322. {
  323. get
  324. {
  325. for (int index = 0; index < requirementValues.Length; index++)
  326. {
  327. if (string.Equals(requirementValues[index], requirement, System.StringComparison.CurrentCultureIgnoreCase))
  328. {
  329. return (SteamVR_Input_ActionFile_Action_Requirements)index;
  330. }
  331. }
  332. return SteamVR_Input_ActionFile_Action_Requirements.suggested;
  333. }
  334. set
  335. {
  336. requirement = value.ToString();
  337. }
  338. }
  339. [JsonIgnore]
  340. public string codeFriendlyName
  341. {
  342. get
  343. {
  344. return SteamVR_Input_ActionFile.GetCodeFriendlyName(name);
  345. }
  346. }
  347. [JsonIgnore]
  348. public string shortName
  349. {
  350. get
  351. {
  352. return SteamVR_Input_ActionFile.GetShortName(name);
  353. }
  354. }
  355. [JsonIgnore]
  356. public string path
  357. {
  358. get
  359. {
  360. int lastIndex = name.LastIndexOf('/');
  361. if (lastIndex != -1 && lastIndex + 1 < name.Length)
  362. {
  363. return name.Substring(0, lastIndex + 1);
  364. }
  365. return name;
  366. }
  367. }
  368. private const string nameTemplate = "/actions/{0}/{1}/{2}";
  369. public static string CreateNewName(string actionSet, string direction)
  370. {
  371. return string.Format(nameTemplate, actionSet, direction, "NewAction");
  372. }
  373. public static string CreateNewName(string actionSet, SteamVR_ActionDirections direction, string actionName)
  374. {
  375. return string.Format(nameTemplate, actionSet, direction.ToString().ToLower(), actionName);
  376. }
  377. public static SteamVR_Input_ActionFile_Action CreateNew(string actionSet, SteamVR_ActionDirections direction, string actionType)
  378. {
  379. return new SteamVR_Input_ActionFile_Action() { name = CreateNewName(actionSet, direction.ToString().ToLower()), type = actionType };
  380. }
  381. [JsonIgnore]
  382. public SteamVR_ActionDirections direction
  383. {
  384. get
  385. {
  386. if (type.ToLower() == SteamVR_Input_ActionFile_ActionTypes.vibration)
  387. return SteamVR_ActionDirections.Out;
  388. return SteamVR_ActionDirections.In;
  389. }
  390. }
  391. protected const string prefix = "/actions/";
  392. [JsonIgnore]
  393. public string actionSet
  394. {
  395. get
  396. {
  397. int setEnd = name.IndexOf('/', prefix.Length);
  398. if (setEnd == -1)
  399. return string.Empty;
  400. return name.Substring(0, setEnd);
  401. }
  402. }
  403. public void SetNewActionSet(string newSetName)
  404. {
  405. name = string.Format(nameTemplate, newSetName, direction.ToString().ToLower(), shortName);
  406. }
  407. public override string ToString()
  408. {
  409. return shortName;
  410. }
  411. public override bool Equals(object obj)
  412. {
  413. if (obj is SteamVR_Input_ActionFile_Action)
  414. {
  415. SteamVR_Input_ActionFile_Action action = (SteamVR_Input_ActionFile_Action)obj;
  416. if (this == obj)
  417. return true;
  418. if (this.name == action.name && this.type == action.type && this.skeleton == action.skeleton && this.requirement == action.requirement)
  419. return true;
  420. return false;
  421. }
  422. return base.Equals(obj);
  423. }
  424. public override int GetHashCode()
  425. {
  426. return base.GetHashCode();
  427. }
  428. }
  429. public class SteamVR_Input_ActionFile_LocalizationItem
  430. {
  431. public const string languageTagKeyName = "language_tag";
  432. public string language;
  433. public Dictionary<string, string> items = new Dictionary<string, string>();
  434. public SteamVR_Input_ActionFile_LocalizationItem(string newLanguage)
  435. {
  436. language = newLanguage;
  437. }
  438. public SteamVR_Input_ActionFile_LocalizationItem(Dictionary<string, string> dictionary)
  439. {
  440. if (dictionary == null)
  441. return;
  442. if (dictionary.ContainsKey(languageTagKeyName))
  443. language = (string)dictionary[languageTagKeyName];
  444. else
  445. Debug.Log("<b>[SteamVR]</b> Input: Error in actions file, no language_tag in localization array item.");
  446. foreach (KeyValuePair<string, string> item in dictionary)
  447. {
  448. if (item.Key != languageTagKeyName)
  449. items.Add(item.Key, (string)item.Value);
  450. }
  451. }
  452. }
  453. public class SteamVR_Input_ManifestFile
  454. {
  455. public string source;
  456. public List<SteamVR_Input_ManifestFile_Application> applications;
  457. }
  458. public class SteamVR_Input_ManifestFile_Application
  459. {
  460. public string app_key;
  461. public string launch_type;
  462. public string url;
  463. public string binary_path_windows;
  464. public string binary_path_linux;
  465. public string binary_path_osx;
  466. public string action_manifest_path;
  467. //public List<SteamVR_Input_ManifestFile_Application_Binding> bindings = new List<SteamVR_Input_ManifestFile_Application_Binding>();
  468. public string image_path;
  469. public Dictionary<string, SteamVR_Input_ManifestFile_ApplicationString> strings = new Dictionary<string, SteamVR_Input_ManifestFile_ApplicationString>();
  470. }
  471. public class SteamVR_Input_Unity_AssemblyFile_Definition
  472. {
  473. public string name = "SteamVR_Actions";
  474. public string[] references = new string[] { "SteamVR" };
  475. public string[] optionalUnityReferences = new string[0];
  476. public string[] includePlatforms = new string[0];
  477. public string[] excludePlatforms = new string[0];
  478. public bool allowUnsafeCode = false;
  479. public bool overrideReferences = false;
  480. public string[] precompiledReferences = new string[0];
  481. public bool autoReferenced = false;
  482. public string[] defineConstraints = new string[0];
  483. }
  484. public class SteamVR_Input_ManifestFile_ApplicationString
  485. {
  486. public string name;
  487. }
  488. public class SteamVR_Input_ManifestFile_Application_Binding
  489. {
  490. public string controller_type;
  491. public string binding_url;
  492. }
  493. public class SteamVR_Input_ManifestFile_Application_Binding_ControllerTypes
  494. {
  495. public static string oculus_touch = "oculus_touch";
  496. public static string vive_controller = "vive_controller";
  497. public static string knuckles = "knuckles";
  498. public static string holographic_controller = "holographic_controller";
  499. public static string vive = "vive";
  500. public static string vive_pro = "vive_pro";
  501. public static string holographic_hmd = "holographic_hmd";
  502. public static string rift = "rift";
  503. public static string vive_tracker_camera = "vive_tracker_camera";
  504. }
  505. static public class SteamVR_Input_ActionFile_ActionTypes
  506. {
  507. public static string boolean = "boolean";
  508. public static string vector1 = "vector1";
  509. public static string vector2 = "vector2";
  510. public static string vector3 = "vector3";
  511. public static string vibration = "vibration";
  512. public static string pose = "pose";
  513. public static string skeleton = "skeleton";
  514. public static string skeletonLeftPath = "\\skeleton\\hand\\left";
  515. public static string skeletonRightPath = "\\skeleton\\hand\\right";
  516. public static string[] listAll = new string[] { boolean, vector1, vector2, vector3, vibration, pose, skeleton };
  517. public static string[] listIn = new string[] { boolean, vector1, vector2, vector3, pose, skeleton };
  518. public static string[] listOut = new string[] { vibration };
  519. public static string[] listSkeletons = new string[] { skeletonLeftPath, skeletonRightPath };
  520. }
  521. static public class SteamVR_Input_ActionFile_ActionSet_Usages
  522. {
  523. public static string leftright = "leftright";
  524. public static string single = "single";
  525. public static string hidden = "hidden";
  526. public static string leftrightDescription = "per hand";
  527. public static string singleDescription = "mirrored";
  528. public static string hiddenDescription = "hidden";
  529. public static string[] listValues = new string[] { leftright, single, hidden };
  530. public static string[] listDescriptions = new string[] { leftrightDescription, singleDescription, hiddenDescription };
  531. }
  532. }