Compare commits

..

No commits in common. "62253abab0ee9a02fa5aba44e3519ef17e638744" and "0df35fc0c976573682b840de789ec6d3df680e15" have entirely different histories.

7 changed files with 62 additions and 169 deletions

View File

@ -4,20 +4,18 @@
#include <QtGlobal> #include <QtGlobal>
#include <QCoreApplication> #include <QCoreApplication>
#include <QDebug>
#include <algorithm> #include <algorithm>
#include <array> #include <array>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <ios> #include <ios>
#include <iostream>
#include <istream> #include <istream>
#include <iterator> #include <iterator>
#include <memory>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <utility> #include <utility>
#include <variant>
static inline QString trMain(char const *sourceText, static inline QString trMain(char const *sourceText,
char const *disambiguation = nullptr, char const *disambiguation = nullptr,
@ -46,26 +44,33 @@ static inline std::array<char, N> readBytes(std::istream &st) {
return std::move(b); return std::move(b);
} }
static inline QDebug operator<<(QDebug debug, std::string const &t) { template<typename It>
debug << QString::fromStdString(t); static inline std::string escapeText(It begin, It end) {
return debug; std::stringstream out;
} out << std::hex << std::uppercase;
for(auto it = begin; it != end; ++it) {
template<typename T> unsigned char c = *it;
static inline QDebug operator<<(QDebug debug, std::unique_ptr<T> const &t) { switch(c) {
debug << *t; case '"': out << "\\\""; break;
return debug; case '\0': out << "\\0"; break;
} case '\\': out << "\\\\"; break;
case '\a': out << "\\a"; break;
template<typename T0, typename... Ts> case '\b': out << "\\b"; break;
static inline QDebug operator <<(QDebug debug, case '\f': out << "\\f"; break;
std::variant<T0, Ts...> const &t) { case '\n': out << "\\n"; break;
std::visit([&](auto &&arg) {debug << arg;}, t); case '\r': out << "\\r"; break;
return debug; case '\t': out << "\\t"; break;
} case '\v': out << "\\v"; break;
default:
static inline std::ifstream openReadBin(std::filesystem::path path) { if(c >= ' ' && c <= '~') {
return std::ifstream{path, std::ios_base::in | std::ios_base::binary}; out << c;
} else {
out << "\\x" << int(c);
}
break;
}
}
return out.str();
} }
// EOF // EOF

View File

@ -42,17 +42,14 @@ static int modeText(int argc, char *argv[]) {
par.process(appl); par.process(appl);
auto fileName = par.value(fileNameOpt).toStdString(); auto fileName = par.value(fileNameOpt).toStdString();
std::ifstream st{fileName, std::ios_base::in | std::ios_base::binary};
auto st = openReadBin(fileName); readPakFile(st);
auto pak = readPak(st);
qDebug() << pak;
return 0; return 0;
} }
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
if(argc <= 1) { if(argc == 0) {
return modeGui(argc, argv); return modeGui(argc, argv);
} else { } else {
return modeText(argc, argv); return modeText(argc, argv);

View File

@ -2,53 +2,14 @@
#include "quam/main_window.h" #include "quam/main_window.h"
#include "quam/pak.h" #include "quam/pak.h"
static void setListToDir(QListWidget &list, PakDir const &dir) {
list.clear();
for(auto const &kv : dir) {
auto const &name = kv.first;
auto const &node = kv.second;
auto var = QVariant::fromValue(node);
auto item = new QListWidgetItem{&list};
item->setText(QString::fromStdString(name));
item->setData(Qt::UserRole, var);
if(std::holds_alternative<PakDir>(node)) {
item->setIcon(QIcon::fromTheme("folder"));
} else {
item->setIcon(QIcon::fromTheme("text-x-generic"));
}
list.addItem(item);
}
}
MainWindow::MainWindow(QWidget *parent) : MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent}, QMainWindow{parent},
Ui::MainWindow{}, Ui::MainWindow{}
m_errors{}
{ {
setupUi(this); setupUi(this);
actionOpen->setShortcut(QKeySequence(QKeySequence::Open));
actionQuit->setShortcut(QKeySequence(QKeySequence::Quit));
} }
void MainWindow::fileOpen() { void MainWindow::openTest() {
auto fileName =
QFileDialog::getOpenFileName(
this,
tr("Open Archive"),
QString{},
tr("Quake PACK file (*.pak);;"
"All files (*)"));
if(!fileName.isEmpty()) {
try {
auto st = openReadBin(fileName.toStdString());
auto pak = readPak(st);
setListToDir(*listWidget, pak);
} catch(std::exception const &exc) {
m_errors.showMessage(tr(exc.what()));
}
}
} }
// EOF // EOF

View File

@ -2,8 +2,6 @@
#include "quam/ui_main_window.h" #include "quam/ui_main_window.h"
#include <QErrorMessage>
#include <QFileDialog>
#include <QMainWindow> #include <QMainWindow>
#include <QWidget> #include <QWidget>
@ -14,10 +12,7 @@ public:
explicit MainWindow(QWidget *parent = nullptr); explicit MainWindow(QWidget *parent = nullptr);
public slots: public slots:
void fileOpen(); void openTest();
private:
QErrorMessage m_errors;
}; };
// EOF // EOF

View File

@ -16,10 +16,17 @@
<widget class="QWidget" name="centralwidget"> <widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_2"> <layout class="QHBoxLayout" name="horizontalLayout_2">
<item> <item>
<widget class="QListWidget" name="listWidget"/> <widget class="QPushButton" name="pushButton">
</item> <property name="sizePolicy">
<item> <sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<widget class="QPlainTextEdit" name="textEdit"/> <horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
</item> </item>
</layout> </layout>
</widget> </widget>
@ -32,61 +39,20 @@
<height>29</height> <height>29</height>
</rect> </rect>
</property> </property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>&amp;File</string>
</property>
<addaction name="actionOpen"/>
<addaction name="separator"/>
<addaction name="actionQuit"/>
</widget>
<addaction name="menuFile"/>
</widget> </widget>
<widget class="QStatusBar" name="statusbar"/> <widget class="QStatusBar" name="statusbar"/>
<action name="actionOpen">
<property name="icon">
<iconset theme="document-open"/>
</property>
<property name="text">
<string>&amp;Open</string>
</property>
</action>
<action name="actionQuit">
<property name="icon">
<iconset theme="application-exit"/>
</property>
<property name="text">
<string>&amp;Quit</string>
</property>
</action>
</widget> </widget>
<resources/> <resources/>
<connections> <connections>
<connection> <connection>
<sender>actionOpen</sender> <sender>pushButton</sender>
<signal>triggered()</signal> <signal>clicked()</signal>
<receiver>MainWindow</receiver> <receiver>MainWindow</receiver>
<slot>fileOpen()</slot> <slot>openTest()</slot>
<hints> <hints>
<hint type="sourcelabel"> <hint type="sourcelabel">
<x>-1</x> <x>59</x>
<y>-1</y> <y>53</y>
</hint>
<hint type="destinationlabel">
<x>319</x>
<y>239</y>
</hint>
</hints>
</connection>
<connection>
<sender>actionQuit</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>close()</slot>
<hints>
<hint type="sourcelabel">
<x>-1</x>
<y>-1</y>
</hint> </hint>
<hint type="destinationlabel"> <hint type="destinationlabel">
<x>319</x> <x>319</x>
@ -96,6 +62,6 @@
</connection> </connection>
</connections> </connections>
<slots> <slots>
<slot>fileOpen()</slot> <slot>openTest()</slot>
</slots> </slots>
</ui> </ui>

View File

@ -8,7 +8,7 @@ struct PakHeader {
struct PakEntry { struct PakEntry {
std::string name; std::string name;
PakFile data; QByteArray data;
}; };
static constexpr quint32 sizeOfPakEntry = 64; static constexpr quint32 sizeOfPakEntry = 64;
@ -42,7 +42,7 @@ static PakEntry readPakEntry(std::istream &st) {
st.seekg(entOffset); st.seekg(entOffset);
PakFile bytes; QByteArray bytes;
bytes.resize(entSize); bytes.resize(entSize);
st.read(bytes.data(), entSize); st.read(bytes.data(), entSize);
@ -50,44 +50,27 @@ static PakEntry readPakEntry(std::istream &st) {
auto zero = std::find(entName.cbegin(), entName.cend(), '\0'); auto zero = std::find(entName.cbegin(), entName.cend(), '\0');
std::string name; std::string out;
std::copy(entName.cbegin(), zero, std::back_inserter(name)); std::copy(entName.cbegin(), zero, std::back_inserter(out));
if(name.front() == '/') {
throw std::runtime_error("empty root directory name");
}
PakEntry ent; PakEntry ent;
ent.name = std::move(name); ent.name = std::move(out);
ent.data = std::move(bytes); ent.data = std::move(bytes);
return ent; return ent;
} }
void insertFile(PakDir &dir, std::string name, PakFile file) { PakFile readPakFile(std::istream &st) {
if(auto slash = name.find('/'); slash != std::string::npos) {
auto folder = name.substr(0, slash);
auto next = name.substr(slash + 1);
dir[folder] = PakNode{PakDir{}};
insertFile(std::get<PakDir>(dir[folder]),
std::move(next),
std::move(file));
} else {
dir[name] = PakNode{std::move(file)};
}
}
PakDir readPak(std::istream &st) {
auto hdr = readPakHeader(st); auto hdr = readPakHeader(st);
st.seekg(hdr.dirOffset); st.seekg(hdr.dirOffset);
PakDir root; PakFile pak;
for(quint32 i = 0; i < hdr.dirNum; i++) { for(quint32 i = 0; i < hdr.dirNum; i++) {
auto ent = readPakEntry(st); auto ent = readPakEntry(st);
insertFile(root, std::move(ent.name), std::move(ent.data)); pak.emplace(std::move(ent.name), std::move(ent.data));
} }
return root; return pak;
} }
// EOF // EOF

View File

@ -1,22 +1,8 @@
#pragma once #pragma once
struct PakNode; class PakFile : public std::map<std::string, QByteArray> {
struct PakDir : public std::map<std::string, PakNode> {
using std::map<std::string, PakNode>::map;
}; };
Q_DECLARE_METATYPE(PakDir)
struct PakFile : public QByteArray { PakFile readPakFile(std::istream &st);
using QByteArray::QByteArray;
};
Q_DECLARE_METATYPE(PakFile)
struct PakNode : public std::variant<PakDir, PakFile> {
using std::variant<PakDir, PakFile>::variant;
};
Q_DECLARE_METATYPE(PakNode)
PakDir readPak(std::istream &st);
// EOF // EOF