2
0

SteamVR_Input_ActionFile.cs 22 KB

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