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