forked from mrkite/TerraFirma
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworldheader.cpp
168 lines (154 loc) · 5.27 KB
/
worldheader.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
* @Copyright 2015 seancode
*
* Reads the header of a world file, as well as the json that defines it.
*/
#include "./worldheader.h"
#include <QFile>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonObject>
#include <QDebug>
WorldHeader::WorldHeader() {}
WorldHeader::~WorldHeader() {}
void WorldHeader::init() {
QFile file(":/res/header.json");
if (!file.open(QIODevice::ReadOnly))
throw InitException("header.json is missing!");
QJsonDocument doc = QJsonDocument::fromJson(file.readAll());
file.close();
if (doc.isNull())
throw InitException("header.json is corrupt");
if (!doc.isArray())
throw InitException("header.json isn't an array");
for (auto const &field : doc.array()) {
QJsonObject const &obj = field.toObject();
fields.append(Field(obj));
}
}
void WorldHeader::load(QSharedPointer<Handle> handle, int version) {
data.clear();
int num;
for (auto const &field : fields) {
if (version >= field.minVersion) {
auto header = QSharedPointer<Header>(new Header());
switch (field.type) {
case Field::Type::BOOLEAN: header->setData(handle->r8()); break;
case Field::Type::BYTE: header->setData(handle->r8()); break;
case Field::Type::INT16: header->setData(handle->r16()); break;
case Field::Type::INT32: header->setData(handle->r32()); break;
case Field::Type::INT64: header->setData(handle->r64()); break;
case Field::Type::FLOAT32: header->setData(handle->rf()); break;
case Field::Type::FLOAT64: header->setData(handle->rd()); break;
case Field::Type::STRING: header->setData(handle->rs()); break;
case Field::Type::ARRAY_BYTE:
num = field.length;
if (!field.dynamicLength.isEmpty())
num = data[field.dynamicLength]->toInt();
for (int i = 0; i < num; i++)
header->append(handle->r8());
break;
case Field::Type::ARRAY_INT32:
num = field.length;
if (!field.dynamicLength.isEmpty())
num = data[field.dynamicLength]->toInt();
for (int i = 0; i < num; i++)
header->append(handle->r32());
break;
case Field::Type::ARRAY_STRING:
num = field.length;
if (!field.dynamicLength.isEmpty())
num = data[field.dynamicLength]->toInt();
for (int i = 0; i < num; i++)
header->append(handle->rs());
break;
}
data[field.name] = header;
}
}
}
QSharedPointer<WorldHeader::Header> WorldHeader::operator[](
QString const &key) const {
if (!data.contains(key))
qDebug() << "Key not found: " << key;
return data[key];
}
bool WorldHeader::is(const QString &key) const {
if (!data.contains(key))
return false;
return data[key]->toInt();
}
int WorldHeader::treeStyle(int x) const {
auto xs = data["treeX"];
int i;
for (i = 0; i < xs->length(); i++) {
if (x <= xs->at(i)->toInt())
break;
}
int style = data["treeStyle"]->at(i)->toInt();
switch (style) {
case 0:
return 0;
case 5:
return 10;
default:
return style + 5;
}
}
WorldHeader::Field::Field(QJsonObject const &data) {
name = data["name"].toString();
QString t = data["type"].toString();
if (t.isNull()) type = Type::BOOLEAN;
else if (t == "s") type = (data.contains("num") || data.contains("relnum")) ?
Type::ARRAY_STRING : Type::STRING;
else if (t == "u8") type = (data.contains("num")
|| data.contains("relnum")) ?
Type::ARRAY_BYTE : Type::BYTE;
else if (t == "i16") type = Type::INT16;
else if (t == "i32") type = (data.contains("num")
|| data.contains("relnum")) ?
Type::ARRAY_INT32 : Type::INT32;
else if (t == "i64") type = Type::INT64;
else if (t == "f32") type = Type::FLOAT32;
else if (t == "f64") type = Type::FLOAT64;
else
throw InitException(QString("Invalid header type: %1").arg(type));
length = data["num"].toInt();
dynamicLength = data["relnum"].toString();
minVersion = data.contains("min") ? data["min"].toInt() : 88;
}
static WorldHeader::Header Null;
WorldHeader::Header::Header() {
ddbl = 0.0;
dint = 0;
}
WorldHeader::Header::~Header() {}
int WorldHeader::Header::length() const {
return arr.count();
}
QSharedPointer<WorldHeader::Header> WorldHeader::Header::at(int i) const {
return arr[i];
}
int WorldHeader::Header::toInt() const {
return dint;
}
double WorldHeader::Header::toDouble() const {
return ddbl;
}
void WorldHeader::Header::setData(quint64 v) {
dint = v;
ddbl = static_cast<double>(v);
}
void WorldHeader::Header::setData(QString s) {
dstr = s;
}
void WorldHeader::Header::append(QString s) {
auto h = QSharedPointer<Header>(new Header());
h->setData(s);
arr.append(h);
}
void WorldHeader::Header::append(quint64 v) {
auto h = QSharedPointer<Header>(new Header());
h->setData(v);
arr.append(h);
}