Skip to content

Commit

Permalink
(#109) adding a _get_signature method to base.Bsp
Browse files Browse the repository at this point in the history
  • Loading branch information
snake-biscuits committed Jun 14, 2023
1 parent d29f0e0 commit 98726d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
15 changes: 13 additions & 2 deletions bsp_tool/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@

class Bsp:
"""Bsp base class"""
bsp_version: int | (int, int) = 0 # .bsp format version
associated_files: List[str] # files in the folder of loaded file with similar names
# TODO: include subfolder files (e.g. graphs/<mapname>.ain)
bsp_version: int | (int, int) = 0 # .bsp format version
branch: ModuleType # soft copy of "branch script"
bsp_file_size: int = 0 # size of .bsp in bytes
endianness: str = "little"
Expand All @@ -22,6 +22,7 @@ class Bsp:
# NOTE: header type is self.branch.LumpHeader
loading_errors: Dict[str, Exception]
# ^ {"LUMP.name": Error("details")}
signature: bytes = b"" # compiler signature; sometimes found between header & data

def __init__(self, branch: ModuleType, filename: str = "untitled.bsp", autoload: bool = True):
if not filename.lower().endswith(".bsp"):
Expand Down Expand Up @@ -53,18 +54,28 @@ def __repr__(self):
version = f"({self.file_magic.decode('ascii', 'ignore')} version {version_number})"
return f"<{self.__class__.__name__} '{self.filename}' {branch_script} {version}>"

def _get_signature(self, header_length: int):
"""check for a signature between header & data"""
# TODO: check for other conspicuous gaps between lumps (> 4 byte padding)
lumps_start = min([h.offset for h in self.headers.values() if h.length != 0])
if lumps_start > header_length:
self.file.seek(header_length)
self.signature = self.file.read(lumps_start - header_length)

def _header_generator(self, offset: int = 4) -> (str, Any):
"""iterator for reading headers from self.file"""
for LUMP in self.branch.LUMP:
self.file.seek(offset + struct.calcsize(self.branch.LumpHeader._format) * LUMP.value)
lump_header = self.branch.LumpHeader.from_stream(self.file)
self.headers[LUMP.name] = lump_header
yield (LUMP.name, lump_header)

def _preload(self):
"""parse .bsp data & prepare dynamic readers"""
raise NotImplementedError()

def lump_as_bytes(self, lump_name: str) -> bytes:
"""Converts the named (unversioned) lump back into bytes"""
"""convert the named lump back into bytes"""
# NOTE: LumpClasses are derived from branch, not lump data!
if not hasattr(self, lump_name):
return b"" # lump is empty / deleted
Expand Down
7 changes: 1 addition & 6 deletions bsp_tool/respawn.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,12 +198,7 @@ def is_related(f): return f.startswith(self.filename.partition(".")[0])
continue # or version has flag (e.g. (50, 1))
self._preload_lump(lump_name, lump_header)
# compiler signature
headers_end = 16 + (16 * 128)
lumps_start = min([h.offset for h in self.headers.values() if h.length != 0])
if lumps_start > headers_end:
self.file.seek(headers_end)
self.signature = self.file.read(lumps_start - headers_end)
# TODO: check for other conspicuous gaps
self._get_signature(16 + (16 * 128))

self.external = ExternalLumpManager(self)

Expand Down

0 comments on commit 98726d9

Please sign in to comment.