SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
ExamplePackFileImpl.h
Go to the documentation of this file.
1#pragma once
2
3#include <vpkpp/PackFile.h>
4
5/*
6 * --- Example Pack File Implementation ---
7 *
8 * This code is a template for adding a new file format to vpkpp. Copy these two files and follow the comments!
9 *
10 * Any methods marked as "[OPTIONAL]" can be deleted if the file format does not support them.
11 *
12 * Note that if you are writing a read-only parser, you will need to make the following deviations:
13 * - Inherit from PackFileReadOnly instead of PackFile
14 * - Don't implement the bake and addEntryInternal methods (marked with "[WRITE]")
15 */
16
17namespace vpkpp {
18
19// Define the accepted extension(s) as constant(s)
20constexpr std::string_view EXAMPLE_EXTENSION = ".example";
21
22// All file formats need a static open method, and need to derive four methods at minimum from PackFile
23class EXAMPLE : public PackFile {
24public:
25 // Always return a unique_ptr to PackFile so it has a uniform return type
26 [[nodiscard]] static std::unique_ptr<PackFile> open(const std::string& path, const EntryCallback& callback = nullptr);
27
28 // Use a site like https://guidgenerator.com/ to generate a GUID - don't copypaste this one!!!
29 static constexpr inline std::string_view GUID = "9F389AF190E74D3DA472C5AA4B881F83";
30
31 // Necessary to identify the type of the pack file
32 [[nodiscard]] constexpr std::string_view getGUID() const noexcept override {
33 return EXAMPLE::GUID;
34 }
35
36 // [OPTIONAL] Implement this and return true if your file format is case-sensitive
37 [[nodiscard]] constexpr bool isCaseSensitive() const noexcept override {
39 }
40
41 // Returns the raw entry data
42 [[nodiscard]] std::optional<std::vector<std::byte>> readEntry(const std::string& path_) const override;
43
44 // [WRITE] Save any changes made to the opened file(s)
45 bool bake(const std::string& outputDir_ /*= ""*/, BakeOptions options /*= {}*/, const EntryCallback& callback /*= nullptr*/) override;
46
47 // [OPTIONAL] Returns any attributes your file format's entries have (refer to the other file formats for more info)
48 [[nodiscard]] Attribute getSupportedEntryAttributes() const override {
50 }
51
52 // [OPTIONAL] Add any custom file info here (refer to the other file formats for how to structure this)
53 [[nodiscard]] explicit operator std::string() const override {
54 return PackFile::operator std::string();
55 }
56
57protected:
59
60 // [WRITE] Adds a new entry from either a filename or a buffer
61 // Again, if your type needs any new options specific to entries, add them to EntryOptions
62 void addEntryInternal(Entry& entry, const std::string& path, std::vector<std::byte>& buffer, EntryOptions options) override;
63
64private:
65 // Finally, register the open method with the extension
66 // Remember since C++ is STUPID you need to add this header to PackFile.cpp as well, or this will get optimized away
68};
69
70} // namespace vpkpp
#define VPKPP_REGISTER_PACKFILE_OPEN(extension, function)
Definition: PackFile.h:242
constexpr bool isCaseSensitive() const noexcept override
Does the format support case-sensitive file names?
static constexpr std::string_view GUID
void addEntryInternal(Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options) override
constexpr std::string_view getGUID() const noexcept override
Get the GUID corresponding to the pack file type.
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
bool bake(const std::string &outputDir_, BakeOptions options, const EntryCallback &callback) override
If output folder is an empty string, it will overwrite the original.
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr)
This class represents the metadata that a file has inside a PackFile.
Definition: Entry.h:14
EntryCallbackBase< void > EntryCallback
Definition: PackFile.h:30
virtual constexpr bool isCaseSensitive() const
Does the format support case-sensitive file names?
Definition: PackFile.h:86
virtual Attribute getSupportedEntryAttributes() const
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
Definition: PackFile.cpp:608
PackFile(const PackFile &other)=delete
Definition: Attribute.h:5
constexpr std::string_view EXAMPLE_EXTENSION
Attribute
Definition: Attribute.h:7