SteamVR_Input_ActionManifest_Manager.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Text;
  6. using UnityEditor;
  7. using UnityEngine;
  8. using Valve.Newtonsoft.Json;
  9. namespace Valve.VR
  10. {
  11. public class SteamVR_Input_ActionManifest_Manager : AssetPostprocessor
  12. {
  13. private static bool importing = false;
  14. static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
  15. {
  16. if (importing)
  17. return;
  18. importing = true;
  19. Dictionary<string, List<SteamVR_PartialInputBindings>> partials = ScanForPartials();
  20. if (partials != null)
  21. {
  22. foreach (var element in partials)
  23. {
  24. if (element.Value != null && element.Value.Count > 0 && element.Value[0].imported == false)
  25. ConfirmImport(element.Value);
  26. }
  27. }
  28. importing = false;
  29. }
  30. public const string partialManifestFilename = "steamvr_partial_manifest.json";
  31. public static void CreatePartial(string name, int version, bool overwriteOld, bool removeUnused)
  32. {
  33. if (SteamVR_Input.actionFile.action_sets.Any(set => set.name == "default"))
  34. {
  35. bool confirm = EditorUtility.DisplayDialog("Confirmation", "We don't recommend you create a partial binding manifest with an action set named 'default'. There will often be collisions with existing actions. Are you sure you want to continue creating this partial binding manifest?", "Create", "Cancel");
  36. if (confirm == false)
  37. return;
  38. }
  39. string folderName = "SteamVR_" + SteamVR_Input_ActionFile.GetCodeFriendlyName(name);
  40. string directorySeparatorChar = System.IO.Path.DirectorySeparatorChar.ToString();
  41. string mainFolderPath = string.Format("{0}", folderName);
  42. string versionFolderPath = string.Format("{1}{0}{2}", directorySeparatorChar, folderName, version.ToString());
  43. string manifestPath = string.Format("{1}{0}{2}{0}{3}", directorySeparatorChar, folderName, version.ToString(), partialManifestFilename);
  44. if (Directory.Exists(mainFolderPath) == false)
  45. {
  46. Directory.CreateDirectory(mainFolderPath);
  47. }
  48. if (Directory.Exists(versionFolderPath) == false)
  49. {
  50. Directory.CreateDirectory(versionFolderPath);
  51. }
  52. SteamVR_PartialInputBindings partial = new SteamVR_PartialInputBindings();
  53. partial.name = name;
  54. partial.version = version;
  55. partial.overwriteOld = overwriteOld;
  56. partial.removeUnused = removeUnused;
  57. string jsonText = JsonConvert.SerializeObject(partial, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
  58. if (File.Exists(manifestPath))
  59. {
  60. FileInfo manifestFile = new FileInfo(manifestPath);
  61. manifestFile.IsReadOnly = false;
  62. }
  63. File.WriteAllText(manifestPath, jsonText);
  64. SteamVR_Input.actionFile.CopyFilesToPath(versionFolderPath, true);
  65. EditorUtility.RevealInFinder(mainFolderPath);
  66. }
  67. protected static string FindLanguageInDictionary(Dictionary<string, string> dictionary)
  68. {
  69. foreach (var localizationMember in dictionary)
  70. {
  71. if (localizationMember.Key == SteamVR_Input_ActionFile_LocalizationItem.languageTagKeyName)
  72. return localizationMember.Value;
  73. }
  74. return null;
  75. }
  76. protected static int ImportLocalization(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile, SteamVR_PartialInputBindings partialBinding)
  77. {
  78. int count = 0;
  79. foreach (var newLocalDictionary in newActionsFile.localization)
  80. {
  81. string newLanguage = FindLanguageInDictionary(newLocalDictionary);
  82. if (string.IsNullOrEmpty(newLanguage))
  83. {
  84. Debug.LogError("<b>[SteamVR Input]</b> Localization entry in partial actions file is missing a language tag: " + partialBinding.path);
  85. continue;
  86. }
  87. int currentLanguage = -1;
  88. for (int currentLanguageIndex = 0; currentLanguageIndex < currentActionsFile.localization.Count; currentLanguageIndex++)
  89. {
  90. string language = FindLanguageInDictionary(currentActionsFile.localization[currentLanguageIndex]);
  91. if (newLanguage == language)
  92. {
  93. currentLanguage = currentLanguageIndex;
  94. break;
  95. }
  96. }
  97. if (currentLanguage == -1)
  98. {
  99. Dictionary<string, string> newDictionary = new Dictionary<string, string>();
  100. foreach (var element in newLocalDictionary)
  101. {
  102. newDictionary.Add(element.Key, element.Value);
  103. count++;
  104. }
  105. currentActionsFile.localization.Add(newDictionary);
  106. }
  107. else
  108. {
  109. foreach (var element in newLocalDictionary)
  110. {
  111. Dictionary<string, string> currentDictionary = currentActionsFile.localization[currentLanguage];
  112. bool exists = currentDictionary.Any(inCurrent => inCurrent.Key == element.Key);
  113. if (exists)
  114. {
  115. //todo: should we overwrite?
  116. currentDictionary[element.Key] = element.Value;
  117. }
  118. else
  119. {
  120. currentDictionary.Add(element.Key, element.Value);
  121. count++;
  122. }
  123. }
  124. }
  125. }
  126. return count;
  127. }
  128. protected static int ImportActionSets(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile)
  129. {
  130. int count = 0;
  131. foreach (var newSet in newActionsFile.action_sets)
  132. {
  133. if (currentActionsFile.action_sets.Any(setInCurrent => newSet.name == setInCurrent.name) == false)
  134. {
  135. currentActionsFile.action_sets.Add(newSet.GetCopy());
  136. count++;
  137. }
  138. }
  139. return count;
  140. }
  141. protected static int ImportActions(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile)
  142. {
  143. int count = 0;
  144. foreach (var newAction in newActionsFile.actions)
  145. {
  146. if (currentActionsFile.actions.Any(actionInCurrent => newAction.name == actionInCurrent.name) == false)
  147. {
  148. currentActionsFile.actions.Add(newAction.GetCopy());
  149. count++;
  150. }
  151. else
  152. {
  153. SteamVR_Input_ActionFile_Action existingAction = currentActionsFile.actions.First(actionInCurrent => newAction.name == actionInCurrent.name);
  154. //todo: better merge? should we overwrite?
  155. existingAction.type = newAction.type;
  156. existingAction.scope = newAction.scope;
  157. existingAction.skeleton = newAction.skeleton;
  158. existingAction.requirement = newAction.requirement;
  159. }
  160. }
  161. return count;
  162. }
  163. protected static SteamVR_Input_BindingFile GetBindingFileObject(string path)
  164. {
  165. if (File.Exists(path) == false)
  166. {
  167. Debug.LogError("<b>[SteamVR]</b> Could not access file at path: " + path);
  168. return null;
  169. }
  170. string jsonText = File.ReadAllText(path);
  171. SteamVR_Input_BindingFile importingBindingFile = JsonConvert.DeserializeObject<SteamVR_Input_BindingFile>(jsonText);
  172. return importingBindingFile;
  173. }
  174. protected static void WriteBindingFileObject(SteamVR_Input_BindingFile currentBindingFile, string currentBindingPath)
  175. {
  176. if (File.Exists(currentBindingPath))
  177. {
  178. FileInfo fileInfo = new FileInfo(currentBindingPath);
  179. fileInfo.IsReadOnly = false;
  180. }
  181. string newJSON = JsonConvert.SerializeObject(currentBindingFile, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
  182. File.WriteAllText(currentBindingPath, newJSON);
  183. Debug.Log("<b>[SteamVR]</b> Added action bindings to: " + currentBindingPath);
  184. }
  185. protected static void ImportBindings(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile, string directory)
  186. {
  187. foreach (var newDefaultPath in newActionsFile.default_bindings)
  188. {
  189. if (currentActionsFile.default_bindings.Any(currentDefaultPath => newDefaultPath.controller_type == currentDefaultPath.controller_type) == false)
  190. {
  191. currentActionsFile.default_bindings.Add(newDefaultPath.GetCopy());
  192. string bindingPath = Path.Combine(directory, newDefaultPath.binding_url);
  193. File.Copy(bindingPath, newDefaultPath.binding_url);
  194. }
  195. else
  196. {
  197. string currentBindingDirectory = SteamVR_Input.GetActionsFileFolder();
  198. string currentBindingPath = currentActionsFile.default_bindings.First(binding => binding.controller_type == newDefaultPath.controller_type).binding_url;
  199. currentBindingPath = Path.Combine(currentBindingDirectory, currentBindingPath);
  200. SteamVR_Input_BindingFile currentBindingFile = GetBindingFileObject(currentBindingPath);
  201. if (currentBindingFile == null)
  202. {
  203. Debug.LogError("<b>[SteamVR]</b> There was an error deserializing the binding at path: " + currentBindingPath);
  204. continue;
  205. }
  206. SteamVR_Input_BindingFile importingBindingFile = GetBindingFileObject(Path.Combine(directory, newDefaultPath.binding_url));
  207. if (importingBindingFile == null)
  208. {
  209. Debug.LogError("<b>[SteamVR]</b> There was an error deserializing the binding at path: " + Path.Combine(directory, newDefaultPath.binding_url));
  210. continue;
  211. }
  212. bool changed = false;
  213. foreach (var importingActionList in importingBindingFile.bindings)
  214. {
  215. if (currentBindingFile.bindings.Any(binding => binding.Key == importingActionList.Key))
  216. {
  217. var currentSetBinding = currentBindingFile.bindings.FirstOrDefault(binding => binding.Key == importingActionList.Key);
  218. //todo: better merge? if we don't have an exact copy of the item then we add a new one
  219. foreach (var importingChord in importingActionList.Value.chords)
  220. {
  221. if (currentSetBinding.Value.chords.Any(currentChord => importingChord.Equals(currentChord)) == false)
  222. {
  223. changed = true;
  224. currentSetBinding.Value.chords.Add(importingChord);
  225. }
  226. }
  227. foreach (var importingHaptic in importingActionList.Value.haptics)
  228. {
  229. if (currentSetBinding.Value.haptics.Any(currentHaptic => importingHaptic.Equals(currentHaptic)) == false)
  230. {
  231. changed = true;
  232. currentSetBinding.Value.haptics.Add(importingHaptic);
  233. }
  234. }
  235. foreach (var importingPose in importingActionList.Value.poses)
  236. {
  237. if (currentSetBinding.Value.poses.Any(currentPose => importingPose.Equals(currentPose)) == false)
  238. {
  239. changed = true;
  240. currentSetBinding.Value.poses.Add(importingPose);
  241. }
  242. }
  243. foreach (var importingSkeleton in importingActionList.Value.skeleton)
  244. {
  245. if (currentSetBinding.Value.skeleton.Any(currentSkeleton => importingSkeleton.Equals(currentSkeleton)) == false)
  246. {
  247. changed = true;
  248. currentSetBinding.Value.skeleton.Add(importingSkeleton);
  249. }
  250. }
  251. foreach (var importingSource in importingActionList.Value.sources)
  252. {
  253. if (currentSetBinding.Value.sources.Any(currentSource => importingSource.Equals(currentSource)) == false)
  254. {
  255. changed = true;
  256. currentSetBinding.Value.sources.Add(importingSource);
  257. }
  258. }
  259. }
  260. else
  261. {
  262. changed = true;
  263. currentBindingFile.bindings.Add(importingActionList.Key, importingActionList.Value);
  264. }
  265. }
  266. if (changed)
  267. {
  268. WriteBindingFileObject(currentBindingFile, currentBindingPath);
  269. }
  270. }
  271. }
  272. }
  273. public static void CleanBindings(bool verbose = false)
  274. {
  275. SteamVR_Input.InitializeFile(true);
  276. SteamVR_Input_ActionFile currentActionsFile = SteamVR_Input.actionFile;
  277. for (int localizationIndex = 0; localizationIndex < currentActionsFile.localization.Count; localizationIndex++)
  278. {
  279. Dictionary<string, string> dictionary = currentActionsFile.localization[localizationIndex];
  280. bool removed;
  281. do
  282. {
  283. removed = false;
  284. string missingAction = null;
  285. foreach (string key in dictionary.Keys)
  286. {
  287. if (key == SteamVR_Input_ActionFile_LocalizationItem.languageTagKeyName)
  288. continue;
  289. if (currentActionsFile.actions.Any(action => string.Equals(action.name, key, StringComparison.CurrentCultureIgnoreCase)) == false)
  290. {
  291. missingAction = key;
  292. }
  293. }
  294. if (missingAction != null)
  295. {
  296. removed = true;
  297. dictionary.Remove(missingAction);
  298. if (verbose)
  299. Debug.Log("<b>[SteamVR Input]</b> Removing localization entry for: " + missingAction);
  300. }
  301. } while (removed);
  302. }
  303. for (int bindingIndex = 0; bindingIndex < currentActionsFile.default_bindings.Count; bindingIndex++)
  304. {
  305. SteamVR_Input_ActionFile_DefaultBinding currentBinding = currentActionsFile.default_bindings[bindingIndex];
  306. string bindingPath = Path.Combine(SteamVR_Input.GetActionsFileFolder(), currentBinding.binding_url);
  307. if (File.Exists(bindingPath) == false)
  308. {
  309. if (verbose)
  310. Debug.Log("<b>[SteamVR Input]</b> Removing binding entry for missing file: '" + currentBinding.controller_type + "' at: " + bindingPath);
  311. currentActionsFile.default_bindings.RemoveAt(bindingIndex);
  312. bindingIndex--;
  313. continue;
  314. }
  315. SteamVR_Input_BindingFile bindingFile = GetBindingFileObject(bindingPath);
  316. if (bindingFile == null)
  317. {
  318. Debug.LogError("<b>[SteamVR Input]</b> Error parsing binding file for: '" + currentBinding.controller_type + "' at: " + bindingPath);
  319. continue;
  320. }
  321. int changed = 0;
  322. foreach (var actionList in bindingFile.bindings)
  323. {
  324. for (int itemIndex = 0; itemIndex < actionList.Value.chords.Count; itemIndex++)
  325. {
  326. string outputActionPath = actionList.Value.chords[itemIndex].output;
  327. if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false)
  328. {
  329. if (verbose)
  330. Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing chord binding for action: " + outputActionPath);
  331. actionList.Value.chords.RemoveAt(itemIndex);
  332. itemIndex--;
  333. changed++;
  334. }
  335. }
  336. for (int itemIndex = 0; itemIndex < actionList.Value.haptics.Count; itemIndex++)
  337. {
  338. string outputActionPath = actionList.Value.haptics[itemIndex].output;
  339. if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false)
  340. {
  341. if (verbose)
  342. Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing haptics binding for action: " + outputActionPath);
  343. actionList.Value.haptics.RemoveAt(itemIndex);
  344. itemIndex--;
  345. changed++;
  346. }
  347. }
  348. for (int itemIndex = 0; itemIndex < actionList.Value.poses.Count; itemIndex++)
  349. {
  350. string outputActionPath = actionList.Value.poses[itemIndex].output;
  351. if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false)
  352. {
  353. if (verbose)
  354. Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing pose binding for action: " + outputActionPath);
  355. actionList.Value.poses.RemoveAt(itemIndex);
  356. itemIndex--;
  357. changed++;
  358. }
  359. }
  360. for (int itemIndex = 0; itemIndex < actionList.Value.skeleton.Count; itemIndex++)
  361. {
  362. string outputActionPath = actionList.Value.skeleton[itemIndex].output;
  363. if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false)
  364. {
  365. if (verbose)
  366. Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing skeleton binding for action: " + outputActionPath);
  367. actionList.Value.skeleton.RemoveAt(itemIndex);
  368. itemIndex--;
  369. changed++;
  370. }
  371. }
  372. for (int itemIndex = 0; itemIndex < actionList.Value.sources.Count; itemIndex++)
  373. {
  374. string outputActionPath = actionList.Value.sources[itemIndex].GetOutput();
  375. if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false)
  376. {
  377. if (verbose)
  378. Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing source binding for action: " + outputActionPath);
  379. actionList.Value.sources.RemoveAt(itemIndex);
  380. itemIndex--;
  381. changed++;
  382. }
  383. }
  384. }
  385. if (changed > 0)
  386. {
  387. WriteBindingFileObject(bindingFile, bindingPath);
  388. }
  389. }
  390. if (SteamVR_Input.HasFileInMemoryBeenModified())
  391. {
  392. SteamVR_Input.actionFile.Save(SteamVR_Input.GetActionsFilePath());
  393. if (verbose)
  394. Debug.Log("<b>[SteamVR Input]</b> Saved new actions file: " + SteamVR_Input.GetActionsFilePath());
  395. }
  396. }
  397. protected static void ImportPartialBinding(SteamVR_PartialInputBindings partialBinding)
  398. {
  399. SteamVR_Input.InitializeFile();
  400. SteamVR_Input_ActionFile currentActionsFile = SteamVR_Input.actionFile;
  401. SteamVR_Input_ActionFile newActionsFile = ReadJson<SteamVR_Input_ActionFile>(partialBinding.GetActionsPath());
  402. /*
  403. int sets = ImportActionSets(currentActionsFile, newActionsFile);
  404. int locs = ImportLocalization(currentActionsFile, newActionsFile, partialBinding);
  405. int actions = ImportActions(currentActionsFile, newActionsFile);
  406. */
  407. ImportActionSets(currentActionsFile, newActionsFile);
  408. ImportLocalization(currentActionsFile, newActionsFile, partialBinding);
  409. ImportActions(currentActionsFile, newActionsFile);
  410. if (SteamVR_Input.HasFileInMemoryBeenModified())
  411. {
  412. SteamVR_Input.actionFile.Save(SteamVR_Input.GetActionsFilePath());
  413. Debug.Log("<b>[SteamVR]</b> Saved new actions file: " + SteamVR_Input.GetActionsFilePath());
  414. }
  415. ImportBindings(currentActionsFile, newActionsFile, partialBinding.GetDirectory());
  416. partialBinding.imported = true;
  417. partialBinding.Save();
  418. SteamVR_Input.InitializeFile(true);
  419. SteamVR_Input_EditorWindow.ReopenWindow();
  420. //todo: ask first?
  421. /*string dialogText = string.Format("{0} new action sets, {1} new actions, and {2} new localization strings have been added. Would you like to regenerate SteamVR Input code files?", sets, actions, locs);
  422. bool confirm = EditorUtility.DisplayDialog("SteamVR Input", dialogText, "Generate", "Cancel");
  423. if (confirm)
  424. SteamVR_Input_Generator.BeginGeneration();
  425. */
  426. SteamVR_Input_Generator.BeginGeneration();
  427. Debug.Log("<b>[SteamVR]</b> Reloaded actions file with additional actions from " + partialBinding.name);
  428. }
  429. protected static void ReplaceBinding(SteamVR_PartialInputBindings partialBinding)
  430. {
  431. SteamVR_Input.DeleteManifestAndBindings();
  432. string newActionsFilePath = partialBinding.GetActionsPath();
  433. if (File.Exists(newActionsFilePath))
  434. {
  435. File.Copy(newActionsFilePath, SteamVR_Input.GetActionsFilePath());
  436. }
  437. string bindingsFolder = SteamVR_Input.GetActionsFileFolder();
  438. SteamVR_Input_ActionFile newActionsFile = ReadJson<SteamVR_Input_ActionFile>(SteamVR_Input.GetActionsFilePath());
  439. string partialBindingDirectory = partialBinding.GetDirectory();
  440. foreach (var newDefaultPath in newActionsFile.default_bindings)
  441. {
  442. string bindingPath = Path.Combine(partialBindingDirectory, newDefaultPath.binding_url);
  443. string newBindingPath = Path.Combine(bindingsFolder, newDefaultPath.binding_url);
  444. File.Copy(bindingPath, newBindingPath, true);
  445. }
  446. partialBinding.imported = true;
  447. partialBinding.Save();
  448. SteamVR_Input.InitializeFile(true);
  449. SteamVR_Input_EditorWindow.ReopenWindow();
  450. //todo: ask first?
  451. /*string dialogText = string.Format("{0} new action sets, {1} new actions, and {2} new localization strings have been added. Would you like to regenerate SteamVR Input code files?", sets, actions, locs);
  452. bool confirm = EditorUtility.DisplayDialog("SteamVR Input", dialogText, "Generate", "Cancel");
  453. if (confirm)
  454. SteamVR_Input_Generator.BeginGeneration();
  455. */
  456. SteamVR_Input_Generator.BeginGeneration();
  457. Debug.Log("<b>[SteamVR Input]</b> Reloaded with new actions from " + partialBinding.name);
  458. }
  459. protected static T ReadJson<T>(string path)
  460. {
  461. if (File.Exists(path))
  462. {
  463. string jsonText = File.ReadAllText(path);
  464. return JsonConvert.DeserializeObject<T>(jsonText);
  465. }
  466. return default(T);
  467. }
  468. protected static List<SteamVR_Input_ActionFile_Action> RemoveOldActions(List<SteamVR_PartialInputBindings> partialBindingList)
  469. {
  470. List<SteamVR_Input_ActionFile_Action> toRemove = new List<SteamVR_Input_ActionFile_Action>();
  471. SteamVR_Input_ActionFile newestActionsFile = ReadJson<SteamVR_Input_ActionFile>(partialBindingList[0].GetActionsPath());
  472. for (int partialBindingIndex = 1; partialBindingIndex < partialBindingList.Count; partialBindingIndex++)
  473. {
  474. SteamVR_Input_ActionFile oldActionsFile = ReadJson<SteamVR_Input_ActionFile>(partialBindingList[partialBindingIndex].GetActionsPath());
  475. for (int oldActionIndex = 0; oldActionIndex < oldActionsFile.actions.Count; oldActionIndex++)
  476. {
  477. var oldAction = oldActionsFile.actions[oldActionIndex];
  478. if (newestActionsFile.actions.Any(newAction => oldAction.Equals(newAction)) == false)
  479. {
  480. var existing = SteamVR_Input.actionFile.actions.FirstOrDefault(action => oldAction.Equals(action));
  481. if (existing != null)
  482. {
  483. SteamVR_Input.actionFile.actions.Remove(existing);
  484. toRemove.Add(oldAction);
  485. }
  486. }
  487. }
  488. }
  489. return toRemove;
  490. }
  491. protected static List<SteamVR_Input_ActionFile_ActionSet> RemoveOldActionSets(List<SteamVR_PartialInputBindings> partialBindingList)
  492. {
  493. List<SteamVR_Input_ActionFile_ActionSet> toRemove = new List<SteamVR_Input_ActionFile_ActionSet>();
  494. SteamVR_Input_ActionFile newestActionsFile = ReadJson<SteamVR_Input_ActionFile>(partialBindingList[0].GetActionsPath());
  495. for (int partialBindingIndex = 1; partialBindingIndex < partialBindingList.Count; partialBindingIndex++)
  496. {
  497. SteamVR_Input_ActionFile oldActionsFile = ReadJson<SteamVR_Input_ActionFile>(partialBindingList[0].GetActionsPath());
  498. for (int oldActionIndex = 0; oldActionIndex < oldActionsFile.action_sets.Count; oldActionIndex++)
  499. {
  500. var oldActionSet = oldActionsFile.action_sets[oldActionIndex];
  501. if (newestActionsFile.action_sets.Any(newAction => oldActionSet.Equals(newAction)) == false)
  502. {
  503. var existing = SteamVR_Input.actionFile.action_sets.FirstOrDefault(actionSet => oldActionSet.Equals(actionSet));
  504. if (existing != null)
  505. {
  506. SteamVR_Input.actionFile.action_sets.Remove(existing);
  507. toRemove.Add(oldActionSet);
  508. }
  509. }
  510. }
  511. }
  512. return toRemove;
  513. }
  514. protected static int RemoveOldLocalizations(List<SteamVR_Input_ActionFile_Action> removedActionList)
  515. {
  516. int count = 0;
  517. foreach (var action in removedActionList)
  518. {
  519. foreach (var locDictionary in SteamVR_Input.actionFile.localization)
  520. {
  521. bool removed = locDictionary.Remove(action.name);
  522. if (removed)
  523. count++;
  524. }
  525. }
  526. return count;
  527. }
  528. protected static void RemoveOldActionsAndSetsFromBindings(List<SteamVR_Input_ActionFile_ActionSet> setsToRemove, List<SteamVR_Input_ActionFile_Action> actionsToRemove)
  529. {
  530. foreach (var defaultBindingItem in SteamVR_Input.actionFile.default_bindings)
  531. {
  532. string currentBindingPath = defaultBindingItem.binding_url;
  533. SteamVR_Input_BindingFile currentBindingFile = GetBindingFileObject(currentBindingPath);
  534. if (currentBindingFile == null)
  535. {
  536. Debug.LogError("<b>[SteamVR]</b> There was an error deserializing the binding at path: " + currentBindingPath);
  537. continue;
  538. }
  539. bool changed = false;
  540. List<string> bindingListsToRemove = new List<string>();
  541. foreach (var actionList in currentBindingFile.bindings)
  542. {
  543. if (setsToRemove.Any(set => set.name == actionList.Key))
  544. {
  545. bindingListsToRemove.Add(actionList.Key);
  546. changed = true;
  547. continue;
  548. }
  549. for (int chordIndex = 0; chordIndex < actionList.Value.chords.Count; chordIndex++)
  550. {
  551. var existingChord = actionList.Value.chords[chordIndex];
  552. if (actionsToRemove.Any(action => action.name == existingChord.output))
  553. {
  554. actionList.Value.chords.Remove(existingChord);
  555. chordIndex--;
  556. changed = true;
  557. }
  558. }
  559. for (int hapticIndex = 0; hapticIndex < actionList.Value.haptics.Count; hapticIndex++)
  560. {
  561. var existingHaptic = actionList.Value.haptics[hapticIndex];
  562. if (actionsToRemove.Any(action => action.name == existingHaptic.output))
  563. {
  564. actionList.Value.haptics.Remove(existingHaptic);
  565. hapticIndex--;
  566. changed = true;
  567. }
  568. }
  569. for (int poseIndex = 0; poseIndex < actionList.Value.poses.Count; poseIndex++)
  570. {
  571. var existingPose = actionList.Value.poses[poseIndex];
  572. if (actionsToRemove.Any(action => action.name == existingPose.output))
  573. {
  574. actionList.Value.poses.Remove(existingPose);
  575. poseIndex--;
  576. changed = true;
  577. }
  578. }
  579. for (int skeletonIndex = 0; skeletonIndex < actionList.Value.skeleton.Count; skeletonIndex++)
  580. {
  581. var existingSkeleton = actionList.Value.skeleton[skeletonIndex];
  582. if (actionsToRemove.Any(action => action.name == existingSkeleton.output))
  583. {
  584. actionList.Value.skeleton.Remove(existingSkeleton);
  585. skeletonIndex--;
  586. changed = true;
  587. }
  588. }
  589. for (int sourceIndex = 0; sourceIndex < actionList.Value.sources.Count; sourceIndex++)
  590. {
  591. var existingSource = actionList.Value.sources[sourceIndex];
  592. if (actionsToRemove.Any(action => action.name == existingSource.GetOutput()))
  593. {
  594. actionList.Value.sources.Remove(existingSource);
  595. sourceIndex--;
  596. changed = true;
  597. }
  598. }
  599. }
  600. for (int bindingListToRemoveIndex = 0; bindingListToRemoveIndex < bindingListsToRemove.Count; bindingListToRemoveIndex++)
  601. {
  602. currentBindingFile.bindings.Remove(bindingListsToRemove[bindingListToRemoveIndex]);
  603. }
  604. if (changed)
  605. {
  606. WriteBindingFileObject(currentBindingFile, currentBindingPath);
  607. }
  608. }
  609. }
  610. protected static void RemoveOldPartialBindings(List<SteamVR_PartialInputBindings> partialBindingList)
  611. {
  612. List<SteamVR_Input_ActionFile_Action> actionsToRemove = RemoveOldActions(partialBindingList);
  613. List<SteamVR_Input_ActionFile_ActionSet> setsToRemove = RemoveOldActionSets(partialBindingList);
  614. int sets = setsToRemove.Count;
  615. int actions = actionsToRemove.Count;
  616. int locs = RemoveOldLocalizations(actionsToRemove);
  617. string dialogText = string.Format("We've found a old {0} action sets, {1} actions, and {2} localization entries from old versions of this partial binding. Would you like to remove them from the actions file and default bindings?", sets, actions, locs);
  618. bool confirm = EditorUtility.DisplayDialog("SteamVR Input", dialogText, "Import", "Cancel");
  619. if (confirm)
  620. {
  621. RemoveOldActionsAndSetsFromBindings(setsToRemove, actionsToRemove);
  622. SteamVR_Input.actionFile.Save(SteamVR_Input.GetActionsFilePath());
  623. SteamVR_Input.InitializeFile(true); // reload after the save
  624. }
  625. else
  626. {
  627. SteamVR_Input.InitializeFile(true); // reload since we actually removed the actions / sets to display this message
  628. }
  629. }
  630. protected const string dontAskAgainTemplate = "{0}_{1}_DontAskAgain";
  631. protected static void ConfirmImport(List<SteamVR_PartialInputBindings> partialBindingList)
  632. {
  633. SteamVR_PartialInputBindings partial = partialBindingList.First();
  634. //bool dontAskAgain = EditorPrefs.GetBool(dontAskAgainTemplate, false);
  635. //todo: implement 'do not ask again'
  636. string dialogText = string.Format("We've found a partial SteamVR Input binding for '{0}' version '{1}'. Would you like to import it?", partial.name, partial.version);
  637. bool confirm = EditorUtility.DisplayDialog("SteamVR Input", dialogText, "Import", "Cancel");
  638. if (confirm)
  639. {
  640. bool actionsExists = SteamVR_Input.DoesActionsFileExist();
  641. if (actionsExists)
  642. {
  643. string mergeDialogText = "You have two options for importing this binding:\n Replace your current action file (delete all your actions)\n Merge the partial action file with your existing actions";
  644. bool shouldMerge = EditorUtility.DisplayDialog("SteamVR Input", mergeDialogText, "Merge", "Replace");
  645. if (shouldMerge)
  646. {
  647. ImportPartialBinding(partial);
  648. }
  649. else
  650. {
  651. ReplaceBinding(partial);
  652. }
  653. }
  654. else
  655. {
  656. ReplaceBinding(partial);
  657. }
  658. if (partialBindingList.Count > 1)
  659. {
  660. RemoveOldPartialBindings(partialBindingList);
  661. }
  662. }
  663. }
  664. public static Dictionary<string, List<SteamVR_PartialInputBindings>> ScanForPartials()
  665. {
  666. string[] partialManifestPaths = Directory.GetFiles(Application.dataPath, partialManifestFilename, SearchOption.AllDirectories);
  667. Dictionary<string, List<SteamVR_PartialInputBindings>> partialBindings = new Dictionary<string, List<SteamVR_PartialInputBindings>>();
  668. for (int partialIndex = 0; partialIndex < partialManifestPaths.Length; partialIndex++)
  669. {
  670. string path = partialManifestPaths[partialIndex];
  671. string jsonText = File.ReadAllText(path);
  672. SteamVR_PartialInputBindings partialBinding = JsonConvert.DeserializeObject<SteamVR_PartialInputBindings>(jsonText);
  673. partialBinding.path = path;
  674. if (partialBindings.ContainsKey(partialBinding.name))
  675. {
  676. for (int versionIndex = 0; versionIndex < partialBindings[partialBinding.name].Count; versionIndex++)
  677. {
  678. if (partialBinding.version < partialBindings[partialBinding.name][versionIndex].version)
  679. partialBindings[partialBinding.name].Insert(versionIndex, partialBinding);
  680. }
  681. }
  682. else
  683. {
  684. partialBindings.Add(partialBinding.name, new List<SteamVR_PartialInputBindings>() { partialBinding });
  685. }
  686. }
  687. return partialBindings;
  688. }
  689. }
  690. public class SteamVR_PartialInputBindings
  691. {
  692. public string name;
  693. public int version;
  694. public bool overwriteOld;
  695. public bool removeUnused;
  696. public bool imported;
  697. [JsonIgnore]
  698. public string path { get; set; }
  699. public string GetActionsPath()
  700. {
  701. return Path.Combine(GetDirectory(), "actions.json");
  702. }
  703. public string GetDirectory()
  704. {
  705. return new FileInfo(path).Directory.FullName;
  706. }
  707. public void Save()
  708. {
  709. FileInfo existingActionsFile = new FileInfo(path);
  710. if (existingActionsFile.Exists)
  711. {
  712. existingActionsFile.IsReadOnly = false;
  713. }
  714. //SanitizeActionFile(); //todo: shouldn't we be doing this?
  715. string json = JsonConvert.SerializeObject(this, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
  716. File.WriteAllText(path, json);
  717. }
  718. }
  719. }