add wad2 file code

This commit is contained in:
alison wright 2019-10-03 17:06:05 -04:00
parent f42028a378
commit efdf96055e
7 changed files with 220 additions and 25 deletions

View File

@ -51,7 +51,9 @@ add_executable(
source/quam/pak.h source/quam/pak.h
source/quam/project.cc source/quam/project.cc
source/quam/project.h source/quam/project.h
source/quam/project.ui) source/quam/project.ui
source/quam/wad.cc
source/quam/wad.h)
make_qt_project(quam) make_qt_project(quam)

View File

@ -25,6 +25,12 @@ static inline QString trMain(char const *sourceText,
return QCoreApplication::translate("main", sourceText, disambiguation, n); return QCoreApplication::translate("main", sourceText, disambiguation, n);
} }
static inline quint8 readByte(std::istream &st) {
char b;
st.read(&b, 1);
return quint8(b);
}
template<typename T> template<typename T>
static inline T readLE(std::istream &st) { static inline T readLE(std::istream &st) {
std::array<char, sizeof(T)> b; std::array<char, sizeof(T)> b;
@ -58,7 +64,7 @@ static inline QDebug operator<<(QDebug debug, std::unique_ptr<T> const &t) {
} }
template<typename T0, typename... Ts> template<typename T0, typename... Ts>
static inline QDebug operator <<(QDebug debug, static inline QDebug operator<<(QDebug debug,
std::variant<T0, Ts...> const &t) { std::variant<T0, Ts...> const &t) {
std::visit([&](auto &&arg) {debug << arg;}, t); std::visit([&](auto &&arg) {debug << arg;}, t);
return debug; return debug;
@ -68,4 +74,12 @@ static inline std::ifstream openReadBin(std::filesystem::path path) {
return std::ifstream{path, std::ios_base::in | std::ios_base::binary}; return std::ifstream{path, std::ios_base::in | std::ios_base::binary};
} }
template<std::size_t N>
static inline std::string ntbsToString(std::array<char, N> const &ntbs) {
std::string str;
auto zero = std::find(ntbs.cbegin(), ntbs.cend(), '\0');
std::copy(ntbs.cbegin(), zero, std::back_inserter(str));
return str;
}
// EOF // EOF

View File

@ -10,18 +10,114 @@ Arc::FileType Arc::getFileType(int n) {
} }
} }
ArcNode::ArcNode(ArcDir &&t, std::string &&n, Arc::FileType ty) : ArcDir::iterator ArcDir::findNode(std::string const &name) {
super_type(std::move(t)), return std::find_if(begin(), end(), [&name](ArcNode const &node) {
return node.name == name;
});
}
ArcNode::ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(f)),
name(std::move(n)), name(std::move(n)),
type(ty) type(t)
{ {
} }
ArcNode::ArcNode(ArcFile &&t, std::string &&n, Arc::FileType ty) : ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(t)), super_type(std::move(f)),
name(std::move(n)), name(std::move(n)),
type(ty) type(t)
{ {
} }
/*
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() == Arc::ColumnName) {
auto icon =
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(index.column()) {
case Arc::ColumnSize:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Arc::ColumnName:
return QVariant{tr(node->name.data())};
}
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 Arc::ColumnSize: return QVariant{tr("Size")};
case Arc::ColumnName: return QVariant{tr("Name")};
}
}
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 Arc::ColumnMax;
}
*/
// EOF // EOF

View File

@ -34,6 +34,8 @@ struct ArcNode;
struct ArcDir : public std::vector<ArcNode> { struct ArcDir : public std::vector<ArcNode> {
using std::vector<ArcNode>::vector; using std::vector<ArcNode>::vector;
ArcDir::iterator findNode(std::string const &name);
}; };
Q_DECLARE_METATYPE(ArcDir) Q_DECLARE_METATYPE(ArcDir)
@ -47,8 +49,8 @@ struct ArcNode : public std::variant<ArcDir, ArcFile> {
ArcNode() = default; ArcNode() = default;
ArcNode(ArcDir &&t, std::string &&n, Arc::FileType ty = Arc::FileNormal); ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t = Arc::FileNormal);
ArcNode(ArcFile &&t, std::string &&n, Arc::FileType ty = Arc::FileNormal); ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t = Arc::FileNormal);
ArcNode(ArcNode const &) = default; ArcNode(ArcNode const &) = default;
ArcNode(ArcNode &&) = default; ArcNode(ArcNode &&) = default;

View File

@ -1,8 +1,5 @@
#include "quam/pak.h" #include "quam/pak.h"
#include <QIcon>
#include <QMetaEnum>
struct PakHeader { struct PakHeader {
quint32 dirOffset; quint32 dirOffset;
quint32 dirNum; quint32 dirNum;
@ -50,9 +47,7 @@ static PakEntry readPakEntry(std::istream &st) {
st.seekg(pos); st.seekg(pos);
std::string name; std::string name = ntbsToString(entName);
auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
std::copy(entName.cbegin(), zero, std::back_inserter(name));
if(name.front() == '/') { if(name.front() == '/') {
throw std::runtime_error("empty root directory name"); throw std::runtime_error("empty root directory name");
@ -64,7 +59,7 @@ static PakEntry readPakEntry(std::istream &st) {
return ent; return ent;
} }
void insertFile(ArcDir &dir, std::string name, ArcFile file) { static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
std::optional<std::string> next; std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) { if(auto slash = name.find('/'); slash != std::string::npos) {
@ -72,19 +67,16 @@ void insertFile(ArcDir &dir, std::string name, ArcFile file) {
name = name.substr(0, slash); name = name.substr(0, slash);
} }
auto existingNode = std::find_if(dir.begin(), dir.end(), auto existing = dir.findNode(name);
[&name](ArcNode const &node) {
return node.name == name;
});
if(next) { if(next) {
auto ref = auto ref =
existingNode != dir.end() existing != dir.end()
? *existingNode ? *existing
: dir.emplace_back(ArcDir{}, std::move(name)); : dir.emplace_back(ArcDir{}, std::move(name));
insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file)); insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file));
} else { } else {
if(existingNode != dir.end()) { if(existing != dir.end()) {
throw std::runtime_error("duplicate file"); throw std::runtime_error("duplicate file");
} }
dir.emplace_back(std::move(file), std::move(name)); dir.emplace_back(std::move(file), std::move(name));

81
source/quam/wad.cc Normal file
View File

@ -0,0 +1,81 @@
#include "quam/wad.h"
namespace Wad {
enum Compression {
CompressNone,
CompressLZSS,
};
}
struct WadHeader {
quint32 dirOffset;
quint32 dirNum;
};
struct WadEntry {
std::string name;
Arc::FileType type;
ArcFile file;
};
static WadHeader readWadHeader(std::istream &st) {
auto magic = readBytes<4>(st);
if(magic != std::array{'W', 'A', 'D', '2'}) {
throw std::runtime_error("not a wad2 file (invalid magic number)");
}
WadHeader hdr;
hdr.dirOffset = readLE<quint32>(st);
hdr.dirNum = readLE<quint32>(st);
return hdr;
}
static WadEntry readWadEntry(std::istream &st) {
auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st);
auto entCSize = readLE<quint32>(st);
auto entType = readByte(st);
auto entCompr = readByte(st);
/* padding */ readBytes<2>(st);
auto entName = readBytes<16>(st);
if(entSize != entCSize || entCompr != Wad::CompressNone) {
throw std::runtime_error("compressed files not implemented");
}
auto pos = st.tellg();
st.seekg(entOffset);
ArcFile file;
file.resize(entSize);
st.read(file.data(), entSize);
st.seekg(pos);
WadEntry ent;
ent.name = ntbsToString(entName);
ent.file = std::move(file);
ent.type = Arc::getFileType(entType);
return ent;
}
ArcDir readWad(std::istream &st) {
auto hdr = readWadHeader(st);
st.seekg(hdr.dirOffset);
ArcDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readWadEntry(st);
if(root.findNode(ent.name) != root.end()) {
throw std::runtime_error("duplicate file");
}
root.emplace_back(std::move(ent.file), std::move(ent.name), ent.type);
}
return root;
}
// EOF

8
source/quam/wad.h Normal file
View File

@ -0,0 +1,8 @@
#pragma once
#include "common.h"
#include "quam/archive.h"
ArcDir readWad(std::istream &st);
// EOF