protected static SteamVR_Input_BindingFile GetBindingFileObject(string path) { if (File.Exists(path) == false) { Debug.LogError("<b>[SteamVR]</b> Could not access file at path: " + path); return(null); } string jsonText = File.ReadAllText(path); SteamVR_Input_BindingFile importingBindingFile = JsonConvert.DeserializeObject <SteamVR_Input_BindingFile>(jsonText); return(importingBindingFile); }
protected static void WriteBindingFileObject(SteamVR_Input_BindingFile currentBindingFile, string currentBindingPath) { if (File.Exists(currentBindingPath)) { FileInfo fileInfo = new FileInfo(currentBindingPath); fileInfo.IsReadOnly = false; } string newJSON = JsonConvert.SerializeObject(currentBindingFile, Formatting.Indented, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); File.WriteAllText(currentBindingPath, newJSON); Debug.Log("<b>[SteamVR]</b> Added action bindings to: " + currentBindingPath); }
protected static void RemoveOldActionsAndSetsFromBindings(List <SteamVR_Input_ActionFile_ActionSet> setsToRemove, List <SteamVR_Input_ActionFile_Action> actionsToRemove) { foreach (var defaultBindingItem in SteamVR_Input.actionFile.default_bindings) { string currentBindingPath = defaultBindingItem.binding_url; SteamVR_Input_BindingFile currentBindingFile = GetBindingFileObject(currentBindingPath); if (currentBindingFile == null) { Debug.LogError("<b>[SteamVR]</b> There was an error deserializing the binding at path: " + currentBindingPath); continue; } bool changed = false; List <string> bindingListsToRemove = new List <string>(); foreach (var actionList in currentBindingFile.bindings) { if (setsToRemove.Any(set => set.name == actionList.Key)) { bindingListsToRemove.Add(actionList.Key); changed = true; continue; } for (int chordIndex = 0; chordIndex < actionList.Value.chords.Count; chordIndex++) { var existingChord = actionList.Value.chords[chordIndex]; if (actionsToRemove.Any(action => action.name == existingChord.output)) { actionList.Value.chords.Remove(existingChord); chordIndex--; changed = true; } } for (int hapticIndex = 0; hapticIndex < actionList.Value.haptics.Count; hapticIndex++) { var existingHaptic = actionList.Value.haptics[hapticIndex]; if (actionsToRemove.Any(action => action.name == existingHaptic.output)) { actionList.Value.haptics.Remove(existingHaptic); hapticIndex--; changed = true; } } for (int poseIndex = 0; poseIndex < actionList.Value.poses.Count; poseIndex++) { var existingPose = actionList.Value.poses[poseIndex]; if (actionsToRemove.Any(action => action.name == existingPose.output)) { actionList.Value.poses.Remove(existingPose); poseIndex--; changed = true; } } for (int skeletonIndex = 0; skeletonIndex < actionList.Value.skeleton.Count; skeletonIndex++) { var existingSkeleton = actionList.Value.skeleton[skeletonIndex]; if (actionsToRemove.Any(action => action.name == existingSkeleton.output)) { actionList.Value.skeleton.Remove(existingSkeleton); skeletonIndex--; changed = true; } } for (int sourceIndex = 0; sourceIndex < actionList.Value.sources.Count; sourceIndex++) { var existingSource = actionList.Value.sources[sourceIndex]; if (actionsToRemove.Any(action => action.name == existingSource.GetOutput())) { actionList.Value.sources.Remove(existingSource); sourceIndex--; changed = true; } } } for (int bindingListToRemoveIndex = 0; bindingListToRemoveIndex < bindingListsToRemove.Count; bindingListToRemoveIndex++) { currentBindingFile.bindings.Remove(bindingListsToRemove[bindingListToRemoveIndex]); } if (changed) { WriteBindingFileObject(currentBindingFile, currentBindingPath); } } }
public static void CleanBindings(bool verbose = false) { SteamVR_Input.InitializeFile(true); SteamVR_Input_ActionFile currentActionsFile = SteamVR_Input.actionFile; for (int localizationIndex = 0; localizationIndex < currentActionsFile.localization.Count; localizationIndex++) { Dictionary <string, string> dictionary = currentActionsFile.localization[localizationIndex]; bool removed; do { removed = false; string missingAction = null; foreach (string key in dictionary.Keys) { if (key == SteamVR_Input_ActionFile_LocalizationItem.languageTagKeyName) { continue; } if (currentActionsFile.actions.Any(action => string.Equals(action.name, key, StringComparison.CurrentCultureIgnoreCase)) == false) { missingAction = key; } } if (missingAction != null) { removed = true; dictionary.Remove(missingAction); if (verbose) { Debug.Log("<b>[SteamVR Input]</b> Removing localization entry for: " + missingAction); } } } while (removed); } for (int bindingIndex = 0; bindingIndex < currentActionsFile.default_bindings.Count; bindingIndex++) { SteamVR_Input_ActionFile_DefaultBinding currentBinding = currentActionsFile.default_bindings[bindingIndex]; if (File.Exists(currentBinding.binding_url) == false) { if (verbose) { Debug.Log("<b>[SteamVR Input]</b> Removing binding entry for missing file: '" + currentBinding.controller_type + "' at: " + currentBinding.binding_url); } currentActionsFile.default_bindings.RemoveAt(bindingIndex); bindingIndex--; continue; } SteamVR_Input_BindingFile bindingFile = GetBindingFileObject(currentBinding.binding_url); if (bindingFile == null) { Debug.LogError("<b>[SteamVR Input]</b> Error parsing binding file for: '" + currentBinding.controller_type + "' at: " + currentBinding.binding_url); continue; } int changed = 0; foreach (var actionList in bindingFile.bindings) { for (int itemIndex = 0; itemIndex < actionList.Value.chords.Count; itemIndex++) { string outputActionPath = actionList.Value.chords[itemIndex].output; if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false) { if (verbose) { Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing chord binding for action: " + outputActionPath); } actionList.Value.chords.RemoveAt(itemIndex); itemIndex--; changed++; } } for (int itemIndex = 0; itemIndex < actionList.Value.haptics.Count; itemIndex++) { string outputActionPath = actionList.Value.haptics[itemIndex].output; if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false) { if (verbose) { Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing haptics binding for action: " + outputActionPath); } actionList.Value.haptics.RemoveAt(itemIndex); itemIndex--; changed++; } } for (int itemIndex = 0; itemIndex < actionList.Value.poses.Count; itemIndex++) { string outputActionPath = actionList.Value.poses[itemIndex].output; if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false) { if (verbose) { Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing pose binding for action: " + outputActionPath); } actionList.Value.poses.RemoveAt(itemIndex); itemIndex--; changed++; } } for (int itemIndex = 0; itemIndex < actionList.Value.skeleton.Count; itemIndex++) { string outputActionPath = actionList.Value.skeleton[itemIndex].output; if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false) { if (verbose) { Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing skeleton binding for action: " + outputActionPath); } actionList.Value.skeleton.RemoveAt(itemIndex); itemIndex--; changed++; } } for (int itemIndex = 0; itemIndex < actionList.Value.sources.Count; itemIndex++) { string outputActionPath = actionList.Value.sources[itemIndex].GetOutput(); if (currentActionsFile.actions.Any(action => string.Equals(action.name, outputActionPath, StringComparison.CurrentCultureIgnoreCase)) == false) { if (verbose) { Debug.Log("<b>[SteamVR Input]</b> " + currentBinding.controller_type + ": Removing source binding for action: " + outputActionPath); } actionList.Value.sources.RemoveAt(itemIndex); itemIndex--; changed++; } } } if (changed > 0) { WriteBindingFileObject(bindingFile, currentBinding.binding_url); } } if (SteamVR_Input.HasFileInMemoryBeenModified()) { SteamVR_Input.actionFile.Save(SteamVR_Input.actionsFilePath); if (verbose) { Debug.Log("<b>[SteamVR Input]</b> Saved new actions file: " + SteamVR_Input.actionsFilePath); } } }
protected static void ImportBindings(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile, string directory) { foreach (var newDefaultPath in newActionsFile.default_bindings) { if (currentActionsFile.default_bindings.Any(currentDefaultPath => newDefaultPath.controller_type == currentDefaultPath.controller_type) == false) { currentActionsFile.default_bindings.Add(newDefaultPath.GetCopy()); string bindingPath = Path.Combine(directory, newDefaultPath.binding_url); File.Copy(bindingPath, newDefaultPath.binding_url); } else { string currentBindingPath = currentActionsFile.default_bindings .First(binding => binding.controller_type == newDefaultPath.controller_type).binding_url; SteamVR_Input_BindingFile currentBindingFile = GetBindingFileObject(currentBindingPath); if (currentBindingFile == null) { Debug.LogError("<b>[SteamVR]</b> There was an error deserializing the binding at path: " + currentBindingPath); continue; } SteamVR_Input_BindingFile importingBindingFile = GetBindingFileObject(newDefaultPath.binding_url); if (importingBindingFile == null) { Debug.LogError("<b>[SteamVR]</b> There was an error deserializing the binding at path: " + newDefaultPath.binding_url); continue; } bool changed = false; foreach (var importingActionList in importingBindingFile.bindings) { if (currentBindingFile.bindings.Any(binding => binding.Key == importingActionList.Key)) { var currentSetBinding = currentBindingFile.bindings.FirstOrDefault(binding => binding.Key == importingActionList.Key); //todo: better merge? if we don't have an exact copy of the item then we add a new one foreach (var importingChord in importingActionList.Value.chords) { if (currentSetBinding.Value.chords.Any(currentChord => importingChord.Equals(currentChord)) == false) { changed = true; currentSetBinding.Value.chords.Add(importingChord); } } foreach (var importingHaptic in importingActionList.Value.haptics) { if (currentSetBinding.Value.haptics.Any(currentHaptic => importingHaptic.Equals(currentHaptic)) == false) { changed = true; currentSetBinding.Value.haptics.Add(importingHaptic); } } foreach (var importingPose in importingActionList.Value.poses) { if (currentSetBinding.Value.poses.Any(currentPose => importingPose.Equals(currentPose)) == false) { changed = true; currentSetBinding.Value.poses.Add(importingPose); } } foreach (var importingSkeleton in importingActionList.Value.skeleton) { if (currentSetBinding.Value.skeleton.Any(currentSkeleton => importingSkeleton.Equals(currentSkeleton)) == false) { changed = true; currentSetBinding.Value.skeleton.Add(importingSkeleton); } } foreach (var importingSource in importingActionList.Value.sources) { if (currentSetBinding.Value.sources.Any(currentSource => importingSource.Equals(currentSource)) == false) { changed = true; currentSetBinding.Value.sources.Add(importingSource); } } } else { changed = true; currentBindingFile.bindings.Add(importingActionList.Key, importingActionList.Value); } } if (changed) { WriteBindingFileObject(currentBindingFile, currentBindingPath); } } } }