![]() |
SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
|
#include <PackFile.h>


Public Types | |
| 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> |
Public Member Functions | |
| PackFile (const PackFile &other)=delete | |
| PackFile & | operator= (const PackFile &other)=delete |
| PackFile (PackFile &&other) noexcept=default | |
| PackFile & | operator= (PackFile &&other) noexcept=default |
| virtual | ~PackFile ()=default |
| virtual constexpr bool | hasEntryChecksums () const |
| Returns true if the format has a checksum for each entry. | |
| virtual std::vector< std::string > | verifyEntryChecksums () const |
| 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. | |
| virtual bool | hasPackFileChecksum () const |
| Returns true if the entire file has a checksum. | |
| virtual bool | verifyPackFileChecksum () const |
| Verify the checksum of the entire file, returns true on success Will return true if there is no checksum ability in the format. | |
| virtual bool | hasPackFileSignature () const |
| Returns true if the file is signed. | |
| virtual bool | verifyPackFileSignature () const |
| Verify the file signature, returns true on success Will return true if there is no signature ability in the format. | |
| 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< Entry > | findEntry (const std::string &path_, bool includeUnbaked=true) const |
| Try to find an entry given the file path. | |
| virtual std::optional< std::vector< std::byte > > | readEntry (const std::string &path_) const =0 |
| Try to read the entry's data to a bytebuffer. | |
| 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 |
| bool | 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. | |
| bool | addEntry (const std::string &path, std::vector< std::byte > &&buffer, EntryOptions options={}) |
| Add a new entry from a buffer. | |
| bool | addEntry (const std::string &path, std::span< const std::byte > buffer, EntryOptions options={}) |
| Add a new entry from a buffer. | |
| int64_t | addDirectory (const std::string &entryBaseDir, const std::string &dir, EntryOptions options={}) |
| Adds new entries using the contents of a given directory. | |
| int64_t | 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. | |
| virtual bool | removeEntry (const std::string &path_) |
| Remove an entry. | |
| virtual std::size_t | removeDirectory (const std::string &dirName_) |
| Remove a directory. | |
| bool | bake () |
| If output folder is an empty string, it will overwrite the original. | |
| bool | bake (const std::string &outputDir_) |
| If output folder is an empty string, it will overwrite the original. | |
| bool | bake (const std::string &outputDir_, BakeOptions options) |
| If output folder is an empty string, it will overwrite the original. | |
| virtual bool | bake (const std::string &outputDir_, BakeOptions options, const EntryCallback &callback)=0 |
| If output folder is an empty string, it will overwrite the original. | |
| 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 EntryTrie & | getBakedEntries () const |
| Get entries saved to disk. | |
| const EntryTrie & | getUnbakedEntries () 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 | |
| virtual std::string | getTruncatedFilestem () const |
| /home/user/pak01_dir.vpk -> pak01 | |
| virtual Attribute | getSupportedEntryAttributes () const |
| Returns a list of supported entry attributes Mostly for GUI programs that show entries and their metadata in a table ;). | |
| virtual | operator std::string () const |
Static Public Member Functions | |
| static std::unique_ptr< PackFile > | open (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. | |
Protected Types | |
| 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)> |
Protected Member Functions | |
| 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 |
| virtual void | addEntryInternal (Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options)=0 |
| 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 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 OpenFactoryFunction & | registerOpenExtensionForTypeFactory (std::string_view extension, const OpenFactoryFunctionBasic &factory) |
| static const OpenFactoryFunction & | registerOpenExtensionForTypeFactory (std::string_view extension, const OpenFactoryFunction &factory) |
Protected Attributes | |
| std::string | fullFilePath |
| EntryTrie | entries |
| EntryTrie | unbakedEntries |
Definition at line 26 of file PackFile.h.
| using vpkpp::PackFile::EntryCallback = EntryCallbackBase<void> |
Definition at line 38 of file PackFile.h.
| using vpkpp::PackFile::EntryCallbackBase = std::function<R(const std::string& path, const Entry& entry)> |
Accepts the entry's path and metadata.
Definition at line 37 of file PackFile.h.
| using vpkpp::PackFile::EntryCreation = std::function<EntryOptions(const std::string& path)> |
Definition at line 42 of file PackFile.h.
| using vpkpp::PackFile::EntryPredicate = EntryCallbackBase<bool> |
Definition at line 39 of file PackFile.h.
| using vpkpp::PackFile::EntryTrie = tsl::htrie_map<char, Entry> |
Definition at line 44 of file PackFile.h.
|
protected |
Definition at line 223 of file PackFile.h.
|
protected |
Definition at line 222 of file PackFile.h.
| using vpkpp::PackFile::OpenPropertyRequest = std::function<std::vector<std::byte>(PackFile* packFile, OpenProperty property)> |
Definition at line 33 of file PackFile.h.
|
strong |
| Enumerator | |
|---|---|
| DECRYPTION_KEY | |
Definition at line 28 of file PackFile.h.
|
delete |
|
defaultnoexcept |
|
virtualdefault |
|
explicitprotected |
Definition at line 118 of file PackFile.cpp.
| int64_t PackFile::addDirectory | ( | const std::string & | entryBaseDir, |
| const std::string & | dir, | ||
| EntryOptions | options = {} ) |
Adds new entries using the contents of a given directory.
Definition at line 247 of file PackFile.cpp.
| int64_t PackFile::addDirectory | ( | const std::string & | entryBaseDir_, |
| const std::string & | dir, | ||
| const EntryCreation & | creation ) |
Adds new entries using the contents of a given directory.
Definition at line 253 of file PackFile.cpp.
| bool PackFile::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.
Definition at line 203 of file PackFile.cpp.
| bool PackFile::addEntry | ( | const std::string & | path, |
| std::span< const std::byte > | buffer, | ||
| EntryOptions | options = {} ) |
Add a new entry from a buffer.
Definition at line 242 of file PackFile.cpp.
| bool PackFile::addEntry | ( | const std::string & | path, |
| std::vector< std::byte > && | buffer, | ||
| EntryOptions | options = {} ) |
Add a new entry from a buffer.
Definition at line 226 of file PackFile.cpp.
|
protectedpure virtual |
Implemented in vpkpp::APK, vpkpp::FGP, vpkpp::GMA, vpkpp::PackFileReadOnly, vpkpp::PAK, vpkpp::PCK, vpkpp::VPK, vpkpp::VPK_VTMB, vpkpp::WAD3, and vpkpp::ZIP.
| bool PackFile::bake | ( | ) |
If output folder is an empty string, it will overwrite the original.
Definition at line 369 of file PackFile.cpp.
| bool PackFile::bake | ( | const std::string & | outputDir_ | ) |
If output folder is an empty string, it will overwrite the original.
Definition at line 373 of file PackFile.cpp.
| bool PackFile::bake | ( | const std::string & | outputDir_, |
| BakeOptions | options ) |
If output folder is an empty string, it will overwrite the original.
Definition at line 377 of file PackFile.cpp.
|
pure virtual |
If output folder is an empty string, it will overwrite the original.
Implemented in bsppp::PakLump, vpkpp::APK, vpkpp::FGP, vpkpp::GMA, vpkpp::PackFileReadOnly, vpkpp::PAK, vpkpp::PCK, vpkpp::VPK, vpkpp::VPK_VTMB, vpkpp::WAD3, and vpkpp::ZIP.
|
nodiscardprotected |
Definition at line 706 of file PackFile.cpp.
|
staticnodiscardprotected |
Definition at line 715 of file PackFile.cpp.
|
staticnodiscard |
On Windows, some characters and file names are invalid - this escapes the given entry path.
Definition at line 646 of file PackFile.cpp.
| bool PackFile::extractAll | ( | const std::string & | outputDir, |
| bool | createUnderPackFileDir = true ) const |
Extract the contents of the pack file to disk at the given directory.
Definition at line 425 of file PackFile.cpp.
| bool PackFile::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.
Definition at line 447 of file PackFile.cpp.
| bool PackFile::extractDirectory | ( | const std::string & | dir_, |
| const std::string & | outputDir ) const |
Extract the given directory to disk under the given output directory.
Definition at line 400 of file PackFile.cpp.
| bool PackFile::extractEntry | ( | const std::string & | entryPath, |
| const std::string & | filepath ) const |
Extract the given entry to disk at the given file path.
Definition at line 381 of file PackFile.cpp.
|
nodiscard |
Try to find an entry given the file path.
Definition at line 172 of file PackFile.cpp.
|
nodiscard |
Get entries saved to disk.
Definition at line 512 of file PackFile.cpp.
|
nodiscardprotected |
Definition at line 670 of file PackFile.cpp.
|
nodiscard |
Get the number of entries in the pack file.
Definition at line 520 of file PackFile.cpp.
|
nodiscard |
/home/user/pak01_dir.vpk -> pak01_dir.vpk
Definition at line 621 of file PackFile.cpp.
|
nodiscard |
/home/user/pak01_dir.vpk
Definition at line 613 of file PackFile.cpp.
|
nodiscard |
/home/user/pak01_dir.vpk -> pak01_dir
Definition at line 630 of file PackFile.cpp.
|
staticnodiscard |
Returns a sorted list of supported extensions for opening, e.g. {".bsp", ".vpk"}.
Definition at line 137 of file PackFile.cpp.
|
staticprotected |
Definition at line 734 of file PackFile.cpp.
|
nodiscardvirtual |
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their metadata in a table ;).
Reimplemented in vpkpp::APK, vpkpp::FGP, vpkpp::GCF, vpkpp::GMA, vpkpp::HOG, vpkpp::OL, vpkpp::OO7, vpkpp::ORE, vpkpp::PAK, vpkpp::PCK, vpkpp::VPK, vpkpp::VPK_VTMB, vpkpp::VPP, vpkpp::WAD3, vpkpp::XZP, and vpkpp::ZIP.
Definition at line 638 of file PackFile.cpp.
|
nodiscard |
/home/user/pak01_dir.vpk -> pak01.vpk
Definition at line 625 of file PackFile.cpp.
|
nodiscard |
/home/user/pak01_dir.vpk -> /home/user/pak01
Definition at line 617 of file PackFile.cpp.
|
nodiscardvirtual |
/home/user/pak01_dir.vpk -> pak01
Reimplemented in vpkpp::VPK, and vpkpp::VPK_VTMB.
Definition at line 634 of file PackFile.cpp.
|
nodiscard |
Get entries that have been added but not yet baked.
Definition at line 516 of file PackFile.cpp.
|
nodiscard |
Check if an entry exists given the file path.
Definition at line 168 of file PackFile.cpp.
|
inlinenodiscardconstexprvirtual |
Returns true if the format has a checksum for each entry.
Reimplemented in vpkpp::GCF, vpkpp::GMA, vpkpp::VPK, and vpkpp::ZIP.
Definition at line 61 of file PackFile.h.
|
nodiscardvirtual |
Returns true if the entire file has a checksum.
Reimplemented in vpkpp::GMA, vpkpp::OO7, and vpkpp::VPK.
Definition at line 152 of file PackFile.cpp.
|
nodiscardvirtual |
Returns true if the file is signed.
Reimplemented in vpkpp::VPK.
Definition at line 160 of file PackFile.cpp.
|
inlinenodiscardconstexprvirtual |
Does the format support case-sensitive file names?
Reimplemented in bsppp::PakLump, vpkpp::GCF, vpkpp::OL, vpkpp::OO7, vpkpp::PCK, and vpkpp::ZIP.
Definition at line 84 of file PackFile.h.
|
inlinenodiscardconstexprvirtualnoexcept |
Reimplemented in vpkpp::PackFileReadOnly, and vpkpp::ZIP.
Definition at line 102 of file PackFile.h.
|
protected |
Definition at line 685 of file PackFile.cpp.
|
staticnodiscard |
Open a generic pack file. The parser is selected based on the file extension.
Definition at line 123 of file PackFile.cpp.
|
explicitnodiscardvirtual |
Reimplemented in bsppp::PakLump, vpkpp::FGP, vpkpp::GCF, vpkpp::GMA, vpkpp::OO7, vpkpp::PackFileReadOnly, vpkpp::PCK, and vpkpp::VPK.
Definition at line 642 of file PackFile.cpp.
|
nodiscard |
Definition at line 185 of file PackFile.cpp.
|
nodiscardpure virtual |
Try to read the entry's data to a bytebuffer.
Implemented in vpkpp::APK, vpkpp::FGP, vpkpp::GCF, vpkpp::GMA, vpkpp::HOG, vpkpp::OL, vpkpp::OO7, vpkpp::ORE, vpkpp::PAK, vpkpp::PCK, vpkpp::VPK, vpkpp::VPK_VTMB, vpkpp::VPP, vpkpp::WAD3, vpkpp::XZP, and vpkpp::ZIP.
|
nodiscard |
Try to read the entry's data to a string.
Definition at line 189 of file PackFile.cpp.
|
staticnodiscardprotected |
Definition at line 719 of file PackFile.cpp.
|
staticprotected |
Definition at line 745 of file PackFile.cpp.
|
staticprotected |
Definition at line 739 of file PackFile.cpp.
|
virtual |
Remove a directory.
Reimplemented in vpkpp::FGP, and vpkpp::VPK.
Definition at line 351 of file PackFile.cpp.
|
virtual |
Remove an entry.
Reimplemented in vpkpp::FGP, and vpkpp::VPK.
Definition at line 334 of file PackFile.cpp.
|
virtual |
Rename an existing directory.
Reimplemented in vpkpp::FGP.
Definition at line 305 of file PackFile.cpp.
|
virtual |
| void PackFile::runForAllEntries | ( | const EntryCallback & | operation, |
| bool | includeUnbaked = true ) const |
Run a callback for each entry in the pack file.
Definition at line 529 of file PackFile.cpp.
| void PackFile::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.
Definition at line 543 of file PackFile.cpp.
|
protected |
Definition at line 571 of file PackFile.cpp.
|
protected |
Definition at line 585 of file PackFile.cpp.
|
protected |
Definition at line 701 of file PackFile.cpp.
|
nodiscardvirtual |
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 in vpkpp::GCF, vpkpp::GMA, vpkpp::VPK, and vpkpp::ZIP.
Definition at line 148 of file PackFile.cpp.
|
nodiscardprotected |
Definition at line 656 of file PackFile.cpp.
|
nodiscardvirtual |
Verify the checksum of the entire file, returns true on success Will return true if there is no checksum ability in the format.
Reimplemented in vpkpp::GMA, vpkpp::OO7, and vpkpp::VPK.
Definition at line 156 of file PackFile.cpp.
|
nodiscardvirtual |
Verify the file signature, returns true on success Will return true if there is no signature ability in the format.
Reimplemented in vpkpp::VPK.
Definition at line 164 of file PackFile.cpp.
|
protected |
Definition at line 232 of file PackFile.h.
|
protected |
Definition at line 231 of file PackFile.h.
|
protected |
Definition at line 233 of file PackFile.h.