/// <summary> /// Read settings from INI file into windows form controls. /// </summary> /// <param name="file">INI file containing settings.</param> /// <param name="iniSection">Read setings from specified section only. Null - read from all sections.</param> public void ReadSettings(string file) { var ini2 = new Ini(file); foreach (string path in SettingsMap.Keys) { string section = path.Split('\\')[0]; // If this is PAD section. var padIndex = SettingName.GetPadIndex(path); if (padIndex > -1) { section = GetInstanceSection(padIndex); // If destination section is empty because controller is not connected then skip. if (string.IsNullOrEmpty(section)) { continue; } } Control control = SettingsMap[path]; string key = path.Split('\\')[1]; string v = ini2.GetValue(section, key); ReadSettingTo(control, key, v); } loadCount++; if (ConfigLoaded != null) { ConfigLoaded(this, new SettingEventArgs(ini2.File.Name, loadCount)); } // Read XML too. SettingsFile.Current.Load(); }
/// <summary> /// Save setting from windows form control to current INI file. /// </summary> /// <param name="path">path of parameter (related to actual control)</param> /// <param name="dstIniSection">if not null then section will be different inside INI file than specified in path</param> public bool SaveSetting(Ini ini, string path) { var control = SettingsMap[path]; string key = path.Split('\\')[1]; string v = string.Empty; if (key == SettingName.HookMode || key.EndsWith(SettingName.DeviceSubType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex)) { var v1 = ((ComboBox)control).SelectedItem; if (v1 == null) { v = "0"; } else if (v1 is KeyValuePair) { v = ((KeyValuePair)v1).Value; } else { v = System.Convert.ToInt32(v1).ToString(); } } // If di menu strip attached. else if (control is ComboBox) { var cbx = (ComboBox)control; if (control.ContextMenuStrip == null) { v = control.Text; } else { v = new SettingsConverter(control.Text, key).ToIniSetting(); // make sure that disabled button value is "0". if (SettingName.IsButton(key) && string.IsNullOrEmpty(v)) { v = "0"; } } } else if (control is TextBox) { // if setting is readonly. if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid) { v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text; } else { v = control.Text; } } else if (control is ListBox) { var lbx = (ListBox)control; v = string.Join(",", lbx.Items.Cast <string>().ToArray()); } else if (control is NumericUpDown) { NumericUpDown nud = (NumericUpDown)control; v = nud.Value.ToString(); } else if (control is TrackBar) { TrackBar tc = (TrackBar)control; // convert 100% to 256 if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone) { v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString(); } // convert 100% to 500 else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod) { v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString(); } // Convert 100% to 32767 else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY) { v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString(); } else { v = tc.Value.ToString(); } } else if (control is CheckBox) { CheckBox tc = (CheckBox)control; v = tc.Checked ? "1" : "0"; } if (SettingName.IsThumbAxis(key)) { v = v.Replace(SettingName.SType.Axis, ""); } if (SettingName.IsDPad(key)) { v = v.Replace(SettingName.SType.DPad, ""); } if (v == "v1") { v = "UP"; } if (v == "v2") { v = "RIGHT"; } if (v == "v3") { v = "DOWN"; } if (v == "v4") { v = "LEFT"; } if (v == "") { if (key == SettingName.DPadUp) { v = "UP"; } if (key == SettingName.DPadDown) { v = "DOWN"; } if (key == SettingName.DPadLeft) { v = "LEFT"; } if (key == SettingName.DPadRight) { v = "RIGHT"; } } // add comment. //var l = SettingName.MaxNameLength - key.Length + 24; //v = string.Format("{0, -" + l + "} # {1} Default: '{2}'.", v, SettingName.GetDescription(key), SettingName.GetDefaultValue(key)); var section = path.Split('\\')[0]; var padIndex = SettingName.GetPadIndex(path); // If this is PAD section then if (padIndex > -1) { section = GetInstanceSection(padIndex); // If destination section is empty because controller is not connected then skip. if (string.IsNullOrEmpty(section)) { return(false); } } var oldValue = ini.GetValue(section, key); var saved = false; if (oldValue != v) { ini.SetValue(section, key, v); saveCount++; saved = true; if (ConfigSaved != null) { ConfigSaved(this, new SettingEventArgs(IniFileName, saveCount)); } } // Flush XML too. SettingsFile.Current.Save(); return(saved); }
public string GetSettingValue(Control control) { var item = SettingsMap.First(x => x.Control == control); var path = item.IniPath; var section = path.Split('\\')[0]; string key = path.Split('\\')[1]; var padIndex = SettingName.GetPadIndex(path); string v = string.Empty; if (key == SettingName.HookMode || key.EndsWith(SettingName.GamePadType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.LeftMotorDirection) || key.EndsWith(SettingName.RightMotorDirection) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex)) { var v1 = ((ComboBox)control).SelectedItem; if (v1 == null) { v = "0"; } else if (v1 is KeyValuePair) { v = ((KeyValuePair)v1).Value; } else { v = System.Convert.ToInt32(v1).ToString(); } } // If di menu strip attached. else if (control is ComboBox) { var cbx = (ComboBox)control; if (control.ContextMenuStrip == null) { v = control.Text; } else { v = SettingsConverter.ToIniValue(control.Text); // make sure that disabled button value is "0". if (SettingName.IsButton(key) && string.IsNullOrEmpty(v)) { v = "0"; } } // Save to XML. if (key == SettingName.InternetDatabaseUrl) { Options.InternetDatabaseUrl = v; } } else if (control is TextBox) { // if setting is read-only. if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid) { v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text; } else { v = control.Text; } } else if (control is NumericUpDown) { NumericUpDown nud = (NumericUpDown)control; v = nud.Value.ToString(); } else if (control is TrackBar) { TrackBar tc = (TrackBar)control; // convert 100% to 256 if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone) { v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString(); } // convert 100% to 500 else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod) { v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString(); } // Convert 100% to 32767 else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY) { v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString(); } else { v = tc.Value.ToString(); } } else if (control is CheckBox) { CheckBox tc = (CheckBox)control; v = tc.Checked ? "1" : "0"; } else if (control is DataGridView) { var grid = (DataGridView)control; if (grid.Enabled) { var data = grid.Rows.Cast <DataGridViewRow>().Where(x => x.Visible).Select(x => x.DataBoundItem as Setting) // Make sure that only enabled controllers are added. .Where(x => x != null && x.IsEnabled).ToArray(); var instances = data.Select(x => GetInstanceSection(x.InstanceGuid)).ToArray(); // Separate devices with comma. x360ce.dll must combine devices separated by comma. v = string.Join(",", instances); } else { v = "AUTO"; } } return(v); }
public string GetSettingValue(object control) { var item = SettingsMap.First(x => x.Control == control); var path = item.IniPath; var section = path.Split('\\')[0]; string key = path.Split('\\')[1]; var padIndex = SettingName.GetPadIndex(path); string v = string.Empty; if (key == SettingName.HookMode || key.EndsWith(SettingName.GamePadType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.LeftMotorDirection) || key.EndsWith(SettingName.RightMotorDirection) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex)) { var v1 = ((ComboBox)control).SelectedItem; if (v1 == null) { v = "0"; } else if (v1 is KeyValuePair) { v = ((KeyValuePair)v1).Value; } else { v = System.Convert.ToInt32(v1).ToString(); } } // If DI menu strip attached. else if (control is ComboBox cbx) { var map = SettingsMap.FirstOrDefault(x => x.Control == control); if (map != null && map.Code != default) { v = SettingsConverter.ToIniValue(cbx.Text); // make sure that disabled button value is "0". if (SettingName.IsButton(key) && string.IsNullOrEmpty(v)) { v = "0"; } } else { v = cbx.Text; } } else if (control is TextBox tbx) { // if setting is read-only. if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid) { v = string.IsNullOrEmpty(tbx.Text) ? Guid.Empty.ToString("D") : tbx.Text; } else { v = tbx.Text; } } else if (control is NumericUpDown nud) { v = nud.Value.ToString(); } else if (control is TrackBar) { TrackBar tc = (TrackBar)control; // convert 100% to 256 if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone) { v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString(); } // convert 100% to 500 else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod) { v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString(); } // Convert 100% to 32767 else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY) { v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString(); } else { v = tc.Value.ToString(); } } else if (control is CheckBox) { CheckBox tc = (CheckBox)control; v = tc.Checked ? "1" : "0"; } else if (control is DataGridView) { var grid = (DataGridView)control; if (grid.Enabled) { var data = grid.Rows.Cast <DataGridViewRow>().Where(x => x.Visible).Select(x => x.DataBoundItem as UserSetting) // Make sure that only enabled controllers are added. .Where(x => x != null && x.IsEnabled).ToArray(); data = FilterSettings(data); var sections = data.Select(x => GetInstanceSection(x.InstanceGuid)).ToArray(); // x360ce.dll must combine devices separated by comma. //v = string.Join(",", sections); // Use backwards compatible mode (one device at the time). v = sections.FirstOrDefault() ?? ""; // Note: Must code device combine workaround. } else { v = "AUTO"; } } return(v); }
string GetSettingValue(Control control) { var item = SettingsMap.First(x => x.Control == control); var path = item.IniPath; var section = path.Split('\\')[0]; string key = path.Split('\\')[1]; var padIndex = SettingName.GetPadIndex(path); string v = string.Empty; if (key == SettingName.HookMode || key.EndsWith(SettingName.DeviceSubType) || key.EndsWith(SettingName.ForceType) || key.EndsWith(SettingName.LeftMotorDirection) || key.EndsWith(SettingName.RightMotorDirection) || key.EndsWith(SettingName.PassThroughIndex) || key.EndsWith(SettingName.CombinedIndex)) { var v1 = ((ComboBox)control).SelectedItem; if (v1 == null) { v = "0"; } else if (v1 is KeyValuePair) { v = ((KeyValuePair)v1).Value; } else { v = System.Convert.ToInt32(v1).ToString(); } } // If di menu strip attached. else if (control is ComboBox) { var cbx = (ComboBox)control; if (control.ContextMenuStrip == null) { v = control.Text; } else { v = new SettingsConverter(control.Text, key).ToIniValue(); // make sure that disabled button value is "0". if (SettingName.IsButton(key) && string.IsNullOrEmpty(v)) { v = "0"; } } } else if (control is TextBox) { // if setting is read-only. if (key == SettingName.InstanceGuid || key == SettingName.ProductGuid) { v = string.IsNullOrEmpty(control.Text) ? Guid.Empty.ToString("D") : control.Text; } else { v = control.Text; } } else if (control is ListBox) { var lbx = (ListBox)control; v = string.Join(",", lbx.Items.Cast <string>().ToArray()); } else if (control is NumericUpDown) { NumericUpDown nud = (NumericUpDown)control; v = nud.Value.ToString(); } else if (control is TrackBar) { TrackBar tc = (TrackBar)control; // convert 100% to 256 if (key == SettingName.AxisToDPadDeadZone || key == SettingName.AxisToDPadOffset || key == SettingName.LeftTriggerDeadZone || key == SettingName.RightTriggerDeadZone) { v = System.Convert.ToInt32((float)tc.Value / 100F * 256F).ToString(); } // convert 100% to 500 else if (key == SettingName.LeftMotorPeriod || key == SettingName.RightMotorPeriod) { v = System.Convert.ToInt32((float)tc.Value / 100F * 500F).ToString(); } // Convert 100% to 32767 else if (key == SettingName.LeftThumbDeadZoneX || key == SettingName.LeftThumbDeadZoneY || key == SettingName.RightThumbDeadZoneX || key == SettingName.RightThumbDeadZoneY) { v = System.Convert.ToInt32((float)tc.Value / 100F * ((float)Int16.MaxValue)).ToString(); } else { v = tc.Value.ToString(); } } else if (control is CheckBox) { CheckBox tc = (CheckBox)control; v = tc.Checked ? "1" : "0"; } else if (control is DataGridView) { var grid = (DataGridView)control; var data = grid.Rows.Cast <DataGridViewRow>().Where(x => x.Visible).Select(x => x.DataBoundItem as Setting).Where(x => x != null).ToArray(); var instances = data.Select(x => string.Format("IG_{0:N}", x.InstanceGuid).ToUpper()).ToArray(); v = string.Join(",", instances); } if (SettingName.IsThumbAxis(key)) { v = v.Replace(SettingName.SType.Axis, ""); } // If this is DPad setting then remove prefix. if (key == SettingName.DPad) { v = v.Replace(SettingName.SType.DPad, ""); } return(v); }