add wad2 file code
This commit is contained in:
parent
f42028a378
commit
efdf96055e
|
@ -51,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)
|
||||
|
||||
|
|
|
@ -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,7 +64,7 @@ static inline QDebug operator<<(QDebug debug, std::unique_ptr<T> const &t) {
|
|||
}
|
||||
|
||||
template<typename T0, typename... Ts>
|
||||
static inline QDebug operator <<(QDebug debug,
|
||||
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
|
||||
|
|
|
@ -10,18 +10,114 @@ Arc::FileType Arc::getFileType(int n) {
|
|||
}
|
||||
}
|
||||
|
||||
ArcNode::ArcNode(ArcDir &&t, std::string &&n, Arc::FileType ty) :
|
||||
super_type(std::move(t)),
|
||||
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(ty)
|
||||
type(t)
|
||||
{
|
||||
}
|
||||
|
||||
ArcNode::ArcNode(ArcFile &&t, std::string &&n, Arc::FileType ty) :
|
||||
super_type(std::move(t)),
|
||||
ArcNode::ArcNode(ArcFile &&f, std::string &&n, Arc::FileType t) :
|
||||
super_type(std::move(f)),
|
||||
name(std::move(n)),
|
||||
type(ty)
|
||||
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
|
||||
|
|
|
@ -34,6 +34,8 @@ struct ArcNode;
|
|||
|
||||
struct ArcDir : public std::vector<ArcNode> {
|
||||
using std::vector<ArcNode>::vector;
|
||||
|
||||
ArcDir::iterator findNode(std::string const &name);
|
||||
};
|
||||
Q_DECLARE_METATYPE(ArcDir)
|
||||
|
||||
|
@ -47,8 +49,8 @@ struct ArcNode : public std::variant<ArcDir, ArcFile> {
|
|||
|
||||
ArcNode() = default;
|
||||
|
||||
ArcNode(ArcDir &&t, std::string &&n, Arc::FileType ty = Arc::FileNormal);
|
||||
ArcNode(ArcFile &&t, std::string &&n, Arc::FileType ty = Arc::FileNormal);
|
||||
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;
|
||||
|
|
|
@ -1,8 +1,5 @@
|
|||
#include "quam/pak.h"
|
||||
|
||||
#include <QIcon>
|
||||
#include <QMetaEnum>
|
||||
|
||||
struct PakHeader {
|
||||
quint32 dirOffset;
|
||||
quint32 dirNum;
|
||||
|
@ -50,9 +47,7 @@ static PakEntry readPakEntry(std::istream &st) {
|
|||
|
||||
st.seekg(pos);
|
||||
|
||||
std::string name;
|
||||
auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
|
||||
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");
|
||||
|
@ -64,7 +59,7 @@ static PakEntry readPakEntry(std::istream &st) {
|
|||
return ent;
|
||||
}
|
||||
|
||||
void insertFile(ArcDir &dir, std::string name, ArcFile 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) {
|
||||
|
@ -72,19 +67,16 @@ void insertFile(ArcDir &dir, std::string name, ArcFile file) {
|
|||
name = name.substr(0, slash);
|
||||
}
|
||||
|
||||
auto existingNode = std::find_if(dir.begin(), dir.end(),
|
||||
[&name](ArcNode const &node) {
|
||||
return node.name == name;
|
||||
});
|
||||
auto existing = dir.findNode(name);
|
||||
|
||||
if(next) {
|
||||
auto ref =
|
||||
existingNode != dir.end()
|
||||
? *existingNode
|
||||
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));
|
||||
|
|
81
source/quam/wad.cc
Normal file
81
source/quam/wad.cc
Normal 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
8
source/quam/wad.h
Normal file
|
@ -0,0 +1,8 @@
|
|||
#pragma once
|
||||
|
||||
#include "common.h"
|
||||
#include "quam/archive.h"
|
||||
|
||||
ArcDir readWad(std::istream &st);
|
||||
|
||||
// EOF
|
Loading…
Reference in New Issue
Block a user