srctools.vpk
Classes for reading and writing Valve’s VPK format, version 1.
- class srctools.vpk.OpenModes
Bases:
EnumModes for opening VPK files.
- READ = 'r'
- WRITE = 'w'
- APPEND = 'a'
- writable
Check if this mode allows modifying the VPK.
- srctools.vpk.get_arch_filename( ) str
Generate the name for a VPK file.
Prefix is the name of the file, usually ‘pak01’. index is the index of the data file, or None for the directory.
- class srctools.vpk.FileInfo(
- vpk: VPK,
- dir: str,
- filename: str,
- ext: str,
- crc: int,
- arch_index: int | None,
- offset: int,
- arch_len: int,
- start_data: bytes,
Represents a file stored inside a VPK.
Do not call the constructor, it is only meant for VPK’s use. Attributes should not be assigned to - use
write()to change the contents.- write( ) None
Replace this file with the given byte data.
arch_index is the pak_01_000 file to put data into (or None for _dir). This is ignored if the VPK is singular. Data written to the directory file is not immediately saved, py:meth:VPK.write_dirfile() must subsequently be called to do so. - If this file already exists in the VPK, the old data is not removed from numeric files.
For this reason VPK writes should be done once per file if possible.
- class srctools.vpk.VPK(
- dir_file: str | PathLike[str],
- *,
- mode: OpenModes | str = 'r',
- dir_data_limit: int | None = 1024,
- version: int = 1,
Represents a VPK archive.
VPKs can either be a singular file, or a main
archive_dir.vpkwith associated numerixarchive_XXX.vpkfiles containing the data.- mode: OpenModes
How the file was opened.
Read mode, the file will not be modified and it must already exist.
Write mode will create the directory if needed.
Append mode will also create the directory, but not wipe the file.
- dir_limit: int | None
The maximum amount of data for files saved to the dir file.
None: No limit.0: Save all to a data file.
The block of data after the header, which contains the file data for files stored in the
_dirfile, not numeric files.
- property file_prefix: str
The VPK filename, without
.vpk, or_dir.vpkif a directory.- Deprecated:
Do not assign to this, it assumes the VPK is always a directory file. Instead, assign to filename or path.
- property is_directory: bool
If true, this is a
_dir.vpkfile, potentially with data stored in auxilliary VPKS.
- load_dirfile() None
Read in the directory file to get all filenames. This erases all changes made to the object.
- write_dirfile() None
Write the directory file with the changes. This must be performed after writing to the VPK.
- for ... in filenames( ) Iterator[str]
Yield filenames from this VPK.
If an extension or folder is specified, only files with this extension or in this folder are returned.
- for ... in folders( ) Iterator[str]
Yield the names of folders present in this VPK.
If an extension is specified, only folders containing files with that extension are returned.
- for ... in fileinfos( ) Iterator[FileInfo]
Yield file info objects from this VPK.
If an extension or folder is specified, only files with this extension or in this folder are returned.
- new_file( ) FileInfo
Create the given file, making it empty by default.
If root is set, files are treated as relative to there, otherwise the filename must be relative.
FileExistsError will be raised if the file is already present.
- add_file(
- filename: str | tuple[str, str] | tuple[str, str, str],
- data: bytes,
- root: str = '',
- arch_index: int | None = 0,
Add the given data to the VPK.
If root is set, files are treated as relative to there, otherwise the filename must be relative. arch_index is the pak01_xxx file to copy this to, if the length is larger than self.dir_limit. If None it’s written to the _dir file.
FileExistsError will be raised if the file is already present.