Compare commits

...

2 Commits

14 changed files with 376 additions and 217 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

@ -5,6 +5,7 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug> #include <QDebug>
#include <QMetaEnum>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
@ -19,6 +20,48 @@
#include <utility> #include <utility>
#include <variant> #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, static inline QString trMain(char const *sourceText,
char const *disambiguation = nullptr, char const *disambiguation = nullptr,
int n = -1) { int n = -1) {
@ -52,6 +95,28 @@ 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;
}
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) { static inline QDebug operator<<(QDebug debug, std::string const &t) {
debug << QString::fromStdString(t); debug << QString::fromStdString(t);
return debug; return debug;
@ -70,16 +135,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,69 +1,87 @@
#include "quam/archive.h" #include "quam/archive.h"
#include <QMetaEnum> #include "quam/pack.h"
#include "quam/wad2.h"
Arc::FileType Arc::getFileType(int n) { #include <QIcon>
if(int t = QMetaEnum::fromType<Arc::FileType>().value(n); n != -1) {
return Arc::FileType(t); namespace Arc {
} else {
throw std::range_error("invalid file type"); 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);
} }
} }
ArcDir::iterator ArcDir::findNode(std::string const &name) { Dir::iterator Dir::findNode(std::string const &name) {
return std::find_if(begin(), end(), [&name](ArcNode const &node) { return std::find_if(begin(), end(), [&name](Node const &node) {
return node.name == name; return node.name == name;
}); });
} }
ArcNode::ArcNode(ArcDir &&f, std::string &&n, Arc::FileType t) : Node::Node(Dir &&f, std::string &&n, FileType t) :
super_type(std::move(f)), super_type(std::move(f)),
name(std::move(n)), name(std::move(n)),
type(t) type(t)
{ {
} }
ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) : Node::Node(File &&f, std::string &&n, FileType t) :
super_type(std::move(f)), super_type(std::move(f)),
name(std::move(n)), name(std::move(n)),
type(t) type(t)
{ {
} }
/* Model::Model(QObject *parent) :
PakDirModel::PakDirModel(PakDir const *root, QObject *parent) :
QAbstractItemModel{parent}, QAbstractItemModel{parent},
m_root{root} m_dir{nullptr}
{ {
} }
PakDirModel::~PakDirModel() { Model::~Model() {
} }
QVariant PakDirModel::data(QModelIndex const &index, int role) const { QVariant Model::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<Node const *>(index.internalPointer());
switch(role) { switch(role) {
case Qt::DecorationRole: case Qt::DecorationRole:
if(index.column() == Arc::ColumnName) { if(index.column() == ColumnName) {
auto icon = auto icon =
std::holds_alternative<PakDir>(*node) ? "folder" std::holds_alternative<Dir>(*node) ? "folder"
: "text-x-generic"; : "text-x-generic";
return QVariant{QIcon::fromTheme(icon)}; return QVariant{QIcon::fromTheme(icon)};
} }
break; break;
case Qt::DisplayRole: case Qt::DisplayRole:
switch(index.column()) { switch(index.column()) {
case Arc::ColumnSize: case ColumnSize:
if(auto file = std::get_if<PakFile>(node)) { if(auto file = std::get_if<File>(node)) {
return QVariant{QString::number(file->size())}; return QVariant{QString::number(file->size())};
} }
break; break;
case Arc::ColumnName: case ColumnType:
return QVariant{tr(enumToString(node->type))};
case ColumnName:
return QVariant{tr(node->name.data())}; return QVariant{tr(node->name.data())};
} }
default: default:
@ -72,7 +90,7 @@ QVariant PakDirModel::data(QModelIndex const &index, int role) const {
return QVariant{}; return QVariant{};
} }
Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const { Qt::ItemFlags Model::flags(QModelIndex const &index) const {
if(!index.isValid()) { if(!index.isValid()) {
return Qt::NoItemFlags; return Qt::NoItemFlags;
} else { } else {
@ -83,41 +101,53 @@ Qt::ItemFlags PakDirModel::flags(QModelIndex const &index) const {
} }
} }
QVariant PakDirModel::headerData(int section, QVariant Model::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) {
switch(section) { switch(section) {
case Arc::ColumnSize: return QVariant{tr("Size")}; case ColumnSize: return QVariant{tr("Size")};
case Arc::ColumnName: return QVariant{tr("Name")}; case ColumnType: return QVariant{tr("Type")};
case ColumnName: return QVariant{tr("Name")};
} }
} }
return QVariant{}; return QVariant{};
} }
QModelIndex PakDirModel::index(int row, QModelIndex Model::index(int row, int col, QModelIndex const &parent) const {
int col, if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
QModelIndex const &parent) const {
if(!hasIndex(row, col, parent) || row > m_root->size()) {
return QModelIndex{}; return QModelIndex{};
} 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<Node *>(&m_dir->at(row)));
} }
} }
QModelIndex PakDirModel::parent(QModelIndex const &) const { QModelIndex Model::parent(QModelIndex const &) const {
return QModelIndex{}; return QModelIndex{};
} }
int PakDirModel::rowCount(QModelIndex const &) const { int Model::rowCount(QModelIndex const &) const {
return m_root->size(); 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::columnCount(QModelIndex const &) const {
return Arc::ColumnMax;
} }
*/
// EOF // EOF

View File

@ -2,62 +2,102 @@
#include "common.h" #include "common.h"
#include <QObject> #include <QAbstractItemModel>
#include <QByteArray> #include <QByteArray>
#include <QObject>
namespace Arc { namespace Arc {
Q_NAMESPACE Q_NAMESPACE
struct Node;
struct Dir;
enum Column { enum Column {
ColumnSize, ColumnSize,
ColumnType,
ColumnName, ColumnName,
ColumnMax,
}; };
Q_ENUM_NS(Column) Q_ENUM_NS(Column)
enum FileType { enum class FileType {
FileNormal, Label,
FileLabel, MipTexture,
Normal,
FilePalette = 64, Palette,
FileTexture, Picture,
FilePicture, Sound,
FileSound, Texture,
FileMipTexture,
}; };
Q_ENUM_NS(FileType) Q_ENUM_NS(FileType)
Arc::FileType getFileType(int n); enum ArchiveType {
ArcPack,
ArcWad2,
};
Q_ENUM_NS(ArchiveType)
Option<ArchiveType> getArchiveType(std::istream &st) noexcept;
Dir readArchive(std::istream &st);
struct Dir : public std::vector<Node> {
using std::vector<Node>::vector;
Dir::iterator findNode(std::string const &name);
};
struct File : public QByteArray {
using QByteArray::QByteArray;
};
struct Node : public std::variant<Dir, File> {
using super_type = std::variant<Dir, File>;
Node() = default;
Node(Dir &&f, std::string &&n, FileType t = FileType::Normal);
Node(File &&f, std::string &&n, FileType t = FileType::Normal);
Node(Node const &) = default;
Node(Node &&) = 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)
struct ArcNode; Q_DECLARE_METATYPE(Arc::File)
Q_DECLARE_METATYPE(Arc::Node)
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 // 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; Arc::File 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);
@ -41,7 +41,7 @@ static PakEntry readPakEntry(std::istream &st) {
st.seekg(entOffset); st.seekg(entOffset);
ArcFile file; Arc::File file;
file.resize(entSize); file.resize(entSize);
st.read(file.data(), entSize); st.read(file.data(), entSize);
@ -50,17 +50,17 @@ 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;
} }
static void insertFile(ArcDir &dir, std::string name, ArcFile file) { static void insertFile(Arc::Dir &dir, std::string name, Arc::File file) {
std::optional<std::string> next; Option<std::string> next;
if(auto slash = name.find('/'); slash != std::string::npos) { if(auto slash = name.find('/'); slash != std::string::npos) {
next = name.substr(slash + 1); next = name.substr(slash + 1);
@ -73,24 +73,24 @@ static void insertFile(ArcDir &dir, std::string name, ArcFile file) {
auto ref = auto ref =
existing != dir.end() existing != dir.end()
? *existing ? *existing
: dir.emplace_back(ArcDir{}, std::move(name)); : dir.emplace_back(Arc::Dir{}, std::move(name));
insertFile(std::get<ArcDir>(ref), *std::move(next), std::move(file)); insertFile(std::get<Arc::Dir>(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) { Arc::Dir readPack(std::istream &st) {
auto hdr = readPakHeader(st); auto hdr = readPackHeader(st);
st.seekg(hdr.dirOffset); st.seekg(hdr.dirOffset);
ArcDir root; Arc::Dir 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); Arc::Dir readPack(std::istream &st);
// EOF // EOF

View File

@ -5,19 +5,32 @@
#include <QMdiArea> #include <QMdiArea>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
Project::Project(QErrorMessage *errors, QMdiArea *parent) : Project::Project(Arc::Dir &&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 Arc::Model{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();
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() { Project::~Project() {
} }
void Project::dirChanged(Arc::Dir *) {
tableView->resizeColumnsToContents();
}
// EOF // EOF

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,12 +17,16 @@ class Project : public QMdiSubWindow, private Ui::Project {
Q_OBJECT Q_OBJECT
public: public:
explicit Project(QErrorMessage *errors, QMdiArea *parent); explicit Project(Arc::Dir &&arc, QErrorMessage *errors, QMdiArea *parent);
virtual ~Project(); virtual ~Project();
private slots:
void dirChanged(Arc::Dir *dir);
private: private:
Arc::Dir m_arc;
QErrorMessage *m_errors; QErrorMessage *m_errors;
QAbstractItemModel *m_model; Arc::Model *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

96
source/quam/wad2.cc Normal file
View File

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

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); Arc::Dir readWad2(std::istream &st);
// EOF // EOF