Moved raw data type handling to DataUtil.py

This commit is contained in:
Ammar Askar committed 2012-10-26 15:31:47 +05:00
1 parent 7c1d5bdc39
commit 402aa6954f
3 files changed
+346 -329

No files matched your search

+84
View File
@@ -0,0 +1,84 @@
import struct
def readBoolean(FileObject):
return struct.unpack('?', FileObject.read(1))[0]
def readByte(FileObject):
return struct.unpack('!b', FileObject.read(1))[0]
def readUnsignedByte(FileObject):
return struct.unpack('!B', FileObject.read(1))[0]
def readShort(FileObject):
return struct.unpack('!h', FileObject.read(2))[0]
def readUnsignedShort(FileObject):
return struct.unpack('!H', FileObject.read(2))[0]
def readInt(FileObject):
return struct.unpack('!i', FileObject.read(4))[0]
def readFloat(FileObject):
return struct.unpack('!f', FileObject.read(4))[0]
def readLong(FileObject):
return struct.unpack('!q', FileObject.read(8))[0]
def readDouble(FileObject):
return struct.unpack('!d', FileObject.read(8))[0]
def readByteArray(FileObject, length):
return struct.unpack(str(length) + "s", FileObject.read(length))[0]
def readString(FileObject):
length = readShort(FileObject) * 2
return FileObject.read(length).decode("utf-16be")
def readEntityMetadata(FileObject):
metadata = {}
byte = struct.unpack('!B', FileObject.read(1))[0]
while byte != 127:
index = byte & 0x1F # Lower 5 bits
ty = byte >> 5 # Upper 3 bits
if ty == 0: val = struct.unpack('!b', FileObject.read(1))[0]
if ty == 1: val = struct.unpack('!h', FileObject.read(2))[0]
if ty == 2: val = readInt(FileObject)
if ty == 3: val = struct.unpack('!f', FileObject.read(4))[0]
if ty == 4:
length = struct.unpack('!h', FileObject.read(2))[0] * 2
val = FileObject.read(length).decode('utf-16be')
if ty == 5:
val = {}
val["id"] = struct.unpack('!h', FileObject.read(2))[0]
if (val["id"] != -1):
val["count"] = struct.unpack('!b', FileObject.read(1))[0]
val["damage"] = struct.unpack('!h', FileObject.read(2))[0]
if ty == 6:
val = []
for i in range(3):
val.append(readInt(FileObject))
metadata[index] = (ty, val)
byte = struct.unpack('!B', FileObject.read(1))[0]
return metadata
def readSlotData(FileObject):
BlockID = struct.unpack('!h', FileObject.read(2))[0]
if(BlockID != -1):
ItemCount = struct.unpack('!b', FileObject.read(1))[0]
Damage = struct.unpack('!h', FileObject.read(2))[0]
MetadataLength = struct.unpack('!h', FileObject.read(2))[0]
if(MetadataLength != -1):
raw = FileObject.read(MetadataLength)
ByteArray = struct.unpack(str(MetadataLength) + "s", raw)[0]
return {'BlockID' : BlockID,
'ItemCount' : ItemCount,
'Damage' : Damage,
'Data' : ByteArray
}
return {'BlockID' : BlockID,
'ItemCount' : ItemCount,
'Damage' : Damage
}
return {'BlockID' : -1,
'ItemCount' : 0
}
+255 -329
View File
@@ -1,90 +1,83 @@
import struct import DataUtil
import zlib import PacketSenderManager
def handle00(FileObject, socket): def handle00(FileObject, socket):
KAid = struct.unpack('!i', FileObject.read(4))[0] KAid = DataUtil.readInt(FileObject)
socket.send("\x00" + struct.pack('!i', KAid)) PacketSenderManager.send00(socket, KAid)
def handle01(FileObject): def handle01(FileObject):
Eid = struct.unpack('!i', FileObject.read(4))[0] Eid = DataUtil.readInt(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 world = DataUtil.readString(FileObject)
world = FileObject.read(length).decode('utf-16be') mode = DataUtil.readByte(FileObject)
mode = struct.unpack('!b', FileObject.read(1))[0] dimension = DataUtil.readByte(FileObject)
dimension = struct.unpack('!b', FileObject.read(1))[0] difficulty = DataUtil.readByte(FileObject)
difficulty = struct.unpack('!b', FileObject.read(1))[0]
FileObject.read(1) FileObject.read(1)
maxplayers = struct.unpack('!B', FileObject.read(1))[0] maxplayers = DataUtil.readByte(FileObject)
toReturn = {'EntityID' : Eid, return {'EntityID' : Eid,
'World' : world, 'World' : world,
'Mode' : mode, 'Mode' : mode,
'Dimension' : dimension, 'Dimension' : dimension,
'Difficulty' : difficulty, 'Difficulty' : difficulty,
'MaxPlayers' : maxplayers 'MaxPlayers' : maxplayers
} }
return toReturn
def handle02(FileObject): def handle02(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 message = DataUtil.readString(FileObject)
message = FileObject.read(length)
message = message.decode('utf-16be', 'strict')
return message return message
def handle03(FileObject): def handle03(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 message = DataUtil.readString(FileObject)
message = FileObject.read(length)
message = message.decode('utf-16be','strict')
return message return message
def handle04(FileObject): def handle04(FileObject):
time = struct.unpack('!q', FileObject.read(8))[0] time = DataUtil.readLong(FileObject)
dayTime = struct.unpack('!q', FileObject.read(8))[0] dayTime = DataUtil.readLong(FileObject)
return {'Time' : time, return {'Time' : time,
'DayTime' : dayTime 'DayTime' : dayTime
} }
def handle05(FileObject): def handle05(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
Slot = struct.unpack('!h', FileObject.read(2))[0] Slot = DataUtil.readShort(FileObject)
Item = decodeSlotData(FileObject) Item = DataUtil.readSlotData(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'Slot' : Slot, 'Slot' : Slot,
'Item' : Item 'Item' : Item
} }
def handle06(FileObject): def handle06(FileObject):
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
return {'x' : x, return {'x' : x,
'y' : y, 'y' : y,
'z' : z 'z' : z
} }
def handle07(FileObject): def handle07(FileObject):
userID = struct.unpack('!i', FileObject.read(4))[0] userID = DataUtil.readInt(FileObject)
targetID = struct.unpack('!i', FileObject.read(4))[0] targetID = DataUtil.readInt(FileObject)
mButton = struct.unpack('?', FileObject.read(1))[0] mButton = DataUtil.readBoolean(FileObject)
return {'userID' : userID, return {'userID' : userID,
'targetID' : targetID, 'targetID' : targetID,
'mButton' : mButton 'mButton' : mButton
} }
def handle08(FileObject): def handle08(FileObject):
health = struct.unpack('!h', FileObject.read(2))[0] health = DataUtil.readShort(FileObject)
food = struct.unpack('!h', FileObject.read(2))[0] food = DataUtil.readShort(FileObject)
saturation = struct.unpack('!f', FileObject.read(4))[0] saturation = DataUtil.readFloat(FileObject)
return {'health' : health, return {'health' : health,
'food' : food, 'food' : food,
'saturation' : saturation 'saturation' : saturation
} }
def handle09(FileObject): def handle09(FileObject):
dimension = struct.unpack('!i', FileObject.read(4))[0] dimension = DataUtil.readInt(FileObject)
difficulty = struct.unpack('!b', FileObject.read(1))[0] difficulty = DataUtil.readByte(FileObject)
mode = struct.unpack('!b', FileObject.read(1))[0] mode = DataUtil.readByte(FileObject)
height = struct.unpack('!h', FileObject.read(2))[0] height = DataUtil.readShort(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 world = DataUtil.readString(FileObject)
world = FileObject.read(length).decode('utf-16be')
return {'Dimension' : dimension, return {'Dimension' : dimension,
'Difficulty' : difficulty, 'Difficulty' : difficulty,
'Mode' : mode, 'Mode' : mode,
@@ -93,13 +86,13 @@ def handle09(FileObject):
} }
def handle0D(FileObject): def handle0D(FileObject):
x = struct.unpack('!d', FileObject.read(8))[0] x = DataUtil.readDouble(FileObject)
stance = struct.unpack('!d', FileObject.read(8))[0] stance = DataUtil.readDouble(FileObject)
y = struct.unpack('!d', FileObject.read(8))[0] y = DataUtil.readDouble(FileObject)
z = struct.unpack('!d', FileObject.read(8))[0] z = DataUtil.readDouble(FileObject)
yaw = struct.unpack('!f', FileObject.read(4))[0] yaw = DataUtil.readFloat(FileObject)
pitch = struct.unpack('!f', FileObject.read(4))[0] pitch = DataUtil.readFloat(FileObject)
onGround = struct.unpack('?', FileObject.read(1))[0] onGround = DataUtil.readBoolean(FileObject)
return {'x' : x, return {'x' : x,
'stance' : stance, 'stance' : stance,
'y' : y, 'y' : y,
@@ -110,11 +103,11 @@ def handle0D(FileObject):
} }
def handle11(FileObject): def handle11(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
FileObject.read(1) #Unused FileObject.read(1) #Unused
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!b', FileObject.read(1))[0] y = DataUtil.readByte(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -122,23 +115,22 @@ def handle11(FileObject):
} }
def handle12(FileObject): def handle12(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
Animation = struct.unpack('!b', FileObject.read(1))[0] Animation = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'AnimationID' : Animation 'AnimationID' : Animation
} }
def handle14(FileObject): def handle14(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 PlayerName = DataUtil.readString(FileObject)
PlayerName = FileObject.read(length).decode('utf-16be') x = DataUtil.readInt(FileObject)
x = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] yaw = DataUtil.readFloat(FileObject)
yaw = struct.unpack('!f', FileObject.read(4))[0] pitch = DataUtil.readFloat(FileObject)
pitch = struct.unpack('!f', FileObject.read(4))[0] curItem = DataUtil.readShort(FileObject)
curItem = struct.unpack('!h', FileObject.read(2))[0] metadata = DataUtil.readEntityMetadata(FileObject)
metadata = readEntityMetadata(FileObject)
toReturn = {'EntityID' : EntityID, toReturn = {'EntityID' : EntityID,
'Player Name' : PlayerName, 'Player Name' : PlayerName,
'x' : x, 'x' : x,
@@ -152,20 +144,20 @@ def handle14(FileObject):
return toReturn return toReturn
def handle15(FileObject): def handle15(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
ItemID = struct.unpack('!h', FileObject.read(2))[0] ItemID = DataUtil.readShort(FileObject)
if (ItemID != -1): if (ItemID != -1):
Count = struct.unpack('!b', FileObject.read(1))[0] Count = DataUtil.readByte(FileObject)
Damage = struct.unpack('!h', FileObject.read(2))[0] Damage = DataUtil.readShort(FileObject)
ArrayLength = struct.unpack('!h', FileObject.read(2))[0] ArrayLength = DataUtil.readShort(FileObject)
if (ArrayLength != -1): if (ArrayLength != -1):
Array = FileObject.read(ArrayLength) #TODO: find out what this does and do stuff accrodingly Array = FileObject.read(ArrayLength) #TODO: find out what this does and do stuff accrodingly
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
Rotation = struct.unpack('!b', FileObject.read(1))[0] Rotation = DataUtil.readByte(FileObject)
Pitch = struct.unpack('!b', FileObject.read(1))[0] Pitch = DataUtil.readByte(FileObject)
Roll = struct.unpack('!b', FileObject.read(1))[0] Roll = DataUtil.readByte(FileObject)
toReturn = {'EntityID' : EntityID, toReturn = {'EntityID' : EntityID,
'ItemID' : ItemID, 'ItemID' : ItemID,
'x' : x, 'x' : x,
@@ -181,23 +173,23 @@ def handle15(FileObject):
return toReturn return toReturn
def handle16(FileObject): def handle16(FileObject):
CollectedID = struct.unpack('!i', FileObject.read(4))[0] CollectedID = DataUtil.readInt(FileObject)
CollectorID = struct.unpack('!i', FileObject.read(4))[0] CollectorID = DataUtil.readInt(FileObject)
return {'CollectedID' : CollectedID, return {'CollectedID' : CollectedID,
'CollectorID' : CollectorID 'CollectorID' : CollectorID
} }
def handle17(FileObject): def handle17(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
Type = struct.unpack('!b', FileObject.read(1))[0] Type = DataUtil.readByte(FileObject)
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
ThrowerEntityID = struct.unpack('!i', FileObject.read(4))[0] ThrowerEntityID = DataUtil.readInt(FileObject)
if(ThrowerEntityID > 0): if(ThrowerEntityID > 0):
SpeedX = struct.unpack('!h', FileObject.read(2))[0] SpeedX = DataUtil.readShort(FileObject)
SpeedY = struct.unpack('!h', FileObject.read(2))[0] SpeedY = DataUtil.readShort(FileObject)
SpeedZ = struct.unpack('!h', FileObject.read(2))[0] SpeedZ = DataUtil.readShort(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'Type' : Type, 'Type' : Type,
'x' : x, 'x' : x,
@@ -217,18 +209,18 @@ def handle17(FileObject):
} }
def handle18(FileObject): def handle18(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
Type = struct.unpack('!b', FileObject.read(1))[0] Type = DataUtil.readByte(FileObject)
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
Yaw = struct.unpack('!b', FileObject.read(1))[0] Yaw = DataUtil.readByte(FileObject)
Pitch = struct.unpack('!b', FileObject.read(1))[0] Pitch = DataUtil.readByte(FileObject)
HeadYaw = struct.unpack('!b', FileObject.read(1))[0] HeadYaw = DataUtil.readByte(FileObject)
VelocityX = struct.unpack('!h', FileObject.read(2))[0] VelocityX = DataUtil.readShort(FileObject)
VelocityY = struct.unpack('!h', FileObject.read(2))[0] VelocityY = DataUtil.readShort(FileObject)
VelocityZ = struct.unpack('!h', FileObject.read(2))[0] VelocityZ = DataUtil.readShort(FileObject)
metadata = readEntityMetadata(FileObject) metadata = DataUtil.readEntityMetadata(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'Type' : Type, 'Type' : Type,
@@ -245,14 +237,12 @@ def handle18(FileObject):
} }
def handle19(FileObject): def handle19(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 Title = DataUtil.readString(FileObject)
Title = FileObject.read(length).decode('utf-16be') x = DataUtil.readInt(FileObject)
raw = FileObject.read(4) y = DataUtil.readInt(FileObject)
x = struct.unpack('!i', raw)[0] z = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] Direction = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0]
Direction = struct.unpack('!i', FileObject.read(4))[0]
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'Title' : Title, 'Title' : Title,
'x' : x, 'x' : x,
@@ -262,11 +252,11 @@ def handle19(FileObject):
} }
def handle1A(FileObject): def handle1A(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
Count = struct.unpack('!h', FileObject.read(2))[0] Count = DataUtil.readShort(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -275,10 +265,10 @@ def handle1A(FileObject):
} }
def handle1C(FileObject): def handle1C(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
VelocityX = struct.unpack('!h', FileObject.read(2))[0] VelocityX = DataUtil.readShort(FileObject)
VelocityY = struct.unpack('!h', FileObject.read(2))[0] VelocityY = DataUtil.readShort(FileObject)
VelocityZ = struct.unpack('!h', FileObject.read(2))[0] VelocityZ = DataUtil.readShort(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'VelocityX' : VelocityX, 'VelocityX' : VelocityX,
'VelocityY' : VelocityY, 'VelocityY' : VelocityY,
@@ -286,21 +276,21 @@ def handle1C(FileObject):
} }
def handle1D(FileObject): def handle1D(FileObject):
EntityArrayLength = struct.unpack('!b', FileObject.read(1))[0] EntityArrayLength = DataUtil.readByte(FileObject)
Entities = [] Entities = []
for i in range(EntityArrayLength): for i in range(EntityArrayLength):
Entities.append(struct.unpack('!i', FileObject.read(4))[0]) Entities.append(DataUtil.readInt(FileObject))
return Entities return Entities
def handle1E(FileObject): def handle1E(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
return EntityID return EntityID
def handle1F(FileObject): def handle1F(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
x = struct.unpack('!b', FileObject.read(1))[0] x = DataUtil.readByte(FileObject)
y = struct.unpack('!b', FileObject.read(1))[0] y = DataUtil.readByte(FileObject)
z = struct.unpack('!b', FileObject.read(1))[0] z = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -308,21 +298,21 @@ def handle1F(FileObject):
} }
def handle20(FileObject): def handle20(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
Yaw = struct.unpack('!b', FileObject.read(1))[0] Yaw = DataUtil.readByte(FileObject)
Pitch = struct.unpack('!b', FileObject.read(1))[0] Pitch = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'Yaw' : Yaw, 'Yaw' : Yaw,
'Pitch' : Pitch 'Pitch' : Pitch
} }
def handle21(FileObject): def handle21(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
x = struct.unpack('!b', FileObject.read(1))[0] x = DataUtil.readByte(FileObject)
y = struct.unpack('!b', FileObject.read(1))[0] y = DataUtil.readByte(FileObject)
z = struct.unpack('!b', FileObject.read(1))[0] z = DataUtil.readByte(FileObject)
Yaw = struct.unpack('!b', FileObject.read(1))[0] Yaw = DataUtil.readByte(FileObject)
Pitch = struct.unpack('!b', FileObject.read(1))[0] Pitch = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -332,12 +322,12 @@ def handle21(FileObject):
} }
def handle22(FileObject): def handle22(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
Yaw = struct.unpack('!b', FileObject.read(1))[0] Yaw = DataUtil.readByte(FileObject)
Pitch = struct.unpack('!b', FileObject.read(1))[0] Pitch = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -347,38 +337,38 @@ def handle22(FileObject):
} }
def handle23(FileObject): def handle23(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
HeadYaw = struct.unpack('!b', FileObject.read(1))[0] HeadYaw = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'HeadYaw' : HeadYaw 'HeadYaw' : HeadYaw
} }
def handle26(FileObject): def handle26(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
Status = struct.unpack('!b', FileObject.read(1))[0] Status = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'Status' : Status 'Status' : Status
} }
def handle27(FileObject): def handle27(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
VehicleID = struct.unpack('!i', FileObject.read(4))[0] VehicleID = DataUtil.readInt(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'VehicleID' : VehicleID 'VehicleID' : VehicleID
} }
def handle28(FileObject): def handle28(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
metadata = readEntityMetadata(FileObject) metadata = DataUtil.readEntityMetadata(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'MetaData' : metadata 'MetaData' : metadata
} }
def handle29(FileObject): def handle29(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
EffectID = struct.unpack('!b', FileObject.read(1))[0] EffectID = DataUtil.readByte(FileObject)
Amplifier = struct.unpack('!b', FileObject.read(1))[0] Amplifier = DataUtil.readByte(FileObject)
Duration = struct.unpack('!h', FileObject.read(2))[0] Duration = DataUtil.readShort(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'EffectID' : EffectID, 'EffectID' : EffectID,
'Amplifier' : Amplifier, 'Amplifier' : Amplifier,
@@ -386,38 +376,38 @@ def handle29(FileObject):
} }
def handle2A(FileObject): def handle2A(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
EffectID = struct.unpack('!b', FileObject.read(1))[0] EffectID = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'EffectID' : EffectID 'EffectID' : EffectID
} }
def handle2B(FileObject): def handle2B(FileObject):
ExperienceBar = struct.unpack('!f', FileObject.read(4))[0] ExperienceBar = DataUtil.readFloat(FileObject)
Level = struct.unpack('!h', FileObject.read(2))[0] Level = DataUtil.readShort(FileObject)
TotalExp = struct.unpack('!h', FileObject.read(2))[0] TotalExp = DataUtil.readShort(FileObject)
return {'ExpBar' : ExperienceBar, return {'ExpBar' : ExperienceBar,
'Level' : Level, 'Level' : Level,
'TotalExp' : TotalExp 'TotalExp' : TotalExp
} }
def handle33(FileObject): def handle33(FileObject):
X = struct.unpack('!i', FileObject.read(4))[0] X = DataUtil.readInt(FileObject)
Z = struct.unpack('!i', FileObject.read(4))[0] Z = DataUtil.readInt(FileObject)
GroundUpContinous = struct.unpack('?', FileObject.read(1))[0] GroundUpContinous = DataUtil.readBoolean(FileObject)
PrimaryBitMap = struct.unpack('!H', FileObject.read(2))[0] PrimaryBitMap = DataUtil.readShort(FileObject)
AddBitMap = struct.unpack('!H', FileObject.read(2))[0] AddBitMap = DataUtil.readShort(FileObject)
CompressedSize = struct.unpack('!i', FileObject.read(4))[0] CompressedSize = DataUtil.readInt(FileObject)
FileObject.read(CompressedSize) #not going to be deflating and using this data until I know how to :3 FileObject.read(CompressedSize) #not going to be inflating and using this data until I know how to :3
return {'x' : X, return {'x' : X,
'z' : Z 'z' : Z
} }
def handle34(FileObject): def handle34(FileObject):
ChunkX = struct.unpack('!i', FileObject.read(4))[0] ChunkX = DataUtil.readInt(FileObject)
ChunkZ = struct.unpack('!i', FileObject.read(4))[0] ChunkZ = DataUtil.readInt(FileObject)
AffectedBlocks = struct.unpack('!h', FileObject.read(2))[0] AffectedBlocks = DataUtil.readShort(FileObject)
DataSize = struct.unpack('!i', FileObject.read(4))[0] DataSize = DataUtil.readInt(FileObject)
FileObject.read(DataSize) #not going to be using this until I know how to. FileObject.read(DataSize) #not going to be using this until I know how to.
return {'ChunkX' : ChunkX, return {'ChunkX' : ChunkX,
'ChunkZ' : ChunkZ, 'ChunkZ' : ChunkZ,
@@ -425,11 +415,11 @@ def handle34(FileObject):
} }
def handle35(FileObject): def handle35(FileObject):
X = struct.unpack('!i', FileObject.read(4))[0] X = DataUtil.readInt(FileObject)
Y = struct.unpack('!b', FileObject.read(1))[0] Y = DataUtil.readByte(FileObject)
Z = struct.unpack('!i', FileObject.read(4))[0] Z = DataUtil.readInt(FileObject)
BlockType = struct.unpack('!h', FileObject.read(2))[0] BlockType = DataUtil.readShort(FileObject)
BlockMetaData = struct.unpack('!b', FileObject.read(1))[0] BlockMetaData = DataUtil.readByte(FileObject)
return {'x' : X, return {'x' : X,
'y' : Y, 'y' : Y,
'z' : Z, 'z' : Z,
@@ -438,12 +428,12 @@ def handle35(FileObject):
} }
def handle36(FileObject): def handle36(FileObject):
X = struct.unpack('!i', FileObject.read(4))[0] X = DataUtil.readInt(FileObject)
Y = struct.unpack('!h', FileObject.read(2))[0] Y = DataUtil.readShort(FileObject)
Z = struct.unpack('!i', FileObject.read(4))[0] Z = DataUtil.readInt(FileObject)
Byte1 = struct.unpack('!b', FileObject.read(1))[0] Byte1 = DataUtil.readByte(FileObject)
Byte2 = struct.unpack('!b', FileObject.read(1))[0] Byte2 = DataUtil.readByte(FileObject)
BlockID = struct.unpack('!h', FileObject.read(2))[0] BlockID = DataUtil.readShort(FileObject)
return {'x' : X, return {'x' : X,
'y' : Y, 'y' : Y,
'z' : Z, 'z' : Z,
@@ -454,19 +444,19 @@ def handle36(FileObject):
def handle37(FileObject): def handle37(FileObject):
#int - EntityID #int - EntityID
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
#int - X cord #int - X cord
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
#int - Y cord #int - Y cord
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
#int - Z cord #int - Z cord
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
#byte - Stage #byte - Stage
DestroyedStage = struct.unpack('!b', FileObject.read(1))[0] DestroyedStage = DataUtil.readByte(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -476,10 +466,10 @@ def handle37(FileObject):
def handle38(FileObject): def handle38(FileObject):
#short - number of chunks #short - number of chunks
ChunkCount = struct.unpack('!h', FileObject.read(2))[0] ChunkCount = DataUtil.readShort(FileObject)
#int - chunk data length #int - chunk data length
ChunkDataLength = struct.unpack('!i', FileObject.read(4))[0] ChunkDataLength = DataUtil.readInt(FileObject)
FileObject.read(ChunkDataLength) #just gonna ignore this for now FileObject.read(ChunkDataLength) #just gonna ignore this for now
#metadata - ignoring this #metadata - ignoring this
@@ -490,16 +480,16 @@ def handle38(FileObject):
} }
def handle3C(FileObject): def handle3C(FileObject):
X = struct.unpack('!d', FileObject.read(8))[0] X = DataUtil.readDouble(FileObject)
Y = struct.unpack('!d', FileObject.read(8))[0] Y = DataUtil.readDouble(FileObject)
Z = struct.unpack('!d', FileObject.read(8))[0] Z = DataUtil.readDouble(FileObject)
Radius = struct.unpack('!f', FileObject.read(4))[0] Radius = DataUtil.readFloat(FileObject)
RecordCount = struct.unpack('!i', FileObject.read(4))[0] RecordCount = DataUtil.readInt(FileObject)
AffectedBlocks = [] AffectedBlocks = []
for i in range((RecordCount * 3)): for i in range((RecordCount * 3)):
x = struct.unpack('!b', FileObject.read(1))[0] x = DataUtil.readByte(FileObject)
y = struct.unpack('!b', FileObject.read(1))[0] y = DataUtil.readByte(FileObject)
z = struct.unpack('!b', FileObject.read(1))[0] z = DataUtil.readByte(FileObject)
AffectedBlocks.append({'x' : x, 'y' : y, 'z' : z}) AffectedBlocks.append({'x' : x, 'y' : y, 'z' : z})
#---Unknown what these floats do #---Unknown what these floats do
FileObject.read(4) FileObject.read(4)
@@ -514,12 +504,12 @@ def handle3C(FileObject):
} }
def handle3D(FileObject): def handle3D(FileObject):
EffectID = struct.unpack('!i', FileObject.read(4))[0] EffectID = DataUtil.readInt(FileObject)
X = struct.unpack('!i', FileObject.read(4))[0] X = DataUtil.readInt(FileObject)
Y = struct.unpack('!b', FileObject.read(1))[0] Y = DataUtil.readByte(FileObject)
Z = struct.unpack('!i', FileObject.read(4))[0] Z = DataUtil.readInt(FileObject)
Data = struct.unpack('!i', FileObject.read(4))[0] Data = DataUtil.readInt(FileObject)
NoVolDecrease = struct.unpack('?', FileObject.read(1))[0] NoVolDecrease = DataUtil.readBoolean(FileObject)
return {'EffectID' : EffectID, return {'EffectID' : EffectID,
'X' : X, 'X' : X,
'Y' : Y, 'Y' : Y,
@@ -529,13 +519,12 @@ def handle3D(FileObject):
} }
def handle3E(FileObject): def handle3E(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 Sound = DataUtil.readString(FileObject)
Sound = FileObject.read(length).decode('utf-16be') x = DataUtil.readInt(FileObject)
x = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] Volume = DataUtil.readFloat(FileObject)
Volume = struct.unpack('!f', FileObject.read(4))[0] Pitch = DataUtil.readByte(FileObject)
Pitch = struct.unpack('!b', FileObject.read(1))[0]
return {'Sound' : Sound, return {'Sound' : Sound,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -545,18 +534,18 @@ def handle3E(FileObject):
} }
def handle46(FileObject): def handle46(FileObject):
Reason = struct.unpack('!b', FileObject.read(1))[0] Reason = DataUtil.readByte(FileObject)
GameMode = struct.unpack('!b', FileObject.read(1))[0] GameMode = DataUtil.readByte(FileObject)
return {'Reason' : Reason, return {'Reason' : Reason,
'GameMode' : GameMode 'GameMode' : GameMode
} }
def handle47(FileObject): def handle47(FileObject):
EntityID = struct.unpack('!i', FileObject.read(4))[0] EntityID = DataUtil.readInt(FileObject)
FileObject.read(1) #Boolean don't do nothing FileObject.read(1) #Boolean don't do nothing
x = struct.unpack('!i', FileObject.read(4))[0] x = DataUtil.readInt(FileObject)
y = struct.unpack('!i', FileObject.read(4))[0] y = DataUtil.readInt(FileObject)
z = struct.unpack('!i', FileObject.read(4))[0] z = DataUtil.readInt(FileObject)
return {'EntityID' : EntityID, return {'EntityID' : EntityID,
'x' : x, 'x' : x,
'y' : y, 'y' : y,
@@ -564,11 +553,10 @@ def handle47(FileObject):
} }
def handle64(FileObject): def handle64(FileObject):
WindowID = struct.unpack('!b', FileObject.read(1))[0] WindowID = DataUtil.readByte(FileObject)
InventoryType = struct.unpack('!b', FileObject.read(1))[0] InventoryType = DataUtil.readByte(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 WindowTitle = DataUtil.readString(FileObject)
WindowTitle = FileObject.read(length).decode("utf-16be") NumberOfSlots = DataUtil.readByte(FileObject)
NumberOfSlots = struct.unpack('!b', FileObject.read(1))[0]
return {'WindowID' : WindowID, return {'WindowID' : WindowID,
'InventoryType' : InventoryType, 'InventoryType' : InventoryType,
'WindowTitle' : WindowTitle, 'WindowTitle' : WindowTitle,
@@ -576,24 +564,24 @@ def handle64(FileObject):
} }
def handle65(FileObject): def handle65(FileObject):
WindowID = struct.unpack('!b', FileObject.read(1))[0] WindowID = DataUtil.readByte(FileObject)
return WindowID return WindowID
def handle67(FileObject): def handle67(FileObject):
WindowID = struct.unpack('!b', FileObject.read(1))[0] WindowID = DataUtil.readByte(FileObject)
Slot = struct.unpack('!h', FileObject.read(2))[0] Slot = DataUtil.readShort(FileObject)
SlotData = decodeSlotData(FileObject) SlotData = DataUtil.readSlotData(FileObject)
return {'WindowID' : WindowID, return {'WindowID' : WindowID,
'Slot' : Slot, 'Slot' : Slot,
'SlotData' : SlotData 'SlotData' : SlotData
} }
def handle68(FileObject): def handle68(FileObject):
WindowID = struct.unpack('!b', FileObject.read(1))[0] WindowID = DataUtil.readByte(FileObject)
Count = struct.unpack('!h', FileObject.read(2))[0] Count = DataUtil.readShort(FileObject)
Slots = [] Slots = []
for i in range(Count): for i in range(Count):
SlotData = decodeSlotData(FileObject) SlotData = DataUtil.readSlotData(FileObject)
Slots.append(SlotData) Slots.append(SlotData)
return {'WindowID' : WindowID, return {'WindowID' : WindowID,
'Count' : Count, 'Count' : Count,
@@ -601,42 +589,38 @@ def handle68(FileObject):
} }
def handle69(FileObject): def handle69(FileObject):
WindowID = struct.unpack('!b', FileObject.read(1))[0] WindowID = DataUtil.readByte(FileObject)
Property = struct.unpack('!h', FileObject.read(2))[0] Property = DataUtil.readShort(FileObject)
Value = struct.unpack('!h', FileObject.read(2))[0] Value = DataUtil.readShort(FileObject)
return {'WindowID' : WindowID, return {'WindowID' : WindowID,
'Property' : Property, 'Property' : Property,
'Value' : Value 'Value' : Value
} }
def handle6A(FileObject): def handle6A(FileObject):
WindowID = struct.unpack('!b', FileObject.read(1))[0] WindowID = DataUtil.readByte(FileObject)
ActionType = struct.unpack('!h', FileObject.read(2))[0] ActionType = DataUtil.readShort(FileObject)
Accepted = struct.unpack('?', FileObject.read(1))[0] Accepted = DataUtil.readBoolean(FileObject)
return {'WindowID' : WindowID, return {'WindowID' : WindowID,
'ActionType' : ActionType, 'ActionType' : ActionType,
'Accepted' : Accepted 'Accepted' : Accepted
} }
def handle6B(FileObject): def handle6B(FileObject):
Slot = struct.unpack('!h', FileObject.read(2))[0] Slot = DataUtil.readShort(FileObject)
ClickedItem = decodeSlotData(FileObject) ClickedItem = DataUtil.readSlotData(FileObject)
return {'Slot' : Slot, return {'Slot' : Slot,
'ClickedItem' : ClickedItem 'ClickedItem' : ClickedItem
} }
def handle82(FileObject): def handle82(FileObject):
X = struct.unpack('!i', FileObject.read(4))[0] X = DataUtil.readInt(FileObject)
Y = struct.unpack('!h', FileObject.read(2))[0] Y = DataUtil.readShort(FileObject)
Z = struct.unpack('!i', FileObject.read(4))[0] Z = DataUtil.readInt(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 Line1 = DataUtil.readString(FileObject)
Line1 = FileObject.read(length).decode("utf-16be") Line2 = DataUtil.readString(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2 Line3 = DataUtil.readString(FileObject)
Line2 = FileObject.read(length).decode("utf-16be") Line4 = DataUtil.readString(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] * 2
Line3 = FileObject.read(length).decode("utf-16be")
length = struct.unpack('!h', FileObject.read(2))[0] * 2
Line4 = FileObject.read(length).decode("utf-16be")
return {'x' : X, return {'x' : X,
'y' : Y, 'y' : Y,
'z' : Z, 'z' : Z,
@@ -647,23 +631,23 @@ def handle82(FileObject):
} }
def handle83(FileObject): def handle83(FileObject):
ItemType = struct.unpack('!h', FileObject.read(2))[0] ItemType = DataUtil.readShort(FileObject)
ItemID = struct.unpack('!h', FileObject.read(2))[0] ItemID = DataUtil.readShort(FileObject)
TextLength = struct.unpack('!B', FileObject.read(1))[0] TextLength = DataUtil.readByte(FileObject)
Text = struct.unpack(str(TextLength) + 'p', FileObject.read(TextLength)) Text = DataUtil.readByteArray(FileObject, TextLength)
return {'ItemType' : ItemType, return {'ItemType' : ItemType,
'ItemID' : ItemID, 'ItemID' : ItemID,
'Text' : Text 'Text' : Text
} }
def handle84(FileObject): def handle84(FileObject):
X = struct.unpack('!i', FileObject.read(4))[0] X = DataUtil.readInt(FileObject)
Y = struct.unpack('!h', FileObject.read(2))[0] Y = DataUtil.readShort(FileObject)
Z = struct.unpack('!i', FileObject.read(4))[0] Z = DataUtil.readInt(FileObject)
Action = struct.unpack('!b', FileObject.read(1))[0] Action = DataUtil.readByte(FileObject)
DataLength = struct.unpack('!h', FileObject.read(2))[0] DataLength = DataUtil.readShort(FileObject)
if (DataLength != -1): if (DataLength != -1):
NBTData = struct.unpack(str(DataLength) + "s", FileObject.read(DataLength))[0] NBTData = DataUtil.readByteArray(FileObject, DataLength)
return {'x' : X, return {'x' : X,
'y' : Y, 'y' : Y,
'z' : Z, 'z' : Z,
@@ -677,17 +661,16 @@ def handle84(FileObject):
} }
def handleC8(FileObject): def handleC8(FileObject):
StatID = struct.unpack('!i', FileObject.read(4))[0] StatID = DataUtil.readInt(FileObject)
Amount = struct.unpack('!b', FileObject.read(1))[0] Amount = DataUtil.readByte(FileObject)
return {'StatID' : StatID, return {'StatID' : StatID,
'Amount' : Amount 'Amount' : Amount
} }
def handleC9(FileObject): def handleC9(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 PlayerName = DataUtil.readString(FileObject)
PlayerName = FileObject.read(length).decode("utf-16be") Online = DataUtil.readBoolean(FileObject)
Online = struct.unpack('?', FileObject.read(1))[0] Ping = DataUtil.readShort(FileObject)
Ping = struct.unpack('!h', FileObject.read(2))[0]
return {'PlayerName' : PlayerName, return {'PlayerName' : PlayerName,
'Online' : Online, 'Online' : Online,
'Ping' : Ping 'Ping' : Ping
@@ -695,29 +678,26 @@ def handleC9(FileObject):
def handleCA(FileObject): def handleCA(FileObject):
#byte - flags #byte - flags
Flags = struct.unpack('!b', FileObject.read(1))[0] Flags = DataUtil.readByte(FileObject)
#byte - fly speed #byte - fly speed
FlySpeed = struct.unpack('!b', FileObject.read(1))[0] FlySpeed = DataUtil.readByte(FileObject)
#byte - walk speed #byte - walk speed
WalkSpeed = struct.unpack('!b', FileObject.read(1))[0] WalkSpeed = DataUtil.readByte(FileObject)
return {'Flags' : Flags, return {'Flags' : Flags,
'Fly Speed' : FlySpeed, 'Fly Speed' : FlySpeed,
'Walk Speed' : WalkSpeed 'Walk Speed' : WalkSpeed
} }
def handleCB(FileObject): def handleCB(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 text = DataUtil.readString(FileObject)
text = FileObject.read(length).decode("utf-16be")
return {'Text' : text} return {'Text' : text}
def handleFA(FileObject): def handleFA(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 Channel = DataUtil.readString(FileObject)
Channel = FileObject.read(length).decode("utf-16be") length = DataUtil.readShort(FileObject)
length = struct.unpack('!h', FileObject.read(2))[0] message = DataUtil.readByteArray(FileObject, length)
raw = FileObject.read(length)
message = struct.unpack(str(length) + 's', raw)
return {'Channel' : Channel, return {'Channel' : Channel,
'message' : message 'message' : message
} }
@@ -725,14 +705,14 @@ def handleFA(FileObject):
def handleFC(FileObject): def handleFC(FileObject):
#short - shared secret length #short - shared secret length
secretLength = struct.unpack('!h', FileObject.read(2))[0] secretLength = DataUtil.readShort(FileObject)
sharedSecret = struct.unpack(str(secretLength) + "s", FileObject.read(secretLength))[0] #ignore this data, it doesn't matter sharedSecret = DataUtil.readByteArray(FileObject, secretLength) #ignore this data, it doesn't matter
#short - token length #short - token length
length = struct.unpack('!h', FileObject.read(2))[0] length = DataUtil.readShort(FileObject)
token = struct.unpack(str(length) + "s", FileObject.read(length))[0] #ignore this data, it doesn't matter token = DataUtil.readByteArray(FileObject, length) #ignore this data, it doesn't matter
return {'Secret Length' : secretLength, return {'Secret Length' : secretLength,
'Shared Secret' : sharedSecret, 'Shared Secret' : sharedSecret,
@@ -744,20 +724,19 @@ def handleFC(FileObject):
def handleFD(FileObject): def handleFD(FileObject):
#string - server id #string - server id
length = struct.unpack('!h', FileObject.read(2))[0] * 2 serverid = DataUtil.readString(FileObject)
serverid = FileObject.read(length).decode("utf-16be")
#short - pub key length #short - pub key length
length = struct.unpack('!h', FileObject.read(2))[0] length = DataUtil.readShort(FileObject)
#byte array - pub key #byte array - pub key
pubkey = struct.unpack(str(length) + "s", FileObject.read(length))[0] pubkey = DataUtil.readByteArray(FileObject, length)
#short - token length #short - token length
length = struct.unpack('!h', FileObject.read(2))[0] length = DataUtil.readShort(FileObject)
#byte array - token #byte array - token
token = struct.unpack(str(length) + "s", FileObject.read(length))[0] token = DataUtil.readByteArray(FileObject, length)
return {'ServerID' : serverid, return {'ServerID' : serverid,
'Public Key' : pubkey, 'Public Key' : pubkey,
@@ -765,58 +744,5 @@ def handleFD(FileObject):
} }
def handleFF(FileObject): def handleFF(FileObject):
length = struct.unpack('!h', FileObject.read(2))[0] * 2 Reason = DataUtil.readString(FileObject)
Reason = FileObject.read(length)
Reason = Reason.decode("utf-16be", 'strict')
print Reason
return Reason return Reason
def readEntityMetadata(FileObject):
metadata = {}
byte = struct.unpack('!B', FileObject.read(1))[0]
while byte != 127:
index = byte & 0x1F # Lower 5 bits
ty = byte >> 5 # Upper 3 bits
if ty == 0: val = struct.unpack('!b', FileObject.read(1))[0]
if ty == 1: val = struct.unpack('!h', FileObject.read(2))[0]
if ty == 2: val = struct.unpack('!i', FileObject.read(4))[0]
if ty == 3: val = struct.unpack('!f', FileObject.read(4))[0]
if ty == 4:
length = struct.unpack('!h', FileObject.read(2))[0] * 2
val = FileObject.read(length).decode('utf-16be')
if ty == 5:
val = {}
val["id"] = struct.unpack('!h', FileObject.read(2))[0]
if (val["id"] != -1):
val["count"] = struct.unpack('!b', FileObject.read(1))[0]
val["damage"] = struct.unpack('!h', FileObject.read(2))[0]
if ty == 6:
val = []
for i in range(3):
val.append(struct.unpack('!i', FileObject.read(4))[0])
metadata[index] = (ty, val)
byte = struct.unpack('!B', FileObject.read(1))[0]
return metadata
def decodeSlotData(FileObject):
BlockID = struct.unpack('!h', FileObject.read(2))[0]
if(BlockID != -1):
ItemCount = struct.unpack('!b', FileObject.read(1))[0]
Damage = struct.unpack('!h', FileObject.read(2))[0]
MetadataLength = struct.unpack('!h', FileObject.read(2))[0]
if(MetadataLength != -1):
raw = FileObject.read(MetadataLength)
ByteArray = struct.unpack(str(MetadataLength) + "s", raw)[0]
Data = zlib.decompress(ByteArray, 15+32)
return {'BlockID' : BlockID,
'ItemCount' : ItemCount,
'Damage' : Damage,
'Data' : Data
}
return {'BlockID' : BlockID,
'ItemCount' : ItemCount,
'Damage' : Damage
}
return {'BlockID' : -1,
'ItemCount' : 0
}
+7
View File
@@ -1,6 +1,13 @@
import socket import socket
import struct import struct
def send00(socket, KAid):
#packet id
socket.send("\x00")
#int - keep alive id
socket.send(struct.pack('!i', KAid))
def sendHandshake(socket, username, host, port): def sendHandshake(socket, username, host, port):
#packet id #packet id
socket.send("\x02") socket.send("\x02")