quake-tools/source/quam/pack.cc

101 lines
2.0 KiB
C++
Raw Normal View History

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