public static bool SaveConfigurationToFile(string pathToFile, ControlConfiguration configToSave) { // ensure file exists if (!File.Exists(pathToFile)) { return(false); } byte[] fileBytes = File.ReadAllBytes(pathToFile); fileBytes[ControlKeyboardOffsets[GameControl.Camera]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Camera].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Target]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Target].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Pageup]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Pageup].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Pagedown]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Pagedown].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Menu]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Menu].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Ok]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Ok].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Cancel]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Cancel].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Switch]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Switch].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Assist]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Assist].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Start]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Start].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Up]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Up].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Right]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Right].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Down]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Down].ConfigValue); fileBytes[ControlKeyboardOffsets[GameControl.Left]] = Convert.ToByte(configToSave.KeyboardInputs[GameControl.Left].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Camera]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Camera].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Target]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Target].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Pageup]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Pageup].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Pagedown]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Pagedown].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Menu]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Menu].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Ok]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Ok].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Cancel]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Cancel].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Switch]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Switch].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Assist]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Assist].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Start]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Start].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Up]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Up].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Right]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Right].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Down]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Down].ConfigValue); fileBytes[ControlGamepadOffsets[GameControl.Left]] = Convert.ToByte(configToSave.GamepadInputs[GameControl.Left].ConfigValue); File.WriteAllBytes(pathToFile, fileBytes); return(true); }
public static ControlConfiguration LoadConfigurationFromFile(string pathToFile) { ControlConfiguration loaded = new ControlConfiguration(); byte[] fileBytes = File.ReadAllBytes(pathToFile); loaded.KeyboardInputs.Add(GameControl.Camera, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Camera]])); loaded.KeyboardInputs.Add(GameControl.Target, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Target]])); loaded.KeyboardInputs.Add(GameControl.Pageup, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Pageup]])); loaded.KeyboardInputs.Add(GameControl.Pagedown, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Pagedown]])); loaded.KeyboardInputs.Add(GameControl.Menu, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Menu]])); loaded.KeyboardInputs.Add(GameControl.Ok, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Ok]])); loaded.KeyboardInputs.Add(GameControl.Cancel, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Cancel]])); loaded.KeyboardInputs.Add(GameControl.Switch, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Switch]])); loaded.KeyboardInputs.Add(GameControl.Assist, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Assist]])); loaded.KeyboardInputs.Add(GameControl.Start, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Start]])); loaded.KeyboardInputs.Add(GameControl.Up, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Up]])); loaded.KeyboardInputs.Add(GameControl.Right, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Right]])); loaded.KeyboardInputs.Add(GameControl.Down, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Down]])); loaded.KeyboardInputs.Add(GameControl.Left, GetControlInputFromConfigValue(fileBytes[ControlKeyboardOffsets[GameControl.Left]])); loaded.GamepadInputs.Add(GameControl.Camera, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Camera]])); loaded.GamepadInputs.Add(GameControl.Target, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Target]])); loaded.GamepadInputs.Add(GameControl.Pageup, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Pageup]])); loaded.GamepadInputs.Add(GameControl.Pagedown, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Pagedown]])); loaded.GamepadInputs.Add(GameControl.Menu, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Menu]])); loaded.GamepadInputs.Add(GameControl.Ok, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Ok]])); loaded.GamepadInputs.Add(GameControl.Cancel, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Cancel]])); loaded.GamepadInputs.Add(GameControl.Switch, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Switch]])); loaded.GamepadInputs.Add(GameControl.Assist, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Assist]])); loaded.GamepadInputs.Add(GameControl.Start, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Start]])); loaded.GamepadInputs.Add(GameControl.Up, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Up]])); loaded.GamepadInputs.Add(GameControl.Right, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Right]])); loaded.GamepadInputs.Add(GameControl.Down, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Down]])); loaded.GamepadInputs.Add(GameControl.Left, GetControlInputFromConfigValue(fileBytes[ControlGamepadOffsets[GameControl.Left]])); return(loaded); }
public static bool CopyConfigurationFileAndSaveAsNew(string pathToExistingFile, string newConfigName, ControlConfiguration configToSave) { // ensure file to copy exists if (!File.Exists(pathToExistingFile)) { return(false); } FileInfo existingFile = new FileInfo(pathToExistingFile); string newFilePath = Path.Combine(existingFile.DirectoryName, newConfigName); // ensure file with same name doesn't already exist if (File.Exists(newFilePath)) { return(false); } File.Copy(pathToExistingFile, newFilePath); return(SaveConfigurationToFile(newFilePath, configToSave)); }
/// <summary> /// Polls for game pad input and maps the non-supported buttons to the keyboard binding. /// Non-supported buttons include D-pad, Left-stick click, Right-stick click /// </summary> internal Task PollForGamepadInput() { if (PollingInput) { return(null); // already polling so just return } PollingInput = true; bool wasUpPressed = false; bool wasDownPressed = false; bool wasLeftPressed = false; bool wasRightPressed = false; return(Task.Factory.StartNew(() => { ControlConfiguration loadedConfig = ControlMapper.LoadConfigurationFromFile(Path.Combine(Sys.PathToControlsFolder, Sys.Settings.GameLaunchSettings.InGameConfigOption)); bool hasDpadBinded = loadedConfig.GamepadInputs.Values.Any(i => i?.GamepadInput.Value == GamePadButton.DPadUp || i?.GamepadInput.Value == GamePadButton.DPadDown || i?.GamepadInput.Value == GamePadButton.DPadLeft || i?.GamepadInput.Value == GamePadButton.DPadRight); ScanCodeShort?upKey = null; bool upIsExtended = false; ScanCodeShort?downKey = null; bool downIsExtended = false; ScanCodeShort?leftKey = null; bool leftIsExtended = false; ScanCodeShort?rightKey = null; bool rightIsExtended = false; GameControl bindedControl; if (hasDpadBinded) { if (loadedConfig.GamepadInputs.Any(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadUp)) { bindedControl = loadedConfig.GamepadInputs.Where(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadUp).Select(kv => kv.Key).FirstOrDefault(); upKey = loadedConfig.KeyboardInputs[bindedControl].KeyScanCode; upIsExtended = loadedConfig.KeyboardInputs[bindedControl].KeyIsExtended; } if (loadedConfig.GamepadInputs.Any(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadDown)) { bindedControl = loadedConfig.GamepadInputs.Where(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadDown).Select(kv => kv.Key).FirstOrDefault(); downKey = loadedConfig.KeyboardInputs[bindedControl].KeyScanCode; downIsExtended = loadedConfig.KeyboardInputs[bindedControl].KeyIsExtended; } if (loadedConfig.GamepadInputs.Any(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadLeft)) { bindedControl = loadedConfig.GamepadInputs.Where(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadLeft).Select(kv => kv.Key).FirstOrDefault(); leftKey = loadedConfig.KeyboardInputs[bindedControl].KeyScanCode; leftIsExtended = loadedConfig.KeyboardInputs[bindedControl].KeyIsExtended; } if (loadedConfig.GamepadInputs.Any(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadRight)) { bindedControl = loadedConfig.GamepadInputs.Where(kv => kv.Value?.GamepadInput.Value == GamePadButton.DPadRight).Select(kv => kv.Key).FirstOrDefault(); rightKey = loadedConfig.KeyboardInputs[bindedControl].KeyScanCode; rightIsExtended = loadedConfig.KeyboardInputs[bindedControl].KeyIsExtended; } } else { // user has not binded their DPAD buttons to any controls so just assume it is going to be used for movement upKey = loadedConfig.KeyboardInputs[GameControl.Up].KeyScanCode; upIsExtended = loadedConfig.KeyboardInputs[GameControl.Up].KeyIsExtended; downKey = loadedConfig.KeyboardInputs[GameControl.Down].KeyScanCode; downIsExtended = loadedConfig.KeyboardInputs[GameControl.Down].KeyIsExtended; leftKey = loadedConfig.KeyboardInputs[GameControl.Left].KeyScanCode; leftIsExtended = loadedConfig.KeyboardInputs[GameControl.Left].KeyIsExtended; rightKey = loadedConfig.KeyboardInputs[GameControl.Right].KeyScanCode; rightIsExtended = loadedConfig.KeyboardInputs[GameControl.Right].KeyIsExtended; } if (ConnectedController == null) { ConnectedController = new GameController(); } while (PollingInput) { if (ConnectedController.IsConnected == false) { // null means no connected controller found so just sleep for a little and check back later Thread.Sleep(1000); DS4ControllerService.Instance?.RootHub?.HotPlug(); ConnectedController.CreateDevice(); continue; } if (ConnectedController.ReadState() == null) { ConnectedController?.ReleaseDevice(); continue; } if (upKey != null && ConnectedController.IsButtonPressed(GamePadButton.DPadUp) && !wasUpPressed) { wasUpPressed = true; SendKey(upKey.Value, upIsExtended); } else if (upKey != null && !ConnectedController.IsButtonPressed(GamePadButton.DPadUp) && wasUpPressed) { ReleaseKey(upKey.Value, upIsExtended); wasUpPressed = false; } if (downKey != null && ConnectedController.IsButtonPressed(GamePadButton.DPadDown) && !wasDownPressed) { wasDownPressed = true; SendKey(downKey.Value, downIsExtended); } else if (downKey != null && !ConnectedController.IsButtonPressed(GamePadButton.DPadDown) && wasDownPressed) { ReleaseKey(downKey.Value, downIsExtended); wasDownPressed = false; } if (leftKey != null && ConnectedController.IsButtonPressed(GamePadButton.DPadLeft) && !wasLeftPressed) { wasLeftPressed = true; SendKey(leftKey.Value, leftIsExtended); } else if (leftKey != null && !ConnectedController.IsButtonPressed(GamePadButton.DPadLeft) && wasLeftPressed) { ReleaseKey(leftKey.Value, leftIsExtended); wasLeftPressed = false; } if (rightKey != null && ConnectedController.IsButtonPressed(GamePadButton.DPadRight) && !wasRightPressed) { wasRightPressed = true; SendKey(rightKey.Value, rightIsExtended); } else if (rightKey != null && !ConnectedController.IsButtonPressed(GamePadButton.DPadRight) && wasRightPressed) { ReleaseKey(rightKey.Value, rightIsExtended); wasRightPressed = false; } } ConnectedController.ReleaseDevice(); })); }