private static void ConnectController() { Console.WriteLine("Searching for XInput controller..."); while (true) { CONTROLLER = new XController(UserIndex.One); // Define controller as the first one plugged in if (CONTROLLER.GetConnected()) { break; } Thread.Sleep(100); } Console.WriteLine("Controller found!"); }
public static void Run() { int buttonsPressed; bool hasSaved = false, hasLoaded = false, hasSwitched = false; bool DLeft, DRight, DUp, DDown, LB; int invertCycle = UserSettings.Default.invertCycle ? -1 : 1; while (true) { while (gameHooked) { if (!isDInput) { if (CONTROLLER == null) { ConnectController(); } else if (!CONTROLLER.GetConnected()) { CONTROLLER = null; Console.WriteLine("Controller disconnected"); continue; } buttonsPressed = (int)CONTROLLER.GetState().Gamepad.Buttons; DUp = (buttonsPressed & 1) != 0 ? true : false; DDown = (buttonsPressed & 2) != 0 ? true : false; DLeft = (buttonsPressed & 4) != 0 ? true : false; DRight = (buttonsPressed & 8) != 0 ? true : false; LB = (buttonsPressed & 256) != 0 ? true : false; } else { bool[] Dinputs = DCONTROLLER.GetState(); if (!Dinputs[5]) { DCONTROLLER.InitializeController(); continue; } DUp = Dinputs[0]; DLeft = Dinputs[1]; DRight = Dinputs[2]; DDown = Dinputs[3]; LB = Dinputs[4]; } if (LB || !UserSettings.Default.extraInput) { if (DLeft && !hasSaved) { if (ProcessMemory.ReadByte(gameProc, baseAddress + 0x3722DE4) != 16) { saveStates[curSaveState] = new SaveState(ref gameProc, ref baseAddress); Console.WriteLine("Saved to {0}", curSaveState); SaveStateSerializer SaveObjectToSerialize = new SaveStateSerializer(saveStates); string json = JsonConvert.SerializeObject(SaveObjectToSerialize, Formatting.Indented); System.IO.File.WriteAllText("file.txt", json); } hasSaved = true; } else if (!DLeft && hasSaved) { hasSaved = false; } if (DRight && !hasLoaded) { if (saveStates[curSaveState] != null) { if (ProcessMemory.ReadByte(gameProc, baseAddress + 0x3722DE4) != 16) { LoadState(saveStates[curSaveState]); Console.WriteLine("Loaded {0}", curSaveState); } } else { Console.WriteLine("Cannot find save state {0}", curSaveState); } hasLoaded = true; } else if (!DRight && hasLoaded) { hasLoaded = false; } if ((DUp || DDown) && !hasSwitched) { if (ProcessMemory.ReadByte(gameProc, baseAddress + 0x3722DE4) != 16) { //Console.WriteLine(Convert.ToInt32(UserSettings.Default.invertCycle)); curSaveState += Convert.ToInt32(DUp) * invertCycle; curSaveState -= Convert.ToInt32(DDown) * invertCycle; if (curSaveState == -1) { curSaveState = 9; } else if (curSaveState == 10) { curSaveState = 0; } Console.WriteLine("Switched to save slot {0}", curSaveState); } hasSwitched = true; } else if (!(DUp || DDown) && hasSwitched) { hasSwitched = false; } System.Threading.Thread.Sleep(10); } } Hook(); } }