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 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 test_get_writable(self): message = b"hello" packet_buffer = PacketBuffer() packet_buffer.send(message) self.assertEqual(packet_buffer.get_writable(), message)
def test_basic_read_write(self): message = b"hello" packet_buffer = PacketBuffer() packet_buffer.send(message) packet_buffer.reset_cursor() self.assertEqual(packet_buffer.read(), message) packet_buffer.reset_cursor() self.assertEqual(packet_buffer.recv(), message) packet_buffer.reset() self.assertNotEqual(packet_buffer.read(), message)
def decode_chunk_data(self): packet_data = PacketBuffer() packet_data.send(self.data) packet_data.reset_cursor() self.chunks = {} for i in range(16): #0-15 self.chunks[i] = Chunk(self.x, i, self.z) if self.bit_mask_y & (1 << i): self.chunks[i].read(packet_data) for e in self.entities: y = e['y'] self.chunks[floor(y/16)].entities.append(e)
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_data(self, data, dimension): file_object = PacketBuffer() file_object.send(data) file_object.reset_cursor() for i in range(16): if self.bitmask & (1 << i): bits_per_block = UnsignedByte.read(file_object) palette = None if bits_per_block < GLOBAL_BITS_PER_BLOCK: palette_length = VarInt.read(file_object) palette = [] for _ in range(palette_length): palette.append(VarInt.read(file_object)) section = ChunkDataPacket.ChunkSection() data_length = VarInt.read(file_object) data = [] for _ in range(data_length): part = file_object.read(8) data.append(int.from_bytes(part, 'big')) section.data = data section.palette = palette block_mask = (1 << bits_per_block) - 1 # print(i) for y in range(16): for z in range(16): for x in range(16): block_mask = (1 << bits_per_block) - 1 number = (((y << 4) + z) << 4) + x long_number = (number * bits_per_block) >> 6 bit_in_long_number = (number * bits_per_block) & 63 block = (data[long_number] >> bit_in_long_number) & (block_mask) if bit_in_long_number + bits_per_block > 64: block |= (data[long_number + 1] & ( (1 << (bit_in_long_number + bits_per_block - 64)) - 1)) << (64 - bit_in_long_number) if palette: # if block > 0: # print(palette) # print(len(palette)) # print(block) # print(bits_per_block) # print((x, y, z, self.x, self.z)) block = palette[block] if type(block) is float: print(block) section.blocks[x][y][z] = block section.light = file_object.read(2048) if dimension == 0: section.sky_light = file_object.read(2048) self.sections[i] = section self.update_blocks()