static public ControllerState ReadFromPacket(byte[] packet) { if (packet.Length != PACKET_SIZE) { return(null); } var state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS [i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } float x = readStick(SignalTool.readByte(packet, BUTTONS.Length)); float y = readStick(SignalTool.readByte(packet, BUTTONS.Length + 8)); state.SetAnalog("stick_x", x); state.SetAnalog("stick_y", y); SignalTool.SetMouseProperties(x, y, state); return(state.Build()); }
static public ControllerState ReadFromPacket(byte[] packet) { if (packet.Length != PACKET_SIZE) { return(null); } var state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS [i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } state.SetAnalog("lstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length))); state.SetAnalog("lstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 8))); state.SetAnalog("cstick_x", readStick(SignalTool.readByte(packet, BUTTONS.Length + 16))); state.SetAnalog("cstick_y", readStick(SignalTool.readByte(packet, BUTTONS.Length + 24))); state.SetAnalog("trig_l", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 32))); state.SetAnalog("trig_r", readTrigger(SignalTool.readByte(packet, BUTTONS.Length + 40))); return(state.Build()); }
static public ControllerState ReadFromPacket_SNES(byte[] packet) { if (packet.Length < BUTTONS_SNES.Length) { return(null); } var state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_SNES.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_SNES[i])) { continue; } state.SetButton(BUTTONS_SNES[i], packet[i] != 0x00); } if (state != null && packet.Length == 32 && packet[15] != 0x00) { float y = (float)(SignalTool.readByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1)) / 127; float x = (float)(SignalTool.readByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1)) / 127; SignalTool.SetMouseProperties(x, y, state); } return(state.Build()); }
static public ControllerState ReadFromPacket(byte[] packet) { if (packet.Length < 1) { return(null); } var state = new ControllerStateBuilder(); if (packet[0] == 0) { if (packet.Length < PACKET_SIZE) { return(null); } for (int i = 0; i < BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS[i])) { continue; } state.SetButton(BUTTONS[i], packet[i] != 0x00); } } else { if (packet.Length < MOUSE_PACKET_SIZE) { return(null); } for (int i = 0; i < MOUSE_BUTTONS.Length; ++i) { if (string.IsNullOrEmpty(MOUSE_BUTTONS[i])) { continue; } state.SetButton(MOUSE_BUTTONS[i], packet[i] != 0x00); } bool ySign = packet[11] != 0; byte yVal = SignalTool.readByte(packet, 14, 7); bool xSign = packet[21] != 0; byte xVal = SignalTool.readByte(packet, 24, 7); float x = readMouse(xSign, xVal); float y = readMouse(ySign, yVal); SignalTool.SetMouseProperties(x, y, state); } return(state.Build()); }
static public ControllerState ReadFromPacket(byte[] packet) { ControllerStateBuilder state = null; if (packet.Length == 13) { return(Classic.ReadFromPacket(packet)); } if (packet.Length == BUTTONS_CD32.Length) { state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_CD32.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_CD32[i])) { continue; } state.SetButton(BUTTONS_CD32[i], (packet[i] & 0b10000000) == 0x00); } state.SetButton("up", (packet[8] & 0b00000001) == 0); state.SetButton("down", (packet[0] & 0b00000100) == 0); state.SetButton("left", (packet[0] & 0b00001000) == 0); state.SetButton("right", (packet[0] & 0b00010000) == 0); } else if (packet.Length == BUTTONS_CDTV_REMOTE.Length) { int checksum = (packet[33] >> 4) | packet[34]; int checkedCheckSum = 0; for (int i = 0; i < 33; ++i) { checkedCheckSum += packet[i] == 0 ? 0 : 1; } if (checksum == checkedCheckSum) { state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_CDTV_REMOTE.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_CDTV_REMOTE[i])) { continue; } state.SetButton(BUTTONS_CDTV_REMOTE[i], packet[i] != 0x00); } float x = 0; float y = 0; if (packet[0] != 0x00) { x = -0.25f; } else if (packet[2] != 0x00) { x = 0.25f; } if (packet[1] != 0x00) { y = 0.25f; } else if (packet[3] != 0x00) { y = -0.25f; } SignalTool.SetMouseProperties(x, y, state, .25f); } } else if (packet.Length == BUTTONS_CDTV_JOYSTICK.Length && packet[0] == 0) { int checksum = (packet[24] >> 4) | packet[25]; int checkedCheckSum = 0; for (int i = 0; i < 24; ++i) { checkedCheckSum += packet[i] == 0 ? 0 : 1; } if (checksum == checkedCheckSum) { state = new ControllerStateBuilder(); for (int i = 0; i < BUTTONS_CDTV_JOYSTICK.Length; ++i) { if (string.IsNullOrEmpty(BUTTONS_CDTV_JOYSTICK[i])) { continue; } state.SetButton(BUTTONS_CDTV_JOYSTICK[i], packet[i] != 0x00); } SignalTool.FakeAnalogStick(packet[6], packet[5], packet[4], packet[3], state, "x", "y"); SignalTool.FakeAnalogStick(packet[12], packet[11], packet[10], packet[9], state, "Joy2x", "Joy2y"); } } else if (packet.Length == 26 && packet[0] == 1) { int checksum = (packet[24] >> 4) | packet[25]; int checkedCheckSum = 0; for (int i = 0; i < 24; ++i) { checkedCheckSum += packet[i] == 0 ? 0 : 1; } if (checksum == checkedCheckSum) { state = new ControllerStateBuilder(); state.SetButton("left_button", packet[2] == 0x00); state.SetButton("right_button", packet[1] == 0x00); sbyte xVal = (sbyte)SignalTool.readByte(packet, 3); sbyte yVal = (sbyte)SignalTool.readByte(packet, 11); SignalTool.SetMouseProperties(xVal / -128.0f, yVal / 128.0f, state); } } else if (packet.Length == 19) { state = new ControllerStateBuilder(); state.SetButton("left_button", packet[0] != 0x00); state.SetButton("right_button", packet[2] != 0x00); sbyte xVal = (sbyte)SignalTool.readByteBackwards(packet, 3); sbyte yVal = (sbyte)SignalTool.readByteBackwards(packet, 11); SignalTool.SetMouseProperties(xVal / -128.0f, yVal / 128.0f, state); } else if (packet.Length == 36) { byte[] reconstructedPacket = new byte[18]; int j = 0; for (int i = 0; i < 18; ++i) { reconstructedPacket[i] = (byte)((packet[j] >> 4) | packet[j + 1]); j += 2; } byte[] polishedPacket = new byte[128]; int checksum = 0; for (int i = 0; i < 16; ++i) { checksum += reconstructedPacket[i]; for (int k = 0; k < 8; ++k) { polishedPacket[(i * 8) + k] = (byte)((reconstructedPacket[i] & (1 << k)) != 0 ? 1 : 0); } } short sentChecksum = (short)((reconstructedPacket[17] << 8) | reconstructedPacket[16]); if (checksum == sentChecksum) { state = new ControllerStateBuilder(); for (int i = 0; i < 128; ++i) { string scanCode = i.ToString("X").ToUpper();; state.SetButton(scanCode, polishedPacket[i] != 0x00); } } } return(state?.Build()); }