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))
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)
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
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__)
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)
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)
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)
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)
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)
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)
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__)
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)
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))
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
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)
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
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)
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
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)
def test_varint(self): self.assertEqual(VarInt.size(2), 1) self.assertEqual(VarInt.size(1250), 2) with self.assertRaises(ValueError): VarInt.size(2 ** 90) with self.assertRaises(ValueError): packet_buffer = PacketBuffer() VarInt.send(2 ** 49, packet_buffer) packet_buffer.reset_cursor() VarInt.read(packet_buffer) packet_buffer = PacketBuffer() VarInt.send(50000, packet_buffer) packet_buffer.reset_cursor() self.assertEqual(VarInt.read(packet_buffer), 50000)
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): record = self.Record() record.read(file_object, self) self.records.append(record)
def read(self, file_object, parent): h_position = UnsignedByte.read(file_object) self.x, self.z = h_position >> 4, h_position & 0xF self.y = UnsignedByte.read(file_object) self.block_state_id = VarInt.read(file_object) # Absolute position in world to be compatible with BlockChangePacket self.location = Vector(self.position.x + parent.chunk_x * 16, self.position.y, self.position.z + parent.chunk_z * 16)
def read(self, file_object): self.x = Integer.read(file_object) self.z = Integer.read(file_object) self.full_chunk = Boolean.read(file_object) self.bit_mask_y = VarInt.read(file_object) self.heightmaps = Nbt.read(file_object) self.biomes = [] if self.full_chunk: for i in range(1024): self.biomes.append(Integer.read(file_object)) size = VarInt.read(file_object) self.data = file_object.read(size) size_entities = VarInt.read(file_object) self.entities = [] for i in range(size_entities): self.entities.append(Nbt.read(file_object)) self.decode_chunk_data()
def read(self, file_object): self.location = Position.read(file_object) blockData = VarInt.read(file_object) if self.context.protocol_version >= 347: # See comments on MultiBlockChangePacket.OpaqueRecord. self.blockStateId = blockData else: self.blockId = (blockData >> 4) self.blockMeta = (blockData & 0xF)
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)
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
def read(self, file_object): self.transaction_id = VarInt.read(file_object) \ if self.context.protocol_later_eq(351) else None self.text = String.read(file_object) if self.context.protocol_earlier_eq(350): self.assume_command = Boolean.read(file_object) \ if self.context.protocol_later_eq(95) else None has_position = Boolean.read(file_object) if has_position: self.looked_at_block = Position.read(file_object)
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)
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, Angle.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))
def read(self, file_object): self.entity_id = VarInt.read(file_object) if self.context.protocol_later_eq(49): self.object_uuid = UUID.read(file_object) if self.context.protocol_later_eq(458): self.type_id = VarInt.read(file_object) else: self.type_id = Byte.read(file_object) xyz_type = Double if self.context.protocol_later_eq(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, Angle.read(file_object)) self.data = Integer.read(file_object) if self.context.protocol_later_eq(49) or self.data > 0: for attr in "velocity_x", "velocity_y", "velocity_z": setattr(self, attr, Short.read(file_object))
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))
def test_packet(self): for protocol_version in SUPPORTED_PROTOCOL_VERSIONS: context = ConnectionContext(protocol_version=protocol_version) packet = serverbound.play.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 = serverbound.play.ChatPacket(context) deserialized.read(packet_buffer) self.assertEqual(packet.message, deserialized.message)
def read(self, file_object): if self.context.protocol_version >= 353: self.origin = VarInt.read(file_object) self.x = Double.read(file_object) self.y = Double.read(file_object) self.z = Double.read(file_object) is_entity = Boolean.read(file_object) if is_entity: # If the entity given by entity ID cannot be found, # this packet should be treated as if is_entity was false. self.entity_id = VarInt.read(file_object) self.entity_origin = VarInt.read(file_object) else: self.entity_id = None else: # Protocol version 352 is_entity = Boolean.read(file_object) self.entity_id = VarInt.read(file_object) if is_entity else None if not is_entity: self.x = Double.read(file_object) self.y = Double.read(file_object) self.z = Double.read(file_object)
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)
def _read(self, file_object): self.gamemode = VarInt.read(file_object)
def read(self, file_object): self.duration = VarInt.read(file_object) self.entity_id = Integer.read(file_object)
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)
def _read(self, file_object): self.ping = VarInt.read(file_object)
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)