srctools.binformat
The binformat module binformat
contains functionality for handling binary formats, esentially expanding on struct
’s functionality.
Constants
-
srctools.binformat.
ST_VEC
: struct.Struct = Struct('fff') A
struct.Struct
with three floats, for unpackingVec
,Angle
, etc.
-
srctools.binformat.
SIZES
: dict[str, struct.Struct] A dict mapping each fixed-size number format character (
i
,L
,h
, etc) to the size of the data.
-
srctools.binformat.
SIZE_CHAR
= 1
-
srctools.binformat.
SIZE_SHORT
= 2
-
srctools.binformat.
SIZE_INT
= 4
-
srctools.binformat.
SIZE_LONG
= 8
-
srctools.binformat.
SIZE_FLOAT
= 4
-
srctools.binformat.
SIZE_DOUBLE
= 8 The size of each of these standard numeric types.
Structure tools
-
class
srctools.binformat.
DeferredWrites
(file: IO[bytes]) Several formats require offsets or similar data to be written referring to data later in the file.
Doing this in one pass would be quite complicated, but this class assists in writing such files. Initially null bytes are written in the slots, then the data is filled in at the end.
To use this class, initialise it with the open and seekable file. Call
defer()
when reaching the relevant parts of the file, passing a format string for the structure and a hashable key used to identify it later. Once the value has been determined, callset_data()
to store the data. When the file is written out, callwrite()
which willseek()
back and fill in the values.-
defer
() None Mark that data of the specified format is going to be written here.
Parameters: - key – Any hashable object, used to identify this location later.
- fmt – The structure of the data, either as an existing
struct.Struct
instance, or a string in the same format. - write – If true, write null bytes to occupy the space in the file. Set to false if you are doing this yourself.
-
set_data
() None Specify the data for the given key.
Parameters: - key – Any hashable object, used to identify the location and format.
- data – The values passed to
struct.pack()
to build the data.
-
-
srctools.binformat.
struct_read
() tuple Read a structure from the file, automatically computing the required number of bytes.
-
srctools.binformat.
str_readvec
(file: IO[bytes]) Vec Shortcut to read a 3-float vector from a file.
-
srctools.binformat.
read_array
(fmt: str, data: bytes) List[int] Read a buffer containing a stream of integers.
- The format string should be one of the integer format characters, optionally prefixed by an
- endianness indicator. As many integers as possible will then be read from the data.
-
srctools.binformat.
write_array
(fmt: str, data: Collection[int]) bytes Build a packed array of integers.
The format string should be one of the integer format characters, optionally prefixed by an endianness indicator. The integers in the data will then be packed into a bytes buffer and returned.
-
srctools.binformat.
read_nullstr
() str Read a null-terminated string from the file.
If the position is
0
, this will instead immediately return an empty string. Otherwise if set this will first seek to the location.
-
srctools.binformat.
read_nullstr_array
() List[str] Read the specified number of consecutive null-terminated strings from a file.
If the count is zero, no reading will be performed at all.