Skip to content

Commit 98506df

Browse files
vpkpp: allow loading vtmb vpk as single file if not using standard layout (closes #64)
1 parent 2cadb5a commit 98506df

1 file changed

Lines changed: 19 additions & 11 deletions

File tree

src/vpkpp/format/VPK_VTMB.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// ReSharper disable CppRedundantQualifier
2+
13
#include <vpkpp/format/VPK_VTMB.h>
24

35
#include <filesystem>
@@ -48,6 +50,12 @@ std::unique_ptr<PackFile> VPK_VTMB::open(const std::string& path, const EntryCal
4850
}
4951
}
5052

53+
if (vpkVTMB->knownArchives.empty()) {
54+
uint32_t archiveIndex;
55+
string::toInt(stem.substr(4), archiveIndex);
56+
vpkVTMB->openNumbered(archiveIndex, path, callback);
57+
}
58+
5159
vpkVTMB->currentArchive++;
5260
return packFile;
5361
}
@@ -56,11 +64,11 @@ void VPK_VTMB::openNumbered(uint32_t archiveIndex, const std::string& path, cons
5664
FileStream reader{path};
5765
reader.seek_in(sizeof(uint32_t) * 2 + sizeof(uint8_t), std::ios::end);
5866

59-
auto fileCount = reader.read<uint32_t>();
60-
auto dirOffset = reader.read<uint32_t>();
67+
const auto fileCount = reader.read<uint32_t>();
68+
const auto dirOffset = reader.read<uint32_t>();
6169

6270
// Make 100% sure
63-
auto version = reader.read<uint8_t>();
71+
const auto version = reader.read<uint8_t>();
6472
if (version != 0) {
6573
return;
6674
}
@@ -90,8 +98,8 @@ void VPK_VTMB::openNumbered(uint32_t archiveIndex, const std::string& path, cons
9098
}
9199

92100
std::optional<std::vector<std::byte>> VPK_VTMB::readEntry(const std::string& path_) const {
93-
auto path = this->cleanEntryPath(path_);
94-
auto entry = this->findEntry(path);
101+
const auto path = this->cleanEntryPath(path_);
102+
const auto entry = this->findEntry(path);
95103
if (!entry) {
96104
return std::nullopt;
97105
}
@@ -122,24 +130,24 @@ bool VPK_VTMB::bake(const std::string& outputDir_, BakeOptions options, const En
122130
}
123131

124132
// Get the proper file output folder
125-
std::string outputDir = this->getBakeOutputDir(outputDir_);
126-
std::string outputPathStem = outputDir + '/' + this->getTruncatedFilestem();
133+
const std::string outputDir = this->getBakeOutputDir(outputDir_);
134+
const std::string outputPathStem = outputDir + '/' + this->getTruncatedFilestem();
127135

128136
// Copy files to temp dir and change current path
129-
auto tempDir = std::filesystem::temp_directory_path() / string::generateUUIDv4();
137+
const auto tempDir = std::filesystem::temp_directory_path() / string::generateUUIDv4();
130138
std::error_code ec;
131139
if (!std::filesystem::create_directory(tempDir, ec)) {
132140
return false;
133141
}
134142
ec.clear();
135-
for (auto vpkIndex : this->knownArchives) {
143+
for (const auto vpkIndex : this->knownArchives) {
136144
std::filesystem::copy(outputPathStem + string::padNumber(vpkIndex, 3) + VPK_VTMB_EXTENSION.data(), tempDir, ec);
137145
if (ec) {
138146
return false;
139147
}
140148
ec.clear();
141149
}
142-
this->fullFilePath = (tempDir / (this->getTruncatedFilestem())).string() + string::padNumber(this->knownArchives[0], 3) + VPK_VTMB_EXTENSION.data();
150+
this->fullFilePath = (tempDir / this->getTruncatedFilestem()).string() + string::padNumber(this->knownArchives[0], 3) + VPK_VTMB_EXTENSION.data();
143151

144152
// Reconstruct data for ease of access
145153
std::unordered_map<uint16_t, std::vector<std::pair<std::string, Entry*>>> entriesToBake;
@@ -166,7 +174,7 @@ bool VPK_VTMB::bake(const std::string& outputDir_, BakeOptions options, const En
166174
}
167175

168176
// Directory
169-
auto dirOffset = stream.tell_out();
177+
const auto dirOffset = stream.tell_out();
170178
for (const auto& [path, entry] : entriesToBakeInArchive) {
171179
stream.write<uint32_t>(path.length());
172180
stream.write(path, false);

0 commit comments

Comments
 (0)