#pragma warning disable CA1508 // Avoid dead conditional code public static ControllerStateEventArgs ReadFromPacket(byte[] packet) { if (packet == null) { throw new ArgumentNullException(nameof(packet)); } if (packet.Length == PACKET_SIZE) { byte[] polishedPacket = new byte[POLISHED_PACKET_SIZE]; polishedPacket[0] = 0; byte j = 8; for (byte i = 0; i < 8; ++i) { j--; polishedPacket[0] |= (byte)((packet[i] == 0 ? 0 : 1) << j); } if (polishedPacket[0] != 0x02 && polishedPacket[0] != 0x16 && polishedPacket[0] != 0xFF) { return(null); } ControllerStateBuilder state = new ControllerStateBuilder(); if (polishedPacket[0] != 0xFF) { for (byte i = 0; i < 16; ++i) { polishedPacket[i + 1] = packet[i + 8] == 0 ? (byte)1 : (byte)0; } byte numExtraBytes = 0; if (polishedPacket[0] == 0x16) { numExtraBytes = 4; } for (int i = 0; i < numExtraBytes; ++i) { polishedPacket[17 + i] = 0; j = 8; for (byte k = 0; k < 8; ++k) { j--; polishedPacket[17 + i] |= (byte)((packet[24 + (i * 8) + k] == 0 ? 0 : 1) << j); } } if (polishedPacket.Length < POLISHED_PACKET_SIZE) { return(null); } for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], polishedPacket[i] != 0x00); } SignalTool.GenerateFakeStick(state, "l_twinstick_x", "l_twinstick_y", polishedPacket[4] != 0x00, polishedPacket[3] != 0x00, polishedPacket[2] != 0x00, polishedPacket[1] != 0x00); SignalTool.GenerateFakeStick(state, "r_twinstick_x", "r_twinstick_y", polishedPacket[11] != 0x00, polishedPacket[8] != 0x00, polishedPacket[10] != 0x00, polishedPacket[12] != 0x00); if (polishedPacket[0] == 0x16) { state.SetAnalog("lstick_x", ReadStick(polishedPacket[17]), polishedPacket[17]); state.SetAnalog("lstick_y", ReadStick(polishedPacket[18]), polishedPacket[18]); state.SetAnalog("trig_r", ReadTrigger(polishedPacket[19]), polishedPacket[19]); state.SetAnalog("trig_l", ReadTrigger(polishedPacket[20]), polishedPacket[20]); } else { state.SetAnalog("lstick_x", ReadStick(128), 128); state.SetAnalog("lstick_y", ReadStick(128), 128); state.SetAnalog("trig_r", ReadTrigger(0), 0); state.SetAnalog("trig_l", ReadTrigger(0), 0); } } else { for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], false); } for (int i = 0; i < MOUSE_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(MOUSE_BUTTONS[i])) { continue; } state.SetButton(MOUSE_BUTTONS[i], packet[i + 8] != 0x00); } state.SetAnalog("lstick_x", ReadStick(128), 128); state.SetAnalog("lstick_y", ReadStick(128), 128); state.SetAnalog("trig_r", ReadTrigger(0), 0); state.SetAnalog("trig_l", ReadTrigger(0), 0); byte xVal = 0; j = 8; for (byte k = 0; k < 8; ++k) { j--; xVal |= (byte)((packet[16 + k] == 0 ? 0 : 1) << j); } byte yVal = 0; j = 8; for (byte k = 0; k < 8; ++k) { j--; yVal |= (byte)((packet[24 + k] == 0 ? 0 : 1) << j); } float x = ReadMouse(packet[11] != 0, packet[9] != 0, xVal); float y = ReadMouse(packet[10] != 0, packet[8] != 0, yVal); SignalTool.SetMouseProperties(x, y, xVal, yVal, state); } return(state.Build()); } else if (packet.Length == KEYBOARD_PACKET_SIZE) { ControllerStateBuilder state = new ControllerStateBuilder(); int j = 0; byte[] reconstructedPacket = new byte[KEYBOARD_POLISHED_PACKET_SIZE]; for (int i = 0; i < KEYBOARD_POLISHED_PACKET_SIZE; ++i) { reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]); j += 2; } for (int i = 0; i < KEYS.Length; ++i) { if (KEYS[i] != null) { state.SetButton(KEYS[i], (reconstructedPacket[i / 8] & (1 << (i % 8))) != 0); } } return(state.Build()); } else { return(null); } }
private static void GenerateDigitalDirections(byte[] binaryPacket, ControllerStateBuilder outState) { byte directions = (byte)(binaryPacket[2] >> 4); bool up = false; bool left = false; bool right = false; bool down = false; switch (directions) { case 1: up = true; left = false; down = false; right = false; break; case 2: up = true; left = false; down = false; right = true; break; case 3: up = false; left = false; down = false; right = true; break; case 4: up = false; left = false; down = true; right = true; break; case 5: up = false; left = false; down = true; right = false; break; case 6: up = false; left = true; down = true; right = false; break; case 7: up = false; left = true; down = false; right = false; break; case 8: up = true; left = true; down = false; right = false; break; } outState.SetButton("up", up); outState.SetButton("left", left); outState.SetButton("down", down); outState.SetButton("right", right); SignalTool.GenerateFakeStick(outState, "x_stick", "y_stick", up, down, left, right); }