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-05 16:58:19 -07:00
|
|
|
Arc::File 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-05 16:58:19 -07:00
|
|
|
Arc::File file;
|
2019-09-28 03:54:04 -07:00
|
|
|
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);
|
2019-09-28 03:54:04 -07:00
|
|
|
ent.file = std::move(file);
|
2019-09-27 00:09:09 -07:00
|
|
|
return ent;
|
|
|
|
}
|
|
|
|
|
2019-10-08 20:43:35 -07:00
|
|
|
static void insertFile(Arc::Dir &dir, std::string &name, Arc::File &file) {
|
2019-10-05 16:58:19 -07:00
|
|
|
Option<std::string> next;
|
2019-09-28 22:29:31 -07:00
|
|
|
|
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) {
|
2019-10-08 20:43:35 -07:00
|
|
|
auto &ref = existing ? *existing
|
|
|
|
: dir.emplace_back(Arc::Dir{}, std::move(name));
|
|
|
|
insertFile(std::get<Arc::Dir>(ref), *next, file);
|
|
|
|
} else if(!existing) {
|
2019-09-28 22:29:31 -07:00
|
|
|
dir.emplace_back(std::move(file), std::move(name));
|
2019-10-08 20:43:35 -07:00
|
|
|
} else {
|
|
|
|
throw FileFormatError("duplicate file");
|
2019-09-27 23:09:51 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-05 16:58:19 -07:00
|
|
|
Arc::Dir readPack(std::istream &st) {
|
2019-10-04 10:37:34 -07:00
|
|
|
auto hdr = readPackHeader(st);
|
2019-09-27 00:09:09 -07:00
|
|
|
st.seekg(hdr.dirOffset);
|
|
|
|
|
2019-10-05 16:58:19 -07:00
|
|
|
Arc::Dir 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);
|
2019-10-08 20:43:35 -07:00
|
|
|
insertFile(root, ent.name, 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
|