Compare commits

...

2 Commits

Author SHA1 Message Date
efdf96055e add wad2 file code 2019-10-03 17:06:05 -04:00
f42028a378 piss 2019-10-03 15:17:16 -04:00
12 changed files with 316 additions and 229 deletions

View File

@ -41,6 +41,8 @@ find_package(
add_executable(
quam WIN32
source/common.h
source/quam/archive.cc
source/quam/archive.h
source/quam/main.cc
source/quam/main_window.cc
source/quam/main_window.h
@ -49,7 +51,9 @@ add_executable(
source/quam/pak.h
source/quam/project.cc
source/quam/project.h
source/quam/project.ui)
source/quam/project.ui
source/quam/wad.cc
source/quam/wad.h)
make_qt_project(quam)

View File

@ -25,6 +25,12 @@ static inline QString trMain(char const *sourceText,
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>
static inline T readLE(std::istream &st) {
std::array<char, sizeof(T)> b;
@ -58,8 +64,8 @@ static inline QDebug operator<<(QDebug debug, std::unique_ptr<T> const &t) {
}
template<typename T0, typename... Ts>
static inline QDebug operator <<(QDebug debug,
std::variant<T0, Ts...> const &t) {
static inline QDebug operator<<(QDebug debug,
std::variant<T0, Ts...> const &t) {
std::visit([&](auto &&arg) {debug << arg;}, t);
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};
}
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

123
source/quam/archive.cc Normal file
View File

@ -0,0 +1,123 @@
#include "quam/archive.h"
#include <QMetaEnum>
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");
}
}
ArcDir::iterator ArcDir::findNode(std::string const &name) {
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)),
type(t)
{
}
ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
super_type(std::move(f)),
name(std::move(n)),
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

63
source/quam/archive.h Normal file
View File

@ -0,0 +1,63 @@
#pragma once
#include "common.h"
#include <QObject>
#include <QByteArray>
namespace Arc {
Q_NAMESPACE
enum Column {
ColumnSize,
ColumnName,
ColumnMax,
};
Q_ENUM_NS(Column)
enum FileType {
FileNormal,
FileLabel,
FilePalette = 64,
FileTexture,
FilePicture,
FileSound,
FileMipTexture,
};
Q_ENUM_NS(FileType)
Arc::FileType getFileType(int n);
}
struct ArcNode;
struct ArcDir : public std::vector<ArcNode> {
using std::vector<ArcNode>::vector;
ArcDir::iterator findNode(std::string const &name);
};
Q_DECLARE_METATYPE(ArcDir)
struct ArcFile : public QByteArray {
using QByteArray::QByteArray;
};
Q_DECLARE_METATYPE(ArcFile)
struct ArcNode : public std::variant<ArcDir, ArcFile> {
using super_type = std::variant<ArcDir, ArcFile>;
ArcNode() = default;
ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t = Arc::FileNormal);
ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t = Arc::FileNormal);
ArcNode(ArcNode const &) = default;
ArcNode(ArcNode &&) = default;
std::string name;
Arc::FileType type;
};
Q_DECLARE_METATYPE(ArcNode)
// EOF

View File

@ -1,4 +1,3 @@
#include "common.h"
#include "quam/main_window.h"
#include "quam/project.h"
@ -34,7 +33,8 @@ void MainWindow::fileOpen() {
try {
auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st);
new Project{std::move(pak), m_errors, mdiArea};
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,5 +1,6 @@
#pragma once
#include "common.h"
#include "quam/pak.h"
#include "quam/ui_main_window.h"

View File

@ -1,9 +1,5 @@
#include "common.h"
#include "quam/pak.h"
#include <QIcon>
#include <QMetaEnum>
struct PakHeader {
quint32 dirOffset;
quint32 dirNum;
@ -11,7 +7,7 @@ struct PakHeader {
struct PakEntry {
std::string name;
PakFile file;
ArcFile file;
};
static constexpr quint32 sizeOfPakEntry = 64;
@ -45,16 +41,13 @@ static PakEntry readPakEntry(std::istream &st) {
st.seekg(entOffset);
PakFile file;
ArcFile file;
file.resize(entSize);
st.read(file.data(), entSize);
st.seekg(pos);
auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
std::string name;
std::copy(entName.cbegin(), zero, std::back_inserter(name));
std::string name = ntbsToString(entName);
if(name.front() == '/') {
throw std::runtime_error("empty root directory name");
@ -66,7 +59,7 @@ static PakEntry readPakEntry(std::istream &st) {
return ent;
}
void insertFile(PakDir &dir, std::string name, PakFile file) {
static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
std::optional<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) {
@ -74,30 +67,27 @@ void insertFile(PakDir &dir, std::string name, PakFile file) {
name = name.substr(0, slash);
}
auto existingNode = std::find_if(dir.begin(), dir.end(),
[&name](PakNode const &node) {
return node.name == name;
});
auto existing = dir.findNode(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));
existing != dir.end()
? *existing
: dir.emplace_back(ArcDir{}, std::move(name));
insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file));
} else {
if(existingNode != dir.end()) {
if(existing != dir.end()) {
throw std::runtime_error("duplicate file");
}
dir.emplace_back(std::move(file), std::move(name));
}
}
PakDir readPak(std::istream &st) {
ArcDir readPak(std::istream &st) {
auto hdr = readPakHeader(st);
st.seekg(hdr.dirOffset);
PakDir root;
ArcDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st);
@ -107,105 +97,4 @@ PakDir readPak(std::istream &st) {
return root;
}
PakDirRoot::PakDirRoot(PakDir &&root, QObject *parent) :
QObject{parent},
PakDir{std::move(root)}
{
}
PakDirRoot::~PakDirRoot() {
}
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());
auto col = Pak::Column(index.column());
switch(role) {
case Qt::DecorationRole:
if(col == Pak::Column::Name) {
auto icon =
std::holds_alternative<PakDir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
}
break;
case Qt::DisplayRole:
switch(col) {
case Pak::Column::Size:
if(auto file = std::get_if<PakFile>(node)) {
return QVariant{QString::number(file->size())};
}
break;
case Pak::Column::Name:
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(Pak::Column(section)) {
case Pak::Column::Size: return QVariant{tr("Size")};
case Pak::Column::Name: 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 QMetaEnum::fromType<Pak::Column>().keyCount();
}
PakDirModelSorter::~PakDirModelSorter() {
}
// EOF

View File

@ -1,83 +1,8 @@
#pragma once
#include <QAbstractItemModel>
#include <QSortFilterProxyModel>
#include "common.h"
#include "quam/archive.h"
namespace Pak {
Q_NAMESPACE
enum class Column {
Size,
Name,
};
Q_ENUM_NS(Column)
}
struct PakNode;
struct PakDir : public std::vector<PakNode> {
using std::vector<PakNode>::vector;
};
Q_DECLARE_METATYPE(PakDir)
struct PakFile : public QByteArray {
using QByteArray::QByteArray;
};
Q_DECLARE_METATYPE(PakFile)
struct PakNode : public std::variant<PakDir, PakFile> {
PakNode() = default;
template<typename T>
constexpr PakNode(T &&t, std::string &&n) :
variant(std::move(t)),
name(std::move(n)) {
}
PakNode(PakNode const &) = default;
PakNode(PakNode &&) = default;
std::string name;
};
Q_DECLARE_METATYPE(PakNode)
class PakDirRoot : public QObject, public PakDir {
Q_OBJECT
public:
explicit PakDirRoot(PakDir &&root, QObject *parent);
virtual ~PakDirRoot();
};
class PakDirModel : public QAbstractItemModel {
Q_OBJECT
public:
explicit PakDirModel(PakDir const *root, QObject *parent);
virtual ~PakDirModel();
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 &index) const override;
int rowCount(QModelIndex const &parent) const override;
int columnCount(QModelIndex const &parent) const override;
private:
PakDir const *const m_root;
};
class PakDirModelSorter : public QSortFilterProxyModel {
Q_OBJECT
public:
using QSortFilterProxyModel::QSortFilterProxyModel;
virtual ~PakDirModelSorter();
};
PakDir readPak(std::istream &st);
ArcDir readPak(std::istream &st);
// EOF

View File

@ -1,4 +1,3 @@
#include "common.h"
#include "quam/project.h"
#include <QAbstractItemModel>
@ -18,24 +17,7 @@ Project::Project(QErrorMessage *errors, QMdiArea *parent) :
showMaximized();
}
Project::Project(PakDir &&dir, QErrorMessage *errors, QMdiArea *parent) :
Project{errors, parent}
{
auto root = new PakDirRoot{std::move(dir), this};
auto model = new PakDirModel{root, this};
setupModel(model);
}
Project::~Project() {
}
void Project::setupModel(QAbstractItemModel *model) {
m_model = model;
m_sorter = new QSortFilterProxyModel{this};
m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter);
tableView->sortByColumn(int(Pak::Column::Name), Qt::AscendingOrder);
tableView->resizeColumnsToContents();
}
// EOF

View File

@ -1,5 +1,6 @@
#pragma once
#include "common.h"
#include "quam/pak.h"
#include "quam/ui_project.h"
@ -15,14 +16,10 @@ class Project : public QMdiSubWindow, private Ui::Project {
Q_OBJECT
public:
explicit Project(PakDir &&dir, QErrorMessage *errors, QMdiArea *parent);
explicit Project(QErrorMessage *errors, QMdiArea *parent);
virtual ~Project();
private:
explicit Project(QErrorMessage *errors, QMdiArea *parent);
void setupModel(QAbstractItemModel *model);
QErrorMessage *m_errors;
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

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