コード例 #1
0
        private object DeserializePlayPacket(UncompressedPacket packet)
        {
            var    br = new SpanReader(packet.Data);
            object innerPacket;

            switch (packet.PacketId)
            {
            // Teleport Confirm
            case 0x00:
                innerPacket = TeleportConfirm.Deserialize(ref br);
                break;

            // Chat Message
            case 0x03:
                innerPacket = ServerboundChatMessage.Deserialize(ref br);
                break;

            // Client Settings
            case 0x05:
                innerPacket = ClientSettings.Deserialize(ref br);
                break;

            // Plugin Message
            case 0x0A:
                innerPacket = ServerboundPluginMessage.Deserialize(ref br);
                break;

            // Keep Alive
            case 0x0C:
                innerPacket = ServerboundKeepAlive.Deserialize(ref br);
                break;

            // Player On Ground
            case 0x0D:
                innerPacket = PlayerOnGround.Deserialize(ref br);
                break;

            // Player Position
            case 0x0E:
                innerPacket = PlayerPosition.Deserialize(ref br);
                break;

            // Position And Look
            case 0x0F:
                innerPacket = ServerboundPositionAndLook.Deserialize(ref br);
                break;

            // Player Look
            case 0x10:
                innerPacket = PlayerLook.Deserialize(ref br);
                break;

            // Player Digging
            case 0x14:
                innerPacket = PlayerDigging.Deserialize(ref br);
                break;

            // Entity Action
            case 0x15:
                innerPacket = EntityAction.Deserialize(ref br);
                break;

            // Held Item Change
            case 0x1A:
                innerPacket = ServerboundHeldItemChange.Deserialize(ref br);
                break;

            // Animation
            case 0x1D:
                innerPacket = ServerboundAnimation.Deserialize(ref br);
                break;

            // Player Block Placement
            case 0x1F:
                innerPacket = PlayerBlockPlacement.Deserialize(ref br);
                break;

            // Use Item
            case 0x20:
                innerPacket = UseItem.Deserialize(ref br);
                break;

            // Click Window
            case 0x08:
                innerPacket = ClickWindow.Deserialize(ref br);
                break;

            // Close Window
            case 0x09:
                innerPacket = ServerboundCloseWindow.Deserialize(ref br);
                break;

            default:
                Logger.LogWarning($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
                return(null);
            }

            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed.");
            }
            return(innerPacket);
        }
コード例 #2
0
        private Task DispatchPacket(UncompressedPacket packet)
        {
            var  br = new SpanReader(packet.Data);
            Task task;

            switch (packet.PacketId)
            {
            // Teleport Confirm
            case 0x00:
                task = DispatchPacket(TeleportConfirm.Deserialize(ref br));
                break;

            // Chat Message
            case 0x03:
                task = DispatchPacket(ServerboundChatMessage.Deserialize(ref br));
                break;

            // Client Settings
            case 0x05:
                task = DispatchPacket(ClientSettings.Deserialize(ref br));
                break;

            // Plugin Message
            case 0x0B:
                task = DispatchPacket(ServerboundPluginMessage.Deserialize(ref br));
                break;

            // Keep Alive
            case 0x0F:
                task = DispatchPacket(ServerboundKeepAlive.Deserialize(ref br));
                break;

            // Player On Ground
            case 0x0D:
                task = DispatchPacket(PlayerOnGround.Deserialize(ref br));
                break;

            // Player Position
            case 0x11:
                task = DispatchPacket(PlayerPosition.Deserialize(ref br));
                break;

            // Position And Look (In new wiki, it is Player Position And Rotation)
            case 0x12:
                task = DispatchPacket(ServerboundPositionAndLook.Deserialize(ref br));
                break;

            // Player Look (In new wiki, it is Player Rotation)
            case 0x13:
                task = DispatchPacket(PlayerLook.Deserialize(ref br));
                break;

            // Player Digging
            case 0x1A:
                task = DispatchPacket(PlayerDigging.Deserialize(ref br));
                break;

            // Entity Action
            case 0x1B:
                task = DispatchPacket(EntityAction.Deserialize(ref br));
                break;

            // Held Item Change
            case 0x23:
                task = DispatchPacket(ServerboundHeldItemChange.Deserialize(ref br));
                break;

            // Animation
            case 0x2A:
                task = DispatchPacket(ServerboundAnimation.Deserialize(ref br));
                break;

            // Player Block Placement
            case 0x2C:
                task = DispatchPacket(PlayerBlockPlacement.Deserialize(ref br));
                break;

            // Use Item
            case 0x2D:
                task = DispatchPacket(UseItem.Deserialize(ref br));
                break;

            // Click Window
            case 0x09:
                task = DispatchPacket(ClickWindow.Deserialize(ref br));
                break;

            // Close Window
            case 0x0A:
                task = DispatchPacket(ServerboundCloseWindow.Deserialize(ref br));
                break;

            default:
                Logger.LogWarning($"Unrecognizable packet id: 0x{packet.PacketId:X2}.");
                return(Task.CompletedTask);
            }

            // Logger.LogInformation($"Got packet id: 0x{packet.PacketId:X2}.");
            if (!br.IsCosumed)
            {
                throw new InvalidDataException($"Packet data is not fully consumed, packet id: 0x{packet.PacketId:X2}.");
            }
            return(task);
        }