Exemple #1
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < 1)
            {
                return(null);
            }

            ControllerStateBuilder 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, xVal, yVal, state);
            }
            return(state.Build());
        }
Exemple #2
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length != PACKET_SIZE)
            {
                return(null);
            }

            ControllerStateBuilder 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, SignalTool.ReadByte(packet, BUTTONS.Length));
            state.SetAnalog("stick_y", y, SignalTool.ReadByte(packet, BUTTONS.Length + 8));

            SignalTool.SetMouseProperties(x, y, SignalTool.ReadByte(packet, BUTTONS.Length), SignalTool.ReadByte(packet, BUTTONS.Length + 8), state);

            return(state.Build());
        }
Exemple #3
0
        public static ControllerStateEventArgs ReadFromPacketSNES(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length < BUTTONS_SNES.Length)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            if (packet.Length == 32 && packet[15] == 0x00 && packet[13] != 0x00)
            {
                for (int i = 0; i < BUTTONS_SNES_NTTDATA.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS_SNES_NTTDATA[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS_SNES_NTTDATA[i], packet[i] != 0x00);
                }
            }
            else
            {
                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 (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, SignalTool.ReadByte(packet, 25, 7, 0x1) * ((packet[24] & 0x1) != 0 ? -1 : 1), SignalTool.ReadByte(packet, 17, 7, 0x1) * ((packet[16] & 0x1) != 0 ? 1 : -1), state);
                }
            }

            return(state.Build());
        }
Exemple #4
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            if (packet.Length == 3)
            {
                ControllerStateBuilder state1 = new ControllerStateBuilder();

                for (int i = 0; i < KEYS.Length; ++i)
                {
                    if (KEYS[i] != null)
                    {
                        state1.SetButton(KEYS[i], false);
                    }
                }

                for (int i = 0; i < packet.Length; ++i)
                {
                    if (KEYS[packet[i]] != null)
                    {
                        state1.SetButton(KEYS[packet[i]], true);
                    }
                }

                state1.SetButton("Function", false);
                for (int i = 0; i < packet.Length; ++i)
                {
                    if (FUNCTION_KEYS[packet[i]] != null)
                    {
                        state1.SetButton(FUNCTION_KEYS[packet[i]], true);
                    }
                }

                return(state1.Build());
            }

            if (packet.Length != PACKET_SIZE && packet.Length != PACKET_SIZE - 8)
            {
                return(null);
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            for (int i = 0; i < BUTTONS.Length; ++i)
            {
                if (string.IsNullOrEmpty(BUTTONS[i]))
                {
                    continue;
                }

                state.SetButton(BUTTONS[i], packet[i] != 0x00);
            }

            for (int i = 0; i < KEYS.Length; ++i)
            {
                if (KEYS[i] != null)
                {
                    state.SetButton(KEYS[i], false);
                }
            }

            for (int i = 0; i < keyboardData.Length; ++i)
            {
                if (KEYS[keyboardData[i]] != null)
                {
                    state.SetButton(KEYS[keyboardData[i]], true);
                }
            }

            state.SetButton("Function", false);
            for (int i = 0; i < keyboardData.Length; ++i)
            {
                if (FUNCTION_KEYS[keyboardData[i]] != null)
                {
                    state.SetButton(FUNCTION_KEYS[keyboardData[i]], true);
                }
            }

            state.SetAnalog("lstick_x", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length)), SignalTool.ReadByte(packet, BUTTONS.Length));
            state.SetAnalog("lstick_y", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 8)), SignalTool.ReadByte(packet, BUTTONS.Length + 8));
            state.SetAnalog("cstick_x", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 16)), SignalTool.ReadByte(packet, BUTTONS.Length + 16));
            state.SetAnalog("cstick_y", ReadStick(SignalTool.ReadByte(packet, BUTTONS.Length + 24)), SignalTool.ReadByte(packet, BUTTONS.Length + 24));
            if (packet.Length == PACKET_SIZE)
            {
                state.SetAnalog("trig_l", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 32)), SignalTool.ReadByte(packet, BUTTONS.Length + 32));
                state.SetAnalog("trig_r", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 40)), SignalTool.ReadByte(packet, BUTTONS.Length + 40));
            }
            else
            {
                state.SetAnalog("trig_l", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 32, 4), 15), SignalTool.ReadByte(packet, BUTTONS.Length + 32, 4));
                state.SetAnalog("trig_r", ReadTrigger(SignalTool.ReadByte(packet, BUTTONS.Length + 36, 4), 15), SignalTool.ReadByte(packet, BUTTONS.Length + 36, 4));
            }
            return(state.Build());
        }
Exemple #5
0
        public static ControllerStateEventArgs ProcessPowerGlove(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            ControllerStateBuilder state = new ControllerStateBuilder();

            sbyte x = (sbyte)SignalTool.ReadByte(packet, 8, 8);

            state.SetAnalog("x", x / 128.0f, x);

            sbyte y = (sbyte)SignalTool.ReadByte(packet, 16, 8);

            state.SetAnalog("y", y / 128.0f, y);

            sbyte z = (sbyte)SignalTool.ReadByte(packet, 24, 8);

            state.SetAnalog("z", z / 128.0f, z);

            byte rotation = SignalTool.ReadByte(packet, 32, 8);

            state.SetAnalog("rotation", rotation / 11.0f, rotation);

            byte[] fingers = new byte[4];
            for (int i = 0; i < 4; ++i)
            {
                fingers[i] = 0;
                for (int j = 0; j < 2; ++j)
                {
                    fingers[i] |= (byte)(packet[40 + (i * 2) + j] == 0 ? 0 : (1 << j));
                }
            }
            for (int i = 0; i < 4; ++i)
            {
                state.SetAnalog(BUTTONS_NES_POWERGLOVE_FINGERS[i], fingers[i] / 4.0f, fingers[i]);
            }

            byte buttons = SignalTool.ReadByte(packet, 48, 8);

            for (int i = 0; i < BUTTONS_NES_POWERGLOVE.Length; ++i)
            {
                state.SetButton(BUTTONS_NES_POWERGLOVE[i], false);
            }
            switch (buttons)
            {
            case 0x00:
                state.SetButton("0", true);
                state.SetButton("center", true);
                break;

            case 0x01:
                state.SetButton("1", true);
                break;

            case 0x02:
                state.SetButton("2", true);
                break;

            case 0x03:
                state.SetButton("3", true);
                break;

            case 0x04:
                state.SetButton("4", true);
                break;

            case 0x05:
                state.SetButton("5", true);
                break;

            case 0x06:
                state.SetButton("6", true);
                break;

            case 0x07:
                state.SetButton("7", true);
                break;

            case 0x08:
                state.SetButton("8", true);
                break;

            case 0x09:
                state.SetButton("9", true);
                break;

            case 0x0A:
                state.SetButton("a", true);
                break;

            case 0x0B:
                state.SetButton("b", true);
                break;

            case 0x0C:
                state.SetButton("left", true);
                break;

            case 0x0D:
                state.SetButton("up", true);
                break;

            case 0x0E:
                state.SetButton("down", true);
                break;

            case 0x0F:
                state.SetButton("right", true);
                break;

            case 0x80:
                state.SetButton("enter", true);
                break;

            case 0x82:
                state.SetButton("start", true);
                break;

            case 0x83:
                state.SetButton("select", true);
                break;

            default:
                break;
            }

            return(state.Build());
        }
Exemple #6
0
        public static ControllerStateEventArgs ReadFromPacket(byte[] packet)
        {
            if (packet == null)
            {
                throw new ArgumentNullException(nameof(packet));
            }

            ControllerStateBuilder state = null;

            if (packet.Length == 13)
            {
                return(Classic.ReadFromPacket(packet));
            }
            else if (packet.Length == 6)
            {
                for (int i = 0; i < BUTTONS_AMIGA_ANALOG.Length; ++i)
                {
                    if (string.IsNullOrEmpty(BUTTONS_AMIGA_ANALOG[i]))
                    {
                        continue;
                    }

                    state.SetButton(BUTTONS_AMIGA_ANALOG[i], packet[i] != 0x00);
                }

                state.SetAnalog("y", (((packet[4] >> 4) | (packet[5])) - 15.0f) / 15.0f, (packet[4] >> 4) | (packet[5]));
                state.SetAnalog("x", AmigaAnalogXAxisData, AmigaAnalogXAxisDataRaw);
            }
            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;
                    int   xRaw = 0;
                    float y    = 0;
                    int   yRaw = 0;

                    if (packet[0] != 0x00)
                    {
                        x    = -0.25f;
                        xRaw = packet[0];
                    }
                    else if (packet[2] != 0x00)
                    {
                        x    = 0.25f;
                        xRaw = packet[2];
                    }

                    if (packet[1] != 0x00)
                    {
                        y    = 0.25f;
                        yRaw = packet[1];
                    }
                    else if (packet[3] != 0x00)
                    {
                        y    = -0.25f;
                        yRaw = packet[3];
                    }

                    SignalTool.SetMouseProperties(x, y, xRaw, yRaw, 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, xVal, yVal, 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, xVal, yVal, 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", CultureInfo.CurrentCulture).ToUpper(CultureInfo.CurrentUICulture);;
                        state.SetButton(scanCode, polishedPacket[i] != 0x00);
                    }
                }
            }

            return(state?.Build());
        }