-
Notifications
You must be signed in to change notification settings - Fork 0
/
nbt.py
720 lines (587 loc) · 21.6 KB
/
nbt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
"""
Handle the NBT (Named Binary Tag) data format
"""
from struct import Struct, error as StructError
from gzip import GzipFile
from collections import MutableMapping, MutableSequence, Sequence
import sys
_PY3 = sys.version_info >= (3,)
if _PY3:
unicode = str
basestring = str
else:
range = xrange
TAG_END = 0
TAG_BYTE = 1
TAG_SHORT = 2
TAG_INT = 3
TAG_LONG = 4
TAG_FLOAT = 5
TAG_DOUBLE = 6
TAG_BYTE_ARRAY = 7
TAG_STRING = 8
TAG_LIST = 9
TAG_COMPOUND = 10
TAG_INT_ARRAY = 11
TAG_LONG_ARRAY = 12
class MalformedFileError(Exception):
"""Exception raised on parse error."""
pass
class TAG(object):
"""TAG, a variable with an intrinsic name."""
id = None
def __init__(self, value=None, name=None):
self.name = name
self.value = value
# Parsers and Generators
def _parse_buffer(self, buffer):
raise NotImplementedError(self.__class__.__name__)
def _render_buffer(self, buffer):
raise NotImplementedError(self.__class__.__name__)
# Printing and Formatting of tree
def tag_info(self):
"""Return Unicode string with class, name and unnested value."""
return self.__class__.__name__ + (
'(%r)' % self.name if self.name
else "") + ": " + self.valuestr()
def valuestr(self):
"""Return Unicode string of unnested value. For iterators, this
returns a summary."""
return unicode(self.value)
def pretty_tree(self, indent=0):
"""Return formated Unicode string of self, where iterable items are
recursively listed in detail."""
return ("\t" * indent) + self.tag_info()
# Python 2 compatibility; Python 3 uses __str__ instead.
def __unicode__(self):
"""Return a unicode string with the result in human readable format.
Unlike valuestr(), the result is recursive for iterators till at least
one level deep."""
return unicode(self.value)
def __str__(self):
"""Return a string (ascii formated for Python 2, unicode for Python 3)
with the result in human readable format. Unlike valuestr(), the result
is recursive for iterators till at least one level deep."""
return str(self.value)
# Unlike regular iterators, __repr__() is not recursive.
# Use pretty_tree for recursive results.
# iterators should use __repr__ or tag_info for each item, like
# regular iterators
def __repr__(self):
"""Return a string (ascii formated for Python 2, unicode for Python 3)
describing the class, name and id for debugging purposes."""
return "<%s(%r) at 0x%x>" % (
self.__class__.__name__, self.name, id(self))
class _TAG_Numeric(TAG):
"""_TAG_Numeric, comparable to int with an intrinsic name"""
def __init__(self, value=None, name=None, buffer=None):
super(_TAG_Numeric, self).__init__(value, name)
if buffer:
self._parse_buffer(buffer)
# Parsers and Generators
def _parse_buffer(self, buffer):
# Note: buffer.read() may raise an IOError, for example if buffer is a
# corrupt gzip.GzipFile
self.value = self.fmt.unpack(buffer.read(self.fmt.size))[0]
def _render_buffer(self, buffer):
buffer.write(self.fmt.pack(self.value))
class _TAG_End(TAG):
id = TAG_END
fmt = Struct(">b")
def _parse_buffer(self, buffer):
# Note: buffer.read() may raise an IOError, for example if buffer is a
# corrupt gzip.GzipFile
value = self.fmt.unpack(buffer.read(1))[0]
if value != 0:
raise ValueError(
"A Tag End must be rendered as '0', not as '%d'." % value)
def _render_buffer(self, buffer):
buffer.write(b'\x00')
# == Value Tags ==#
class TAG_Byte(_TAG_Numeric):
"""Represent a single tag storing 1 byte."""
id = TAG_BYTE
fmt = Struct(">b")
class TAG_Short(_TAG_Numeric):
"""Represent a single tag storing 1 short."""
id = TAG_SHORT
fmt = Struct(">h")
class TAG_Int(_TAG_Numeric):
"""Represent a single tag storing 1 int."""
id = TAG_INT
fmt = Struct(">i")
"""Struct(">i"), 32-bits integer, big-endian"""
class TAG_Long(_TAG_Numeric):
"""Represent a single tag storing 1 long."""
id = TAG_LONG
fmt = Struct(">q")
class TAG_Float(_TAG_Numeric):
"""Represent a single tag storing 1 IEEE-754 floating point number."""
id = TAG_FLOAT
fmt = Struct(">f")
class TAG_Double(_TAG_Numeric):
"""Represent a single tag storing 1 IEEE-754 double precision floating
point number."""
id = TAG_DOUBLE
fmt = Struct(">d")
class TAG_Byte_Array(TAG, MutableSequence):
"""
TAG_Byte_Array, comparable to a collections.UserList with
an intrinsic name whose values must be bytes
"""
id = TAG_BYTE_ARRAY
def __init__(self, name=None, buffer=None):
# TODO: add a value parameter as well
super(TAG_Byte_Array, self).__init__(name=name)
if buffer:
self._parse_buffer(buffer)
# Parsers and Generators
def _parse_buffer(self, buffer):
length = TAG_Int(buffer=buffer)
self.value = bytearray(buffer.read(length.value))
def _render_buffer(self, buffer):
length = TAG_Int(len(self.value))
length._render_buffer(buffer)
buffer.write(bytes(self.value))
# Mixin methods
def __len__(self):
return len(self.value)
def __iter__(self):
return iter(self.value)
def __contains__(self, item):
return item in self.value
def __getitem__(self, key):
return self.value[key]
def __setitem__(self, key, value):
# TODO: check type of value
self.value[key] = value
def __delitem__(self, key):
del (self.value[key])
def insert(self, key, value):
# TODO: check type of value, or is this done by self.value already?
self.value.insert(key, value)
# Printing and Formatting of tree
def valuestr(self):
return "[%i byte(s)]" % len(self.value)
def __unicode__(self):
return '[' + ",".join([unicode(x) for x in self.value]) + ']'
def __str__(self):
return '[' + ",".join([str(x) for x in self.value]) + ']'
class TAG_Int_Array(TAG, MutableSequence):
"""
TAG_Int_Array, comparable to a collections.UserList with
an intrinsic name whose values must be integers
"""
id = TAG_INT_ARRAY
def __init__(self, name=None, buffer=None):
# TODO: add a value parameter as well
super(TAG_Int_Array, self).__init__(name=name)
if buffer:
self._parse_buffer(buffer)
def update_fmt(self, length):
""" Adjust struct format description to length given """
self.fmt = Struct(">" + str(length) + "i")
# Parsers and Generators
def _parse_buffer(self, buffer):
length = TAG_Int(buffer=buffer).value
self.update_fmt(length)
self.value = list(self.fmt.unpack(buffer.read(self.fmt.size)))
def _render_buffer(self, buffer):
length = len(self.value)
self.update_fmt(length)
TAG_Int(length)._render_buffer(buffer)
buffer.write(self.fmt.pack(*self.value))
# Mixin methods
def __len__(self):
return len(self.value)
def __iter__(self):
return iter(self.value)
def __contains__(self, item):
return item in self.value
def __getitem__(self, key):
return self.value[key]
def __setitem__(self, key, value):
self.value[key] = value
def __delitem__(self, key):
del (self.value[key])
def insert(self, key, value):
self.value.insert(key, value)
# Printing and Formatting of tree
def valuestr(self):
return "[%i int(s)]" % len(self.value)
class TAG_Long_Array(TAG, MutableSequence):
"""
TAG_Long_Array, comparable to a collections.UserList with
an intrinsic name whose values must be integers
"""
id = TAG_LONG_ARRAY
def __init__(self, name=None, buffer=None):
super(TAG_Long_Array, self).__init__(name=name)
if buffer:
self._parse_buffer(buffer)
def update_fmt(self, length):
""" Adjust struct format description to length given """
self.fmt = Struct(">" + str(length) + "q")
# Parsers and Generators
def _parse_buffer(self, buffer):
length = TAG_Int(buffer=buffer).value
self.update_fmt(length)
self.value = list(self.fmt.unpack(buffer.read(self.fmt.size)))
def _render_buffer(self, buffer):
length = len(self.value)
self.update_fmt(length)
TAG_Int(length)._render_buffer(buffer)
buffer.write(self.fmt.pack(*self.value))
# Mixin methods
def __len__(self):
return len(self.value)
def __iter__(self):
return iter(self.value)
def __contains__(self, item):
return item in self.value
def __getitem__(self, key):
return self.value[key]
def __setitem__(self, key, value):
self.value[key] = value
def __delitem__(self, key):
del (self.value[key])
def insert(self, key, value):
self.value.insert(key, value)
# Printing and Formatting of tree
def valuestr(self):
return "[%i long(s)]" % len(self.value)
class TAG_String(TAG, Sequence):
"""
TAG_String, comparable to a collections.UserString with an
intrinsic name
"""
id = TAG_STRING
def __init__(self, value=None, name=None, buffer=None):
super(TAG_String, self).__init__(value, name)
if buffer:
self._parse_buffer(buffer)
# Parsers and Generators
def _parse_buffer(self, buffer):
length = TAG_Short(buffer=buffer)
read = buffer.read(length.value)
if len(read) != length.value:
raise StructError()
self.value = read.decode("utf-8")
def _render_buffer(self, buffer):
save_val = self.value.encode("utf-8")
length = TAG_Short(len(save_val))
length._render_buffer(buffer)
buffer.write(save_val)
# Mixin methods
def __len__(self):
return len(self.value)
def __iter__(self):
return iter(self.value)
def __contains__(self, item):
return item in self.value
def __getitem__(self, key):
return self.value[key]
# Printing and Formatting of tree
def __repr__(self):
return self.value
# == Collection Tags ==#
class TAG_List(TAG, MutableSequence):
"""
TAG_List, comparable to a collections.UserList with an intrinsic name
"""
id = TAG_LIST
def __init__(self, type=None, value=None, name=None, buffer=None):
super(TAG_List, self).__init__(value, name)
if type:
self.tagID = type.id
else:
self.tagID = None
self.tags = []
if buffer:
self._parse_buffer(buffer)
# if self.tagID == None:
# raise ValueError("No type specified for list: %s" % (name))
# Parsers and Generators
def _parse_buffer(self, buffer):
self.tagID = TAG_Byte(buffer=buffer).value
self.tags = []
length = TAG_Int(buffer=buffer)
for x in range(length.value):
self.tags.append(TAGLIST[self.tagID](buffer=buffer))
def _render_buffer(self, buffer):
TAG_Byte(self.tagID)._render_buffer(buffer)
length = TAG_Int(len(self.tags))
length._render_buffer(buffer)
for i, tag in enumerate(self.tags):
if tag.id != self.tagID:
raise ValueError(
"List element %d(%s) has type %d != container type %d" %
(i, tag, tag.id, self.tagID))
tag._render_buffer(buffer)
# Mixin methods
def __len__(self):
return len(self.tags)
def __iter__(self):
return iter(self.tags)
def __contains__(self, item):
return item in self.tags
def __getitem__(self, key):
return self.tags[key]
def __setitem__(self, key, value):
self.tags[key] = value
def __delitem__(self, key):
del (self.tags[key])
def insert(self, key, value):
self.tags.insert(key, value)
# Printing and Formatting of tree
def __repr__(self):
return "%i entries of type %s" % (
len(self.tags), TAGLIST[self.tagID].__name__)
# Printing and Formatting of tree
def valuestr(self):
return "[%i %s(s)]" % (len(self.tags), TAGLIST[self.tagID].__name__)
def __unicode__(self):
return "[" + ", ".join([tag.tag_info() for tag in self.tags]) + "]"
def __str__(self):
return "[" + ", ".join([tag.tag_info() for tag in self.tags]) + "]"
def pretty_tree(self, indent=0):
output = [super(TAG_List, self).pretty_tree(indent)]
if len(self.tags):
output.append(("\t" * indent) + "{")
output.extend([tag.pretty_tree(indent + 1) for tag in self.tags])
output.append(("\t" * indent) + "}")
return '\n'.join(output)
class TAG_Compound(TAG, MutableMapping):
"""
TAG_Compound, comparable to a collections.OrderedDict with an
intrinsic name
"""
id = TAG_COMPOUND
def __init__(self, buffer=None, name=None):
# TODO: add a value parameter as well
super(TAG_Compound, self).__init__()
self.tags = []
self.name = ""
if buffer:
self._parse_buffer(buffer)
# Parsers and Generators
def _parse_buffer(self, buffer):
while True:
type = TAG_Byte(buffer=buffer)
if type.value == TAG_END:
# print("found tag_end")
break
else:
name = TAG_String(buffer=buffer).value
try:
tag = TAGLIST[type.value]()
except KeyError:
raise ValueError("Unrecognised tag type %d" % type.value)
tag.name = name
self.tags.append(tag)
tag._parse_buffer(buffer)
def _render_buffer(self, buffer):
for tag in self.tags:
TAG_Byte(tag.id)._render_buffer(buffer)
TAG_String(tag.name)._render_buffer(buffer)
tag._render_buffer(buffer)
buffer.write(b'\x00') # write TAG_END
# Mixin methods
def __len__(self):
return len(self.tags)
def __iter__(self):
for key in self.tags:
yield key.name
def __contains__(self, key):
if isinstance(key, int):
return key <= len(self.tags)
elif isinstance(key, basestring):
for tag in self.tags:
if tag.name == key:
return True
return False
elif isinstance(key, TAG):
return key in self.tags
return False
def __getitem__(self, key):
if isinstance(key, int):
return self.tags[key]
elif isinstance(key, basestring):
for tag in self.tags:
if tag.name == key:
return tag
else:
raise KeyError("Tag %s does not exist" % key)
else:
raise TypeError(
"key needs to be either name of tag, or index of tag, "
"not a %s" % type(key).__name__)
def __setitem__(self, key, value):
assert isinstance(value, TAG), "value must be an nbt.TAG"
if isinstance(key, int):
# Just try it. The proper error will be raised if it doesn't work.
self.tags[key] = value
elif isinstance(key, basestring):
value.name = key
for i, tag in enumerate(self.tags):
if tag.name == key:
self.tags[i] = value
return
self.tags.append(value)
def __delitem__(self, key):
if isinstance(key, int):
del (self.tags[key])
elif isinstance(key, basestring):
self.tags.remove(self.__getitem__(key))
else:
raise ValueError(
"key needs to be either name of tag, or index of tag")
def keys(self):
return [tag.name for tag in self.tags]
def iteritems(self):
for tag in self.tags:
yield (tag.name, tag)
# Printing and Formatting of tree
def __unicode__(self):
return "{" + ", ".join([tag.tag_info() for tag in self.tags]) + "}"
def __str__(self):
return "{" + ", ".join([tag.tag_info() for tag in self.tags]) + "}"
def valuestr(self):
return '{%i Entries}' % len(self.tags)
def pretty_tree(self, indent=0):
output = [super(TAG_Compound, self).pretty_tree(indent)]
if len(self.tags):
output.append(("\t" * indent) + "{")
output.extend([tag.pretty_tree(indent + 1) for tag in self.tags])
output.append(("\t" * indent) + "}")
return '\n'.join(output)
TAGLIST = {TAG_END: _TAG_End, TAG_BYTE: TAG_Byte, TAG_SHORT: TAG_Short,
TAG_INT: TAG_Int, TAG_LONG: TAG_Long, TAG_FLOAT: TAG_Float,
TAG_DOUBLE: TAG_Double, TAG_BYTE_ARRAY: TAG_Byte_Array,
TAG_STRING: TAG_String, TAG_LIST: TAG_List,
TAG_COMPOUND: TAG_Compound, TAG_INT_ARRAY: TAG_Int_Array,
TAG_LONG_ARRAY: TAG_Long_Array}
class NBTFile(TAG_Compound):
"""Represent an NBT file object."""
def __init__(self, filename=None, buffer=None, fileobj=None):
"""
Create a new NBTFile object.
Specify either a filename, file object or data buffer.
If filename of file object is specified, data should be GZip-compressed.
If a data buffer is specified, it is assumed to be uncompressed.
If filename is specified, the file is closed after reading and writing.
If file object is specified, the caller is responsible for closing the
file.
"""
super(NBTFile, self).__init__()
self.filename = filename
self.type = TAG_Byte(self.id)
closefile = True
# make a file object
if filename:
self.filename = filename
self.file = GzipFile(filename, 'rb')
elif buffer:
if hasattr(buffer, 'name'):
self.filename = buffer.name
self.file = buffer
closefile = False
elif fileobj:
if hasattr(fileobj, 'name'):
self.filename = fileobj.name
self.file = GzipFile(fileobj=fileobj)
else:
self.file = None
closefile = False
# parse the file given initially
if self.file:
self.parse_file()
if closefile:
# Note: GzipFile().close() does NOT close the fileobj,
# So we are still responsible for closing that.
try:
self.file.close()
except (AttributeError, IOError):
pass
self.file = None
def parse_file(self, filename=None, buffer=None, fileobj=None):
"""Completely parse a file, extracting all tags."""
if filename:
self.file = GzipFile(filename, 'rb')
elif buffer:
if hasattr(buffer, 'name'):
self.filename = buffer.name
self.file = buffer
elif fileobj:
if hasattr(fileobj, 'name'):
self.filename = fileobj.name
self.file = GzipFile(fileobj=fileobj)
if self.file:
try:
type = TAG_Byte(buffer=self.file)
if type.value == self.id:
name = TAG_String(buffer=self.file).value
self._parse_buffer(self.file)
self.name = name
self.file.close()
else:
raise MalformedFileError(
"First record is not a Compound Tag")
except StructError as e:
raise MalformedFileError(
"Partial File Parse: file possibly truncated.")
else:
raise ValueError(
"NBTFile.parse_file(): Need to specify either a "
"filename or a file object"
)
def write_file(self, filename=None, buffer=None, fileobj=None):
"""Write this NBT file to a file."""
closefile = True
if buffer:
self.filename = None
self.file = buffer
closefile = False
elif filename:
self.filename = filename
self.file = GzipFile(filename, "wb")
elif fileobj:
self.filename = None
self.file = GzipFile(fileobj=fileobj, mode="wb")
elif self.filename:
self.file = GzipFile(self.filename, "wb")
elif not self.file:
raise ValueError(
"NBTFile.write_file(): Need to specify either a "
"filename or a file object"
)
# Render tree to file
TAG_Byte(self.id)._render_buffer(self.file)
TAG_String(self.name)._render_buffer(self.file)
self._render_buffer(self.file)
# make sure the file is complete
try:
self.file.flush()
except (AttributeError, IOError):
pass
if closefile:
try:
self.file.close()
except (AttributeError, IOError):
pass
def __repr__(self):
"""
Return a string (ascii formated for Python 2, unicode
for Python 3) describing the class, name and id for
debugging purposes.
"""
if self.filename:
return "<%s(%r) with %s(%r) at 0x%x>" % (
self.__class__.__name__, self.filename,
TAG_Compound.__name__, self.name, id(self)
)
else:
return "<%s with %s(%r) at 0x%x>" % (
self.__class__.__name__, TAG_Compound.__name__,
self.name, id(self)
)