Compare commits

..

No commits in common. "dab41d87ef7a2d3b33f675f6b2a630ce5193f267" and "efdf96055ea497db940aba862138afcca8ed17a1" have entirely different histories.

14 changed files with 217 additions and 376 deletions

View File

@ -47,13 +47,13 @@ add_executable(
source/quam/main_window.cc
source/quam/main_window.h
source/quam/main_window.ui
source/quam/pack.cc
source/quam/pack.h
source/quam/pak.cc
source/quam/pak.h
source/quam/project.cc
source/quam/project.h
source/quam/project.ui
source/quam/wad2.cc
source/quam/wad2.h)
source/quam/wad.cc
source/quam/wad.h)
make_qt_project(quam)

View File

@ -5,7 +5,6 @@
#include <QCoreApplication>
#include <QDebug>
#include <QMetaEnum>
#include <algorithm>
#include <array>
@ -20,48 +19,6 @@
#include <utility>
#include <variant>
inline constexpr std::nullopt_t None{std::nullopt};
template<typename T>
using Option = std::optional<T>;
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;};
struct MemoryStreamBuf : public std::streambuf {
MemoryStreamBuf(char *base, std::size_t size) {
setg(base, base, base + size);
}
};
struct IMemoryStream : public virtual MemoryStreamBuf, public std::istream {
IMemoryStream(char *base, std::size_t size) :
MemoryStreamBuf{base, size},
std::istream(static_cast<std::streambuf *>(this))
{
}
IMemoryStream(char const *base, std::size_t size) :
IMemoryStream{const_cast<char *>(base), size}
{
}
IMemoryStream(QByteArray &bytes) :
IMemoryStream{bytes.data(), std::size_t(bytes.size())}
{
}
IMemoryStream(QByteArray const &bytes) :
IMemoryStream{bytes.data(), std::size_t(bytes.size())}
{
}
};
static inline QString trMain(char const *sourceText,
char const *disambiguation = nullptr,
int n = -1) {
@ -95,28 +52,6 @@ static inline std::array<char, N> readBytes(std::istream &st) {
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;
}
template<typename T>
static inline char const *enumToString(T const t) {
return QMetaEnum::fromType<T>().valueToKey(int(t));
}
template<typename T>
static inline int enumMax() {
return QMetaEnum::fromType<T>().keyCount();
}
static inline QDebug operator<<(QDebug debug, std::string const &t) {
debug << QString::fromStdString(t);
return debug;
@ -135,4 +70,16 @@ static inline QDebug operator<<(QDebug 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

View File

@ -1,87 +1,69 @@
#include "quam/archive.h"
#include "quam/pack.h"
#include "quam/wad2.h"
#include <QMetaEnum>
#include <QIcon>
namespace Arc {
Option<ArchiveType> getArchiveType(std::istream &st) noexcept {
try {
auto pos = st.tellg();
auto magic = readBytes<4>(st);
st.seekg(pos);
if(magic == std::array{'P', 'A', 'C', 'K'}) {return ArcPack;}
if(magic == std::array{'W', 'A', 'D', '2'}) {return ArcWad2;}
} catch(...) {
}
return None;
}
Dir readArchive(std::istream &st) {
switch(auto v = getArchiveType(st);
v.has_value() ? *v : throw FileFormatError("not an archive")) {
case ArcPack: return readPack(st);
case ArcWad2: return readWad2(st);
Arc::FileType Arc::getFileType(int n) {
if(int t = QMetaEnum::fromType<Arc::FileType>().value(n); n != -1) {
return Arc::FileType(t);
} else {
throw std::range_error("invalid file type");
}
}
Dir::iterator Dir::findNode(std::string const &name) {
return std::find_if(begin(), end(), [&name](Node const &node) {
ArcDir::iterator ArcDir::findNode(std::string const &name) {
return std::find_if(begin(), end(), [&name](ArcNode const &node) {
return node.name == name;
});
}
Node::Node(Dir &&f, std::string &&n, FileType t) :
ArcNode::ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(f)),
name(std::move(n)),
type(t)
{
}
Node::Node(File &&f, std::string &&n, FileType t) :
ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(f)),
name(std::move(n)),
type(t)
{
}
Model::Model(QObject *parent) :
/*
PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
QAbstractItemModel{parent},
m_dir{nullptr}
m_root{root}
{
}
Model::~Model() {
PakDirModel::~PakDirModel() {
}
QVariant Model::data(QModelIndex const &index, int role) const {
QVariant PakDirModel::data(QModelIndex const &index, int role) const {
if(!index.isValid()) {
return QVariant{};
}
auto node = static_cast<Node const *>(index.internalPointer());
auto node = static_cast<PakNode *>(index.internalPointer());
switch(role) {
case Qt::DecorationRole:
if(index.column() == ColumnName) {
if(index.column() == Arc::ColumnName) {
auto icon =
std::holds_alternative<Dir>(*node) ? "folder"
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(index.column()) {
case ColumnSize:
if(auto file = std::get_if<File>(node)) {
case Arc::ColumnSize:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case ColumnType:
return QVariant{tr(enumToString(node->type))};
case ColumnName:
case Arc::ColumnName:
return QVariant{tr(node->name.data())};
}
default:
@ -90,7 +72,7 @@ QVariant Model::data(QModelIndex const &index, int role) const {
return QVariant{};
}
Qt::ItemFlags Model::flags(QModelIndex const &index) const {
Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const {
if(!index.isValid()) {
return Qt::NoItemFlags;
} else {
@ -101,53 +83,41 @@ Qt::ItemFlags Model::flags(QModelIndex const &index) const {
}
}
QVariant Model::headerData(int section,
QVariant PakDirModel::headerData(int section,
Qt::Orientation orientation,
int role) const {
if(orientation == Qt::Horizontal && role == Qt::DisplayRole) {
switch(section) {
case ColumnSize: return QVariant{tr("Size")};
case ColumnType: return QVariant{tr("Type")};
case ColumnName: return QVariant{tr("Name")};
case Arc::ColumnSize: return QVariant{tr("Size")};
case Arc::ColumnName: return QVariant{tr("Name")};
}
}
return QVariant{};
}
QModelIndex Model::index(int row, int col, QModelIndex const &parent) const {
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
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<Node *>(&m_dir->at(row)));
return createIndex(row, col, const_cast<PakNode *>(&m_root->at(row)));
}
}
QModelIndex Model::parent(QModelIndex const &) const {
QModelIndex PakDirModel::parent(QModelIndex const &) const {
return QModelIndex{};
}
int Model::rowCount(QModelIndex const &) const {
return m_dir ? m_dir->size() : 0;
}
int Model::columnCount(QModelIndex const &) const {
return enumMax<Column>();
}
Dir *Model::dir() const {
return m_dir;
}
void Model::setDir(Dir *dir) {
if(dir != m_dir) {
m_dir = dir;
emit layoutChanged();
emit dirChanged(dir);
}
int PakDirModel::rowCount(QModelIndex const &) const {
return m_root->size();
}
int PakDirModel::columnCount(QModelIndex const &) const {
return Arc::ColumnMax;
}
*/
// EOF

View File

@ -2,102 +2,62 @@
#include "common.h"
#include <QAbstractItemModel>
#include <QByteArray>
#include <QObject>
#include <QByteArray>
namespace Arc {
Q_NAMESPACE
struct Node;
struct Dir;
enum Column {
ColumnSize,
ColumnType,
ColumnName,
ColumnMax,
};
Q_ENUM_NS(Column)
enum class FileType {
Label,
MipTexture,
Normal,
Palette,
Picture,
Sound,
Texture,
enum FileType {
FileNormal,
FileLabel,
FilePalette = 64,
FileTexture,
FilePicture,
FileSound,
FileMipTexture,
};
Q_ENUM_NS(FileType)
enum ArchiveType {
ArcPack,
ArcWad2,
};
Q_ENUM_NS(ArchiveType)
Arc::FileType getFileType(int n);
}
Option<ArchiveType> getArchiveType(std::istream &st) noexcept;
struct ArcNode;
Dir readArchive(std::istream &st);
struct ArcDir : public std::vector<ArcNode> {
using std::vector<ArcNode>::vector;
struct Dir : public std::vector<Node> {
using std::vector<Node>::vector;
ArcDir::iterator findNode(std::string const &name);
};
Q_DECLARE_METATYPE(ArcDir)
Dir::iterator findNode(std::string const &name);
};
struct File : public QByteArray {
struct ArcFile : public QByteArray {
using QByteArray::QByteArray;
};
};
Q_DECLARE_METATYPE(ArcFile)
struct Node : public std::variant<Dir, File> {
using super_type = std::variant<Dir, File>;
struct ArcNode : public std::variant<ArcDir, ArcFile> {
using super_type = std::variant<ArcDir, ArcFile>;
Node() = default;
ArcNode() = default;
Node(Dir &&f, std::string &&n, FileType t = FileType::Normal);
Node(File &&f, std::string &&n, FileType t = FileType::Normal);
ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t = Arc::FileNormal);
ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t = Arc::FileNormal);
Node(Node const &) = default;
Node(Node &&) = default;
ArcNode(ArcNode const &) = default;
ArcNode(ArcNode &&) = default;
std::string name;
FileType type;
};
class Model : public QAbstractItemModel {
Q_OBJECT
public:
explicit Model(QObject *parent);
~Model();
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 = QModelIndex())
const override;
QModelIndex parent(QModelIndex const &index = QModelIndex()) const override;
int rowCount(QModelIndex const &index = QModelIndex()) const override;
int columnCount(QModelIndex const &index = QModelIndex()) const override;
Dir *dir() const;
public slots:
void setDir(Dir *dir);
signals:
void dirChanged(Dir *dir);
private:
Dir *m_dir;
};
}
Q_DECLARE_METATYPE(Arc::Dir)
Q_DECLARE_METATYPE(Arc::File)
Q_DECLARE_METATYPE(Arc::Node)
Arc::FileType type;
};
Q_DECLARE_METATYPE(ArcNode)
// EOF

View File

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

View File

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

View File

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

View File

@ -1,38 +1,38 @@
#include "quam/pack.h"
#include "quam/pak.h"
struct PackHeader {
struct PakHeader {
quint32 dirOffset;
quint32 dirNum;
};
struct PackEntry {
struct PakEntry {
std::string name;
Arc::File file;
ArcFile file;
};
static constexpr quint32 sizeOfPackEntry = 64;
static constexpr quint32 sizeOfPakEntry = 64;
static PackHeader readPackHeader(std::istream &st) {
static PakHeader readPakHeader(std::istream &st) {
auto magic = readBytes<4>(st);
if(magic != std::array{'P', 'A', 'C', 'K'}) {
throw FileFormatError("not a pak file (invalid magic number)");
throw std::runtime_error("not a pak file (invalid magic number)");
}
auto dirOffset = readLE<quint32>(st);
auto dirSize = readLE<quint32>(st);
if(dirSize % sizeOfPackEntry != 0) {
throw FileFormatError("invalid directory size");
if(dirSize % sizeOfPakEntry != 0) {
throw std::runtime_error("invalid directory size");
}
PackHeader hdr;
PakHeader hdr;
hdr.dirOffset = dirOffset;
hdr.dirNum = dirSize / sizeOfPackEntry;
hdr.dirNum = dirSize / sizeOfPakEntry;
return hdr;
}
static PackEntry readPackEntry(std::istream &st) {
static PakEntry readPakEntry(std::istream &st) {
auto entName = readBytes<56>(st);
auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st);
@ -41,7 +41,7 @@ static PackEntry readPackEntry(std::istream &st) {
st.seekg(entOffset);
Arc::File file;
ArcFile file;
file.resize(entSize);
st.read(file.data(), entSize);
@ -50,17 +50,17 @@ static PackEntry readPackEntry(std::istream &st) {
std::string name = ntbsToString(entName);
if(name.front() == '/') {
throw FileFormatError("empty root directory name");
throw std::runtime_error("empty root directory name");
}
PackEntry ent;
PakEntry ent;
ent.name = std::move(name);
ent.file = std::move(file);
return ent;
}
static void insertFile(Arc::Dir &dir, std::string name, Arc::File file) {
Option<std::string> next;
static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) {
next = name.substr(slash + 1);
@ -73,24 +73,24 @@ static void insertFile(Arc::Dir &dir, std::string name, Arc::File file) {
auto ref =
existing != dir.end()
? *existing
: dir.emplace_back(Arc::Dir{}, std::move(name));
insertFile(std::get<Arc::Dir>(ref), *std::move(next), std::move(file));
: dir.emplace_back(ArcDir{}, std::move(name));
insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file));
} else {
if(existing != dir.end()) {
throw FileFormatError("duplicate file");
throw std::runtime_error("duplicate file");
}
dir.emplace_back(std::move(file), std::move(name));
}
}
Arc::Dir readPack(std::istream &st) {
auto hdr = readPackHeader(st);
ArcDir readPak(std::istream &st) {
auto hdr = readPakHeader(st);
st.seekg(hdr.dirOffset);
Arc::Dir root;
ArcDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPackEntry(st);
auto ent = readPakEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.file));
}

View File

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

View File

@ -5,32 +5,19 @@
#include <QMdiArea>
#include <QSortFilterProxyModel>
Project::Project(Arc::Dir &&arc, QErrorMessage *errors, QMdiArea *parent) :
Project::Project(QErrorMessage *errors, QMdiArea *parent) :
QMdiSubWindow{parent},
Ui::Project{},
m_arc{std::move(arc)},
m_errors{errors},
m_model{new Arc::Model{this}},
m_sorter{new QSortFilterProxyModel{this}}
m_errors{errors}
{
auto widget = new QWidget(this);
setupUi(widget);
setWidget(widget);
setAttribute(Qt::WA_DeleteOnClose);
showMaximized();
connect(m_model, &Arc::Model::dirChanged, this, &Project::dirChanged);
m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter);
m_model->setDir(&m_arc);
}
Project::~Project() {
}
void Project::dirChanged(Arc::Dir *) {
tableView->resizeColumnsToContents();
}
// EOF

View File

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

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

View File

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

View File

@ -1,96 +0,0 @@
#include "quam/wad2.h"
namespace Wad2 {
enum Compression {
CompressNone,
CompressLZSS,
};
}
struct Wad2Header {
quint32 dirNum;
quint32 dirOffset;
};
struct Wad2Entry {
std::string name;
Arc::FileType type;
Arc::File file;
};
static Arc::FileType getFileType(int n) {
switch(n) {
case 0: return Arc::FileType::Normal;
case 1: return Arc::FileType::Label;
case 64: return Arc::FileType::Palette;
case 65: return Arc::FileType::Texture;
case 66: return Arc::FileType::Picture;
case 67: return Arc::FileType::Sound;
case 68: return Arc::FileType::MipTexture;
default: throw EnumError("invalid file type");
}
}
static Wad2Header readWad2Header(std::istream &st) {
auto magic = readBytes<4>(st);
if(magic != std::array{'W', 'A', 'D', '2'}) {
throw FileFormatError("not a wad2 file (invalid magic number)");
}
Wad2Header hdr;
hdr.dirNum = readLE<quint32>(st);
hdr.dirOffset = readLE<quint32>(st);
return hdr;
}
static Wad2Entry readWad2Entry(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 != Wad2::CompressNone) {
throw UnimplementedError("compressed files not supported");
}
auto pos = st.tellg();
st.seekg(entOffset);
Arc::File file;
file.resize(entSize);
st.read(file.data(), entSize);
st.seekg(pos);
Wad2Entry ent;
ent.name = ntbsToString(entName);
ent.file = std::move(file);
ent.type = getFileType(entType);
return ent;
}
Arc::Dir readWad2(std::istream &st) {
auto hdr = readWad2Header(st);
st.seekg(hdr.dirOffset);
Arc::Dir root;
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readWad2Entry(st);
/*
if(root.findNode(ent.name) != root.end()) {
throw FileFormatError("duplicate file");
}
*/
root.emplace_back(std::move(ent.file), std::move(ent.name), ent.type);
}
return root;
}
// EOF