diff --git a/minecraft/networking/packets.py b/minecraft/networking/packets.py index 64d8e76..038f2ae 100644 --- a/minecraft/networking/packets.py +++ b/minecraft/networking/packets.py @@ -21,6 +21,9 @@ class PacketBuffer(object): def read(self, length): return self.bytes.read(length) + def recv(self, length): + return self.read(length) + def reset(self): self.bytes = BytesIO() @@ -56,7 +59,7 @@ class Packet(object): def read(self, file_object): for field in self.definition: - for var_name, data_type in field.iteritems(): + for var_name, data_type in field.items(): value = data_type.read(file_object) setattr(self, var_name, value) @@ -68,7 +71,7 @@ class Packet(object): VarInt.send(self.id, packet_buffer) for field in self.definition: - for var_name, data_type in field.iteritems(): + for var_name, data_type in field.items(): data = getattr(self, var_name) data_type.send(data, packet_buffer) diff --git a/minecraft/networking/types.py b/minecraft/networking/types.py index 099fd07..3995d35 100644 --- a/minecraft/networking/types.py +++ b/minecraft/networking/types.py @@ -198,6 +198,6 @@ class String(Type): @staticmethod def send(value, socket): - value = str(value).encode('utf-8') + value = value.encode('utf-8') VarInt.send(len(value), socket) socket.send(value) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index bb5b292..5bcef72 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -1,10 +1,16 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- import unittest +import string +from random import choice +from zlib import decompress from minecraft.networking.types import ( Type, Boolean, UnsignedByte, Byte, Short, UnsignedShort, Integer, VarInt, Long, Float, Double, ShortPrefixedByteArray, - VarIntPrefixedByteArray, String + VarIntPrefixedByteArray, String as StringType ) -from minecraft.networking.packets import PacketBuffer +from minecraft.networking.packets import PacketBuffer, ChatPacket + TEST_DATA = { Boolean: [True, False], @@ -19,7 +25,7 @@ TEST_DATA = { Double: [36.004002], ShortPrefixedByteArray: [bytes(245)], VarIntPrefixedByteArray: [bytes(1234)], - String: ["hello world"] + StringType: ["hello world"] } @@ -40,3 +46,59 @@ class SerializationTest(unittest.TestCase): self.assertAlmostEquals(test_data, deserialized, 3) else: self.assertEqual(test_data, deserialized) + + 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_socket(packet_buffer), 50000) + + 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_compressed_packet(self): + msg = ''.join(choice(string.ascii_lowercase) for i in range(500)) + + packet = ChatPacket() + packet.message = msg + + packet_buffer = PacketBuffer() + packet.write(packet_buffer, compression_threshold=20) + + 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)