Compare commits

..

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

12 changed files with 229 additions and 316 deletions

View File

@ -41,8 +41,6 @@ find_package(
add_executable( add_executable(
quam WIN32 quam WIN32
source/common.h source/common.h
source/quam/archive.cc
source/quam/archive.h
source/quam/main.cc source/quam/main.cc
source/quam/main_window.cc source/quam/main_window.cc
source/quam/main_window.h source/quam/main_window.h
@ -51,9 +49,7 @@ 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,12 +25,6 @@ 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;
@ -64,7 +58,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;
@ -74,12 +68,4 @@ 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

@ -1,123 +0,0 @@
#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

View File

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

View File

@ -1,5 +1,9 @@
#include "common.h"
#include "quam/pak.h" #include "quam/pak.h"
#include <QIcon>
#include <QMetaEnum>
struct PakHeader { struct PakHeader {
quint32 dirOffset; quint32 dirOffset;
quint32 dirNum; quint32 dirNum;
@ -7,7 +11,7 @@ struct PakHeader {
struct PakEntry { struct PakEntry {
std::string name; std::string name;
ArcFile file; PakFile file;
}; };
static constexpr quint32 sizeOfPakEntry = 64; static constexpr quint32 sizeOfPakEntry = 64;
@ -41,13 +45,16 @@ static PakEntry readPakEntry(std::istream &st) {
st.seekg(entOffset); st.seekg(entOffset);
ArcFile file; PakFile file;
file.resize(entSize); file.resize(entSize);
st.read(file.data(), entSize); st.read(file.data(), entSize);
st.seekg(pos); st.seekg(pos);
std::string name = ntbsToString(entName); auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
std::string name;
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");
@ -59,7 +66,7 @@ static PakEntry readPakEntry(std::istream &st) {
return ent; return ent;
} }
static void insertFile(ArcDir &dir, std::string name, ArcFile file) { void insertFile(PakDir &dir, std::string name, PakFile 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) {
@ -67,27 +74,30 @@ static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
name = name.substr(0, slash); name = name.substr(0, slash);
} }
auto existing = dir.findNode(name); auto existingNode = std::find_if(dir.begin(), dir.end(),
[&name](PakNode const &node) {
return node.name == name;
});
if(next) { if(next) {
auto ref = auto ref =
existing != dir.end() existingNode != dir.end()
? *existing ? *existingNode
: dir.emplace_back(ArcDir{}, std::move(name)); : dir.emplace_back(PakDir{}, std::move(name));
insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file)); insertFile(std::get<PakDir>(ref), *std::move(next), std::move(file));
} else { } else {
if(existing != dir.end()) { if(existingNode != 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));
} }
} }
ArcDir readPak(std::istream &st) { PakDir readPak(std::istream &st) {
auto hdr = readPakHeader(st); auto hdr = readPakHeader(st);
st.seekg(hdr.dirOffset); st.seekg(hdr.dirOffset);
ArcDir root; PakDir root;
for(quint32 i = 0; i < hdr.dirNum; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st); auto ent = readPakEntry(st);
@ -97,4 +107,105 @@ ArcDir readPak(std::istream &st) {
return root; 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 // EOF

View File

@ -1,8 +1,83 @@
#pragma once #pragma once
#include "common.h" #include <QAbstractItemModel>
#include "quam/archive.h" #include <QSortFilterProxyModel>
ArcDir readPak(std::istream &st); 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);
// EOF // EOF

View File

@ -1,3 +1,4 @@
#include "common.h"
#include "quam/project.h" #include "quam/project.h"
#include <QAbstractItemModel> #include <QAbstractItemModel>
@ -17,7 +18,24 @@ Project::Project(QErrorMessage *errors, QMdiArea *parent) :
showMaximized(); 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() { 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 // EOF

View File

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

View File

@ -1,81 +0,0 @@
#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,8 +0,0 @@
#pragma once
#include "common.h"
#include "quam/archive.h"
ArcDir readWad(std::istream &st);
// EOF