tools/hwmap2txt/hwmapconverter/mainwindow.cpp
author alfadur
Mon, 10 Sep 2018 23:31:44 +0300
changeset 13807 24fe56d3c6a2
parent 11015 7a905f0070ce
permissions -rw-r--r--
improve error handling for TLS handshake
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11015
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     1
#include <QByteArray>
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     2
#include <QFile>
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     3
#include <QFileDialog>
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     4
#include <QtEndian>
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     5
#include <QRegExp>
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     6
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     7
#include "mainwindow.h"
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     8
#include "ui_mainwindow.h"
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
     9
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    10
MainWindow::MainWindow(QWidget *parent) :
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    11
    QMainWindow(parent),
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    12
    ui(new Ui::MainWindow)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    13
{
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    14
    ui->setupUi(this);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    15
}
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    16
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    17
MainWindow::~MainWindow()
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    18
{
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    19
    delete ui;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    20
}
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    21
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    22
void MainWindow::on_pbLoad_clicked()
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    23
{
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    24
    QString fileName = QFileDialog::getOpenFileName(this, QString(), QString(), "Hedgewars drawn maps (*.hwmap);;All files (*.*)");
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    25
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    26
    if(!fileName.isEmpty())
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    27
    {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    28
        QFile f(fileName);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    29
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    30
        if(f.open(QFile::ReadOnly))
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    31
        {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    32
            QByteArray data = qUncompress(QByteArray::fromBase64(f.readAll()));
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    33
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    34
            QStringList decoded;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    35
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    36
            bool isSpecial = true;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    37
            while(data.size() >= 5)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    38
            {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    39
                qint16 px = qFromBigEndian(*(qint16 *)data.data());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    40
                data.remove(0, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    41
                qint16 py = qFromBigEndian(*(qint16 *)data.data());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    42
                data.remove(0, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    43
                quint8 flags = *(quint8 *)data.data();
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    44
                data.remove(0, 1);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    45
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    46
                if(flags & 0x80)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    47
                {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    48
                    if(isSpecial && !decoded.isEmpty())
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    49
                        decoded << "// drawings";
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    50
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    51
                    isSpecial = false;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    52
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    53
                    quint8 penWidth = flags & 0x3f;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    54
                    bool isErasing = flags & 0x40;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    55
                    decoded << QString("%1 %2 %3 %4")
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    56
                               .arg(px, 5).arg(py, 6)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    57
                               .arg(isErasing ? "e" : "s")
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    58
                               .arg(penWidth, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    59
                } else
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    60
                    if(isSpecial)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    61
                    {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    62
                        if(decoded.isEmpty())
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    63
                            decoded << "// special points (these are always before all drawings!)";
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    64
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    65
                        decoded << QString("%1 %2 %3")
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    66
                                   .arg(px, 5).arg(py, 6)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    67
                                   .arg(flags);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    68
                    } else
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    69
                    {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    70
                        decoded << QString("%1 %2")
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    71
                                   .arg(px, 5).arg(py, 6);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    72
                    }
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    73
            }
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    74
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    75
            ui->textEdit->setPlainText(decoded.join("\n"));
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    76
            ui->statusBar->showMessage("Load OK");
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    77
        } else
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    78
            ui->statusBar->showMessage(QString("Can't open file %1").arg(fileName));
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    79
    }
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    80
}
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    81
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    82
void MainWindow::on_pbSave_clicked()
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    83
{
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    84
    QRegExp rxSP("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*(\\d+)\\s*$");
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    85
    QRegExp rxLS("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*([es])\\s*(\\d+)\\s*$");
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    86
    QRegExp rxP("^\\s*(-?\\d+)\\s*(-?\\d+)\\s*$");
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    87
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    88
    QString fileName = QFileDialog::getSaveFileName(this, QString(), QString(), "Hedgewars drawn maps (*.hwmap);;All files (*.*)");
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    89
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    90
    QFile file(fileName);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    91
    if(file.open(QFile::WriteOnly))
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    92
    {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    93
        QByteArray b;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    94
        QStringList sl = ui->textEdit->toPlainText().split('\n');
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    95
        bool isSpecial = true;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    96
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    97
        foreach(const QString & line, sl)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    98
            if(!line.startsWith("//"))
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
    99
            {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   100
                if(rxLS.indexIn(line) != -1)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   101
                {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   102
                    isSpecial = false;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   103
                    qint16 px = qToBigEndian((qint16)rxLS.cap(1).toInt());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   104
                    qint16 py = qToBigEndian((qint16)rxLS.cap(2).toInt());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   105
                    quint8 flags = 0x80;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   106
                    if(rxLS.cap(3) == "e") flags |= 0x40;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   107
                    flags = flags + rxLS.cap(4).toUInt();
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   108
                    b.append((const char *)&px, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   109
                    b.append((const char *)&py, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   110
                    b.append((const char *)&flags, 1);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   111
                } else
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   112
                if(isSpecial && (rxSP.indexIn(line) != -1))
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   113
                {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   114
                    qint16 px = qToBigEndian((qint16)rxSP.cap(1).toInt());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   115
                    qint16 py = qToBigEndian((qint16)rxSP.cap(2).toInt());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   116
                    quint8 flags = rxSP.cap(3).toUInt();
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   117
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   118
                    b.append((const char *)&px, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   119
                    b.append((const char *)&py, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   120
                    b.append((const char *)&flags, 1);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   121
                } else
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   122
                if(rxP.indexIn(line) != -1)
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   123
                {
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   124
                    isSpecial = false;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   125
                    qint16 px = qToBigEndian((qint16)rxP.cap(1).toInt());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   126
                    qint16 py = qToBigEndian((qint16)rxP.cap(2).toInt());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   127
                    quint8 flags = 0;
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   128
                    b.append((const char *)&px, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   129
                    b.append((const char *)&py, 2);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   130
                    b.append((const char *)&flags, 1);
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   131
                } else
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   132
                    ui->statusBar->showMessage(QString("Can't parse or misplaced special point: %1").arg(line));
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   133
            }
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   134
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   135
        file.write(qCompress(b).toBase64());
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   136
    }
7a905f0070ce CRLF purge
nemo
parents: 10936
diff changeset
   137
}