Commit 8c167c02 authored by dylan grafmyre's avatar dylan grafmyre
Browse files

pyvmf bsp header parser

parent c16f9b67
import sys
import struct
lump_t = struct.Struct('iii4c')
class Lump:
size = lump_t.size
def __init__(self, bin_):
tup = lump_t.unpack(bin_)
self.fileofs, self.filelen, self.version = tup[:3]
self.fourcc = tup[3:]
@property
def fourcc_b(self):
return b''.join(self.fourcc)
def __repr__(self):
if any((self.fileofs, self.filelen, self.version, any(i != b'\x00' for i in self.fourcc[0]))):
return '<{s.__class__.__name__}{version}{ofs}{len}{fourcc}>'.format(
s=self,
version=('.v{}'.format(self.version) if self.version else ''),
ofs=(' ofs:{}'.format(self.fileofs) if self.fileofs else ''),
len=(' len:{}'.format(self.filelen) if self.filelen else ''),
fourcc=(' fourcc:{}'.format(self.fourcc_b) if any(i != b'\x00' for i in self.fourcc) else ''),
)
return '<{s.__class__.__name__} null>'.format(s=self)
@classmethod
def iter_unpack(cls, bins):
i = 0
bins_len = len(bins)
while i < bins_len:
yield cls(bins[0:lump_t.size])
i += lump_t.size
class Dheader:
# little-endian "VBSP" 0x50534256
# define IDBSPHEADER (('P'<<24)+('S'<<16)+('B'<<8)+'V')
VBSP = ((ord('P')<<24)+(ord('S')<<16)+(ord('B')<<8)+ord('V'))
def __init__(self, file_):
self.ident, self.version = struct.unpack('ii', file_.read(8))
if self.ident != self.VBSP:
raise TypeError('file is missing VBSP identifier')
self.lumps = list(Lump.iter_unpack(file_.read(Lump.size * 64)))
self.map_revision, = struct.unpack('i', file_.read(4))
def __repr__(self):
return '<{s.__class__.__name__} VBSP lumps:64 rev:{s.map_revision}>'.format(s=self)
with open(sys.argv[1], 'rb') as fh:
bsp = Dheader(fh)
print(bsp)
#print(bsp.lumps)
......@@ -6,7 +6,7 @@ import io
import pyvmf.objects
import pyvmf.objects as o
import pyvmf.re as r
import pyvmf.re_ as r
import pyvmf.exceptions as e
CFG_TOKENS = {
......
......@@ -6,7 +6,7 @@ import io
import pyvmf.objects
import pyvmf.objects as o
import pyvmf.re as r
import pyvmf.re_ as r
import pyvmf.exceptions as e
VMF_TOKENS = {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment