quake-tools/source/quam/pack.cc
2019-10-04 13:37:34 -04:00

101 lines
2.0 KiB
C++

#include "quam/pack.h"
struct PackHeader {
quint32 dirOffset;
quint32 dirNum;
};
struct PackEntry {
std::string name;
ArcFile file;
};
static constexpr quint32 sizeOfPackEntry = 64;
static PackHeader readPackHeader(std::istream &st) {
auto magic = readBytes<4>(st);
if(magic != std::array{'P', 'A', 'C', 'K'}) {
throw FileFormatError("not a pak file (invalid magic number)");
}
auto dirOffset = readLE<quint32>(st);
auto dirSize = readLE<quint32>(st);
if(dirSize % sizeOfPackEntry != 0) {
throw FileFormatError("invalid directory size");
}
PackHeader hdr;
hdr.dirOffset = dirOffset;
hdr.dirNum = dirSize / sizeOfPackEntry;
return hdr;
}
static PackEntry readPackEntry(std::istream &st) {
auto entName = readBytes<56>(st);
auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st);
auto pos = st.tellg();
st.seekg(entOffset);
ArcFile file;
file.resize(entSize);
st.read(file.data(), entSize);
st.seekg(pos);
std::string name = ntbsToString(entName);
if(name.front() == '/') {
throw FileFormatError("empty root directory name");
}
PackEntry ent;
ent.name = std::move(name);
ent.file = std::move(file);
return ent;
}
static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) {
next = name.substr(slash + 1);
name = name.substr(0, slash);
}
auto existing = dir.findNode(name);
if(next) {
auto ref =
existing != dir.end()
? *existing
: dir.emplace_back(ArcDir{}, std::move(name));
insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file));
} else {
if(existing != dir.end()) {
throw FileFormatError("duplicate file");
}
dir.emplace_back(std::move(file), std::move(name));
}
}
ArcDir readPack(std::istream &st) {
auto hdr = readPackHeader(st);
st.seekg(hdr.dirOffset);
ArcDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPackEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.file));
}
return root;
}
// EOF