Skip to content

Instantly share code, notes, and snippets.

@joselitosn
Created November 27, 2017 17:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save joselitosn/89189c6c6214dfdc6f51b4a7b56a2078 to your computer and use it in GitHub Desktop.
Save joselitosn/89189c6c6214dfdc6f51b4a7b56a2078 to your computer and use it in GitHub Desktop.
Library to retrieve information on GoldSource BSP file format files
#!/usr/bin/env python3
"""
BSPLoader
"""
__author__ = "Vinicios R Portella"
__version__ = "0.1.0"
__license__ = "BSD"
from enum import Enum, auto
from struct import unpack
from sys import argv
class LUMPS(Enum):
ENTITIES = 0
PLANES = 1
TEXTURES = 2
VERTEXES = 3
VISIBILITY = 4
NODES = 5
TEXINFO = 6
FACES = 7
LIGHTING = 8
CLIPNODES = 9
LEAFS = 10
MARKSURFACES = 11
EDGES = 12
SURFEDGES = 13
MODELS = 14
class GoldSrcBSP():
BSPVERSION = 30
LUMPS = ('ENTITIES', 'PLANES', 'TEXTURES', 'VERTEXES', 'VISIBILITY',
'NODES', 'TEXINFO', 'FACES', 'LIGHTING', 'CLIPNODES', 'LEAFS',
'MARKSURFACES', 'EDGES', 'SURFEDGES', 'MODELS')
def __init__(self, filename):
try:
with open(filename, 'rb') as file:
self.header = self.BSPHeader(file.read(4 + len(LUMPS) * 8))
if self.header.version != self.BSPVERSION:
raise Exception('Wrong BSP version. Expected {}, got {}.'.format(self.BSPVERSION, self.header.version))
file.seek(self.header.lumps[self.LUMPS.index('ENTITIES')][0])
self.entities = self.BSPEntities(file.read(self.header.lumps[self.LUMPS.index('ENTITIES')][1]))
finally:
pass
class BSPHeader():
def __init__(self, data):
self.version = unpack('=i', data[:4])[0]
self.lumps = tuple(unpack('=ii', data[idx:idx+8]) for idx in range(4, len(GoldSrcBSP.LUMPS)*8, 8))
class BSPEntities():
def __init__(self, data):
self.data = data
def __str__(self):
return self.data.decode('UTF-8')
def main(filename):
""" Main entry point of the app """
bsp = GoldSrcBSP(filename)
print('{}'.format(bsp.entities))
if __name__ == "__main__":
""" This is executed when run from the command line """
if len(argv) > 1:
main(argv[1])
else:
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment