public static bool LoadBindingConfigFromFile(string configPath)
        {
            if (string.IsNullOrEmpty(configPath) || !File.Exists(configPath))
            {
                return(false);
            }

            using (var inputFile = new StreamReader(configPath))
            {
                s_bindingConfig = JsonUtility.FromJson <BindingConfig>(inputFile.ReadToEnd());

                foreach (var roleData in s_bindingConfig.roles)
                {
                    foreach (var binding in roleData.bindings)
                    {
                        if (VRModule.IsDeviceConnected(binding.device_sn))
                        {
                            s_modelHintTable[binding.device_sn] = VRModule.GetCurrentDeviceState(VRModule.GetConnectedDeviceIndex(binding.device_sn)).deviceModel;
                        }
                        else
                        {
                            s_modelHintTable[binding.device_sn] = binding.device_model;
                        }
                    }
                }

                return(true);
            }
        }
Exemplo n.º 2
0
        public void RefreshDisplayInfo(ViveRole.IMap roleMap)
        {
            var roleInfo    = roleMap.RoleValueInfo;
            var roleValue   = roleMap.GetBoundRoleValueByDevice(deviceSN);
            var deviceModel = ViveRoleBindingsHelper.GetDeviceModelHint(deviceSN);

            m_deviceSN.text = deviceSN;
            m_roleName.text = roleInfo.GetNameByRoleValue(roleValue);

            BindingInterfaceSpriteManager.SetupDeviceIcon(m_modelIcon, deviceModel, VRModule.IsDeviceConnected(deviceSN));
        }
        public static void LoadBindingConfigFromRoleMap(params Type[] roleTypeFilter)
        {
            var roleDataList = ListPool <RoleData> .Get();

            var filterUsed = roleTypeFilter != null && roleTypeFilter.Length > 0;

            if (filterUsed)
            {
                roleDataList.AddRange(s_bindingConfig.roles);
            }

            for (int i = 0, imax = ViveRoleEnum.ValidViveRoleTable.Count; i < imax; ++i)
            {
                var roleType = ViveRoleEnum.ValidViveRoleTable.GetValueByIndex(i);
                var roleName = ViveRoleEnum.ValidViveRoleTable.GetKeyByIndex(i);
                var roleMap  = ViveRole.GetMap(roleType);

                if (filterUsed)
                {
                    // apply filter
                    var filtered = false;
                    foreach (var t in roleTypeFilter)
                    {
                        if (roleType == t)
                        {
                            filtered = true; break;
                        }
                    }
                    if (!filtered)
                    {
                        continue;
                    }
                }

                if (roleMap.BindingCount > 0)
                {
                    var bindingTable = roleMap.BindingTable;

                    var roleData = new RoleData()
                    {
                        type     = roleName,
                        bindings = new Binding[bindingTable.Count],
                    };

                    for (int j = 0, jmax = bindingTable.Count; j < jmax; ++j)
                    {
                        var binding = new Binding();
                        binding.device_sn  = bindingTable.GetKeyByIndex(j);
                        binding.role_value = bindingTable.GetValueByIndex(j);
                        binding.role_name  = roleMap.RoleValueInfo.GetNameByRoleValue(binding.role_value);

                        // save the device_model for better recognition of the device
                        if (VRModule.IsDeviceConnected(binding.device_sn))
                        {
                            binding.device_model = VRModule.GetCurrentDeviceState(VRModule.GetConnectedDeviceIndex(binding.device_sn)).deviceModel;
                            s_modelHintTable[binding.device_sn] = binding.device_model;
                        }
                        else if (!s_modelHintTable.TryGetValue(binding.device_sn, out binding.device_model))
                        {
                            binding.device_model = VRModuleDeviceModel.Unknown;
                        }

                        roleData.bindings[j] = binding;
                    }

                    if (filterUsed)
                    {
                        // merge with existing role data
                        var roleDataIndex = roleDataList.FindIndex((item) => item.type == roleName);
                        if (roleDataIndex >= 0)
                        {
                            roleDataList[roleDataIndex] = roleData;
                        }
                        else
                        {
                            roleDataList.Add(roleData);
                        }
                    }
                    else
                    {
                        roleDataList.Add(roleData);
                    }
                }
                else
                {
                    if (roleDataList.Count > 0)
                    {
                        // don't write to config if no bindings
                        roleDataList.RemoveAll((item) => item.type == roleName);
                    }
                }
            }

            s_bindingConfig.roles = roleDataList.ToArray();

            ListPool <RoleData> .Release(roleDataList);
        }