From 7c2f98a20c9ba88cafc6d37081cb5ee21e2b023c Mon Sep 17 00:00:00 2001 From: Ammar Askar Date: Thu, 12 Apr 2012 03:21:16 +0500 Subject: [PATCH] Moar parsing --- NetworkManager.py | 20 +++++++ PacketManager.py | 135 +++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 142 insertions(+), 13 deletions(-) diff --git a/NetworkManager.py b/NetworkManager.py index 690ee3d..17a638d 100644 --- a/NetworkManager.py +++ b/NetworkManager.py @@ -83,6 +83,26 @@ class PacketListener(threading.Thread): PacketManager.handle01(self.socket) if(response[0] == "\x03"): PacketManager.handle03(self.socket) + if(response[0] == "\x04"): + PacketManager.handle04(self.socket) + if(response[0] == "\x05"): + PacketManager.handle05(self.socket) + if(response[0] == "\x06"): + PacketManager.handle06(self.socket) + if(response[0] == "\x07"): + PacketManager.handle07(self.socket) + if(response[0] == "\x08"): + PacketManager.handle08(self.socket) + if(response[0] == "\x09"): + PacketManager.handle09(self.socket) + if(response[0] == "\x0D"): + PacketManager.handle0D(self.socket) + if(response[0] == "\x11"): + PacketManager.handle11(self.socket) + if(response[0] == "\x12"): + PacketManager.handle12(self.socket) + if(response[0] == "\x14"): + PacketManager.handle14(self.socket) if(response[0] == "\xFF"): DisconMessage = PacketManager.handleFF(self.socket, response) if(self.NoGUI == False): diff --git a/PacketManager.py b/PacketManager.py index 11b35f1..faea8fd 100644 --- a/PacketManager.py +++ b/PacketManager.py @@ -2,12 +2,6 @@ import socket import struct import sys -def sendString( packetid, string, socket): - length = struct.pack('!h', string.__len__()) - socket.send(packetid) - socket.send(length) - socket.send(string.encode('utf-16be','strict')) - def sendLoginRequest(socket, username): socket.send("\x01") socket.send(struct.pack('!i', 29)) @@ -31,18 +25,19 @@ def handle01(socket): length = struct.unpack('!h', socket.recv(2))[0] * 2 socket.recv(length) length = struct.unpack('!h', socket.recv(2))[0] * 2 - socket.recv(length) + world = socket.recv(length).decode('utf-16be') mode = struct.unpack('!i', socket.recv(4))[0] dimension = struct.unpack('!i', socket.recv(4))[0] difficulty = struct.unpack('!b', socket.recv(1))[0] socket.recv(1) maxplayers = struct.unpack('!B', socket.recv(1))[0] toReturn = {'EntityID' : Eid, - 'Mode' : mode, - 'Dimension' : dimension, - 'Difficulty' : difficulty, - 'MaxPlayers' : maxplayers - } + 'World' : world, + 'Mode' : mode, + 'Dimension' : dimension, + 'Difficulty' : difficulty, + 'MaxPlayers' : maxplayers + } print toReturn return toReturn @@ -53,12 +48,126 @@ def handle02(socket): return message def handle03(socket): - length = struct.unpack('!h', socket.recv(2))[0] + length = struct.unpack('!h', socket.recv(2))[0] * 2 message = socket.recv(length) message = message.decode('utf-16be','strict') print message return message +def handle04(socket): + time = struct.unpack('!l', socket.recv(4))[0] + return time + +def handle05(socket): + EntityID = struct.unpack('!i', socket.recv(4))[0] + Slot = struct.unpack('!h', socket.recv(2))[0] + ItemID = struct.unpack('!h', socket.recv(2))[0] + Damage = struct.unpack('!h', socket.recv(2))[0] + return {'EntityID' : EntityID, + 'Slot' : Slot, + 'ItemID' : ItemID, + 'Damage' : Damage + } + +def handle06(socket): + x = struct.unpack('!i', socket.recv(4))[0] + y = struct.unpack('!i', socket.recv(4))[0] + z = struct.unpack('!i', socket.recv(4))[0] + return {'x' : x, + 'y' : y, + 'z' : z + } + +def handle07(socket): + userID = struct.unpack('!i', socket.recv(4))[0] + targetID = struct.unpack('!i', socket.recv(4))[0] + mButton = struct.unpack('?', socket.recv(1))[0] + return {'userID' : userID, + 'targetID' : targetID, + 'mButton' : mButton + } + +def handle08(socket): + health = struct.unpack('!h', socket.recv(2))[0] + food = struct.unpack('!h', socket.recv(2))[0] + saturation = struct.unpack('!f', socket.recv(4))[0] + return {'health' : health, + 'food' : food, + 'saturation' : saturation + } + +def handle09(socket): + dimension = struct.unpack('!i', socket.recv(4))[0] + difficulty = struct.unpack('!b', socket.recv(1))[0] + mode = struct.unpack('!b', socket.recv(1))[0] + height = struct.unpack('!h', socket.recv(2))[0] + length = struct.unpack('!h', socket.recv(2))[0] * 2 + world = socket.recv(length).decode('utf-16be') + return {'Dimension' : dimension, + 'Difficulty' : difficulty, + 'Mode' : mode, + 'Height' : height, + 'World' : world + } + +def handle0D(socket): + x = struct.unpack('!d', socket.recv(8))[0] + stance = struct.unpack('!d', socket.recv(8))[0] + y = struct.unpack('!d', socket.recv(8))[0] + z = struct.unpack('!d', socket.recv(8))[0] + yaw = struct.unpack('!f', socket.recv(4))[0] + pitch = struct.unpack('!f', socket.recv(4))[0] + onGround = struct.unpack('?', socket.recv(1))[0] + return {'x' : x, + 'stance' : stance, + 'y' : y, + 'z' : z, + 'yaw' : yaw, + 'pitch' : pitch, + 'onGround' : onGround + } + +def handle11(socket): + EntityID = struct.unpack('!i', socket.recv(4))[0] + socket.recv(1) #Unused + x = struct.unpack('!i', socket.recv(4))[0] + y = struct.unpack('!b', socket.recv(1))[0] + z = struct.unpack('!i', socket.recv(4))[0] + return {'EntityID' : EntityID, + 'x' : x, + 'y' : y, + 'z' : z + } + +def handle12(socket): + EntityID = struct.unpack('!i', socket.recv(4))[0] + Animation = struct.unpack('!b', socket.recv(1))[0] + return {'EntityID' : EntityID, + 'AnimationID' : Animation + } + +def handle14(socket): + EntityID = struct.unpack('!i', socket.recv(4))[0] + length = struct.unpack('!h', socket.recv(2))[0] * 2 + PlayerName = socket.recv(length).decode('utf-16be') + x = struct.unpack('!i', socket.recv(4))[0] + y = struct.unpack('!i', socket.recv(4))[0] + z = struct.unpack('!i', socket.recv(4))[0] + yaw = struct.unpack('!f', socket.recv(4))[0] + pitch = struct.unpack('!f', socket.recv(4))[0] + curItem = struct.unpack('!h', socket.recv(2))[0] + toReturn = {'EntityID' : EntityID, + 'Player Name' : PlayerName, + 'x' : x, + 'y' : y, + 'z' : z, + 'yaw' : yaw, + 'pitch' : pitch, + 'curItem' : curItem + } + print toReturn + return toReturn + def handleFF(socket, response): response = socket.recv(2) length = struct.unpack('!h', response)[0] * 2