quake-tools/source/quam/pak.cc

201 lines
4.5 KiB
C++
Raw Normal View History

2019-09-27 00:09:09 -07:00
#include "common.h"
#include "quam/pak.h"
2019-09-28 22:29:31 -07:00
#include <QIcon>
2019-09-27 00:09:09 -07:00
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;
}
2019-09-28 22:29:31 -07:00
static PakEntry readPakEntry(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);
PakFile file;
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) {
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);
}
auto existingNode = std::find_if(dir.begin(), dir.end(),
[&name](PakNode const &node) {
return node.name == name;
});
if(next) {
auto ref =
existingNode != dir.end()
? *existingNode
: dir.emplace_back(PakDir{}, std::move(name));
insertFile(std::get<PakDir>(ref), *std::move(next), std::move(file));
2019-09-27 23:09:51 -07:00
} else {
2019-09-28 22:29:31 -07:00
if(existingNode != dir.end()) {
throw std::runtime_error("duplicate file");
}
dir.emplace_back(std::move(file), std::move(name));
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
2019-09-28 22:29:31 -07:00
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(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
}
2019-09-28 22:29:31 -07:00
PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
QAbstractItemModel{parent},
m_root{root}
{
}
PakDirModel::~PakDirModel() {
}
QVariant PakDirModel::data(QModelIndex const &index, int role) const {
if(!index.isValid()) {
return QVariant{};
}
auto node = static_cast<PakNode *>(index.internalPointer());
switch(role) {
case Qt::DecorationRole:
if(index.column() == Pak::ColumnName) {
auto icon =
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
2019-09-28 22:29:31 -07:00
break;
case Qt::DisplayRole:
switch(index.column()) {
case Pak::ColumnSize:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Pak::ColumnName:
return QVariant{tr(node->name.data())};
}
2019-09-28 22:29:31 -07:00
default:
break;
}
return QVariant{};
}
Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const {
if(!index.isValid()) {
return Qt::NoItemFlags;
} else {
return Qt::ItemIsSelectable |
Qt::ItemIsDragEnabled |
Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren;
}
}
QVariant PakDirModel::headerData(int section,
Qt::Orientation orientation,
int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case Pak::ColumnSize: return QVariant{tr("Size")};
case Pak::ColumnName: return QVariant{tr("Name")};
}
}
2019-09-28 22:29:31 -07:00
return QVariant{};
}
QModelIndex PakDirModel::index(int row,
int col,
QModelIndex const &parent) const {
if(!hasIndex(row, col, parent) || row > m_root->size()) {
return QModelIndex{};
} else {
// despite index data being const, this function does not take a const
// pointer, which is very annoying!
return createIndex(row, col, const_cast<PakNode *>(&m_root->at(row)));
}
}
QModelIndex PakDirModel::parent(QModelIndex const &) const {
return QModelIndex{};
}
int PakDirModel::rowCount(QModelIndex const &) const {
return m_root->size();
}
int PakDirModel::columnCount(QModelIndex const &) const {
return Pak::ColumnMax;
}
PakDirModelSorter::~PakDirModelSorter() {
}
2019-09-27 00:09:09 -07:00
// EOF