quake-tools/source/quam/pak.cc

140 lines
3.3 KiB
C++
Raw Normal View History

2019-09-27 00:09:09 -07:00
#include "common.h"
#include "quam/pak.h"
struct PakHeader {
quint32 dirOffset;
quint32 dirNum;
};
struct PakEntry {
std::string name;
PakFile file;
2019-09-27 00:09:09 -07:00
};
static constexpr quint32 sizeOfPakEntry = 64;
static PakHeader readPakHeader(std::istream &st) {
auto magic = readBytes<4>(st);
if(magic != std::array{'P', 'A', 'C', 'K'}) {
throw std::runtime_error("not a pak file (invalid magic number)");
}
auto dirOffset = readLE<quint32>(st);
auto dirSize = readLE<quint32>(st);
if(dirSize % sizeOfPakEntry != 0) {
throw std::runtime_error("invalid directory size");
}
PakHeader hdr;
hdr.dirOffset = dirOffset;
hdr.dirNum = dirSize / sizeOfPakEntry;
return hdr;
}
static PakEntry readPakEntry(std::istream &st, quint32 id) {
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);
PakFile file;
file.id = id;
file.resize(entSize);
st.read(file.data(), entSize);
2019-09-27 00:09:09 -07:00
st.seekg(pos);
auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
2019-09-27 23:09:51 -07:00
std::string name;
std::copy(entName.cbegin(), zero, std::back_inserter(name));
if(name.front() == '/') {
2019-09-28 01:17:55 -07:00
throw std::runtime_error("empty root directory name");
2019-09-27 23:09:51 -07:00
}
2019-09-27 00:09:09 -07:00
PakEntry 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-09-27 23:09:51 -07:00
void insertFile(PakDir &dir, std::string name, PakFile file) {
if(auto slash = name.find('/'); slash != std::string::npos) {
auto folder = name.substr(0, slash);
auto next = name.substr(slash + 1);
2019-09-28 01:17:55 -07:00
dir[folder] = PakNode{PakDir{}};
insertFile(std::get<PakDir>(dir[folder]),
2019-09-27 23:09:51 -07:00
std::move(next),
std::move(file));
} else {
2019-09-28 01:17:55 -07:00
dir[name] = PakNode{std::move(file)};
2019-09-27 23:09:51 -07:00
}
}
PakDir readPak(std::istream &st) {
2019-09-27 00:09:09 -07:00
auto hdr = readPakHeader(st);
st.seekg(hdr.dirOffset);
2019-09-27 23:09:51 -07:00
PakDir root;
2019-09-27 00:09:09 -07:00
for(quint32 id = 0; id < hdr.dirNum; id++) {
auto ent = readPakEntry(st, id);
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
}
void setTableToPakDir(QTableWidget &table, PakDir const &dir) {
constexpr auto Flags = Qt::ItemIsSelectable |
Qt::ItemIsDragEnabled |
Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren;
auto sorted = table.isSortingEnabled();
table.clearContents();
table.setSortingEnabled(false);
quint32 row{0};
for(auto const &kv : dir) {
auto const &name = kv.first;
auto const &node = kv.second;
table.setRowCount(row + 1);
{
auto item = new QTableWidgetItem;
item->setFlags(Flags);
if(auto file = std::get_if<PakFile>(&node)) {
item->setText(QString::number(file->id));
}
table.setItem(row, Pak::ColumnId, item);
}
{
auto item = new QTableWidgetItem;
item->setFlags(Flags);
if(auto file = std::get_if<PakFile>(&node)) {
item->setText(QString::number(file->size()));
}
table.setItem(row, Pak::ColumnSize, item);
}
{
auto item = new QTableWidgetItem;
auto icon = std::holds_alternative<PakDir>(node) ? "folder" :
"text-x-generic";
item->setFlags(Flags);
item->setText(QString::fromStdString(name));
item->setIcon(QIcon::fromTheme(icon));
table.setItem(row, Pak::ColumnName, item);
}
++row;
}
table.setSortingEnabled(sorted);
table.resizeColumnsToContents();
}
2019-09-27 00:09:09 -07:00
// EOF