Skip to content

Commit 61de3db

Browse files
committed
nbt changes?
1 parent d2b4365 commit 61de3db

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

nbt.py

+38-9
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,35 @@
11

2-
from gzip import GzipFile
3-
from StringIO import StringIO
2+
#from gzip import GzipFile
3+
#from StringIO import StringIO
44
import struct
55

6+
from subprocess import Popen, PIPE
7+
from fcntl import fcntl, F_SETFL, F_GETFL
8+
from select import select
9+
from os import O_NONBLOCK, write
10+
11+
def cmd(args, text):
12+
proc = Popen(args, stdin=PIPE, stdout=PIPE)
13+
output = ''
14+
set_non_blocking = lambda fd: fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK)
15+
set_non_blocking(proc.stdin)
16+
set_non_blocking(proc.stdout)
17+
while proc.poll() is None:
18+
r,w,x = select([proc.stdout], [proc.stdin] if text else [], [], 0.1)
19+
if r:
20+
output += proc.stdout.read()
21+
if w:
22+
n = write(proc.stdin.fileno(), text)
23+
text = text[n:]
24+
if not text:
25+
proc.stdin.close()
26+
if proc.returncode:
27+
raise OSError(args, proc.returncode)
28+
r,w,x = select([proc.stdout], [], [], 0.1)
29+
if r:
30+
output += proc.stdout.read()
31+
return output
32+
633
class BadNBTData(Exception):
734
pass
835

@@ -28,16 +55,18 @@ def read(f, n):
2855

2956
def gunzip(s):
3057
"""Unzips a string compressed with gzip compression (level 9)"""
31-
return GzipFile(fileobj=StringIO(s), mode='r').read()
58+
#return GzipFile(fileobj=StringIO(s), mode='r').read()
59+
return cmd(['gunzip'], s)
3260

3361
def gzip(s):
3462
"""Zips a string with gzip compression (level 9)"""
35-
sio = StringIO()
36-
gz = GzipFile(fileobj=sio, mode='w')
37-
gz.write(s)
38-
gz.close()
39-
sio.seek(0)
40-
return sio.read()
63+
# sio = StringIO()
64+
# gz = GzipFile(fileobj=sio, mode='w')
65+
# gz.write(s)
66+
# gz.close()
67+
# sio.seek(0)
68+
# return sio.read()
69+
return cmd(['gzip'], s)
4170

4271

4372
class NBTCompound(dict):

packet_decoder.py

+10
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ def __str__(self):
3636
from_to = {CLIENT_TO_SERVER: "to server", SERVER_TO_CLIENT: "from server"}[self.direction]
3737
return "%s packet %s: %s" % (self.name(), from_to, repr(self.data))
3838

39+
def copy(self):
40+
p = Packet()
41+
p.direction = self.direction
42+
p.ident = self.ident
43+
p.process = self.process
44+
p.transmit = self.transmit
45+
p.data = self.data.copy()
46+
return p
47+
48+
3949
SLOT_EXTRA_DATA_IDS = [
4050
0x103, 0x105, 0x15A, 0x167,
4151
0x10C, 0x10D, 0x10E, 0x10F, 0x122,

0 commit comments

Comments
 (0)