def read(stream, packet_size): channel_len = StreamIO.read_varint(stream) channel = StreamIO.read(stream, channel_len).decode("utf8") plugin_bytes = StreamIO.read( stream, packet_size - StreamIO.size_varint(channel_len) - channel_len) return PluginMessageServerPacket(channel, plugin_bytes)
def read(self, stream): packet_size = StreamIO.read_varint(stream) if self.is_compression_enabled(): data_size = StreamIO.read_varint(stream) if data_size == 0: packet_size -= StreamIO.size_varint(0) data = StreamIO.read(stream, packet_size) else: data = StreamIO.read( stream, packet_size - StreamIO.size_varint(data_size)) data = zlib.decompress(data) packet_size = data_size else: data = StreamIO.read(stream, packet_size) buf = StringIO(data) packet_id = StreamIO.read_varint(buf) packet_size -= StreamIO.size_varint(packet_id) packet_direction = None if self._mode == PacketSerializer.Mode.SERVER: packet_direction = PacketDirection.SERVERBOUND elif self._mode == PacketSerializer.Mode.CLIENT: packet_direction = PacketDirection.CLIENTBOUND try: packet_class = PacketProvider.get_packet_class( self._protocol, self._state, packet_direction, packet_id) except KeyError: buf.close() return BasePacket() # Unknown packet packet = packet_class.read(buf, packet_size) buf.close() return packet