21#include <FileStream.h>
53 FileStream stream{path, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
62 if (!std::filesystem::exists(path)) {
67 auto* wad =
new WAD3{path};
68 std::unique_ptr<PackFile> packFile{wad};
74 if (
auto signature = reader.read<uint32_t>(); signature !=
WAD3_SIGNATURE) {
79 auto lumpCount = reader.read<uint32_t>();
80 auto lumpOffset = reader.read<uint32_t>();
83 reader.seek_in(lumpOffset);
84 for (uint32_t i = 0; i < lumpCount; i++) {
86 auto offset = reader.read<int32_t>();
87 auto size = reader.read<int32_t>();
88 reader.skip_in<int32_t>();
89 auto type = reader.read<int8_t>();
90 reader.skip_in<int8_t>();
91 reader.skip_in<int16_t>();
106 auto entryPath = wad->cleanEntryPath(name);
107 wad->entries.emplace(entryPath, entry);
110 callback(entryPath, entry);
118std::optional<std::vector<std::byte>>
WAD3::readEntry(
const std::string& path_)
const {
126 if (entry->unbaked) {
135 stream.seek_in_u(entry->offset);
136 return stream.read_bytes(entry->length);
140 entry.
length = buffer.size();
149 std::string outputPath = outputDir +
'/' + this->
getFilename();
152 FileStream stream{outputPath, FileStream::OPT_TRUNCATE | FileStream::OPT_CREATE_IF_NONEXISTENT};
156 std::vector<std::byte> fileData;
159 uint32_t totalDataLength = 0;
161 totalDataLength += entry.
length;
163 fileData.reserve(totalDataLength);
166 uint32_t currentOffset =
sizeof(uint32_t) * 3;
171 stream.write<uint32_t>(currentOffset);
178 if (
auto binData = this->
readEntry(path)) {
179 entry.
offset = currentOffset + fileData.size();
180 fileData.insert(fileData.end(), binData->begin(), binData->end());
189 const std::size_t pos = path.find_last_of(
'.');
191 const std::string_view ext = path.c_str() + pos + 1;
199 path = path.substr(0, pos);
203 stream.write<uint32_t>(entry.
offset);
204 stream.write<uint32_t>(0);
205 stream.write<uint32_t>(entry.
length);
206 stream.write<uint8_t>(type);
207 stream.write<uint8_t>(0);
208 stream.write<uint16_t>(0);
212 callback(path, entry);
217 stream.write(fileData);
constexpr std::string_view k_FileTypeNames[]
This class represents the metadata that a file has inside a PackFile.
bool unbaked
Used to check if entry is saved to disk.
uint64_t offset
Offset, format-specific meaning - 0 if unused, or if the offset genuinely is 0.
uint64_t length
Length in bytes (in formats with compression, this is the uncompressed length)
EntryCallbackBase< void > EntryCallback
void mergeUnbakedEntries()
std::optional< Entry > findEntry(const std::string &path_, bool includeUnbaked=true) const
Try to find an entry given the file path.
void runForAllEntriesInternal(const std::function< void(const std::string &, Entry &)> &operation, bool includeUnbaked=true)
std::string getFilename() const
/home/user/pak01_dir.vpk -> pak01_dir.vpk
std::string getBakeOutputDir(const std::string &outputDir) const
void runForAllEntries(const EntryCallback &operation, bool includeUnbaked=true) const
Run a callback for each entry in the pack file.
void setFullFilePath(const std::string &outputDir)
std::string cleanEntryPath(const std::string &path) const
static Entry createNewEntry()
std::size_t getEntryCount(bool includeUnbaked=true) const
Get the number of entries in the pack file.
static std::optional< std::vector< std::byte > > readUnbakedEntry(const Entry &entry)
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr)
Open a WAD3 file.
static std::unique_ptr< PackFile > create(const std::string &path)
Create a WAD3 file.
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::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
void addEntryInternal(Entry &entry, const std::string &path, std::vector< std::byte > &buffer, EntryOptions options) override
bool iequals(std::string_view s1, std::string_view s2)
constexpr int8_t WAD3_FILENAME_MAX_SIZE
constexpr auto WAD3_SIGNATURE