Compare commits

..

No commits in common. "d904117b798dcf71d32728f90f4201c8dec817ae" and "881fa8c9bd421d73648d9ff25882c6edf52eda2e" have entirely different histories.

15 changed files with 208 additions and 842 deletions

View File

@ -27,12 +27,9 @@ macro(make_qt_project TARGET_NAME)
target_compile_definitions( target_compile_definitions(
${TARGET_NAME} ${TARGET_NAME}
PUBLIC PUBLIC
-DQT_DISABLE_DEPRECATED_BEFORE=0x051300 -DQT_DEPRECATED_WARNINGS
-DQT_NO_CAST_FROM_ASCII -DQT_STRICT_ITERATORS
-DQT_NO_CAST_TO_ASCII -DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT)
-DQT_NO_NARROWING_CONVERSIONS_IN_CONNECT
-DQT_NO_URL_CAST_FROM_STRING
-DQT_STRICT_ITERATORS)
endmacro() endmacro()
find_package( find_package(

View File

@ -11,7 +11,6 @@
#include <array> #include <array>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <functional>
#include <ios> #include <ios>
#include <istream> #include <istream>
#include <iterator> #include <iterator>
@ -29,16 +28,12 @@ using Option = std::optional<T>;
template<typename... Ts> template<typename... Ts>
using Variant = std::variant<Ts...>; using Variant = std::variant<Ts...>;
template<typename T>
using UniquePtr = std::unique_ptr<T>;
struct Error : public std::runtime_error { struct Error : public std::runtime_error {
using std::runtime_error::runtime_error; using std::runtime_error::runtime_error;
}; };
struct EnumError : public Error {using Error::Error;}; struct EnumError : public Error {using Error::Error;};
struct FileFormatError : public Error {using Error::Error;}; struct FileFormatError : public Error {using Error::Error;};
struct FileSystemError : public Error {using Error::Error;};
struct UnimplementedError : public Error {using Error::Error;}; struct UnimplementedError : public Error {using Error::Error;};
struct MemoryStreamBuf : public std::streambuf { struct MemoryStreamBuf : public std::streambuf {
@ -125,15 +120,6 @@ static inline int enumMax() {
return QMetaEnum::fromType<T>().keyCount(); return QMetaEnum::fromType<T>().keyCount();
} }
template<typename T>
static inline T orThrow(Option<T> opt, Error err) {
if(opt) {
return *opt;
} else {
throw err;
}
}
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;
@ -155,14 +141,4 @@ static inline QDebug operator<<(QDebug debug, Option<T> const &t) {
return debug; return debug;
} }
template<typename T>
static inline QDebug operator<<(QDebug debug, UniquePtr<T> const &t) {
if(t) {
debug << *t;
} else {
debug << "nullptr";
}
return debug;
}
// EOF // EOF

View File

@ -18,153 +18,33 @@ namespace Arc {
return None; return None;
} }
Node::Node(std::string _name) : Dir readArchive(std::istream &st) {
name(_name), switch(auto v = getArchiveType(st);
parent(nullptr) v.has_value() ? *v : throw FileFormatError("not an archive")) {
{
}
Node::~Node() {
}
Dir *Node::getRoot() {
if(!parent) {
return getDir();
} else {
Dir *rover = parent;
while(rover->parent) {
rover = rover->parent;
}
return rover;
}
}
Node *Node::findPath(std::string path) {
if(path.empty()) {
return nullptr;
}
Dir *rover;
// get the initial dir, which will either be:
// - the root if the path starts with /
// - the node itself
// - the node's current Dir (when it's not a Dir)
if(path.front() == '/') {
path.erase(path.begin());
rover = getRoot();
} else {
rover = getDir();
if(!rover) {
rover = parent;
}
}
if(path.empty()) {
// no file, so just the directory itself
return rover;
} else if(auto slash = path.find('/'); slash != std::string::npos) {
std::string prev;
prev = path.substr(0, slash);
path = path.substr(slash + 1);
if(prev == "..") {
// parent dir on ".."
rover = rover->parent ? rover->parent : rover;
} else if(prev != ".") {
if(auto node = rover->findNode(prev)) {
// we want a subdirectory since this path continues
rover = node->getDir();
} else {
// took a wrong turn
rover = nullptr;
}
}
// recurse
return rover ? rover->findPath(path) : nullptr;
} else {
// must be a node inside a directory
return rover->findNode(path);
}
}
std::string Node::getPath(bool fromChild) const {
if(parent) {
return parent->getPath(true) + '/' + name;
} else if(fromChild) {
return std::string{};
} else {
return "/";
}
}
Dir::Dir(std::string _name) :
Node(_name),
m_data()
{
}
Dir::~Dir() {
}
UniquePtr<Node> const &Dir::emplaceBack(Node *node) {
node->parent = this;
return m_data.emplace_back(node);
}
Dir Dir::readArchive(std::istream &st, ArcType type) {
switch(type) {
case ArcType::Pack: return readPack(st); case ArcType::Pack: return readPack(st);
case ArcType::Wad2: return readWad2(st); case ArcType::Wad2: return readWad2(st);
} }
Q_UNREACHABLE();
} }
Node *Dir::findNode(std::string const &name) { Node *Dir::findNode(std::string const &name) {
auto it = std::find_if(m_data.begin(), m_data.end(), auto it = std::find_if(begin(), end(), [&name](Node &node) {
[&name](auto const &node) { return node.name == name;
return node->name == name; });
}); return it != end() ? &*it : nullptr;
return it != m_data.end() ? &**it : nullptr;
} }
File::File(std::string _name, QByteArray &&_data) : Node::Node(Dir &&f, std::string &&n, FileType t) :
Node(_name), superType(std::move(f)),
m_data(std::move(_data)) name(std::move(n)),
type(t)
{ {
} }
File::~File() { Node::Node(File &&f, std::string &&n, FileType t) :
} superType(std::move(f)),
name(std::move(n)),
ArcInfo::ArcInfo(ArcType arcType) noexcept : type(t)
type{arcType}
{ {
switch(type) {
case ArcType::Pack:
pathMaxChars = 56;
validatorType = PackValidator::staticMetaObject;
break;
case ArcType::Wad2:
pathMaxChars = 16;
validatorType = Wad2Validator::staticMetaObject;
break;
}
}
Arc::Arc(std::istream &st, QObject *parent) :
QObject{parent},
info{ArcInfo(orThrow(getArchiveType(st),
FileFormatError("not an archive")))},
root{Dir::readArchive(st, info.type)},
validator{qobject_cast<QValidator *>(info.validatorType
.newInstance(Q_ARG(QObject *,
this)))}
{
}
Arc::~Arc() {
} }
Model::Model(QObject *parent) : Model::Model(QObject *parent) :
@ -187,23 +67,21 @@ namespace Arc {
switch(role) { switch(role) {
case Qt::DecorationRole: case Qt::DecorationRole:
if(col == Column::Name) { if(col == Column::Name) {
auto icon = node->getDir() ? "folder" : "text-x-generic"; auto icon =
return QVariant{QIcon::fromTheme(QString::fromUtf8(icon))}; std::holds_alternative<Dir>(*node) ? "folder"
: "text-x-generic";
return QVariant{QIcon::fromTheme(icon)};
} }
break; break;
case Qt::DisplayRole: case Qt::DisplayRole:
switch(col) { switch(col) {
case Column::Size: case Column::Size:
if(auto file = node->getFile()) { if(auto file = std::get_if<File>(node)) {
return QVariant{QString::number(file->size())}; return QVariant{QString::number(file->size())};
} }
break; break;
case Column::Type: case Column::Type:
if(auto file = node->getFile()) { return QVariant{tr(enumToString(node->type))};
return QVariant{tr(enumToString(file->type))};
} else {
return QVariant{tr("Directory")};
}
case Column::Name: case Column::Name:
return QVariant{tr(node->name.data())}; return QVariant{tr(node->name.data())};
} }
@ -220,9 +98,9 @@ namespace Arc {
return Qt::NoItemFlags; return Qt::NoItemFlags;
} else { } else {
return Qt::ItemIsSelectable | return Qt::ItemIsSelectable |
Qt::ItemIsDragEnabled | Qt::ItemIsDragEnabled |
Qt::ItemIsEnabled | Qt::ItemIsEnabled |
Qt::ItemNeverHasChildren; Qt::ItemNeverHasChildren;
} }
} }
@ -245,7 +123,7 @@ namespace Arc {
if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) { if(!m_dir || !hasIndex(row, col, parent) || row > m_dir->size()) {
return QModelIndex{}; return QModelIndex{};
} else { } else {
return createIndex(row, col, m_dir->at(row)); return createIndex(row, col, &m_dir->at(row));
} }
} }
@ -265,49 +143,11 @@ namespace Arc {
return m_dir; return m_dir;
} }
Node *Model::findPath(std::string path) { void Model::setDir(Dir *dir) {
return m_dir->findPath(path); if(dir != m_dir) {
} m_dir = dir;
void Model::setDir(Dir *to) {
auto from = m_dir;
if(to != from) {
emit layoutAboutToBeChanged();
for(int row = 0, rows = rowCount(); row < rows; row++) {
for(int col = 0, cols = columnCount(); col < cols; col++) {
changePersistentIndex(index(row, col), QModelIndex{});
}
}
m_dir = to;
emit layoutChanged(); emit layoutChanged();
emit dirChanged(from, to); emit dirChanged(dir);
}
}
void Model::setDirToIndex(QModelIndex const &ind) {
auto node = static_cast<Node *>(ind.data(Qt::UserRole).value<void *>());
if(auto dir = node->getDir()) {
setDir(dir);
}
}
bool Model::goUp() {
if(m_dir && m_dir->parent) {
setDir(m_dir->parent);
return true;
} else {
return false;
}
}
bool Model::goPath(std::string path) {
Node *node;
Dir *dir;
if(m_dir && (node = m_dir->findPath(path)) && (dir = node->getDir())) {
setDir(dir);
return true;
} else {
return false;
} }
} }
} }

View File

@ -4,13 +4,14 @@
#include <QAbstractItemModel> #include <QAbstractItemModel>
#include <QByteArray> #include <QByteArray>
#include <QMetaObject>
#include <QObject> #include <QObject>
#include <QValidator>
namespace Arc { namespace Arc {
Q_NAMESPACE Q_NAMESPACE
struct Node;
struct Dir;
enum class Column { enum class Column {
Size, Size,
Type, Type,
@ -35,83 +36,33 @@ namespace Arc {
}; };
Q_ENUM_NS(ArcType) Q_ENUM_NS(ArcType)
class Dir; Option<ArcType> getArchiveType(std::istream &st) noexcept;
class File;
class Node { Dir readArchive(std::istream &st);
public:
Node() = delete;
Node(Node const &) = delete;
Node(Node &&) = default;
virtual ~Node();
virtual Dir const *getDir() const = 0; struct Dir : public std::vector<Node> {
virtual Dir *getDir() = 0; using std::vector<Node>::vector;
virtual File const *getFile() const = 0;
virtual File *getFile() = 0;
Dir *getRoot();
Node *findPath(std::string path);
std::string getPath(bool fromChild = false) const;
Dir *parent{nullptr};
std::string name;
protected:
Node(std::string name);
};
class Dir : public Node {
public:
using DataType = std::vector<UniquePtr<Node>>;
Dir(std::string name);
Dir(Dir const &) = delete;
Dir(Dir &&) = default;
virtual ~Dir();
Dir const *getDir() const override {return this;}
Dir *getDir() override {return this;}
File const *getFile() const override {return nullptr;}
File *getFile() override {return nullptr;}
DataType const &data() const {return m_data;}
Node *at(std::size_t i) const {return &*m_data.at(i);}
std::size_t size() const {return m_data.size();}
UniquePtr<Node> const &emplaceBack(Node *node);
Node *findNode(std::string const &name); Node *findNode(std::string const &name);
static Dir readArchive(std::istream &st, ArcType type);
private:
DataType m_data;
}; };
class File : public Node { struct File : public QByteArray {
public: using QByteArray::QByteArray;
using DataType = QByteArray; };
File(std::string name, QByteArray &&data); struct Node : public Variant<Dir, File> {
File(File const &) = delete; using superType = Variant<Dir, File>;
File(File &&) = default;
virtual ~File();
Dir const *getDir() const override {return nullptr;} Node() = default;
Dir *getDir() override {return nullptr;}
File const *getFile() const override {return this;}
File *getFile() override {return this;}
DataType const &data() const {return m_data;} Node(Dir &&f, std::string &&n, FileType t = FileType::Normal);
Node(File &&f, std::string &&n, FileType t = FileType::Normal);
std::size_t size() const {return m_data.size();} Node(Node const &) = default;
Node(Node &&) = default;
FileType type{FileType::Normal}; std::string name;
FileType type;
private:
DataType m_data;
}; };
class Model : public QAbstractItemModel { class Model : public QAbstractItemModel {
@ -136,63 +87,24 @@ namespace Arc {
Dir *dir() const; Dir *dir() const;
Node *findPath(std::string path);
public slots: public slots:
void setDir(Dir *dir); void setDir(Dir *dir);
void setDirToIndex(QModelIndex const &ind);
bool goUp();
bool goPath(std::string path);
signals: signals:
void dirChanged(Dir *from, Dir *to); void dirChanged(Dir *dir);
private: private:
Dir *m_dir; Dir *m_dir;
}; };
class ArcInfo {
public:
ArcInfo(ArcType arcType) noexcept;
ArcType type;
std::size_t pathMaxChars;
QMetaObject validatorType;
};
class Arc : QObject {
Q_OBJECT
public:
explicit Arc(std::istream &st, QObject *parent = nullptr);
~Arc();
ArcInfo info;
Dir root;
QValidator *validator;
};
Option<ArcType> getArchiveType(std::istream &st) noexcept;
}
static inline QDebug operator<<(QDebug debug, Arc::Dir const &t) {
debug << t.data();
return debug;
}
static inline QDebug operator<<(QDebug debug, Arc::File const &t) {
debug << t.data();
return debug;
} }
Q_DECLARE_METATYPE(Arc::Dir)
Q_DECLARE_METATYPE(Arc::File)
Q_DECLARE_METATYPE(Arc::Node)
static inline QDebug operator<<(QDebug debug, Arc::Node const &t) { static inline QDebug operator<<(QDebug debug, Arc::Node const &t) {
debug << "Arc::Node(" << t.name << ", "; debug << "Arc::Node(" << t.name << ", ";
if(auto v = t.getDir()) { std::visit([&](auto &&arg) {debug << arg;},
debug << *v; static_cast<Variant<Arc::Dir, Arc::File>>(t));
} else if(auto v = t.getFile()) {
debug << *v;
}
debug << ")"; debug << ")";
return debug; return debug;
} }

View File

@ -6,10 +6,10 @@
#include <QCoreApplication> #include <QCoreApplication>
static void setupAppl(QCoreApplication &appl) { static void setupAppl(QCoreApplication &appl) {
appl.setApplicationName(QString::fromUtf8("QuAM!")); appl.setApplicationName("QuAM!");
appl.setApplicationVersion(QString::fromUtf8("1.0")); appl.setApplicationVersion("1.0");
appl.setOrganizationDomain(QString::fromUtf8("greyserv.net")); appl.setOrganizationDomain("greyserv.net");
appl.setOrganizationName(QString::fromUtf8("Project Golan")); appl.setOrganizationName("Project Golan");
} }
static int modeGui(int argc, char *argv[]) { static int modeGui(int argc, char *argv[]) {
@ -26,15 +26,14 @@ static int modeText(int argc, char *argv[]) {
QCommandLineParser par; QCommandLineParser par;
par.setApplicationDescription(QString::fromUtf8("Quake Archive Manager")); par.setApplicationDescription("Quake Archive Manager");
par.setSingleDashWordOptionMode( par.setSingleDashWordOptionMode(
QCommandLineParser::ParseAsCompactedShortOptions); QCommandLineParser::ParseAsCompactedShortOptions);
par.addHelpOption(); par.addHelpOption();
par.addVersionOption(); par.addVersionOption();
QCommandLineOption fileNameOpt{QStringList{QString::fromUtf8("f"), QCommandLineOption fileNameOpt{QStringList{"f", "file"},
QString::fromUtf8("file")},
trMain("Open the archive <file>."), trMain("Open the archive <file>."),
trMain("file")}; trMain("file")};
par.addOption(fileNameOpt); par.addOption(fileNameOpt);
@ -44,7 +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);
qDebug() << Arc::Arc(st).root; auto arc = Arc::readArchive(st);
qDebug() << arc;
return 0; return 0;
} }

View File

@ -5,16 +5,6 @@
#include <QErrorMessage> #include <QErrorMessage>
#include <QFileDialog> #include <QFileDialog>
#include <QMdiSubWindow>
static bool applyActiveWin(QMdiArea *area, std::function<bool(Project *)> fn) {
if(auto subWin = area->activeSubWindow()) {
if(auto win = qobject_cast<Project *>(subWin->widget())) {
return fn(win);
}
}
return false;
}
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent}, QMainWindow{parent},
@ -26,11 +16,6 @@ MainWindow::MainWindow(QWidget *parent) :
actionClose->setShortcut(QKeySequence{QKeySequence::Close}); actionClose->setShortcut(QKeySequence{QKeySequence::Close});
actionOpen->setShortcut(QKeySequence{QKeySequence::Open}); actionOpen->setShortcut(QKeySequence{QKeySequence::Open});
actionQuit->setShortcut(QKeySequence{QKeySequence::Quit}); actionQuit->setShortcut(QKeySequence{QKeySequence::Quit});
actionUp->setShortcut(Qt::ALT + Qt::Key_Up);
actionTop->setShortcut(Qt::ALT + Qt::Key_Home);
actionBack->setShortcut(QKeySequence{QKeySequence::Back});
actionForward->setShortcut(QKeySequence{QKeySequence::Forward});
} }
MainWindow::~MainWindow() { MainWindow::~MainWindow() {
@ -49,15 +34,9 @@ void MainWindow::fileOpen() {
if(!fileName.isEmpty()) { if(!fileName.isEmpty()) {
try { try {
auto path = std::filesystem::path(fileName.toStdString()); auto st = openReadBin(fileName.toStdString());
auto st = openReadBin(path); auto arc = Arc::readArchive(st);
auto win = new QMdiSubWindow; new Project{std::move(arc), m_errors, mdiArea};
auto proj = new Project{st, m_errors, win};
win->setWidget(proj);
win->setAttribute(Qt::WA_DeleteOnClose);
win->setWindowTitle(QString::fromStdString(path.filename()));
mdiArea->addSubWindow(win);
win->showMaximized();
} catch(std::exception const &exc) { } catch(std::exception const &exc) {
m_errors->showMessage(tr(exc.what())); m_errors->showMessage(tr(exc.what()));
} }
@ -70,20 +49,4 @@ void MainWindow::fileClose() {
} }
} }
bool MainWindow::goBack() {
return applyActiveWin(mdiArea, [](auto win) {return win->goBack();});
}
bool MainWindow::goForward() {
return applyActiveWin(mdiArea, [](auto win) {return win->goForward();});
}
bool MainWindow::goTop() {
return applyActiveWin(mdiArea, [](auto win) {return win->goTop();});
}
bool MainWindow::goUp() {
return applyActiveWin(mdiArea, [](auto win) {return win->goUp();});
}
// EOF // EOF

View File

@ -20,11 +20,6 @@ public slots:
void fileOpen(); void fileOpen();
void fileClose(); void fileClose();
bool goBack();
bool goForward();
bool goTop();
bool goUp();
private: private:
QErrorMessage *m_errors; QErrorMessage *m_errors;
}; };

View File

@ -17,15 +17,6 @@
<layout class="QHBoxLayout" name="horizontalLayout_4"> <layout class="QHBoxLayout" name="horizontalLayout_4">
<item> <item>
<widget class="QMdiArea" name="mdiArea"> <widget class="QMdiArea" name="mdiArea">
<property name="background">
<brush brushstyle="Dense7Pattern">
<color alpha="255">
<red>0</red>
<green>0</green>
<blue>0</blue>
</color>
</brush>
</property>
<property name="viewMode"> <property name="viewMode">
<enum>QMdiArea::TabbedView</enum> <enum>QMdiArea::TabbedView</enum>
</property> </property>
@ -48,7 +39,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>640</width> <width>640</width>
<height>24</height> <height>29</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile"> <widget class="QMenu" name="menuFile">
@ -60,17 +51,7 @@
<addaction name="separator"/> <addaction name="separator"/>
<addaction name="actionQuit"/> <addaction name="actionQuit"/>
</widget> </widget>
<widget class="QMenu" name="menuGo">
<property name="title">
<string>&amp;Go</string>
</property>
<addaction name="actionUp"/>
<addaction name="actionBack"/>
<addaction name="actionForward"/>
<addaction name="actionTop"/>
</widget>
<addaction name="menuFile"/> <addaction name="menuFile"/>
<addaction name="menuGo"/>
</widget> </widget>
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen"> <action name="actionOpen">
@ -100,42 +81,6 @@
<string>&amp;Close</string> <string>&amp;Close</string>
</property> </property>
</action> </action>
<action name="actionUp">
<property name="icon">
<iconset theme="go-up">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Up</string>
</property>
</action>
<action name="actionBack">
<property name="icon">
<iconset theme="go-previous">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Back</string>
</property>
</action>
<action name="actionForward">
<property name="icon">
<iconset theme="go-next">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Forward</string>
</property>
</action>
<action name="actionTop">
<property name="icon">
<iconset theme="go-top">
<normaloff>.</normaloff>.</iconset>
</property>
<property name="text">
<string>&amp;Top</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections> <connections>
@ -187,78 +132,10 @@
</hint> </hint>
</hints> </hints>
</connection> </connection>
<connection>
<sender>actionUp</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>goUp()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>actionTop</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>goTop()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>actionBack</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>goBack()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>actionForward</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>goForward()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
</connections> </connections>
<slots> <slots>
<slot>fileOpen()</slot> <slot>fileOpen()</slot>
<slot>selectCell(int,int)</slot> <slot>selectCell(int,int)</slot>
<slot>fileClose()</slot> <slot>fileClose()</slot>
<slot>goUp()</slot>
<slot>goForward()</slot>
<slot>goBack()</slot>
<slot>goTop()</slot>
</slots> </slots>
</ui> </ui>

View File

@ -1,8 +1,18 @@
#include "quam/pack.h" #include "quam/pack.h"
struct PackHeader {
quint32 dirOffset;
quint32 dirNum;
};
struct PackEntry {
std::string name;
Arc::File file;
};
static constexpr quint32 sizeOfPackEntry = 64; static constexpr quint32 sizeOfPackEntry = 64;
static std::pair<quint32, quint32> readPackHeader(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'}) {
@ -16,31 +26,40 @@ static std::pair<quint32, quint32> readPackHeader(std::istream &st) {
throw FileFormatError("invalid directory size"); throw FileFormatError("invalid directory size");
} }
return std::make_pair(dirOffset, dirSize / sizeOfPackEntry); PackHeader hdr;
hdr.dirOffset = dirOffset;
hdr.dirNum = dirSize / sizeOfPackEntry;
return hdr;
} }
static Arc::File readPackEntry(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);
if(entName.front() == '/') {
throw FileFormatError("empty root directory name");
}
auto pos = st.tellg(); auto pos = st.tellg();
st.seekg(entOffset); st.seekg(entOffset);
QByteArray data; Arc::File file;
data.resize(entSize); file.resize(entSize);
st.read(data.data(), entSize); st.read(file.data(), entSize);
st.seekg(pos); st.seekg(pos);
return Arc::File{ntbsToString(entName), std::move(data)}; std::string name = ntbsToString(entName);
if(name.front() == '/') {
throw FileFormatError("empty root directory name");
}
PackEntry ent;
ent.name = std::move(name);
ent.file = std::move(file);
return ent;
} }
static void insertFile(Arc::Dir &dir, Arc::File &file, std::string name) { static void insertFile(Arc::Dir &dir, std::string &name, Arc::File &file) {
Option<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) {
@ -50,18 +69,11 @@ static void insertFile(Arc::Dir &dir, Arc::File &file, std::string name) {
auto existing = dir.findNode(name); auto existing = dir.findNode(name);
if(next) { if(next) {
Arc::Node *ref; auto &ref = existing ? *existing
if(existing) { : dir.emplace_back(Arc::Dir{}, std::move(name));
ref = existing; insertFile(std::get<Arc::Dir>(ref), *next, file);
} else {
ref = new Arc::Dir(name);
dir.emplaceBack(ref);
}
insertFile(*ref->getDir(), file, *next);
} else if(!existing) { } else if(!existing) {
file.name = name; dir.emplace_back(std::move(file), std::move(name));
file.parent = &dir;
dir.emplaceBack(new Arc::File(std::move(file)));
} else { } else {
throw FileFormatError("duplicate file"); throw FileFormatError("duplicate file");
} }
@ -69,25 +81,16 @@ static void insertFile(Arc::Dir &dir, Arc::File &file, std::string name) {
Arc::Dir readPack(std::istream &st) { Arc::Dir readPack(std::istream &st) {
auto hdr = readPackHeader(st); auto hdr = readPackHeader(st);
st.seekg(hdr.first); st.seekg(hdr.dirOffset);
Arc::Dir root{std::string{}}; Arc::Dir root;
for(quint32 i = 0; i < hdr.second; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto file = readPackEntry(st); auto ent = readPackEntry(st);
insertFile(root, file, file.name); insertFile(root, ent.name, ent.file);
} }
return root; return root;
} }
PackValidator::PackValidator(QObject *parent) :
QValidator{parent}
{
}
QValidator::State PackValidator::validate(QString &input, int &pos) const {
return QValidator::Acceptable;
}
// EOF // EOF

View File

@ -4,17 +4,6 @@
#include "quam/archive.h" #include "quam/archive.h"
#include <QValidator>
class PackValidator : public QValidator {
Q_OBJECT
public:
Q_INVOKABLE explicit PackValidator(QObject *parent);
QValidator::State validate(QString &input, int &pos) const override;
};
Arc::Dir readPack(std::istream &st); Arc::Dir readPack(std::istream &st);
// EOF // EOF

View File

@ -1,103 +1,47 @@
#include "quam/project.h" #include "quam/project.h"
#include <QAbstractItemModel>
#include <QErrorMessage> #include <QErrorMessage>
#include <QMdiArea>
#include <QSortFilterProxyModel> #include <QSortFilterProxyModel>
Project::Project(std::istream &st, QErrorMessage *errors, QWidget *parent) : Project::Project(Arc::Dir &&arc, QErrorMessage *errors, QMdiArea *parent) :
QWidget{parent}, QMdiSubWindow{parent},
Ui::Project{}, Ui::Project{},
m_seekingDir{false}, m_arc{std::move(arc)},
m_histPos{0},
m_hist(),
m_arc{new Arc::Arc{st, this}},
m_errors{errors}, m_errors{errors},
m_model{new Arc::Model{this}}, m_model{new Arc::Model{this}},
m_sorter{new QSortFilterProxyModel{this}} m_sorter{new QSortFilterProxyModel{this}}
{ {
setupUi(this); auto widget = new QWidget(this);
setupUi(widget);
setWidget(widget);
setAttribute(Qt::WA_DeleteOnClose);
showMaximized();
connect(m_model, &Arc::Model::dirChanged, connect(m_model, &Arc::Model::dirChanged,
this, &Project::dirChanged); this, &Project::dirChanged);
connect(tableView, &QAbstractItemView::doubleClicked, connect(tableView, &QAbstractItemView::doubleClicked,
m_model, &Arc::Model::setDirToIndex); this, &Project::viewDoubleClicked);
dirName->setValidator(m_arc->validator);
m_sorter->setSourceModel(m_model); m_sorter->setSourceModel(m_model);
tableView->setModel(m_sorter); tableView->setModel(m_sorter);
seekTop(); m_model->setDir(&m_arc);
} }
Project::~Project() { Project::~Project() {
} }
void Project::dirChanged(Arc::Dir *from, Arc::Dir *to) { void Project::dirChanged(Arc::Dir *) {
if(!m_seekingDir) {
if(std::ptrdiff_t(m_histPos) < std::ptrdiff_t(m_hist.size()) - 1) {
auto beg = m_hist.begin();
std::advance(beg, m_histPos + 1);
m_hist.erase(beg, m_hist.end());
}
m_hist.push_back(to->getPath());
m_histPos = m_hist.size() - 1;
} else {
m_seekingDir = false;
}
tableView->sortByColumn(int(Arc::Column::Type), Qt::AscendingOrder);
tableView->resizeColumnsToContents(); tableView->resizeColumnsToContents();
} }
void Project::seekTop() { void Project::viewDoubleClicked(QModelIndex const &index) {
m_model->setDir(&m_arc->root); auto node = static_cast<Arc::Node *>(index.data(Qt::UserRole)
} .value<void *>());
if(auto dir = std::get_if<Arc::Dir>(node)) {
bool Project::seekHistory(std::ptrdiff_t newPos) { m_model->setDir(dir);
if(newPos < 0 || newPos >= m_hist.size()) {
return false;
} }
m_histPos = newPos;
qDebug() << "hist pos changed:" << m_histPos;
auto newPath = m_hist.at(newPos);
if(auto node = m_model->findPath(newPath)) {
if(auto dir = node->getDir()) {
m_seekingDir = true;
m_model->setDir(dir);
return true;
} else {
m_errors->showMessage(QString::fromStdString("'"s + newPath + "' ") +
tr("is not a directory"));
return false;
}
} else {
m_errors->showMessage(tr("could not find directory") +
QString::fromStdString(" '"s + newPath + "'"));
return false;
}
}
bool Project::goBack() {
return seekHistory(std::ptrdiff_t(m_histPos) - 1);
}
bool Project::goForward() {
return seekHistory(std::ptrdiff_t(m_histPos) + 1);
}
bool Project::goTop() {
seekTop();
return true;
}
bool Project::goUp() {
return m_model->goUp();
}
bool Project::goPath(std::string path) {
return m_model->goPath(path);
} }
// EOF // EOF

View File

@ -5,40 +5,30 @@
#include "quam/archive.h" #include "quam/archive.h"
#include "quam/ui_project.h" #include "quam/ui_project.h"
#include <QMainWindow>
#include <QMdiSubWindow>
#include <QWidget> #include <QWidget>
class QAbstractItemModel;
class QErrorMessage; class QErrorMessage;
class QSortFilterProxyModel; class QSortFilterProxyModel;
class Project : public QWidget, private Ui::Project { class Project : public QMdiSubWindow, private Ui::Project {
Q_OBJECT Q_OBJECT
public: public:
explicit Project(std::istream &st, QErrorMessage *errors, QWidget *parent); explicit Project(Arc::Dir &&arc, QErrorMessage *errors, QMdiArea *parent);
virtual ~Project(); virtual ~Project();
bool seekHistory(std::ptrdiff_t newPos);
void seekTop();
public slots:
bool goBack();
bool goForward();
bool goTop();
bool goUp();
bool goPath(std::string path);
private slots: private slots:
void dirChanged(Arc::Dir *from, Arc::Dir *to); void dirChanged(Arc::Dir *dir);
void viewDoubleClicked(QModelIndex const &index);
private: private:
bool m_seekingDir; Arc::Dir m_arc;
std::size_t m_histPos; QErrorMessage *m_errors;
std::vector<std::string> m_hist; Arc::Model *m_model;
Arc::Arc *m_arc; QSortFilterProxyModel *m_sorter;
QErrorMessage *m_errors;
Arc::Model *m_model;
QSortFilterProxyModel *m_sorter;
}; };
// EOF // EOF

View File

@ -13,110 +13,49 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Project View</string> <string>Project View</string>
</property> </property>
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout">
<item> <item>
<widget class="QSplitter" name="splitter"> <widget class="QSplitter" name="splitter">
<property name="orientation"> <property name="orientation">
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<widget class="QWidget" name=""> <widget class="QTableView" name="tableView">
<layout class="QVBoxLayout" name="verticalLayout"> <property name="verticalScrollBarPolicy">
<item> <enum>Qt::ScrollBarAlwaysOn</enum>
<layout class="QHBoxLayout" name="horizontalLayout"> </property>
<item> <property name="horizontalScrollBarPolicy">
<widget class="QPushButton" name="buttonBack"> <enum>Qt::ScrollBarAlwaysOff</enum>
<property name="sizePolicy"> </property>
<sizepolicy hsizetype="Minimum" vsizetype="Minimum"> <property name="autoScroll">
<horstretch>0</horstretch> <bool>false</bool>
<verstretch>0</verstretch> </property>
</sizepolicy> <property name="tabKeyNavigation">
</property> <bool>true</bool>
<property name="icon"> </property>
<iconset theme="go-previous"/> <property name="alternatingRowColors">
</property> <bool>true</bool>
<property name="flat"> </property>
<bool>true</bool> <property name="selectionMode">
</property> <enum>QAbstractItemView::SingleSelection</enum>
</widget> </property>
</item> <property name="selectionBehavior">
<item> <enum>QAbstractItemView::SelectRows</enum>
<widget class="QPushButton" name="buttonForward"> </property>
<property name="sizePolicy"> <property name="showGrid">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum"> <bool>false</bool>
<horstretch>0</horstretch> </property>
<verstretch>0</verstretch> <property name="sortingEnabled">
</sizepolicy> <bool>true</bool>
</property> </property>
<property name="icon"> <attribute name="horizontalHeaderVisible">
<iconset theme="go-next"/> <bool>true</bool>
</property> </attribute>
<property name="flat"> <attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool> <bool>true</bool>
</property> </attribute>
</widget> <attribute name="verticalHeaderVisible">
</item> <bool>false</bool>
<item> </attribute>
<widget class="QPushButton" name="buttonUp">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Minimum">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="icon">
<iconset theme="go-up"/>
</property>
<property name="flat">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="dirName"/>
</item>
</layout>
</item>
<item>
<widget class="QTableView" name="tableView">
<property name="verticalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOn</enum>
</property>
<property name="horizontalScrollBarPolicy">
<enum>Qt::ScrollBarAlwaysOff</enum>
</property>
<property name="autoScroll">
<bool>false</bool>
</property>
<property name="tabKeyNavigation">
<bool>true</bool>
</property>
<property name="alternatingRowColors">
<bool>true</bool>
</property>
<property name="selectionMode">
<enum>QAbstractItemView::SingleSelection</enum>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="sortingEnabled">
<bool>true</bool>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
</widget>
</item>
</layout>
</widget> </widget>
<widget class="QPlainTextEdit" name="textEdit"/> <widget class="QPlainTextEdit" name="textEdit"/>
</widget> </widget>
@ -124,60 +63,5 @@
</layout> </layout>
</widget> </widget>
<resources/> <resources/>
<connections> <connections/>
<connection>
<sender>buttonBack</sender>
<signal>clicked()</signal>
<receiver>Project</receiver>
<slot>goBack()</slot>
<hints>
<hint type="sourcelabel">
<x>18</x>
<y>17</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonForward</sender>
<signal>clicked()</signal>
<receiver>Project</receiver>
<slot>goForward()</slot>
<hints>
<hint type="sourcelabel">
<x>47</x>
<y>17</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonUp</sender>
<signal>clicked()</signal>
<receiver>Project</receiver>
<slot>goUp()</slot>
<hints>
<hint type="sourcelabel">
<x>76</x>
<y>17</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
</connections>
<slots>
<slot>goUp()</slot>
<slot>goForward()</slot>
<slot>goBack()</slot>
<slot>goTop()</slot>
</slots>
</ui> </ui>

View File

@ -7,6 +7,17 @@ namespace Wad2 {
}; };
} }
struct Wad2Header {
quint32 dirNum;
quint32 dirOffset;
};
struct Wad2Entry {
std::string name;
Arc::FileType type;
Arc::File file;
};
static Arc::FileType getFileType(int n) { static Arc::FileType getFileType(int n) {
switch(n) { switch(n) {
case 0: return Arc::FileType::Normal; case 0: return Arc::FileType::Normal;
@ -20,19 +31,20 @@ static Arc::FileType getFileType(int n) {
} }
} }
static std::pair<quint32, quint32> readWad2Header(std::istream &st) { static Wad2Header readWad2Header(std::istream &st) {
auto magic = readBytes<4>(st); auto magic = readBytes<4>(st);
if(magic != std::array{'W', 'A', 'D', '2'}) { if(magic != std::array{'W', 'A', 'D', '2'}) {
throw FileFormatError("not a wad2 file (invalid magic number)"); throw FileFormatError("not a wad2 file (invalid magic number)");
} }
auto dirNum = readLE<quint32>(st); Wad2Header hdr;
auto dirOffset = readLE<quint32>(st); hdr.dirNum = readLE<quint32>(st);
return std::make_pair(dirOffset, dirNum); hdr.dirOffset = readLE<quint32>(st);
return hdr;
} }
static Arc::File readWad2Entry(std::istream &st) { static Wad2Entry readWad2Entry(std::istream &st) {
auto entOffset = readLE<quint32>(st); auto entOffset = readLE<quint32>(st);
auto entSize = readLE<quint32>(st); auto entSize = readLE<quint32>(st);
auto entCSize = readLE<quint32>(st); auto entCSize = readLE<quint32>(st);
@ -49,37 +61,31 @@ static Arc::File readWad2Entry(std::istream &st) {
st.seekg(entOffset); st.seekg(entOffset);
QByteArray data; Arc::File file;
file.resize(entSize);
st.read(file.data(), entSize);
data.resize(entSize);
st.read(data.data(), entSize);
st.seekg(pos); st.seekg(pos);
Arc::File file{ntbsToString(entName), std::move(data)}; Wad2Entry ent;
file.type = getFileType(entType); ent.name = ntbsToString(entName);
return file; ent.file = std::move(file);
ent.type = getFileType(entType);
return ent;
} }
Arc::Dir readWad2(std::istream &st) { Arc::Dir readWad2(std::istream &st) {
auto hdr = readWad2Header(st); auto hdr = readWad2Header(st);
st.seekg(hdr.first); st.seekg(hdr.dirOffset);
Arc::Dir root{std::string{}}; Arc::Dir root;
for(quint32 i = 0; i < hdr.second; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
root.emplaceBack(new Arc::File(readWad2Entry(st))); auto ent = readWad2Entry(st);
root.emplace_back(std::move(ent.file), std::move(ent.name), ent.type);
} }
return root; return root;
} }
Wad2Validator::Wad2Validator(QObject *parent) :
QValidator{parent}
{
}
QValidator::State Wad2Validator::validate(QString &input, int &pos) const {
return QValidator::Acceptable;
}
// EOF // EOF

View File

@ -4,17 +4,6 @@
#include "quam/archive.h" #include "quam/archive.h"
#include <QValidator>
class Wad2Validator : public QValidator {
Q_OBJECT
public:
Q_INVOKABLE explicit Wad2Validator(QObject *parent);
QValidator::State validate(QString &input, int &pos) const override;
};
Arc::Dir readWad2(std::istream &st); Arc::Dir readWad2(std::istream &st);
// EOF // EOF