Long awaited hwmap<->txt converter, which took less than hour to make
authorunC0Rr
Thu, 07 May 2015 17:29:12 +0300
changeset 10936 104727dfaa1b
parent 10934 d6cc62ef78ef
child 10937 f7fd8a70dd93
Long awaited hwmap<->txt converter, which took less than hour to make
tools/hwmap2txt/hwmapconverter/hwmapconverter.pro
tools/hwmap2txt/hwmapconverter/main.cpp
tools/hwmap2txt/hwmapconverter/mainwindow.cpp
tools/hwmap2txt/hwmapconverter/mainwindow.h
tools/hwmap2txt/hwmapconverter/mainwindow.ui
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/hwmap2txt/hwmapconverter/hwmapconverter.pro	Thu May 07 17:29:12 2015 +0300
@@ -0,0 +1,20 @@
+#-------------------------------------------------
+#
+# Project created by QtCreator 2015-05-07T16:38:43
+#
+#-------------------------------------------------
+
+QT       += core gui
+
+greaterThan(QT_MAJOR_VERSION, 4): QT += widgets
+
+TARGET = hwmapconverter
+TEMPLATE = app
+
+
+SOURCES += main.cpp\
+        mainwindow.cpp
+
+HEADERS  += mainwindow.h
+
+FORMS    += mainwindow.ui
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/hwmap2txt/hwmapconverter/main.cpp	Thu May 07 17:29:12 2015 +0300
@@ -0,0 +1,11 @@
+#include "mainwindow.h"
+#include <QApplication>
+
+int main(int argc, char *argv[])
+{
+    QApplication a(argc, argv);
+    MainWindow w;
+    w.show();
+
+    return a.exec();
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/hwmap2txt/hwmapconverter/mainwindow.cpp	Thu May 07 17:29:12 2015 +0300
@@ -0,0 +1,137 @@
+#include <QByteArray>
+#include <QFile>
+#include <QFileDialog>
+#include <QtEndian>
+#include <QRegExp>
+
+#include "mainwindow.h"
+#include "ui_mainwindow.h"
+
+MainWindow::MainWindow(QWidget *parent) :
+    QMainWindow(parent),
+    ui(new Ui::MainWindow)
+{
+    ui->setupUi(this);
+}
+
+MainWindow::~MainWindow()
+{
+    delete ui;
+}
+
+void MainWindow::on_pbLoad_clicked()
+{
+    QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(), "Hedgewars drawn maps (*.hwmap);;All files (*.*)");
+
+    if(!fileName.isEmpty())
+    {
+        QFile f(fileName);
+
+        if(f.open(QFile::ReadOnly))
+        {
+            QByteArray data = qUncompress(QByteArray::fromBase64(f.readAll()));
+
+            QStringList decoded;
+
+            bool isSpecial = true;
+            while(data.size() >= 5)
+            {
+                qint16 px = qFromBigEndian(*(qint16 *)data.data());
+                data.remove(0, 2);
+                qint16 py = qFromBigEndian(*(qint16 *)data.data());
+                data.remove(0, 2);
+                quint8 flags = *(quint8 *)data.data();
+                data.remove(0, 1);
+
+                if(flags & 0x80)
+                {
+                    if(isSpecial && !decoded.isEmpty())
+                        decoded << "// drawings";
+
+                    isSpecial = false;
+
+                    quint8 penWidth = flags & 0x3f;
+                    bool isErasing = flags & 0x40;
+                    decoded << QString("%1 %2 %3 %4")
+                               .arg(px, 5).arg(py, 6)
+                               .arg(isErasing ? "e" : "s")
+                               .arg(penWidth, 2);
+                } else
+                    if(isSpecial)
+                    {
+                        if(decoded.isEmpty())
+                            decoded << "// special points (these are always before all drawings!)";
+
+                        decoded << QString("%1 %2 %3")
+                                   .arg(px, 5).arg(py, 6)
+                                   .arg(flags);
+                    } else
+                    {
+                        decoded << QString("%1 %2")
+                                   .arg(px, 5).arg(py, 6);
+                    }
+            }
+
+            ui->textEdit->setPlainText(decoded.join("\n"));
+            ui->statusBar->showMessage("Load OK");
+        } else
+            ui->statusBar->showMessage(QString("Can't open file %1").arg(fileName));
+    }
+}
+
+void MainWindow::on_pbSave_clicked()
+{
+    QRegExp rxSP("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*(\\d+)\\s*$");
+    QRegExp rxLS("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*([es])\\s*(\\d+)\\s*$");
+    QRegExp rxP("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*$");
+
+    QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), "Hedgewars drawn maps (*.hwmap);;All files (*.*)");
+
+    QFile file(fileName);
+    if(file.open(QFile::WriteOnly))
+    {
+        QByteArray b;
+        QStringList sl = ui->textEdit->toPlainText().split('\n');
+        bool isSpecial = true;
+
+        foreach(const QString & line, sl)
+            if(!line.startsWith("//"))
+            {
+                if(rxLS.indexIn(line) != -1)
+                {
+                    isSpecial = false;
+                    qint16 px = qToBigEndian((qint16)rxLS.cap(1).toInt());
+                    qint16 py = qToBigEndian((qint16)rxLS.cap(2).toInt());
+                    quint8 flags = 0x80;
+                    if(rxLS.cap(3) == "e") flags |= 0x40;
+                    flags = flags + rxLS.cap(4).toUInt();
+                    b.append((const char *)&px, 2);
+                    b.append((const char *)&py, 2);
+                    b.append((const char *)&flags, 1);
+                } else
+                if(isSpecial && (rxSP.indexIn(line) != -1))
+                {
+                    qint16 px = qToBigEndian((qint16)rxSP.cap(1).toInt());
+                    qint16 py = qToBigEndian((qint16)rxSP.cap(2).toInt());
+                    quint8 flags = rxSP.cap(3).toUInt();
+
+                    b.append((const char *)&px, 2);
+                    b.append((const char *)&py, 2);
+                    b.append((const char *)&flags, 1);
+                } else
+                if(rxP.indexIn(line) != -1)
+                {
+                    isSpecial = false;
+                    qint16 px = qToBigEndian((qint16)rxP.cap(1).toInt());
+                    qint16 py = qToBigEndian((qint16)rxP.cap(2).toInt());
+                    quint8 flags = 0;
+                    b.append((const char *)&px, 2);
+                    b.append((const char *)&py, 2);
+                    b.append((const char *)&flags, 1);
+                } else
+                    ui->statusBar->showMessage(QString("Can't parse or misplaced special point: %1").arg(line));
+            }
+
+        file.write(qCompress(b).toBase64());
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/hwmap2txt/hwmapconverter/mainwindow.h	Thu May 07 17:29:12 2015 +0300
@@ -0,0 +1,27 @@
+#ifndef MAINWINDOW_H
+#define MAINWINDOW_H
+
+#include <QMainWindow>
+
+namespace Ui {
+class MainWindow;
+}
+
+class MainWindow : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    explicit MainWindow(QWidget *parent = 0);
+    ~MainWindow();
+
+private slots:
+    void on_pbLoad_clicked();
+
+    void on_pbSave_clicked();
+
+private:
+    Ui::MainWindow *ui;
+};
+
+#endif // MAINWINDOW_H
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/hwmap2txt/hwmapconverter/mainwindow.ui	Thu May 07 17:29:12 2015 +0300
@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<ui version="4.0">
+ <class>MainWindow</class>
+ <widget class="QMainWindow" name="MainWindow">
+  <property name="geometry">
+   <rect>
+    <x>0</x>
+    <y>0</y>
+    <width>400</width>
+    <height>300</height>
+   </rect>
+  </property>
+  <property name="windowTitle">
+   <string>MainWindow</string>
+  </property>
+  <widget class="QWidget" name="centralWidget">
+   <layout class="QGridLayout" name="gridLayout">
+    <item row="1" column="0">
+     <widget class="QPushButton" name="pbLoad">
+      <property name="text">
+       <string>Load</string>
+      </property>
+     </widget>
+    </item>
+    <item row="1" column="1">
+     <widget class="QPushButton" name="pbSave">
+      <property name="text">
+       <string>Save</string>
+      </property>
+     </widget>
+    </item>
+    <item row="1" column="2">
+     <spacer name="horizontalSpacer">
+      <property name="orientation">
+       <enum>Qt::Horizontal</enum>
+      </property>
+      <property name="sizeHint" stdset="0">
+       <size>
+        <width>217</width>
+        <height>20</height>
+       </size>
+      </property>
+     </spacer>
+    </item>
+    <item row="0" column="0" colspan="3">
+     <widget class="QTextEdit" name="textEdit">
+      <property name="font">
+       <font>
+        <family>Liberation Mono</family>
+        <pointsize>10</pointsize>
+       </font>
+      </property>
+     </widget>
+    </item>
+   </layout>
+  </widget>
+  <widget class="QStatusBar" name="statusBar"/>
+ </widget>
+ <layoutdefault spacing="6" margin="11"/>
+ <resources/>
+ <connections/>
+</ui>