Compare commits

...

2 Commits

Author SHA1 Message Date
62253abab0 finish pak opening support 2019-09-28 04:17:55 -04:00
e5cf9ec68c finish pak loader 2019-09-28 02:09:51 -04:00
7 changed files with 174 additions and 67 deletions

View File

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

View File

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

View File

@ -2,14 +2,53 @@
#include "quam/main_window.h"
#include "quam/pak.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent},
Ui::MainWindow{}
{
setupUi(this);
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);
}
}
void MainWindow::openTest() {
MainWindow::MainWindow(QWidget *parent) :
QMainWindow{parent},
Ui::MainWindow{},
m_errors{}
{
setupUi(this);
actionOpen->setShortcut(QKeySequence(QKeySequence::Open));
actionQuit->setShortcut(QKeySequence(QKeySequence::Quit));
}
void MainWindow::fileOpen() {
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

View File

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

View File

@ -16,17 +16,10 @@
<widget class="QWidget" name="centralwidget">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QPushButton" name="pushButton">
<property name="sizePolicy">
<sizepolicy hsizetype="Preferred" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>PushButton</string>
</property>
</widget>
<widget class="QListWidget" name="listWidget"/>
</item>
<item>
<widget class="QPlainTextEdit" name="textEdit"/>
</item>
</layout>
</widget>
@ -39,20 +32,61 @@
<height>29</height>
</rect>
</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 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>
<resources/>
<connections>
<connection>
<sender>pushButton</sender>
<signal>clicked()</signal>
<sender>actionOpen</sender>
<signal>triggered()</signal>
<receiver>MainWindow</receiver>
<slot>openTest()</slot>
<slot>fileOpen()</slot>
<hints>
<hint type="sourcelabel">
<x>59</x>
<y>53</y>
<x>-1</x>
<y>-1</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 type="destinationlabel">
<x>319</x>
@ -62,6 +96,6 @@
</connection>
</connections>
<slots>
<slot>openTest()</slot>
<slot>fileOpen()</slot>
</slots>
</ui>

View File

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

View File

@ -1,8 +1,22 @@
#pragma once
class PakFile : public std::map<std::string, QByteArray> {
};
struct PakNode;
PakFile readPakFile(std::istream &st);
struct PakDir : public std::map<std::string, PakNode> {
using std::map<std::string, PakNode>::map;
};
Q_DECLARE_METATYPE(PakDir)
struct PakFile : public QByteArray {
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