<s> import struct <EOL> import nbt <EOL> import StringIO <EOL> data_types = { <EOL> "<STR_LIT>" : ( '<STR_LIT:B>' , <NUM_LIT:1> ) , <EOL> "<STR_LIT>" : ( '<STR_LIT:b>' , <NUM_LIT:1> ) , <EOL> "<STR_LIT:bool>" : ( '<STR_LIT:?>' , <NUM_LIT:1> ) , <EOL> "<STR_LIT>" : ( '<STR_LIT:h>' , <NUM_LIT:2> ) , <EOL> "<STR_LIT:float>" : ( '<STR_LIT:f>' , <NUM_LIT:4> ) , <EOL> "<STR_LIT:int>" : ( '<STR_LIT:i>' , <NUM_LIT:4> ) , <EOL> "<STR_LIT>" : ( '<STR_LIT:I>' , <NUM_LIT:4> ) , <EOL> "<STR_LIT>" : ( '<STR_LIT:d>' , <NUM_LIT:8> ) , <EOL> "<STR_LIT>" : ( '<STR_LIT:q>' , <NUM_LIT:8> ) <EOL> } <EOL> enchantable = [ <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> <EOL> ] <EOL> def pack ( data_type , data ) : <EOL> if data_type in data_types : <EOL> format = data_types [ data_type ] <EOL> return pack_real ( format [ <NUM_LIT:0> ] , data ) <EOL> if data_type == "<STR_LIT>" : return pack ( "<STR_LIT>" , len ( data ) ) + data <EOL> if data_type == "<STR_LIT>" : return pack ( "<STR_LIT>" , len ( data ) ) + data . encode ( '<STR_LIT>' ) <EOL> if data_type == "<STR_LIT>" : <EOL> o = pack ( '<STR_LIT>' , data [ '<STR_LIT:id>' ] ) <EOL> if data [ '<STR_LIT:id>' ] > <NUM_LIT:0> : <EOL> o += pack ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> o += pack ( '<STR_LIT>' , data [ '<STR_LIT>' ] ) <EOL> if data [ '<STR_LIT:id>' ] in enchantable : <EOL> if '<STR_LIT>' in data : <EOL> o += pack ( '<STR_LIT>' , len ( data [ '<STR_LIT>' ] ) ) <EOL> o += data [ '<STR_LIT>' ] <EOL> elif '<STR_LIT>' in data : <EOL> ench = nbt . encode ( data [ '<STR_LIT>' ] ) <EOL> ench = nbt . compress ( ench ) <EOL> o += pack ( '<STR_LIT>' , len ( ench ) ) <EOL> o += ench <EOL> else : <EOL> o += pack ( '<STR_LIT>' , - <NUM_LIT:1> ) <EOL> return o <EOL> if data_type == "<STR_LIT>" : <EOL> o = '<STR_LIT>' <EOL> for key , tmp in data : <EOL> ty , val = tmp <EOL> x = key | ( ty << <NUM_LIT:5> ) <EOL> o += pack ( '<STR_LIT>' , x ) <EOL> if ty == <NUM_LIT:0> : o += pack ( '<STR_LIT>' , val ) <EOL> if ty == <NUM_LIT:1> : o += pack ( '<STR_LIT>' , val ) <EOL> if ty == <NUM_LIT:2> : o += pack ( '<STR_LIT:int>' , val ) <EOL> if ty == <NUM_LIT:3> : o += pack ( '<STR_LIT:float>' , val ) <EOL> if ty == <NUM_LIT:4> : o += pack ( '<STR_LIT>' , val ) <EOL> if ty == <NUM_LIT:5> : <EOL> o += pack ( '<STR_LIT>' , val [ '<STR_LIT:id>' ] ) <EOL> o += pack ( '<STR_LIT>' , val [ '<STR_LIT:count>' ] ) <EOL> o += pack ( '<STR_LIT>' , val [ '<STR_LIT>' ] ) <EOL> if ty == <NUM_LIT:6> : <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> o += pack ( '<STR_LIT:int>' , val [ i ] ) <EOL> o += pack ( '<STR_LIT>' , <NUM_LIT> ) <EOL> return o <EOL> def unpack ( bbuff , data_type ) : <EOL> if data_type in data_types : <EOL> format = data_types [ data_type ] <EOL> return unpack_real ( bbuff , format [ <NUM_LIT:0> ] , format [ <NUM_LIT:1> ] ) <EOL> if data_type == "<STR_LIT>" : <EOL> length = unpack ( bbuff , '<STR_LIT>' ) <EOL> return bbuff . recv ( length ) <EOL> if data_type == "<STR_LIT>" : <EOL> length = unpack ( bbuff , '<STR_LIT>' ) <EOL> return bbuff . recv ( <NUM_LIT:2> * length ) . decode ( '<STR_LIT>' ) <EOL> if data_type == "<STR_LIT>" : <EOL> o = { } <EOL> o [ "<STR_LIT:id>" ] = unpack ( bbuff , '<STR_LIT>' ) <EOL> if o [ "<STR_LIT:id>" ] > <NUM_LIT:0> : <EOL> o [ "<STR_LIT>" ] = unpack ( bbuff , '<STR_LIT>' ) <EOL> o [ "<STR_LIT>" ] = unpack ( bbuff , '<STR_LIT>' ) <EOL> if o [ '<STR_LIT:id>' ] in enchantable : <EOL> length = unpack ( bbuff , '<STR_LIT>' ) <EOL> if length > <NUM_LIT:0> : <EOL> ench = bbuff . recv ( length ) <EOL> try : <EOL> ench = nbt . decompress ( ench ) <EOL> ench = nbt . decode ( StringIO . StringIO ( ench ) ) <EOL> o [ "<STR_LIT>" ] = ench <EOL> except : <EOL> o [ "<STR_LIT>" ] = ench <EOL> return o <EOL> if data_type == "<STR_LIT>" : <EOL> metadata = [ ] <EOL> x = unpack ( bbuff , '<STR_LIT>' ) <EOL> while x != <NUM_LIT> : <EOL> key = x & <NUM_LIT> <EOL> ty = x >> <NUM_LIT:5> <EOL> if ty == <NUM_LIT:0> : val = unpack ( bbuff , '<STR_LIT>' ) <EOL> if ty == <NUM_LIT:1> : val = unpack ( bbuff , '<STR_LIT>' ) <EOL> if ty == <NUM_LIT:2> : val = unpack ( bbuff , '<STR_LIT:int>' ) <EOL> if ty == <NUM_LIT:3> : val = unpack ( bbuff , '<STR_LIT:float>' ) <EOL> if ty == <NUM_LIT:4> : val = unpack ( bbuff , '<STR_LIT>' ) <EOL> if ty == <NUM_LIT:5> : <EOL> val = { } <EOL> val [ "<STR_LIT:id>" ] = unpack ( bbuff , '<STR_LIT>' ) <EOL> val [ "<STR_LIT:count>" ] = unpack ( bbuff , '<STR_LIT>' ) <EOL> val [ "<STR_LIT>" ] = unpack ( bbuff , '<STR_LIT>' ) <EOL> if ty == <NUM_LIT:6> : <EOL> val = [ ] <EOL> for i in range ( <NUM_LIT:3> ) : <EOL> val . append ( unpack ( bbuff , '<STR_LIT:int>' ) ) <EOL> metadata . append ( ( key , ( ty , val ) ) ) <EOL> x = unpack ( bbuff , '<STR_LIT>' ) <EOL> return metadata <EOL> def unpack_real ( bbuff , data_type , length ) : <EOL> return struct . unpack_from ( '<STR_LIT:!>' + data_type , bbuff . recv ( length ) ) [ <NUM_LIT:0> ] <EOL> def pack_real ( data_type , data ) : <EOL> return struct . pack ( '<STR_LIT:!>' + data_type , data ) <EOL> def unpack_array ( bbuff , data_type , count ) : <EOL> a = [ ] <EOL> for i in range ( count ) : <EOL> a . append ( unpack ( bbuff , data_type ) ) <EOL> return a <EOL> def pack_array ( data_type , data ) : <EOL> o = '<STR_LIT>' <EOL> for d in data : <EOL> o += pack ( data_type , d ) <EOL> return o <EOL> def unpack_array_fast ( bbuff , data_type , count ) : <EOL> data_type = data_types [ data_type ] <EOL> length = data_type [ <NUM_LIT:1> ] * count <EOL> format = data_type [ <NUM_LIT:0> ] * count <EOL> return struct . unpack_from ( format , bbuff . recv ( length ) ) <EOL> def pack_array_fast ( data_type , data ) : <EOL> data_type = data_types [ data_type ] <EOL> return struct . pack ( data_type [ <NUM_LIT:0> ] * len ( data ) , * data ) </s>
