SourcePP
Several modern C++20 libraries for sanely parsing Valve's formats.
Loading...
Searching...
No Matches
HOG.cpp
Go to the documentation of this file.
1#include <vpkpp/format/HOG.h>
2
3#include <filesystem>
4
5#include <FileStream.h>
6
7using namespace sourcepp;
8using namespace vpkpp;
9
10std::unique_ptr<PackFile> HOG::open(const std::string& path, const EntryCallback& callback) {
11 if (!std::filesystem::exists(path)) {
12 // File does not exist
13 return nullptr;
14 }
15
16 auto* hog = new HOG{path};
17 auto packFile = std::unique_ptr<PackFile>(hog);
18
19 FileStream reader{hog->fullFilePath};
20 reader.seek_in(0);
21
22 // Verify signature
23 if (const auto signature = reader.read_string(3); signature != HOG_SIGNATURE) {
24 return nullptr;
25 }
26
27 // Read file entries
28 while (true) {
29 // Create new entry
30 Entry entry = createNewEntry();
31
32 // Get file path
33 const auto entryPath = hog->cleanEntryPath(reader.read_string(13));
34
35 // Check if we're at EOF (must perform the check after reading beyond file bounds)
36 if (!reader) {
37 break;
38 }
39
40 // Get file size and offset
41 entry.length = reader.read<uint32_t>();
42 entry.offset = reader.tell_in();
43
44 // Seek past file data
45 reader.seek_in_u(entry.offset + entry.length);
46
47 // Put it in
48 hog->entries.emplace(entryPath, entry);
49
50 if (callback) {
51 callback(entryPath, entry);
52 }
53 }
54
55 return packFile;
56}
57
58std::optional<std::vector<std::byte>> HOG::readEntry(const std::string& path_) const {
59 auto path = this->cleanEntryPath(path_);
60 auto entry = this->findEntry(path);
61 if (!entry) {
62 return std::nullopt;
63 }
64 if (entry->unbaked) {
65 return readUnbakedEntry(*entry);
66 }
67
68 // It's baked into the file on disk
69 FileStream stream{this->fullFilePath};
70 if (!stream) {
71 return std::nullopt;
72 }
73 stream.seek_in_u(entry->offset);
74 return stream.read_bytes(entry->length);
75}
76
78 using enum Attribute;
79 return LENGTH;
80}
This class represents the metadata that a file has inside a PackFile.
Definition: Entry.h:14
uint64_t offset
Offset, format-specific meaning - 0 if unused, or if the offset genuinely is 0.
Definition: Entry.h:33
uint64_t length
Length in bytes (in formats with compression, this is the uncompressed length)
Definition: Entry.h:26
Definition: HOG.h:10
Attribute getSupportedEntryAttributes() const override
Returns a list of supported entry attributes Mostly for GUI programs that show entries and their meta...
Definition: HOG.cpp:77
static std::unique_ptr< PackFile > open(const std::string &path, const EntryCallback &callback=nullptr)
Open a HOG file.
Definition: HOG.cpp:10
std::optional< std::vector< std::byte > > readEntry(const std::string &path_) const override
Try to read the entry's data to a bytebuffer.
Definition: HOG.cpp:58
EntryCallbackBase< void > EntryCallback
Definition: PackFile.h:30
std::optional< Entry > findEntry(const std::string &path_, bool includeUnbaked=true) const
Try to find an entry given the file path.
Definition: PackFile.cpp:163
std::string fullFilePath
Definition: PackFile.h:219
std::string cleanEntryPath(const std::string &path) const
Definition: PackFile.cpp:677
static Entry createNewEntry()
Definition: PackFile.cpp:686
static std::optional< std::vector< std::byte > > readUnbakedEntry(const Entry &entry)
Definition: PackFile.cpp:690
Definition: LZMA.h:11
Definition: Attribute.h:5
Attribute
Definition: Attribute.h:7
constexpr std::string_view HOG_SIGNATURE
Definition: HOG.h:7