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
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
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)
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)
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)
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)
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)
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 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)
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)
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)
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)
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)
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 write_fields(self, packet_buffer): if self.context.protocol_later_eq(346): VarInt.send(self.transaction_id, packet_buffer) VarInt.send(self.start, packet_buffer) VarInt.send(self.length, packet_buffer) VarInt.send(len(self.matches), packet_buffer) for tabmatch in self.matches: String.send(tabmatch.match, packet_buffer) if self.context.protocol_later_eq(357): Boolean.send(tabmatch.tooltip is not None, packet_buffer) if tabmatch.tooltip is not None: String.send(tabmatch.tooltip, packet_buffer)
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)
def make_modlist_packet(self, packet): packet.channel = 'FML|HS' buffer = PacketBuffer() Byte.send(2, buffer) modlist = [] __location__ = os.path.realpath( os.path.join(os.getcwd(), os.path.dirname(__file__))) with open(os.path.join(__location__, 'modlist5.txt'), 'r') as f: mods = f.read().split(',') for mod in mods: mod_name = mod.split('@')[0] mod_version = mod[len(mod_name) + 1:] modlist.append([mod_name, mod_version]) VarInt.send(len(modlist), buffer) for mod in modlist: String.send(mod[0], buffer) String.send(mod[1], buffer) buffer.reset_cursor() packet.data = buffer.read() print(packet.data)
def write_fields(self, packet_buffer): VarInt.send(self.entity_id, packet_buffer) if self.context.protocol_later_eq(49): UUID.send(self.object_uuid, packet_buffer) if self.context.protocol_later_eq(458): VarInt.send(self.type_id, packet_buffer) else: Byte.send(self.type_id, packet_buffer) # pylint: disable=no-member xyz_type = Double if self.context.protocol_later_eq(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: Angle.send(coord, packet_buffer) Integer.send(self.data, packet_buffer) if self.context.protocol_later_eq(49) or self.data > 0: for coord in self.velocity_x, self.velocity_y, self.velocity_z: Short.send(coord, packet_buffer)
def write(self, socket, compression_threshold=None): packet_buffer = PacketBuffer() VarInt.send(self.id, 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: type_and_direction = (icon.direction << 4) & 0xF0 type_and_direction |= (icon.type & 0xF) UnsignedByte.send(type_and_direction, packet_buffer) Byte.send(icon.location[0], packet_buffer) Byte.send(icon.location[1], 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) self._write_buffer(socket, packet_buffer, compression_threshold)
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)
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)
def write_fields(self, packet_buffer): VarInt.send(self.entity_id, packet_buffer) VarInt.send(int(self.type), packet_buffer) if self.type == self.Type.INTERACT_AT: Float.send(x, packet_buffer) Float.send(y, packet_buffer) Float.send(z, packet_buffer) if self.type == self.Type.INTERACT or self.type == self.Type.INTERACT_AT: VarInt.send(self.hand, packet_buffer) Boolean.send(self.sneaking, packet_buffer)
def _send(self, packet_buffer): String.send(self.name, packet_buffer) VarInt.send(len(self.properties), packet_buffer) for property in self.properties: property.send(packet_buffer) VarInt.send(self.gamemode, packet_buffer) VarInt.send(self.ping, packet_buffer) if self.display_name is not None: Boolean.send(True, packet_buffer) String.send(self.display_name, packet_buffer) else: Boolean.send(False, packet_buffer)
def write_fields(self, packet_buffer): Integer.send(self.x, packet_buffer) Integer.send(self.z, packet_buffer) Boolean.send(self.full_chunk, packet_buffer) VarInt.send(self.bit_mask_y, packet_buffer) Nbt.send(self.heightmaps, packet_buffer) if self.full_chunk: for i in range(1024): Integer.send(self.biomes[i], packet_buffer) VarInt.send(len(self.data), packet_buffer) packet_buffer.send(self.data) VarInt.send(len(self.entities), packet_buffer) for e in self.entities: Nbt.send(e, packet_buffer)
def write_fields(self, packet_buffer): if self.context.protocol_version >= 353: VarInt.send(self.origin, packet_buffer) Double.send(self.x, packet_buffer) Double.send(self.y, packet_buffer) Double.send(self.z, packet_buffer) if self.entity_id is not None: Boolean.send(True, packet_buffer) VarInt.send(self.entity_id, packet_buffer) VarInt.send(self.entity_origin, packet_buffer) else: Boolean.send(False, packet_buffer) else: # Protocol version 352 if self.entity_id is not None: Boolean.send(True, packet_buffer) VarInt.send(self.entity_id, packet_buffer) else: Boolean.send(False, packet_buffer) Double.send(self.x, packet_buffer) Double.send(self.y, packet_buffer) Double.send(self.z, packet_buffer)
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
def write(self, packet_buffer): VarInt.send(self.duration, packet_buffer) Integer.send(self.entity_id, packet_buffer)
def write_fields(self, packet_buffer): VarInt.send(self.length, packet_buffer) for i in self.entities: VarInt.send(self.entities, packet_buffer)
def write_fields(self, packet_buffer): VarInt.send(self.event.id, packet_buffer) self.event.write(packet_buffer)
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)
def _send(self, packet_buffer): VarInt.send(self.ping, packet_buffer)
def write_fields(self, packet_buffer): VarInt.send(self.action_type.action_id, packet_buffer) VarInt.send(len(self.actions), packet_buffer) for action in self.actions: action.send(packet_buffer)