Example #1
0
        protected static int ImportActions(SteamVR_Input_ActionFile currentActionsFile,
                                           SteamVR_Input_ActionFile newActionsFile)
        {
            int count = 0;

            foreach (var newAction in newActionsFile.actions)
            {
                if (currentActionsFile.actions.Any(actionInCurrent => newAction.name == actionInCurrent.name) == false)
                {
                    currentActionsFile.actions.Add(newAction.GetCopy());
                    count++;
                }
                else
                {
                    SteamVR_Input_ActionFile_Action existingAction =
                        currentActionsFile.actions.First(actionInCurrent => newAction.name == actionInCurrent.name);

                    //todo: better merge? should we overwrite?
                    existingAction.type        = newAction.type;
                    existingAction.scope       = newAction.scope;
                    existingAction.skeleton    = newAction.skeleton;
                    existingAction.requirement = newAction.requirement;
                }
            }

            return(count);
        }
Example #2
0
        protected static List <SteamVR_Input_ActionFile_ActionSet> RemoveOldActionSets(
            List <SteamVR_PartialInputBindings> partialBindingList)
        {
            List <SteamVR_Input_ActionFile_ActionSet> toRemove = new List <SteamVR_Input_ActionFile_ActionSet>();

            SteamVR_Input_ActionFile newestActionsFile =
                ReadJson <SteamVR_Input_ActionFile>(partialBindingList[0].GetActionsPath());

            for (int partialBindingIndex = 1; partialBindingIndex < partialBindingList.Count; partialBindingIndex++)
            {
                SteamVR_Input_ActionFile oldActionsFile =
                    ReadJson <SteamVR_Input_ActionFile>(partialBindingList[0].GetActionsPath());

                for (int oldActionIndex = 0; oldActionIndex < oldActionsFile.action_sets.Count; oldActionIndex++)
                {
                    var oldActionSet = oldActionsFile.action_sets[oldActionIndex];

                    if (newestActionsFile.action_sets.Any(newAction => oldActionSet.Equals(newAction)) == false)
                    {
                        var existing =
                            SteamVR_Input.actionFile.action_sets.FirstOrDefault(actionSet =>
                                                                                oldActionSet.Equals(actionSet));
                        if (existing != null)
                        {
                            SteamVR_Input.actionFile.action_sets.Remove(existing);
                            toRemove.Add(oldActionSet);
                        }
                    }
                }
            }

            return(toRemove);
        }
        protected static int ImportLocalization(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile, SteamVR_PartialInputBindings partialBinding)
        {
            int count = 0;

            foreach (var newLocalDictionary in newActionsFile.localization)
            {
                string newLanguage = FindLanguageInDictionary(newLocalDictionary);

                if (string.IsNullOrEmpty(newLanguage))
                {
                    Debug.LogError("<b>[SteamVR Input]</b> Localization entry in partial actions file is missing a language tag: " + partialBinding.path);
                    continue;
                }

                int currentLanguage = -1;
                for (int currentLanguageIndex = 0; currentLanguageIndex < currentActionsFile.localization.Count; currentLanguageIndex++)
                {
                    string language = FindLanguageInDictionary(currentActionsFile.localization[currentLanguageIndex]);
                    if (newLanguage == language)
                    {
                        currentLanguage = currentLanguageIndex;
                        break;
                    }
                }

                if (currentLanguage == -1)
                {
                    Dictionary <string, string> newDictionary = new Dictionary <string, string>();
                    foreach (var element in newLocalDictionary)
                    {
                        newDictionary.Add(element.Key, element.Value);
                        count++;
                    }

                    currentActionsFile.localization.Add(newDictionary);
                }
                else
                {
                    foreach (var element in newLocalDictionary)
                    {
                        Dictionary <string, string> currentDictionary = currentActionsFile.localization[currentLanguage];
                        bool exists = currentDictionary.Any(inCurrent => inCurrent.Key == element.Key);

                        if (exists)
                        {
                            //todo: should we overwrite?
                            currentDictionary[element.Key] = element.Value;
                        }
                        else
                        {
                            currentDictionary.Add(element.Key, element.Value);
                            count++;
                        }
                    }
                }
            }

            return(count);
        }
Example #4
0
        /// <summary>Gets the last part of the path for this action. Removes "actions" and direction.</summary>
        public string GetShortName()
        {
            if (cachedShortName == null)
            {
                cachedShortName = SteamVR_Input_ActionFile.GetShortName(fullPath);
            }

            return(cachedShortName);
        }
Example #5
0
        public static void CreatePartial(string name, int version, bool overwriteOld, bool removeUnused)
        {
            if (SteamVR_Input.actionFile.action_sets.Any(set => set.name == "default"))
            {
                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");
                if (confirm == false)
                {
                    return;
                }
            }


            string folderName = "SteamVR_" + SteamVR_Input_ActionFile.GetCodeFriendlyName(name);

            string mainFolderPath    = string.Format("{0}", folderName);
            string versionFolderPath = string.Format("{0}/{1}", folderName, version.ToString());
            string manifestPath      = string.Format("{0}/{1}/{2}", folderName, version.ToString(), partialManifestFilename);

            if (Directory.Exists(mainFolderPath) == false)
            {
                Directory.CreateDirectory(mainFolderPath);
            }

            if (Directory.Exists(versionFolderPath) == false)
            {
                Directory.CreateDirectory(versionFolderPath);
            }


            SteamVR_PartialInputBindings partial = new SteamVR_PartialInputBindings();

            partial.name         = name;
            partial.version      = version;
            partial.overwriteOld = overwriteOld;
            partial.removeUnused = removeUnused;


            string jsonText = JsonConvert.SerializeObject(partial, Formatting.Indented,
                                                          new JsonSerializerSettings {
                NullValueHandling = NullValueHandling.Ignore
            });

            if (File.Exists(manifestPath))
            {
                FileInfo manifestFile = new FileInfo(manifestPath);
                manifestFile.IsReadOnly = false;
            }

            File.WriteAllText(manifestPath, jsonText);

            SteamVR_Input.actionFile.CopyFilesToPath(versionFolderPath, true);

            EditorUtility.RevealInFinder(mainFolderPath);
        }
        public static SteamVR_Input_ActionFile Open(string path)
        {
            if (File.Exists(path))
            {
                string jsonText = File.ReadAllText(path);

                SteamVR_Input_ActionFile actionFile = Newtonsoft.Json.JsonConvert.DeserializeObject <SteamVR_Input_ActionFile>(jsonText);
                actionFile.filePath = path;
                actionFile.InitializeHelperLists();

                return(actionFile);
            }

            return(null);
        }
Example #7
0
        protected static int ImportActionSets(SteamVR_Input_ActionFile currentActionsFile, SteamVR_Input_ActionFile newActionsFile)
        {
            int count = 0;

            foreach (var newSet in newActionsFile.action_sets)
            {
                if (currentActionsFile.action_sets.Any(setInCurrent => newSet.name == setInCurrent.name) == false)
                {
                    currentActionsFile.action_sets.Add(newSet.GetCopy());
                    count++;
                }
            }

            return(count);
        }
Example #8
0
        protected static void ImportPartialBinding(SteamVR_PartialInputBindings partialBinding)
        {
            SteamVR_Input.InitializeFile();
            SteamVR_Input_ActionFile currentActionsFile = SteamVR_Input.actionFile;

            SteamVR_Input_ActionFile newActionsFile =
                ReadJson <SteamVR_Input_ActionFile>(partialBinding.GetActionsPath());

            /*
             * int sets = ImportActionSets(currentActionsFile, newActionsFile);
             * int locs = ImportLocalization(currentActionsFile, newActionsFile, partialBinding);
             * int actions = ImportActions(currentActionsFile, newActionsFile);
             */

            ImportActionSets(currentActionsFile, newActionsFile);
            ImportLocalization(currentActionsFile, newActionsFile, partialBinding);
            ImportActions(currentActionsFile, newActionsFile);

            if (SteamVR_Input.HasFileInMemoryBeenModified())
            {
                SteamVR_Input.actionFile.Save(SteamVR_Input.actionsFilePath);

                Debug.Log("<b>[SteamVR]</b> Saved new actions file: " + SteamVR_Input.actionsFilePath);
            }

            ImportBindings(currentActionsFile, newActionsFile, partialBinding.GetDirectory());

            partialBinding.imported = true;
            partialBinding.Save();

            SteamVR_Input.InitializeFile(true);
            SteamVR_Input_EditorWindow.ReopenWindow();

            //todo: ask first?

            /*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);
             *
             * bool confirm = EditorUtility.DisplayDialog("SteamVR Input", dialogText, "Generate", "Cancel");
             * if (confirm)
             *  SteamVR_Input_Generator.BeginGeneration();
             */

            SteamVR_Input_Generator.BeginGeneration();

            Debug.Log("<b>[SteamVR]</b> Reloaded actions file with additional actions from " + partialBinding.name);
        }
        protected static void ReplaceBinding(SteamVR_PartialInputBindings partialBinding)
        {
            SteamVR_Input.DeleteManifestAndBindings();

            string newActionsFilePath = partialBinding.GetActionsPath();

            if (File.Exists(newActionsFilePath))
            {
                File.Copy(newActionsFilePath, SteamVR_Input.GetActionsFilePath());
            }

            string bindingsFolder = SteamVR_Input.GetActionsFileFolder();

            SteamVR_Input_ActionFile newActionsFile = ReadJson <SteamVR_Input_ActionFile>(SteamVR_Input.GetActionsFilePath());
            string partialBindingDirectory          = partialBinding.GetDirectory();

            foreach (var newDefaultPath in newActionsFile.default_bindings)
            {
                string bindingPath    = Path.Combine(partialBindingDirectory, newDefaultPath.binding_url);
                string newBindingPath = Path.Combine(bindingsFolder, newDefaultPath.binding_url);
                File.Copy(bindingPath, newBindingPath, true);
            }

            partialBinding.imported = true;
            partialBinding.Save();

            SteamVR_Input.InitializeFile(true);
            SteamVR_Input_EditorWindow.ReopenWindow();

            //todo: ask first?

            /*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);
             *
             * bool confirm = EditorUtility.DisplayDialog("SteamVR Input", dialogText, "Generate", "Cancel");
             * if (confirm)
             *  SteamVR_Input_Generator.BeginGeneration();
             */

            SteamVR_Input_Generator.BeginGeneration();

            Debug.Log("<b>[SteamVR Input]</b> Reloaded with new actions from " + partialBinding.name);
        }
Example #10
0
        /// <summary>
        /// Load from disk and deserialize the actions file
        /// </summary>
        /// <param name="force">Force a refresh of this file from disk</param>
        public static bool InitializeFile(bool force = false)
        {
            string projectPath = Application.dataPath;
            int    lastIndex   = projectPath.LastIndexOf("/");

            projectPath     = projectPath.Remove(lastIndex, projectPath.Length - lastIndex);
            actionsFilePath = Path.Combine(projectPath, SteamVR_Settings.instance.actionsFilePath);

            string jsonText = null;

            if (File.Exists(actionsFilePath))
            {
                jsonText = System.IO.File.ReadAllText(actionsFilePath);
            }
            else
            {
                Debug.LogErrorFormat("[SteamVR] Actions file does not exist in project root: {0}", actionsFilePath);
                return(false);
            }

            if (fileInitialized == true || (fileInitialized == true && force == false))
            {
                string newHash = SteamVR_Utils.GetBadMD5Hash(jsonText);

                if (newHash == actionFileHash)
                {
                    return(true);
                }

                actionFileHash = newHash;
            }

            actionFile = Newtonsoft.Json.JsonConvert.DeserializeObject <SteamVR_Input_ActionFile>(jsonText);
            actionFile.InitializeHelperLists();
            fileInitialized = true;
            return(true);
        }
Example #11
0
        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);
                }
            }
        }
Example #12
0
        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);
                    }
                }
            }
        }