This commit is contained in:
alison wright 2019-10-04 13:37:34 -04:00
parent efdf96055e
commit 18aa69f573
13 changed files with 163 additions and 82 deletions

View File

@ -47,13 +47,13 @@ add_executable(
source/quam/main_window.cc source/quam/main_window.cc
source/quam/main_window.h source/quam/main_window.h
source/quam/main_window.ui source/quam/main_window.ui
source/quam/pak.cc source/quam/pack.cc
source/quam/pak.h source/quam/pack.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/wad2.cc
source/quam/wad.h) source/quam/wad2.h)
make_qt_project(quam) make_qt_project(quam)

View File

@ -19,6 +19,14 @@
#include <utility> #include <utility>
#include <variant> #include <variant>
struct Error : public std::runtime_error {
using std::runtime_error::runtime_error;
};
struct EnumError : public Error {using Error::Error;};
struct FileFormatError : public Error {using Error::Error;};
struct UnimplementedError : public Error {using Error::Error;};
static inline QString trMain(char const *sourceText, static inline QString trMain(char const *sourceText,
char const *disambiguation = nullptr, char const *disambiguation = nullptr,
int n = -1) { int n = -1) {
@ -52,6 +60,18 @@ static inline std::array<char, N> readBytes(std::istream &st) {
return std::move(b); return std::move(b);
} }
static inline std::ifstream openReadBin(std::filesystem::path path) {
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;
}
static inline QDebug operator<<(QDebug debug, std::string const &t) { static inline QDebug operator<<(QDebug debug, std::string const &t) {
debug << QString::fromStdString(t); debug << QString::fromStdString(t);
return debug; return debug;
@ -70,16 +90,4 @@ static inline QDebug operator<<(QDebug debug,
return debug; return debug;
} }
static inline std::ifstream openReadBin(std::filesystem::path path) {
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

@ -1,12 +1,40 @@
#include "quam/archive.h" #include "quam/archive.h"
#include "quam/pack.h"
#include "quam/wad2.h"
#include <QIcon>
#include <QMetaEnum> #include <QMetaEnum>
Arc::FileType Arc::getFileType(int n) { Arc::FileType Arc::getFileType(int n) {
if(int t = QMetaEnum::fromType<Arc::FileType>().value(n); n != -1) { if(int t = QMetaEnum::fromType<Arc::FileType>().value(n); n != -1) {
return Arc::FileType(t); return Arc::FileType(t);
} else { } else {
throw std::range_error("invalid file type"); throw EnumError("invalid file type");
}
}
Arc::ArchiveType Arc::getArchiveType(std::istream &st) noexcept {
Arc::ArchiveType ret = Arc::ArcNone;
try {
auto pos = st.tellg();
auto magic = readBytes<4>(st);
if(magic == std::array{'P', 'A', 'C', 'K'}) {
ret = Arc::ArcPack;
} else if(magic == std::array{'W', 'A', 'D', '2'}) {
ret = Arc::ArcWad2;
}
st.seekg(pos);
} catch(...) {
}
return ret;
}
ArcDir Arc::readArchive(std::istream &st) {
switch(Arc::getArchiveType(st)) {
case Arc::ArcPack: return readPack(st);
case Arc::ArcWad2: return readWad2(st);
case Arc::ArcNone: throw FileFormatError("not an archive");
} }
} }
@ -30,28 +58,27 @@ ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
{ {
} }
/* ArcModel::ArcModel(ArcDir const *root, QObject *parent) :
PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
QAbstractItemModel{parent}, QAbstractItemModel{parent},
m_root{root} m_root{root}
{ {
} }
PakDirModel::~PakDirModel() { ArcModel::~ArcModel() {
} }
QVariant PakDirModel::data(QModelIndex const &index, int role) const { QVariant ArcModel::data(QModelIndex const &index, int role) const {
if(!index.isValid()) { if(!index.isValid()) {
return QVariant{}; return QVariant{};
} }
auto node = static_cast<PakNode *>(index.internalPointer()); auto node = static_cast<ArcNode *>(index.internalPointer());
switch(role) { switch(role) {
case Qt::DecorationRole: case Qt::DecorationRole:
if(index.column() == Arc::ColumnName) { if(index.column() == Arc::ColumnName) {
auto icon = auto icon =
std::holds_alternative<PakDir>(*node) ? "folder" std::holds_alternative<ArcDir>(*node) ? "folder"
: "text-x-generic"; : "text-x-generic";
return QVariant{QIcon::fromTheme(icon)}; return QVariant{QIcon::fromTheme(icon)};
} }
@ -59,7 +86,7 @@ QVariant PakDirModel::data(QModelIndex const &index, int role) const {
case Qt::DisplayRole: case Qt::DisplayRole:
switch(index.column()) { switch(index.column()) {
case Arc::ColumnSize: case Arc::ColumnSize:
if(auto file = std::get_if<PakFile>(node)) { if(auto file = std::get_if<ArcFile>(node)) {
return QVariant{QString::number(file->size())}; return QVariant{QString::number(file->size())};
} }
break; break;
@ -72,7 +99,7 @@ QVariant PakDirModel::data(QModelIndex const &index, int role) const {
return QVariant{}; return QVariant{};
} }
Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const { Qt::ItemFlags ArcModel::flags(QModelIndex const &index) const {
if(!index.isValid()) { if(!index.isValid()) {
return Qt::NoItemFlags; return Qt::NoItemFlags;
} else { } else {
@ -83,7 +110,7 @@ Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const {
} }
} }
QVariant PakDirModel::headerData(int section, QVariant ArcModel::headerData(int section,
Qt::Orientation orientation, Qt::Orientation orientation,
int role) const { int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) { if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
@ -95,7 +122,7 @@ QVariant PakDirModel::headerData(int section,
return QVariant{}; return QVariant{};
} }
QModelIndex PakDirModel::index(int row, QModelIndex ArcModel::index(int row,
int col, int col,
QModelIndex const &parent) const { QModelIndex const &parent) const {
if(!hasIndex(row, col, parent) || row > m_root->size()) { if(!hasIndex(row, col, parent) || row > m_root->size()) {
@ -103,21 +130,20 @@ QModelIndex PakDirModel::index(int row,
} else { } else {
// despite index data being const, this function does not take a const // despite index data being const, this function does not take a const
// pointer, which is very annoying! // pointer, which is very annoying!
return createIndex(row, col, const_cast<PakNode *>(&m_root->at(row))); return createIndex(row, col, const_cast<ArcNode *>(&m_root->at(row)));
} }
} }
QModelIndex PakDirModel::parent(QModelIndex const &) const { QModelIndex ArcModel::parent(QModelIndex const &) const {
return QModelIndex{}; return QModelIndex{};
} }
int PakDirModel::rowCount(QModelIndex const &) const { int ArcModel::rowCount(QModelIndex const &) const {
return m_root->size(); return m_root->size();
} }
int PakDirModel::columnCount(QModelIndex const &) const { int ArcModel::columnCount(QModelIndex const &) const {
return Arc::ColumnMax; return Arc::ColumnMax;
} }
*/
// EOF // EOF

View File

@ -2,8 +2,11 @@
#include "common.h" #include "common.h"
#include <QObject> #include <QAbstractItemModel>
#include <QByteArray> #include <QByteArray>
#include <QObject>
struct ArcDir;
namespace Arc { namespace Arc {
Q_NAMESPACE Q_NAMESPACE
@ -27,7 +30,17 @@ namespace Arc {
}; };
Q_ENUM_NS(FileType) Q_ENUM_NS(FileType)
enum ArchiveType {
ArcNone,
ArcPack,
ArcWad2,
};
Q_ENUM_NS(ArchiveType)
Arc::FileType getFileType(int n); Arc::FileType getFileType(int n);
Arc::ArchiveType getArchiveType(std::istream &st) noexcept;
ArcDir readArchive(std::istream &st);
} }
struct ArcNode; struct ArcNode;
@ -60,4 +73,25 @@ struct ArcNode : public std::variant<ArcDir, ArcFile> {
}; };
Q_DECLARE_METATYPE(ArcNode) Q_DECLARE_METATYPE(ArcNode)
class ArcModel : public QAbstractItemModel {
Q_OBJECT
public:
explicit ArcModel(ArcDir const *root, QObject *parent);
~ArcModel();
QVariant data(QModelIndex const &index, int role) const override;
Qt::ItemFlags flags(QModelIndex const &index) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role)
const override;
QModelIndex index(int row, int col, QModelIndex const &parent)
const override;
QModelIndex parent(QModelIndex const &) const override;
int rowCount(QModelIndex const &) const override;
int columnCount(QModelIndex const &) const override;
private:
ArcDir const *const m_root;
};
// EOF // EOF

View File

@ -1,6 +1,5 @@
#include "common.h" #include "quam/archive.h"
#include "quam/main_window.h" #include "quam/main_window.h"
#include "quam/pak.h"
#include <QApplication> #include <QApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
@ -44,9 +43,9 @@ static int modeText(int argc, char *argv[]) {
auto fileName = par.value(fileNameOpt).toStdString(); auto fileName = par.value(fileNameOpt).toStdString();
auto st = openReadBin(fileName); auto st = openReadBin(fileName);
auto pak = readPak(st); auto arc = Arc::readArchive(st);
qDebug() << pak; qDebug() << arc;
return 0; return 0;
} }

View File

@ -1,4 +1,6 @@
#include "quam/main_window.h" #include "quam/main_window.h"
#include "quam/archive.h"
#include "quam/project.h" #include "quam/project.h"
#include <QErrorMessage> #include <QErrorMessage>
@ -25,16 +27,16 @@ void MainWindow::fileOpen() {
this, this,
tr("Open Archive"), tr("Open Archive"),
QString{}, QString{},
tr("Quake PACK file (*.pak);;" tr("Quake archive file (*.pak *.wad);;"
"Quake PACK file (*.pak);;"
"Quake WAD2 file (*.wad);;" "Quake WAD2 file (*.wad);;"
"All files (*)")); "All files (*)"));
if(!fileName.isEmpty()) { if(!fileName.isEmpty()) {
try { try {
auto st = openReadBin(fileName.toStdString()); auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st); auto arc = Arc::readArchive(st);
Q_UNUSED(pak); new Project{std::move(arc), m_errors, mdiArea};
//new Project{std::move(pak), m_errors, mdiArea};
} catch(std::exception const &exc) { } catch(std::exception const &exc) {
m_errors->showMessage(tr(exc.what())); m_errors->showMessage(tr(exc.what()));
} }

View File

@ -1,7 +1,7 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "quam/pak.h"
#include "quam/ui_main_window.h" #include "quam/ui_main_window.h"
#include <QMainWindow> #include <QMainWindow>

View File

@ -1,38 +1,38 @@
#include "quam/pak.h" #include "quam/pack.h"
struct PakHeader { struct PackHeader {
quint32 dirOffset; quint32 dirOffset;
quint32 dirNum; quint32 dirNum;
}; };
struct PakEntry { struct PackEntry {
std::string name; std::string name;
ArcFile file; ArcFile file;
}; };
static constexpr quint32 sizeOfPakEntry = 64; static constexpr quint32 sizeOfPackEntry = 64;
static PakHeader readPakHeader(std::istream &st) { static PackHeader readPackHeader(std::istream &st) {
auto magic = readBytes<4>(st); auto magic = readBytes<4>(st);
if(magic != std::array{'P', 'A', 'C', 'K'}) { if(magic != std::array{'P', 'A', 'C', 'K'}) {
throw std::runtime_error("not a pak file (invalid magic number)"); throw FileFormatError("not a pak file (invalid magic number)");
} }
auto dirOffset = readLE<quint32>(st); auto dirOffset = readLE<quint32>(st);
auto dirSize = readLE<quint32>(st); auto dirSize = readLE<quint32>(st);
if(dirSize % sizeOfPakEntry != 0) { if(dirSize % sizeOfPackEntry != 0) {
throw std::runtime_error("invalid directory size"); throw FileFormatError("invalid directory size");
} }
PakHeader hdr; PackHeader hdr;
hdr.dirOffset = dirOffset; hdr.dirOffset = dirOffset;
hdr.dirNum = dirSize / sizeOfPakEntry; hdr.dirNum = dirSize / sizeOfPackEntry;
return hdr; return hdr;
} }
static PakEntry readPakEntry(std::istream &st) { static PackEntry readPackEntry(std::istream &st) {
auto entName = readBytes<56>(st); auto entName = readBytes<56>(st);
auto entOffset = readLE<quint32>(st); auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st); auto entSize = readLE<quint32>(st);
@ -50,10 +50,10 @@ static PakEntry readPakEntry(std::istream &st) {
std::string name = ntbsToString(entName); std::string name = ntbsToString(entName);
if(name.front() == '/') { if(name.front() == '/') {
throw std::runtime_error("empty root directory name"); throw FileFormatError("empty root directory name");
} }
PakEntry ent; PackEntry ent;
ent.name = std::move(name); ent.name = std::move(name);
ent.file = std::move(file); ent.file = std::move(file);
return ent; return ent;
@ -77,20 +77,20 @@ static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
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(existing != dir.end()) { if(existing != dir.end()) {
throw std::runtime_error("duplicate file"); throw FileFormatError("duplicate file");
} }
dir.emplace_back(std::move(file), std::move(name)); dir.emplace_back(std::move(file), std::move(name));
} }
} }
ArcDir readPak(std::istream &st) { ArcDir readPack(std::istream &st) {
auto hdr = readPakHeader(st); auto hdr = readPackHeader(st);
st.seekg(hdr.dirOffset); st.seekg(hdr.dirOffset);
ArcDir root; ArcDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st); auto ent = readPackEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.file)); insertFile(root, std::move(ent.name), std::move(ent.file));
} }

View File

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

View File

@ -5,16 +5,22 @@
#include <QMdiArea> #include <QMdiArea>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
Project::Project(QErrorMessage *errors, QMdiArea *parent) : Project::Project(ArcDir &&arc, QErrorMessage *errors, QMdiArea *parent) :
QMdiSubWindow{parent}, QMdiSubWindow{parent},
Ui::Project{}, Ui::Project{},
m_errors{errors} m_arc{std::move(arc)},
m_errors{errors},
m_model{new ArcModel{&m_arc, this}},
m_sorter{new QSortFilterProxyModel{this}}
{ {
auto widget = new QWidget(this); auto widget = new QWidget(this);
setupUi(widget); setupUi(widget);
setWidget(widget); setWidget(widget);
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
showMaximized(); showMaximized();
m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter);
} }
Project::~Project() { Project::~Project() {

View File

@ -1,7 +1,8 @@
#pragma once #pragma once
#include "common.h" #include "common.h"
#include "quam/pak.h"
#include "quam/archive.h"
#include "quam/ui_project.h" #include "quam/ui_project.h"
#include <QMainWindow> #include <QMainWindow>
@ -16,10 +17,11 @@ class Project : public QMdiSubWindow, private Ui::Project {
Q_OBJECT Q_OBJECT
public: public:
explicit Project(QErrorMessage *errors, QMdiArea *parent); explicit Project(ArcDir &&arc, QErrorMessage *errors, QMdiArea *parent);
virtual ~Project(); virtual ~Project();
private: private:
ArcDir m_arc;
QErrorMessage *m_errors; QErrorMessage *m_errors;
QAbstractItemModel *m_model; QAbstractItemModel *m_model;
QSortFilterProxyModel *m_sorter; QSortFilterProxyModel *m_sorter;

View File

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

View File

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