finish pak opening support

This commit is contained in:
alison wright 2019-09-28 04:17:55 -04:00
parent e5cf9ec68c
commit 62253abab0
7 changed files with 120 additions and 32 deletions

View File

@ -64,4 +64,8 @@ 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};
}
// EOF // EOF

View File

@ -42,14 +42,17 @@ 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);
auto pak = readPak(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 == 0) { if(argc <= 1) {
return modeGui(argc, argv); return modeGui(argc, argv);
} else { } else {
return modeText(argc, argv); return modeText(argc, argv);

View File

@ -2,14 +2,53 @@
#include "quam/main_window.h" #include "quam/main_window.h"
#include "quam/pak.h" #include "quam/pak.h"
MainWindow::MainWindow(QWidget *parent) : static void setListToDir(QListWidget &list, PakDir const &dir) {
QMainWindow{parent}, list.clear();
Ui::MainWindow{} for(auto const &kv : dir) {
{ auto const &name = kv.first;
setupUi(this); 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 // EOF

View File

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

View File

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

View File

@ -54,7 +54,7 @@ static PakEntry readPakEntry(std::istream &st) {
std::copy(entName.cbegin(), zero, std::back_inserter(name)); std::copy(entName.cbegin(), zero, std::back_inserter(name));
if(name.front() == '/') { if(name.front() == '/') {
name.erase(0, 1); throw std::runtime_error("empty root directory name");
} }
PakEntry ent; PakEntry ent;
@ -67,12 +67,12 @@ void insertFile(PakDir &dir, std::string name, PakFile file) {
if(auto slash = name.find('/'); slash != std::string::npos) { if(auto slash = name.find('/'); slash != std::string::npos) {
auto folder = name.substr(0, slash); auto folder = name.substr(0, slash);
auto next = name.substr(slash + 1); auto next = name.substr(slash + 1);
dir[folder] = std::make_unique<PakNode>(PakDir{}); dir[folder] = PakNode{PakDir{}};
insertFile(std::get<PakDir>(*dir[folder]), insertFile(std::get<PakDir>(dir[folder]),
std::move(next), std::move(next),
std::move(file)); std::move(file));
} else { } else {
dir[name] = std::make_unique<PakNode>(std::move(file)); dir[name] = PakNode{std::move(file)};
} }
} }

View File

@ -2,17 +2,20 @@
struct PakNode; struct PakNode;
struct PakDir : public std::map<std::string, std::unique_ptr<PakNode>> { struct PakDir : public std::map<std::string, PakNode> {
using std::map<std::string, std::unique_ptr<PakNode>>::map; using std::map<std::string, PakNode>::map;
}; };
Q_DECLARE_METATYPE(PakDir)
struct PakFile : public QByteArray { struct PakFile : public QByteArray {
using QByteArray::QByteArray; using QByteArray::QByteArray;
}; };
Q_DECLARE_METATYPE(PakFile)
struct PakNode : public std::variant<PakDir, PakFile> { struct PakNode : public std::variant<PakDir, PakFile> {
using std::variant<PakDir, PakFile>::variant; using std::variant<PakDir, PakFile>::variant;
}; };
Q_DECLARE_METATYPE(PakNode)
PakDir readPak(std::istream &st); PakDir readPak(std::istream &st);