SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
vpkpp::VPK Class Reference

#include <VPK.h>

Inheritance diagram for vpkpp::VPK:
Collaboration diagram for vpkpp::VPK:

Classes

struct  Footer2
struct  FreedChunk
struct  Header1
struct  Header2
struct  MD5Entry

Public Member Functions

constexpr std::string_view getGUID () const override
 Get the GUID corresponding to the pack file type.
constexpr bool hasEntryChecksums () const override
 Returns true if the format has a checksum for each entry.
std::vector< std::string > verifyEntryChecksums () const override
 Verify the checksums of each file, if a file fails the check its path will be added to the vector If there is no checksum ability in the format, it will return an empty vector.
bool hasPackFileChecksum () const override
 Returns true if the entire file has a checksum.
bool verifyPackFileChecksum () const override
 Verify the checksum of the entire file, returns true on success Will return true if there is no checksum ability in the format.
bool hasPackFileSignature () const override
 Returns true if the file is signed.
bool verifyPackFileSignature () const override
 Verify the file signature, returns true on success Will return true if there is no signature ability in the format.
std::optional< std::vector< std::byte > > readEntry (const std::string &path_) const override
 Try to read the entry's data to a bytebuffer.
bool removeEntry (const std::string &filename_) override
 Remove an entry.
std::size_t removeDirectory (const std::string &dirName_) override
 Remove a directory.
bool bake (const std::string &outputDir_, BakeOptions options, const EntryCallback &callback) override
 If output folder is an empty string, it will overwrite the original.
std::string getTruncatedFilestem () const override
 /home/user/pak01_dir.vpk -> pak01
Attribute getSupportedEntryAttributes () const override
 Returns a list of supported entry attributes Mostly for GUI programs that show entries and their metadata in a table ;)
 operator std::string () const override
bool sign (const std::string &filename_)
 Sign the VPK with the given private key KeyValues file. (See below comment)
bool sign (const std::vector< std::byte > &privateKey, const std::vector< std::byte > &publicKey)
 Sign the VPK with the given keypair. Run this after VPK::bake if modifying the VPK file contents.
uint32_t getVersion () const
 Returns 1 for v1, 2 for v2.
void setVersion (uint32_t version)
 Change the version of the VPK. Valid values are 1 and 2.
uint32_t getChunkSize () const
 Get the VPK chunk size in bytes (size of generated archives when baking)
void setChunkSize (uint32_t newChunkSize)
 Set the VPK chunk size in bytes (size of generated archives when baking)
Public Member Functions inherited from vpkpp::PackFile
 PackFile (const PackFile &other)=delete
PackFileoperator= (const PackFile &other)=delete
 PackFile (PackFile &&other) noexcept=default
PackFileoperator= (PackFile &&other) noexcept=default
virtual ~PackFile ()=default
template<typename T>
requires requires (const T&) {{T::GUID} -> std::convertible_to<std::string_view>;}
bool isInstanceOf () const
 Check if the pack file is an instance of the given pack file class.
virtual constexpr bool isCaseSensitive () const
 Does the format support case-sensitive file names?
bool hasEntry (const std::string &path, bool includeUnbaked=true) const
 Check if an entry exists given the file path.
std::optional< EntryfindEntry (const std::string &path_, bool includeUnbaked=true) const
 Try to find an entry given the file path.
std::optional< std::vector< std::byte > > operator[] (const std::string &path_) const
std::optional< std::string > readEntryText (const std::string &path) const
 Try to read the entry's data to a string.
virtual constexpr bool isReadOnly () const noexcept
void addEntry (const std::string &entryPath, const std::string &filepath, EntryOptions options={})
 Add a new entry from a file path - the first parameter is the path in the PackFile, the second is the path on disk.
void addEntry (const std::string &path, std::vector< std::byte > &&buffer, EntryOptions options={})
 Add a new entry from a buffer.
void addEntry (const std::string &path, std::span< const std::byte > buffer, EntryOptions options={})
 Add a new entry from a buffer.
void addDirectory (const std::string &entryBaseDir, const std::string &dir, EntryOptions options={})
 Adds new entries using the contents of a given directory.
void addDirectory (const std::string &entryBaseDir_, const std::string &dir, const EntryCreation &creation)
 Adds new entries using the contents of a given directory.
virtual bool renameEntry (const std::string &oldPath_, const std::string &newPath_)
 Rename an existing entry.
virtual bool renameDirectory (const std::string &oldDir_, const std::string &newDir_)
 Rename an existing directory.
bool extractEntry (const std::string &entryPath, const std::string &filepath) const
 Extract the given entry to disk at the given file path.
bool extractDirectory (const std::string &dir_, const std::string &outputDir) const
 Extract the given directory to disk under the given output directory.
bool extractAll (const std::string &outputDir, bool createUnderPackFileDir=true) const
 Extract the contents of the pack file to disk at the given directory.
bool extractAll (const std::string &outputDir, const EntryPredicate &predicate, bool stripSharedDirs=true) const
 Extract the contents of the pack file to disk at the given directory - only entries which match the predicate are extracted.
const EntryTriegetBakedEntries () const
 Get entries saved to disk.
const EntryTriegetUnbakedEntries () const
 Get entries that have been added but not yet baked.
std::size_t getEntryCount (bool includeUnbaked=true) const
 Get the number of entries in the pack file.
void runForAllEntries (const EntryCallback &operation, bool includeUnbaked=true) const
 Run a callback for each entry in the pack file.
void runForAllEntries (const std::string &parentDir, const EntryCallback &operation, bool recursive=true, bool includeUnbaked=true) const
 Run a callback for each entry in the pack file under the parent directory.
std::string_view getFilepath () const
 /home/user/pak01_dir.vpk
std::string getTruncatedFilepath () const
 /home/user/pak01_dir.vpk -> /home/user/pak01
std::string getFilename () const
 /home/user/pak01_dir.vpk -> pak01_dir.vpk
std::string getTruncatedFilename () const
 /home/user/pak01_dir.vpk -> pak01.vpk
std::string getFilestem () const
 /home/user/pak01_dir.vpk -> pak01_dir

Static Public Member Functions

static std::unique_ptr< PackFilecreate (const std::string &path, uint32_t version=2)
 Create a new directory VPK file - should end in "_dir.vpk"! This is not enforced but STRONGLY recommended.
static std::unique_ptr< PackFileopen (const std::string &path, const EntryCallback &callback=nullptr)
 Open a VPK file.
static bool generateKeyPairFiles (const std::string &name)
 Generate keypair files, which can be used to sign a VPK Input is a truncated file path, e.g.
Static Public Member Functions inherited from vpkpp::PackFile
static std::unique_ptr< PackFileopen (const std::string &path, const EntryCallback &callback=nullptr, const OpenPropertyRequest &requestProperty=nullptr)
 Open a generic pack file. The parser is selected based on the file extension.
static std::vector< std::string > getOpenableExtensions ()
 Returns a sorted list of supported extensions for opening, e.g. {".bsp", ".vpk"}.
static std::string escapeEntryPathForWrite (const std::string &path)
 On Windows, some characters and file names are invalid - this escapes the given entry path.

Static Public Attributes

static constexpr std::string_view GUID = "98148F7C8701469CB2D8F8620FD738A3"

Protected Member Functions

void addEntryInternal (Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options) override
bool hasExtendedHeader () const
bool hasCompression () const
uint32_t getHeaderLength () const
 PackFile (const PackFile &other)=delete
 PackFile (PackFile &&other) noexcept=default
 PackFile (std::string fullFilePath_)
Protected Member Functions inherited from vpkpp::PackFile
 PackFile (std::string fullFilePath_)
void runForAllEntriesInternal (const std::function< void(const std::string &, Entry &)> &operation, bool includeUnbaked=true)
void runForAllEntriesInternal (const std::string &parentDir, const std::function< void(const std::string &, Entry &)> &operation, bool recursive=true, bool includeUnbaked=true)
std::vector< std::string > verifyEntryChecksumsUsingCRC32 () const
std::string getBakeOutputDir (const std::string &outputDir) const
void mergeUnbakedEntries ()
void setFullFilePath (const std::string &outputDir)
std::string cleanEntryPath (const std::string &path) const

Static Protected Member Functions

static std::unique_ptr< PackFileopenInternal (const std::string &path, const EntryCallback &callback=nullptr)
Static Protected Member Functions inherited from vpkpp::PackFile
static Entry createNewEntry ()
static std::optional< std::vector< std::byte > > readUnbakedEntry (const Entry &entry)
static std::unordered_map< std::string, std::vector< OpenFactoryFunction > > & getOpenExtensionRegistry ()
static const OpenFactoryFunctionregisterOpenExtensionForTypeFactory (std::string_view extension, const OpenFactoryFunctionBasic &factory)
static const OpenFactoryFunctionregisterOpenExtensionForTypeFactory (std::string_view extension, const OpenFactoryFunction &factory)

Protected Attributes

int32_t numArchives = -1
uint32_t currentlyFilledChunkSize = 0
uint32_t chunkSize = VPK_DEFAULT_CHUNK_SIZE
std::vector< FreedChunkfreedChunks
Header1 header1 {}
Header2 header2 {}
Footer2 footer2 {}
std::vector< MD5Entrymd5Entries
Protected Attributes inherited from vpkpp::PackFile
std::string fullFilePath
EntryTrie entries
EntryTrie unbakedEntries

Additional Inherited Members

Public Types inherited from vpkpp::PackFile
enum class  OpenProperty { DECRYPTION_KEY }
using OpenPropertyRequest = std::function<std::vector<std::byte>(PackFile* packFile, OpenProperty property)>
template<typename R>
using EntryCallbackBase = std::function<R(const std::string& path, const Entry& entry)>
 Accepts the entry's path and metadata.
using EntryCallback = EntryCallbackBase<void>
using EntryPredicate = EntryCallbackBase<bool>
using EntryCreation = std::function<EntryOptions(const std::string& path)>
using EntryTrie = tsl::htrie_map<char, Entry>
Protected Types inherited from vpkpp::PackFile
using OpenFactoryFunctionBasic = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback)>
using OpenFactoryFunction = std::function<std::unique_ptr<PackFile>(const std::string& path, const EntryCallback& callback, const OpenPropertyRequest& requestProperty)>

Detailed Description

Definition at line 26 of file VPK.h.

Member Function Documentation

◆ addEntryInternal()

void VPK::addEntryInternal ( Entry & entry,
const std::string & path,
std::vector< std::byte > & buffer,
EntryOptions options )
overrideprotectedvirtual

Implements vpkpp::PackFile.

Definition at line 403 of file VPK.cpp.

◆ bake()

bool VPK::bake ( const std::string & outputDir_,
BakeOptions options,
const EntryCallback & callback )
overridevirtual

If output folder is an empty string, it will overwrite the original.

Implements vpkpp::PackFile.

Definition at line 480 of file VPK.cpp.

◆ create()

std::unique_ptr< PackFile > VPK::create ( const std::string & path,
uint32_t version = 2 )
static

Create a new directory VPK file - should end in "_dir.vpk"! This is not enforced but STRONGLY recommended.

Definition at line 52 of file VPK.cpp.

◆ generateKeyPairFiles()

bool VPK::generateKeyPairFiles ( const std::string & name)
static

Generate keypair files, which can be used to sign a VPK Input is a truncated file path, e.g.

"/x/y/z/key" or just "key" for the CWD It will append ".publickey.vdf" and ".privatekey.vdf" to the input and save those files

Definition at line 804 of file VPK.cpp.

◆ getChunkSize()

uint32_t VPK::getChunkSize ( ) const
nodiscard

Get the VPK chunk size in bytes (size of generated archives when baking)

Definition at line 904 of file VPK.cpp.

◆ getGUID()

std::string_view vpkpp::VPK::getGUID ( ) const
inlinenodiscardconstexproverridevirtual

Get the GUID corresponding to the pack file type.

Implements vpkpp::PackFile.

Definition at line 77 of file VPK.h.

◆ getHeaderLength()

uint32_t VPK::getHeaderLength ( ) const
nodiscardprotected

Definition at line 920 of file VPK.cpp.

◆ getSupportedEntryAttributes()

Attribute VPK::getSupportedEntryAttributes ( ) const
nodiscardoverridevirtual

Returns a list of supported entry attributes Mostly for GUI programs that show entries and their metadata in a table ;)

Reimplemented from vpkpp::PackFile.

Definition at line 794 of file VPK.cpp.

◆ getTruncatedFilestem()

std::string VPK::getTruncatedFilestem ( ) const
nodiscardoverridevirtual

/home/user/pak01_dir.vpk -> pak01

Reimplemented from vpkpp::PackFile.

Definition at line 785 of file VPK.cpp.

◆ getVersion()

uint32_t VPK::getVersion ( ) const
nodiscard

Returns 1 for v1, 2 for v2.

Definition at line 887 of file VPK.cpp.

◆ hasCompression()

bool VPK::hasCompression ( ) const
nodiscardprotected

Definition at line 916 of file VPK.cpp.

◆ hasEntryChecksums()

bool vpkpp::VPK::hasEntryChecksums ( ) const
inlinenodiscardconstexproverridevirtual

Returns true if the format has a checksum for each entry.

Reimplemented from vpkpp::PackFile.

Definition at line 81 of file VPK.h.

◆ hasExtendedHeader()

bool VPK::hasExtendedHeader ( ) const
nodiscardprotected

Definition at line 912 of file VPK.cpp.

◆ hasPackFileChecksum()

bool VPK::hasPackFileChecksum ( ) const
nodiscardoverridevirtual

Returns true if the entire file has a checksum.

Reimplemented from vpkpp::PackFile.

Definition at line 263 of file VPK.cpp.

◆ hasPackFileSignature()

bool VPK::hasPackFileSignature ( ) const
nodiscardoverridevirtual

Returns true if the file is signed.

Reimplemented from vpkpp::PackFile.

Definition at line 293 of file VPK.cpp.

◆ open()

std::unique_ptr< PackFile > VPK::open ( const std::string & path,
const EntryCallback & callback = nullptr )
staticnodiscard

Open a VPK file.

Definition at line 81 of file VPK.cpp.

◆ openInternal()

std::unique_ptr< PackFile > VPK::openInternal ( const std::string & path,
const EntryCallback & callback = nullptr )
staticnodiscardprotected

Definition at line 99 of file VPK.cpp.

◆ operator std::string()

VPK::operator std::string ( ) const
explicitnodiscardoverridevirtual

Reimplemented from vpkpp::PackFile.

Definition at line 799 of file VPK.cpp.

◆ PackFile() [1/3]

vpkpp::PackFile::PackFile ( const PackFile & other)
protecteddelete

◆ PackFile() [2/3]

vpkpp::PackFile::PackFile ( PackFile && other)
protecteddefaultnoexcept

◆ PackFile() [3/3]

PackFile::PackFile ( std::string fullFilePath_)
explicitprotected

Definition at line 201 of file PackFile.cpp.

◆ readEntry()

std::optional< std::vector< std::byte > > VPK::readEntry ( const std::string & path_) const
nodiscardoverridevirtual

Try to read the entry's data to a bytebuffer.

Implements vpkpp::PackFile.

Definition at line 324 of file VPK.cpp.

◆ removeDirectory()

std::size_t VPK::removeDirectory ( const std::string & dirName_)
overridevirtual

Remove a directory.

Reimplemented from vpkpp::PackFile.

Definition at line 467 of file VPK.cpp.

◆ removeEntry()

bool VPK::removeEntry ( const std::string & path_)
overridevirtual

Remove an entry.

Reimplemented from vpkpp::PackFile.

Definition at line 459 of file VPK.cpp.

◆ setChunkSize()

void VPK::setChunkSize ( uint32_t newChunkSize)

Set the VPK chunk size in bytes (size of generated archives when baking)

Definition at line 908 of file VPK.cpp.

◆ setVersion()

void VPK::setVersion ( uint32_t version)

Change the version of the VPK. Valid values are 1 and 2.

Definition at line 891 of file VPK.cpp.

◆ sign() [1/2]

bool VPK::sign ( const std::string & filename_)

Sign the VPK with the given private key KeyValues file. (See below comment)

Definition at line 835 of file VPK.cpp.

◆ sign() [2/2]

bool VPK::sign ( const std::vector< std::byte > & privateKey,
const std::vector< std::byte > & publicKey )

Sign the VPK with the given keypair. Run this after VPK::bake if modifying the VPK file contents.

Definition at line 854 of file VPK.cpp.

◆ verifyEntryChecksums()

std::vector< std::string > VPK::verifyEntryChecksums ( ) const
nodiscardoverridevirtual

Verify the checksums of each file, if a file fails the check its path will be added to the vector If there is no checksum ability in the format, it will return an empty vector.

Reimplemented from vpkpp::PackFile.

Definition at line 259 of file VPK.cpp.

◆ verifyPackFileChecksum()

bool VPK::verifyPackFileChecksum ( ) const
nodiscardoverridevirtual

Verify the checksum of the entire file, returns true on success Will return true if there is no checksum ability in the format.

Reimplemented from vpkpp::PackFile.

Definition at line 267 of file VPK.cpp.

◆ verifyPackFileSignature()

bool VPK::verifyPackFileSignature ( ) const
nodiscardoverridevirtual

Verify the file signature, returns true on success Will return true if there is no signature ability in the format.

Reimplemented from vpkpp::PackFile.

Definition at line 303 of file VPK.cpp.

Member Data Documentation

◆ chunkSize

uint32_t vpkpp::VPK::chunkSize = VPK_DEFAULT_CHUNK_SIZE
protected

Definition at line 147 of file VPK.h.

◆ currentlyFilledChunkSize

uint32_t vpkpp::VPK::currentlyFilledChunkSize = 0
protected

Definition at line 146 of file VPK.h.

◆ footer2

Footer2 vpkpp::VPK::footer2 {}
protected

Definition at line 153 of file VPK.h.

◆ freedChunks

std::vector<FreedChunk> vpkpp::VPK::freedChunks
protected

Definition at line 149 of file VPK.h.

◆ GUID

std::string_view vpkpp::VPK::GUID = "98148F7C8701469CB2D8F8620FD738A3"
staticconstexpr

Definition at line 75 of file VPK.h.

◆ header1

Header1 vpkpp::VPK::header1 {}
protected

Definition at line 151 of file VPK.h.

◆ header2

Header2 vpkpp::VPK::header2 {}
protected

Definition at line 152 of file VPK.h.

◆ md5Entries

std::vector<MD5Entry> vpkpp::VPK::md5Entries
protected

Definition at line 155 of file VPK.h.

◆ numArchives

int32_t vpkpp::VPK::numArchives = -1
protected

Definition at line 145 of file VPK.h.


The documentation for this class was generated from the following files: