Exemplo n.º 1
0
    def write_read_packet(self, packet, compression_threshold):
        for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
            context = ConnectionContext(protocol_version=protocol_version)

            packet_buffer = PacketBuffer()
            packet.write(packet_buffer, compression_threshold)

            packet_buffer.reset_cursor()

            VarInt.read(packet_buffer)
            compressed_size = VarInt.read(packet_buffer)

            if compressed_size > 0:
                decompressed = decompress(packet_buffer.read(compressed_size))
                packet_buffer.reset()
                packet_buffer.send(decompressed)
                packet_buffer.reset_cursor()

            packet_id = VarInt.read(packet_buffer)
            self.assertEqual(packet_id, packet.id)

            deserialized = serverbound.play.ChatPacket(context)
            deserialized.read(packet_buffer)

            self.assertEqual(packet.message, deserialized.message)
Exemplo n.º 2
0
 def write(self, socket, compression_threshold=None):
     packet_buffer = PacketBuffer()
     (x, y, z) = self.location
     Position.send(x, y, z, packet_buffer)
     blockData = ((self.blockId << 4) | (self.blockMeta & 0xF))
     VarInt.send(blockData)
     self._write_buffer(socket, packet_buffer, compression_threshold)
Exemplo n.º 3
0
    def make_add_player_packet(display_name=True):
        packet_buffer = PacketBuffer()

        VarInt.send(0, packet_buffer)  # action_id
        VarInt.send(1, packet_buffer)  # action count
        UUID.send(fake_uuid, packet_buffer)  # uuid
        String.send("player", packet_buffer)  # player name

        VarInt.send(2, packet_buffer)  # number of properties
        String.send("property1", packet_buffer)
        String.send("value1", packet_buffer)
        Boolean.send(False, packet_buffer)  # is signed
        String.send("property2", packet_buffer)
        String.send("value2", packet_buffer)
        Boolean.send(True, packet_buffer)  # is signed
        String.send("signature", packet_buffer)

        VarInt.send(42, packet_buffer)  # game mode
        VarInt.send(69, packet_buffer)  # ping
        Boolean.send(display_name, packet_buffer)  # has display name
        if display_name:
            String.send("display", packet_buffer)  # display name

        packet_buffer.reset_cursor()
        return packet_buffer
Exemplo n.º 4
0
    def _test_read_write_packet(self, packet_in, context=None, **kwargs):
        """
        If kwargs are specified, the key will be tested against the
        respective delta value. Useful for testing FixedPointNumbers
        where there is precision lost in the resulting value.
        """
        if context is None:
            for protocol_version in TEST_VERSIONS:
                logging.debug('protocol_version = %r' % protocol_version)
                context = ConnectionContext(protocol_version=protocol_version)
                self._test_read_write_packet(packet_in, context)
        else:
            packet_in.context = context
            packet_buffer = PacketBuffer()
            packet_in.write(packet_buffer)
            packet_buffer.reset_cursor()
            VarInt.read(packet_buffer)
            packet_id = VarInt.read(packet_buffer)
            self.assertEqual(packet_id, packet_in.id)

            packet_out = type(packet_in)(context=context)
            packet_out.read(packet_buffer)
            self.assertIs(type(packet_in), type(packet_out))

            for packet_attr, precision in kwargs.items():
                packet_attribute_in = packet_in.__dict__.pop(packet_attr)
                packet_attribute_out = packet_out.__dict__.pop(packet_attr)
                self.assertAlmostEqual(packet_attribute_in,
                                       packet_attribute_out,
                                       delta=precision)

            self.assertEqual(packet_in.__dict__, packet_out.__dict__)
Exemplo n.º 5
0
    def packet_roundtrip(self, context, **kwds):
        packet = self.make_map_packet(context, **kwds)

        packet_buffer = PacketBuffer()
        packet.write(packet_buffer)

        packet_buffer.reset_cursor()

        # Read the length and packet id
        VarInt.read(packet_buffer)
        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet.id)

        p = MapPacket(context)
        p.read(packet_buffer)

        self.assertEqual(p.map_id, packet.map_id)
        self.assertEqual(p.scale, packet.scale)
        self.assertEqual(p.is_tracking_position, packet.is_tracking_position)
        self.assertEqual(p.width, packet.width)
        self.assertEqual(p.height, packet.height)
        self.assertEqual(p.offset, packet.offset)
        self.assertEqual(p.pixels, packet.pixels)
        self.assertEqual(str(p.icons[0]), str(packet.icons[0]))
        self.assertEqual(str(p.icons[1]), str(packet.icons[1]))
        self.assertEqual(str(p), str(packet))
Exemplo n.º 6
0
    def test_invalid_action(self):
        packet_buffer = PacketBuffer()
        VarInt.send(200, packet_buffer)  # action_id
        packet_buffer.reset_cursor()

        with self.assertRaises(ValueError):
            PlayerListItemPacket().read(packet_buffer)
Exemplo n.º 7
0
    def test_invalid_action(self):
        packet_buffer = PacketBuffer()
        VarInt.send(200, packet_buffer)  # action_id
        packet_buffer.reset_cursor()

        with self.assertRaises(ValueError):
            PlayerListItemPacket().read(packet_buffer)
Exemplo n.º 8
0
 def write_fields(self, packet_buffer):
     VarInt.send(self.message_id, packet_buffer)
     successful = getattr(self, "data", None) is not None
     successful = getattr(self, "successful", successful)
     Boolean.send(successful, packet_buffer)
     if successful:
         TrailingByteArray.send(self.data, packet_buffer)
Exemplo n.º 9
0
    def read(self, file_object):
        self.empty = False
        self.block_count = Short.read(file_object)
        self.bpb = UnsignedByte.read(file_object)
        if self.bpb <= 4:
            self.bpb = 4

        if self.bpb <= 8: # Indirect palette
            self.palette = []
            size = VarInt.read(file_object)
            for i in range(size):
                self.palette.append(VarInt.read(file_object))
        else: # Direct palette
            self.palette = None

        size = VarInt.read(file_object)
        longs = []
        for i in range(size):
            longs.append(UnsignedLong.read(file_object))

        self.blocks = []
        mask = (1 << self.bpb)-1
        for i in range(4096):
            l1 = int((i*self.bpb)/64)
            offset = (i*self.bpb)%64
            l2 = int(((i+1)*self.bpb-1)/64)
            n = longs[l1] >> offset
            if l2>l1:
                n |= longs[l2] << (64-offset)
            n &= mask
            if self.palette:
                n = self.palette[n]
            self.blocks.append(n)
Exemplo n.º 10
0
 def write_fields(self, packet_buffer):
     VarInt.send(self.message_id, packet_buffer)
     successful = getattr(self, 'data', None) is not None
     successful = getattr(self, 'successful', successful)
     Boolean.send(successful, packet_buffer)
     if successful:
         TrailingByteArray.send(self.data, packet_buffer)
Exemplo n.º 11
0
    def make_action_base(action_id):
        packet_buffer = PacketBuffer()
        VarInt.send(action_id, packet_buffer)
        VarInt.send(1, packet_buffer)  # action count
        UUID.send(fake_uuid, packet_buffer)

        return packet_buffer
Exemplo n.º 12
0
    def make_add_player_packet(display_name=True):
        packet_buffer = PacketBuffer()

        VarInt.send(0, packet_buffer)  # action_id
        VarInt.send(1, packet_buffer)  # action count
        UUID.send(fake_uuid, packet_buffer)  # uuid
        String.send("player", packet_buffer)  # player name

        VarInt.send(2, packet_buffer)  # number of properties
        String.send("property1", packet_buffer)
        String.send("value1", packet_buffer)
        Boolean.send(False, packet_buffer)  # is signed
        String.send("property2", packet_buffer)
        String.send("value2", packet_buffer)
        Boolean.send(True, packet_buffer)  # is signed
        String.send("signature", packet_buffer)

        VarInt.send(42, packet_buffer)  # game mode
        VarInt.send(69, packet_buffer)  # ping
        Boolean.send(display_name, packet_buffer)  # has display name
        if display_name:
            String.send("display", packet_buffer)  # display name

        packet_buffer.reset_cursor()
        return packet_buffer
Exemplo n.º 13
0
    def packet_roundtrip(self, context, **kwds):
        packet = self.make_map_packet(context, **kwds)

        packet_buffer = PacketBuffer()
        packet.write(packet_buffer)

        packet_buffer.reset_cursor()

        # Read the length and packet id
        VarInt.read(packet_buffer)
        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet.id)

        p = MapPacket(context)
        p.read(packet_buffer)

        self.assertEqual(p.map_id, packet.map_id)
        self.assertEqual(p.scale, packet.scale)
        self.assertEqual(p.is_tracking_position, packet.is_tracking_position)
        self.assertEqual(p.width, packet.width)
        self.assertEqual(p.height, packet.height)
        self.assertEqual(p.offset, packet.offset)
        self.assertEqual(p.pixels, packet.pixels)
        self.assertEqual(str(p.icons[0]), str(packet.icons[0]))
        self.assertEqual(str(p.icons[1]), str(packet.icons[1]))
        self.assertEqual(str(p), str(packet))
Exemplo n.º 14
0
    def write_read_packet(self, packet, compression_threshold):
        for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
            context = ConnectionContext(protocol_version=protocol_version)

            packet_buffer = PacketBuffer()
            packet.write(packet_buffer, compression_threshold)

            packet_buffer.reset_cursor()

            VarInt.read(packet_buffer)
            compressed_size = VarInt.read(packet_buffer)

            if compressed_size > 0:
                decompressed = decompress(packet_buffer.read(compressed_size))
                packet_buffer.reset()
                packet_buffer.send(decompressed)
                packet_buffer.reset_cursor()

            packet_id = VarInt.read(packet_buffer)
            self.assertEqual(packet_id, packet.id)

            deserialized = ChatPacket(context)
            deserialized.read(packet_buffer)

            self.assertEqual(packet.message, deserialized.message)
Exemplo n.º 15
0
    def read(self, file_object):
        self.map_id = VarInt.read(file_object)
        self.scale = Byte.read(file_object)

        if self.context.protocol_version >= 107:
            self.is_tracking_position = Boolean.read(file_object)
        else:
            self.is_tracking_position = True

        icon_count = VarInt.read(file_object)
        self.icons = []
        for i in range(icon_count):
            type, direction = divmod(UnsignedByte.read(file_object), 16)
            x = Byte.read(file_object)
            z = Byte.read(file_object)
            icon = MapPacket.MapIcon(type, direction, (x, z))
            self.icons.append(icon)
        self.width = UnsignedByte.read(file_object)
        if self.width:
            self.height = UnsignedByte.read(file_object)
            x = Byte.read(file_object)
            z = Byte.read(file_object)
            self.offset = (x, z)
            self.pixels = VarIntPrefixedByteArray.read(file_object)
        else:
            self.height = 0
            self.offset = None
            self.pixels = None
Exemplo n.º 16
0
    def make_action_base(action_id):
        packet_buffer = PacketBuffer()
        VarInt.send(action_id, packet_buffer)
        VarInt.send(1, packet_buffer)  # action count
        UUID.send(fake_uuid, packet_buffer)

        return packet_buffer
Exemplo n.º 17
0
 def write(self, socket, compression_threshold=None):
     # buffer the data since we need to know the length of each packet's
     # payload
     packet_buffer = PacketBuffer()
     # write packet's id right off the bat in the header
     VarInt.send(self.id, packet_buffer)
     # write every individual field
     self.write_fields(packet_buffer)
     self._write_buffer(socket, packet_buffer, compression_threshold)
Exemplo n.º 18
0
 def read(self, file_object):
     action_id = VarInt.read(file_object)
     self.action_type = PlayerListItemPacket.Action.type_from_id(action_id)
     action_count = VarInt.read(file_object)
     self.actions = []
     for i in range(action_count):
         action = self.action_type()
         action.read(file_object)
         self.actions.append(action)
Exemplo n.º 19
0
    def test_varint(self):
        self.assertEqual(VarInt.size(2), 1)
        self.assertEqual(VarInt.size(1250), 2)

        packet_buffer = PacketBuffer()
        VarInt.send(50000, packet_buffer)
        packet_buffer.reset_cursor()

        self.assertEqual(VarInt.read(packet_buffer), 50000)
Exemplo n.º 20
0
 def write(self, socket, compression_threshold=None):
     # buffer the data since we need to know the length of each packet's
     # payload
     packet_buffer = PacketBuffer()
     # write packet's id right off the bat in the header
     VarInt.send(self.id, packet_buffer)
     # write every individual field
     self.write_fields(packet_buffer)
     self._write_buffer(socket, packet_buffer, compression_threshold)
Exemplo n.º 21
0
    def test_varint(self):
        self.assertEqual(VarInt.size(2), 1)
        self.assertEqual(VarInt.size(1250), 2)

        packet_buffer = PacketBuffer()
        VarInt.send(50000, packet_buffer)
        packet_buffer.reset_cursor()

        self.assertEqual(VarInt.read(packet_buffer), 50000)
Exemplo n.º 22
0
 def read(self, file_object):
     action_id = VarInt.read(file_object)
     self.action_type = PlayerListItemPacket.Action.type_from_id(action_id)
     action_count = VarInt.read(file_object)
     self.actions = []
     for i in range(action_count):
         action = self.action_type()
         action.read(file_object)
         self.actions.append(action)
Exemplo n.º 23
0
 def read(self, file_object):
     self.entity_id = VarInt.read(file_object)
     self.type = Type(VarInt.read(file_object))
     if self.type == Type.INTERACT_AT:
         self.x = Float.read(file_object)
         self.y = Float.read(file_object)
         self.z = Float.read(file_object)
     if self.type == Type.INTERACT or self.type == Type.INTERACT_AT:
         self.hand = VarInt.read(file_object)
     self.sneaking = Boolean.read(file_object)
Exemplo n.º 24
0
 def write_fields(self, packet_buffer):
     if self.context.protocol_later_eq(351):
         VarInt.send(self.transaction_id, packet_buffer)
     String.send(self.text, packet_buffer)
     if self.context.protocol_earlier_eq(350):
         if self.context.protocol_later_eq(95):
             Boolean.send(self.assume_command, packet_buffer)
         Boolean.send(self.looked_at_block is not None, packet_buffer)
         if self.looked_at_block is not None:
             Position.send(self.looked_at_block, packet_buffer)
Exemplo n.º 25
0
    def test_exceptions(self):
        base_type = Type()
        with self.assertRaises(NotImplementedError):
            base_type.read(None)

        with self.assertRaises(NotImplementedError):
            base_type.send(None, None)

        empty_socket = PacketBuffer()
        with self.assertRaises(Exception):
            VarInt.read(empty_socket)
Exemplo n.º 26
0
 def read(self, file_object):
     self.chunk_x = Integer.read(file_object)
     self.chunk_z = Integer.read(file_object)
     records_count = VarInt.read(file_object)
     record_type = self.BaseRecord.get_subclass(self.context)
     self.records = []
     for i in range(records_count):
         record = record_type(h_position=UnsignedByte.read(file_object),
                              y_coordinate=UnsignedByte.read(file_object),
                              blockData=VarInt.read(file_object))
         self.records.append(record)
Exemplo n.º 27
0
    def test_exceptions(self):
        base_type = Type()
        with self.assertRaises(NotImplementedError):
            base_type.read(None)

        with self.assertRaises(NotImplementedError):
            base_type.send(None, None)

        empty_socket = PacketBuffer()
        with self.assertRaises(Exception):
            VarInt.read(empty_socket)
Exemplo n.º 28
0
 def send_with_context(self, record, socket, context):
     if context.protocol_later_eq(741):
         value = record.block_state_id << 12 | \
                 (record.x & 0xF) << 8 | \
                 (record.z & 0xF) << 4 | \
                 record.y & 0xF
         VarLong.send(value, socket)
     else:
         UnsignedByte.send(record.x << 4 | record.z & 0xF, socket)
         UnsignedByte.send(record.y, socket)
         VarInt.send(record.block_state_id, socket)
Exemplo n.º 29
0
 def send_with_context(self, record, socket, context):
     if context.protocol_version >= 741:
         value = (record.block_state_id << 12
                  | (record.x & 0xF) << 8
                  | (record.z & 0xF) << 4
                  | record.y & 0xF)
         VarLong.send(value, socket)
     else:
         UnsignedByte.send(record.x << 4 | record.z & 0xF, socket)
         UnsignedByte.send(record.y, socket)
         VarInt.send(record.block_state_id, socket)
Exemplo n.º 30
0
 def read(self, file_object):
     self.chunk_x = Integer.read(file_object)
     self.chunk_z = Integer.read(file_object)
     records_count = VarInt.read(file_object)
     self.records = []
     for i in range(records_count):
         rec_horizontal_position = UnsignedByte.read(file_object)
         rec_y_coordinate = UnsignedByte.read(file_object)
         rec_blockData = VarInt.read(file_object)
         record = MultiBlockChangePacket.Record(
                      rec_horizontal_position,
                      rec_y_coordinate, rec_blockData)
         self.records.append(record)
Exemplo n.º 31
0
    def _test_read_write_packet(self, packet_in):
        packet_in.context = self.context
        packet_buffer = PacketBuffer()
        packet_in.write(packet_buffer)
        packet_buffer.reset_cursor()
        VarInt.read(packet_buffer)
        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet_in.id)

        packet_out = type(packet_in)(context=self.context)
        packet_out.read(packet_buffer)
        self.assertIs(type(packet_in), type(packet_out))
        self.assertEqual(packet_in.__dict__, packet_out.__dict__)
Exemplo n.º 32
0
    def write(self, socket, compression_threshold=None):
        # buffer the data since we need to know the length of each packet's
        # payload
        packet_buffer = PacketBuffer()
        # write packet's id right off the bat in the header
        VarInt.send(self.id, packet_buffer)
        # write every individual field
        for field in self.definition:
            for var_name, data_type in field.items():
                data = getattr(self, var_name)
                data_type.send(data, packet_buffer)

        self._write_buffer(socket, packet_buffer, compression_threshold)
Exemplo n.º 33
0
    def _test_read_write_packet(self, packet_in):
        packet_in.context = self.context
        packet_buffer = PacketBuffer()
        packet_in.write(packet_buffer)
        packet_buffer.reset_cursor()
        VarInt.read(packet_buffer)
        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet_in.id)

        packet_out = type(packet_in)(context=self.context)
        packet_out.read(packet_buffer)
        self.assertIs(type(packet_in), type(packet_out))
        self.assertEqual(packet_in.__dict__, packet_out.__dict__)
Exemplo n.º 34
0
    def read(self, file_object):
        # print('Reading chunk packet...')
        self.x = Integer.read(file_object)
        self.z = Integer.read(file_object)
        self.gu_continuous = Boolean.read(file_object)
        self.bitmask = VarInt.read(file_object)
        self.data_length = VarInt.read(file_object)
        self.data = file_object.read(self.data_length)
        self.chunk = ChunkDataPacket.Chunk(self.x, self.z, self.gu_continuous,
                                           self.bitmask)

        self.number_of_entities = VarInt.read(file_object)
        for _ in range(self.number_of_entities):
            self.chunk.entities.append(mynbt.parse_bytes(file_object))
Exemplo n.º 35
0
 def _read(self, file_object):
     self.name = String.read(file_object)
     prop_count = VarInt.read(file_object)
     self.properties = []
     for i in range(prop_count):
         property = PlayerListItemPacket.PlayerProperty()
         property.read(file_object)
         self.properties.append(property)
     self.gamemode = VarInt.read(file_object)
     self.ping = VarInt.read(file_object)
     has_display_name = Boolean.read(file_object)
     if has_display_name:
         self.display_name = String.read(file_object)
     else:
         self.display_name = None
Exemplo n.º 36
0
 def read(self, file_object):
     if self.context.protocol_later_eq(346):
         self.transaction_id = VarInt.read(file_object)
         self.start = VarInt.read(file_object)
         self.length = VarInt.read(file_object)
     count = VarInt.read(file_object)
     self.matches = []
     for i in range(count):
         match = String.read(file_object)
         has_tooltip = False
         if self.context.protocol_later_eq(357):
             has_tooltip = Boolean.read(file_object)
         tooltip = String.read(file_object) if has_tooltip else None
         tabmatch = TabCompletePacket.TabMatch(match, tooltip)
         self.matches.append(tabmatch)
Exemplo n.º 37
0
 def _read(self, file_object):
     self.name = String.read(file_object)
     prop_count = VarInt.read(file_object)
     self.properties = []
     for i in range(prop_count):
         property = PlayerListItemPacket.PlayerProperty()
         property.read(file_object)
         self.properties.append(property)
     self.gamemode = VarInt.read(file_object)
     self.ping = VarInt.read(file_object)
     has_display_name = Boolean.read(file_object)
     if has_display_name:
         self.display_name = String.read(file_object)
     else:
         self.display_name = None
Exemplo n.º 38
0
 def read(self, file_object):
     self.message_id = VarInt.read(file_object)
     self.successful = Boolean.read(file_object)
     if self.successful:
         self.data = TrailingByteArray.read(file_object)
     else:
         self.data = None
Exemplo n.º 39
0
 def read(self, file_object):
     self.message_id = VarInt.read(file_object)
     self.successful = Boolean.read(file_object)
     if self.successful:
         self.data = TrailingByteArray.read(file_object)
     else:
         self.data = None
Exemplo n.º 40
0
    def read(self, file_object):
        self.entity_id = VarInt.read(file_object)
        if self._context.protocol_version >= 49:
            self.objectUUID = UUID.read(file_object)
        type_id = Byte.read(file_object)
        self.type = SpawnObjectPacket.EntityType.get_type_by_id(type_id)

        if self._context.protocol_version >= 100:
            self.x = Double.read(file_object)
            self.y = Double.read(file_object)
            self.z = Double.read(file_object)
        else:
            self.x = Integer.read(file_object)
            self.y = Integer.read(file_object)
            self.z = Integer.read(file_object)

        self.pitch = UnsignedByte.read(file_object)
        self.yaw = UnsignedByte.read(file_object)
        self.data = Integer.read(file_object)

        if self._context.protocol_version < 49:
            if self.data > 0:
                self.velocity_x = Short.read(file_object)
                self.velocity_y = Short.read(file_object)
                self.velocity_z = Short.read(file_object)
        else:
            self.velocity_x = Short.read(file_object)
            self.velocity_y = Short.read(file_object)
            self.velocity_z = Short.read(file_object)
Exemplo n.º 41
0
    def write_fields(self, packet_buffer):
        VarInt.send(self.entity_id, packet_buffer)
        if self.context.protocol_version >= 49:
            UUID.send(self.object_uuid, packet_buffer)
        Byte.send(self.type_id, packet_buffer)

        xyz_type = Double if self.context.protocol_version >= 100 else Integer
        for coord in self.x, self.y, self.z:
            xyz_type.send(coord, packet_buffer)
        for coord in self.pitch, self.yaw:
            UnsignedByte.send(coord, packet_buffer)

        Integer.send(self.data, packet_buffer)
        if self.context.protocol_version >= 49 or self.data > 0:
            for coord in self.velocity_x, self.velocity_y, self.velocity_z:
                Short.send(coord, packet_buffer)
Exemplo n.º 42
0
    def read(self, file_object):
        self.map_id = VarInt.read(file_object)
        self.scale = Byte.read(file_object)

        if self.context.protocol_in_range(107, PRE | 6):
            self.is_tracking_position = Boolean.read(file_object)
        elif self.context.protocol_earlier(107):
            self.is_tracking_position = True

        if self.context.protocol_later_eq(452):
            self.is_locked = Boolean.read(file_object)
        else:
            self.is_locked = False

        if self.context.protocol_later_eq(PRE | 6):
            self.is_tracking_position = Boolean.read(file_object)

        icon_count = VarInt.read(file_object)
        self.icons = []
        for i in range(icon_count):
            if self.context.protocol_later_eq(373):
                type = VarInt.read(file_object)
            else:
                type, direction = divmod(UnsignedByte.read(file_object), 16)
            x = Byte.read(file_object)
            z = Byte.read(file_object)
            if self.context.protocol_later_eq(373):
                direction = UnsignedByte.read(file_object)
            if self.context.protocol_later_eq(364):
                has_name = Boolean.read(file_object)
                display_name = String.read(file_object) if has_name else None
            else:
                display_name = None
            icon = MapPacket.MapIcon(type, direction, (x, z), display_name)
            self.icons.append(icon)

        self.width = UnsignedByte.read(file_object)
        if self.width:
            self.height = UnsignedByte.read(file_object)
            x = Byte.read(file_object)
            z = Byte.read(file_object)
            self.offset = (x, z)
            self.pixels = VarIntPrefixedByteArray.read(file_object)
        else:
            self.height = 0
            self.offset = None
            self.pixels = None
Exemplo n.º 43
0
    def test_packet(self):
        packet = ChatPacket()
        packet.message = u"κόσμε"

        packet_buffer = PacketBuffer()
        packet.write(packet_buffer)

        packet_buffer.reset_cursor()
        # Read the length and packet id
        VarInt.read(packet_buffer)
        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet.id)

        deserialized = ChatPacket()
        deserialized.read(packet_buffer)

        self.assertEqual(packet.message, deserialized.message)
Exemplo n.º 44
0
    def test_packet(self):
        packet = ChatPacket()
        packet.message = u"κόσμε"

        packet_buffer = PacketBuffer()
        packet.write(packet_buffer)

        packet_buffer.reset_cursor()
        # Read the length and packet id
        VarInt.read(packet_buffer)
        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet.id)

        deserialized = ChatPacket()
        deserialized.read(packet_buffer)

        self.assertEqual(packet.message, deserialized.message)
Exemplo n.º 45
0
    def read(self, file_object):
        self.map_id = VarInt.read(file_object)
        self.scale = Byte.read(file_object)

        if self.context.protocol_version >= 107:
            self.is_tracking_position = Boolean.read(file_object)
        else:
            self.is_tracking_position = True

        if self.context.protocol_version >= 452:
            self.is_locked = Boolean.read(file_object)
        else:
            self.is_locked = False

        icon_count = VarInt.read(file_object)
        self.icons = []
        for i in range(icon_count):
            if self.context.protocol_version >= 373:
                type = VarInt.read(file_object)
            else:
                type, direction = divmod(UnsignedByte.read(file_object), 16)
            x = Byte.read(file_object)
            z = Byte.read(file_object)
            if self.context.protocol_version >= 373:
                direction = UnsignedByte.read(file_object)
            if self.context.protocol_version >= 364:
                has_name = Boolean.read(file_object)
                display_name = String.read(file_object) if has_name else None
            else:
                display_name = None
            icon = MapPacket.MapIcon(type, direction, (x, z), display_name)
            self.icons.append(icon)

        self.width = UnsignedByte.read(file_object)
        if self.width:
            self.height = UnsignedByte.read(file_object)
            x = Byte.read(file_object)
            z = Byte.read(file_object)
            self.offset = (x, z)
            self.pixels = VarIntPrefixedByteArray.read(file_object)
        else:
            self.height = 0
            self.offset = None
            self.pixels = None
Exemplo n.º 46
0
    def read(self, file_object):
        self.entity_id = VarInt.read(file_object)
        if self.context.protocol_version >= 49:
            self.object_uuid = UUID.read(file_object)

        if self.context.protocol_version >= 458:
            self.type_id = VarInt.read(file_object)
        else:
            self.type_id = Byte.read(file_object)

        xyz_type = Double if self.context.protocol_version >= 100 else Integer
        for attr in 'x', 'y', 'z':
            setattr(self, attr, xyz_type.read(file_object))
        for attr in 'pitch', 'yaw':
            setattr(self, attr, UnsignedByte.read(file_object))

        self.data = Integer.read(file_object)
        if self.context.protocol_version >= 49 or self.data > 0:
            for attr in 'velocity_x', 'velocity_y', 'velocity_z':
                setattr(self, attr, Short.read(file_object))
Exemplo n.º 47
0
    def write_fields(self, packet_buffer):
        VarInt.send(self.entity_id, packet_buffer)
        if self.context.protocol_version >= 49:
            UUID.send(self.object_uuid, packet_buffer)

        if self.context.protocol_version >= 458:
            VarInt.send(self.type_id, packet_buffer)
        else:
            Byte.send(self.type_id, packet_buffer)

        xyz_type = Double if self.context.protocol_version >= 100 else Integer
        for coord in self.x, self.y, self.z:
            xyz_type.send(coord, packet_buffer)
        for coord in self.pitch, self.yaw:
            UnsignedByte.send(coord, packet_buffer)

        Integer.send(self.data, packet_buffer)
        if self.context.protocol_version >= 49 or self.data > 0:
            for coord in self.velocity_x, self.velocity_y, self.velocity_z:
                Short.send(coord, packet_buffer)
Exemplo n.º 48
0
    def test_packet(self):
        for protocol_version in SUPPORTED_PROTOCOL_VERSIONS:
            context = ConnectionContext(protocol_version=protocol_version)

            packet = ChatPacket(context)
            packet.message = u"κόσμε"

            packet_buffer = PacketBuffer()
            packet.write(packet_buffer)

            packet_buffer.reset_cursor()
            # Read the length and packet id
            VarInt.read(packet_buffer)
            packet_id = VarInt.read(packet_buffer)
            self.assertEqual(packet_id, packet.id)

            deserialized = ChatPacket(context)
            deserialized.read(packet_buffer)

            self.assertEqual(packet.message, deserialized.message)
Exemplo n.º 49
0
    def write_fields(self, packet_buffer):
        VarInt.send(self.map_id, packet_buffer)
        Byte.send(self.scale, packet_buffer)
        if self.context.protocol_version >= 107:
            Boolean.send(self.is_tracking_position, packet_buffer)

        VarInt.send(len(self.icons), packet_buffer)
        for icon in self.icons:
            if self.context.protocol_version >= 373:
                VarInt.send(icon.type, packet_buffer)
            else:
                type_and_direction = (icon.type << 4) & 0xF0
                type_and_direction |= (icon.direction & 0xF)
                UnsignedByte.send(type_and_direction, packet_buffer)
            Byte.send(icon.location[0], packet_buffer)
            Byte.send(icon.location[1], packet_buffer)
            if self.context.protocol_version >= 373:
                UnsignedByte.send(icon.direction, packet_buffer)
            if self.context.protocol_version >= 364:
                Boolean.send(icon.display_name is not None, packet_buffer)
                if icon.display_name is not None:
                    String.send(icon.display_name, packet_buffer)

        UnsignedByte.send(self.width, packet_buffer)
        if self.width:
            UnsignedByte.send(self.height, packet_buffer)
            UnsignedByte.send(self.offset[0], packet_buffer)  # x
            UnsignedByte.send(self.offset[1], packet_buffer)  # z
            VarIntPrefixedByteArray.send(self.pixels, packet_buffer)
Exemplo n.º 50
0
    def test_add_and_others(self):
        player_list = PlayerListItemPacket.PlayerList()
        by_uuid = player_list.players_by_uuid

        packet_buffer = self.make_add_player_packet(display_name=False)
        self.read_and_apply(packet_buffer, player_list)
        self.assertEqual(by_uuid[fake_uuid].gamemode, 42)
        self.assertEqual(by_uuid[fake_uuid].ping, 69)
        self.assertIsNone(by_uuid[fake_uuid].display_name)

        # Change the game mode
        packet_buffer = self.make_action_base(1)
        VarInt.send(43, packet_buffer)  # gamemode
        self.read_and_apply(packet_buffer, player_list)
        self.assertEqual(by_uuid[fake_uuid].gamemode, 43)

        # Change the ping
        packet_buffer = self.make_action_base(2)
        VarInt.send(70, packet_buffer)  # ping
        self.read_and_apply(packet_buffer, player_list)
        self.assertEqual(by_uuid[fake_uuid].ping, 70)

        # Change the display name
        packet_buffer = self.make_action_base(3)
        Boolean.send(True, packet_buffer)
        String.send("display2", packet_buffer)
        self.read_and_apply(packet_buffer, player_list)
        self.assertEqual(by_uuid[fake_uuid].display_name, "display2")

        # Remove the display name
        packet_buffer = self.make_action_base(3)
        Boolean.send(False, packet_buffer)
        self.read_and_apply(packet_buffer, player_list)
        self.assertIsNone(by_uuid[fake_uuid].display_name)

        # Remove the player
        packet_buffer = self.make_action_base(4)
        self.read_and_apply(packet_buffer, player_list)
        self.assertNotIn(fake_uuid, player_list.players_by_uuid)
Exemplo n.º 51
0
    def write_read_packet(self, packet, compression_threshold):

        packet_buffer = PacketBuffer()
        packet.write(packet_buffer, compression_threshold)

        packet_buffer.reset_cursor()

        VarInt.read(packet_buffer)
        compressed_size = VarInt.read(packet_buffer)

        if compressed_size > 0:
            decompressed = decompress(packet_buffer.read(compressed_size))
            packet_buffer.reset()
            packet_buffer.send(decompressed)
            packet_buffer.reset_cursor()

        packet_id = VarInt.read(packet_buffer)
        self.assertEqual(packet_id, packet.id)

        deserialized = ChatPacket()
        deserialized.read(packet_buffer)

        self.assertEqual(packet.message, deserialized.message)
Exemplo n.º 52
0
    def _write_buffer(self, socket, packet_buffer, compression_threshold):
        # compression_threshold of None means compression is disabled
        if compression_threshold is not None:
            if len(packet_buffer.get_writable()) > compression_threshold != -1:
                # compress the current payload
                packet_data = packet_buffer.get_writable()
                compressed_data = compress(packet_data)
                packet_buffer.reset()
                # write out the length of the uncompressed payload
                VarInt.send(len(packet_data), packet_buffer)
                # write the compressed payload itself
                packet_buffer.send(compressed_data)
            else:
                # write out a 0 to indicate uncompressed data
                packet_data = packet_buffer.get_writable()
                packet_buffer.reset()
                VarInt.send(0, packet_buffer)
                packet_buffer.send(packet_data)

        VarInt.send(len(packet_buffer.get_writable()), socket)  # Packet Size
        socket.send(packet_buffer.get_writable())  # Packet Payload
Exemplo n.º 53
0
 def read(self, file_object):
     self.duration = VarInt.read(file_object)
     self.entity_id = Integer.read(file_object)
Exemplo n.º 54
0
 def read(self, file_object):
     self.player_id = VarInt.read(file_object)
     self.entity_id = Integer.read(file_object)
     self.message = String.read(file_object)
Exemplo n.º 55
0
 def write(self, packet_buffer):
     VarInt.send(self.player_id, packet_buffer)
     Integer.send(self.entity_id, packet_buffer)
     String.send(self.message, packet_buffer)
Exemplo n.º 56
0
 def read(self, file_object):
     event_id = VarInt.read(file_object)
     self.event = CombatEventPacket.EventType.type_from_id(event_id)()
     self.event.read(file_object)
Exemplo n.º 57
0
 def write_fields(self, packet_buffer):
     VarInt.send(self.event.id, packet_buffer)
     self.event.write(packet_buffer)
Exemplo n.º 58
0
 def write(self, packet_buffer):
     VarInt.send(self.duration, packet_buffer)
     Integer.send(self.entity_id, packet_buffer)
Exemplo n.º 59
0
 def _read(self, file_object):
     self.gamemode = VarInt.read(file_object)
Exemplo n.º 60
0
 def _read(self, file_object):
     self.ping = VarInt.read(file_object)