-
Notifications
You must be signed in to change notification settings - Fork 1
/
jsonsprite.cpp
379 lines (349 loc) · 11.6 KB
/
jsonsprite.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
#include "jsonsprite.h"
JSONDisplay::JSONDisplay(const QJsonObject& d, DisplayType type) {
description = d["Description"].toString();
extrabit = d["ExtraBit"].toBool();
if (auto it = d.find("GFXInfo"); it != d.constEnd()) {
gfxinfo = GFXInfo{it->toObject()};
}
if (type == DisplayType::XY) {
x_or_index = d["X"].toInt();
y_or_value = d["Y"].toInt();
} else {
x_or_index = d["Index"].toInt();
y_or_value = d["Value"].toInt();
}
useText = d["UseText"].toBool();
auto tilesArr = d["Tiles"].toArray();
if (useText) {
tiles.reserve(1);
tiles.append(Tile(tilesArr[0].toObject()));
displaytext = d["DisplayText"].toString();
} else {
tiles.reserve(tilesArr.size());
std::for_each(tilesArr.cbegin(), tilesArr.cend(), [&](auto& t) {
tiles.append(Tile(t.toObject()));
});
}
}
JSONDisplay::JSONDisplay(const QString& d, const QVector<Tile>& ts, bool bit, int xx, int yy, bool text, const QString& disp, const GFXInfo& info) :
description(d),
extrabit(bit),
x_or_index(xx),
y_or_value(yy),
useText(text),
displaytext(disp),
gfxinfo(info)
{
tiles.append(ts);
}
QJsonObject JSONDisplay::toJson(DisplayType type) const {
QJsonObject obj{};
obj["Description"] = description;
obj["ExtraBit"] = extrabit;
if (type == DisplayType::XY) {
obj["X"] = x_or_index;
obj["Y"] = y_or_value;
} else {
obj["Index"] = x_or_index;
obj["Value"] = y_or_value;
}
obj["UseText"] = useText;
QJsonArray tilesArr{};
if (useText) {
obj["DisplayText"] = displaytext;
tilesArr.append(tiles[0].toJson());
} else {
obj["DisplayText"] = "";
std::for_each(tiles.cbegin(), tiles.cend(), [&](auto& t) {
tilesArr.append(t.toJson());
});
}
obj["Tiles"] = tilesArr;
obj["GFXInfo"] = gfxinfo.toJson();
return obj;
}
SingleGFXFile::SingleGFXFile(bool separate, int value) : separate{separate}, value{value} {
}
SingleGFXFile::SingleGFXFile(const QJsonObject& g) {
separate = g["Separate"].toBool();
value = g["Value"].toInt();
}
QJsonObject SingleGFXFile::toJson() const {
QJsonObject obj{};
obj["Separate"] = separate;
obj["Value"] = value;
return obj;
}
GFXInfo::GFXInfo(SingleGFXFile s0, SingleGFXFile s1, SingleGFXFile s2, SingleGFXFile s3) :
sp0(s0),
sp1(s1),
sp2(s2),
sp3(s3)
{
}
GFXInfo::GFXInfo(const QJsonObject& g) : sp0{false, 0x7F}, sp1{false, 0x7F}, sp2{false, 0x7F}, sp3{false, 0x7F} {
if (auto it = g.find("0"); it != g.constEnd()) {
sp0 = SingleGFXFile{it->toObject()};
}
if (auto it = g.find("1"); it != g.constEnd()) {
sp1 = SingleGFXFile{it->toObject()};
}
if (auto it = g.find("2"); it != g.constEnd()) {
sp2 = SingleGFXFile{it->toObject()};
}
if (auto it = g.find("3"); it != g.constEnd()) {
sp3 = SingleGFXFile{it->toObject()};
}
}
QJsonObject GFXInfo::toJson() const {
QJsonObject obj{};
if (sp0.value != 0x7F)
obj["0"] = sp0.toJson();
if (sp1.value != 0x7F)
obj["1"] = sp1.toJson();
if (sp2.value != 0x7F)
obj["2"] = sp2.toJson();
if (sp3.value != 0x7F)
obj["3"] = sp3.toJson();
return obj;
}
Tile::Tile(const QJsonObject& t) {
xoff = t["X offset"].toInt();
yoff = t["Y offset"].toInt();
tilenumber = t["map16 tile"].toInt();
}
Tile::Tile(int x, int y, int tileno) : xoff(x), yoff(y), tilenumber(tileno) {
}
QJsonObject Tile::toJson() const {
QJsonObject obj{};
obj["X offset"] = xoff;
obj["Y offset"] = yoff;
obj["map16 tile"] = tilenumber;
return obj;
}
Collection::Collection(const QJsonObject& c) {
name = c["Name"].toString();
extrabit = c["ExtraBit"].toBool();
for (int i = 1; i <= 12; i++) {
QString str = QString::asprintf("Extra Property Byte %d", i);
if (c.contains(str))
prop[i - 1] = c[str].toInt();
else
prop[i - 1] = 0;
}
}
QJsonObject Collection::toJson() const {
QJsonObject obj{};
obj["Name"] = name;
obj["ExtraBit"] = extrabit;
for (int i = 1; i <= 12; i++) {
QString str = QString::asprintf("Extra Property Byte %d", i);
obj[str] = prop[i - 1];
}
return obj;
}
void JsonSprite::from_file(const QString& name) {
if (name.length() == 0)
return;
qDebug() << "Reading from " << name;
m_name = name;
QFile file{m_name};
file.open(QFile::OpenModeFlag::ReadOnly);
if (name.endsWith(".json")) {
auto doc = QJsonDocument::fromJson(file.readAll());
obj = doc.object();
deserialize();
}
else if (name.endsWith(".cfg")) {
deserialize_cfg(file);
}
else {
QMessageBox::warning(nullptr, "Error", "Unrecognized file extension, valid extensions are: .cfg, .json", QMessageBox::Ok );
return;
}
}
void JsonSprite::deserialize_cfg(QFile& file) {
type = QString{file.readLine()}.toInt(nullptr, 16);
actlike = QString{file.readLine()}.toInt(nullptr, 16);
auto tweaks = QString{file.readLine()}.split(" ", Qt::SplitBehaviorFlags::SkipEmptyParts);
if (tweaks.length() != 6) {
QMessageBox::warning(nullptr, "Error", "CFG has unrecognized format", QMessageBox::Ok);
return;
}
t1656.from_byte(tweaks[0].toInt(nullptr, 16));
t1662.from_byte(tweaks[1].toInt(nullptr, 16));
t166e.from_byte(tweaks[2].toInt(nullptr, 16));
t167a.from_byte(tweaks[3].toInt(nullptr, 16));
t1686.from_byte(tweaks[4].toInt(nullptr, 16));
t190f.from_byte(tweaks[5].toInt(nullptr, 16));
auto props = QString{file.readLine()}.split(" ", Qt::SplitBehaviorFlags::SkipEmptyParts);
if (props.length() != 2) {
QMessageBox::warning(nullptr, "Error", "CFG has unrecognized format", QMessageBox::Ok);
return;
}
extraProp1 = props[0].toInt(nullptr, 16);
extraProp2 = props[1].toInt(nullptr, 16);
asmfile = QString{file.readLine()};
auto bytecount = QString{file.readLine()}.split(QRegularExpression(R"(\s|:)"), Qt::SplitBehaviorFlags::SkipEmptyParts);
if (bytecount.length() != 2) {
addbcountclear = 0;
addbcountset = 0;
} else {
addbcountclear = bytecount[0].toInt(nullptr, 16);
addbcountset = bytecount[1].toInt(nullptr, 16);
}
}
QByteArray JsonSprite::serialize_cfg() {
QByteArray text{};
QTextStream stream{&text};
stream << QString::asprintf("%02X\n%02X\n", type, actlike);
stream << QString::asprintf("%02X %02X %02X %02X %02X %02X\n", t1656.to_byte(), t1662.to_byte(), t166e.to_byte(), t167a.to_byte(), t1686.to_byte(), t190f.to_byte());
stream << QString::asprintf("%02X %02X\n", extraProp1, extraProp2);
stream << asmfile.trimmed() << '\n';
stream << QString::asprintf("%02X:%02X", addbcountclear, addbcountset);
stream.flush();
return text;
}
JsonSprite::JsonSprite() {
t1656 = J1656();
t1662 = J1662();
t166e = J166E();
t167a = J167A();
t1686 = J1686();
t190f = J190F();
asmfile = QString();
actlike = 0;
type = 0;
extraProp1 = 0;
extraProp2 = 0;
addbcountclear = 0;
addbcountset = 0;
map16 = QString();
displays = QVector<JSONDisplay>();
collections = QVector<Collection>();
dispType = DisplayType::XY;
}
void JsonSprite::reset() {
m_name.clear();
t1656.from_byte(0);
t1662.from_byte(0);
t166e.from_byte(0);
t167a.from_byte(0);
t1686.from_byte(0);
t190f.from_byte(0);
asmfile.clear();
actlike = 0;
type = 0;
extraProp1 = 0;
extraProp2 = 0;
addbcountclear = 0;
addbcountset = 0;
map16.clear();
displays.clear();
collections.clear();
dispType = DisplayType::XY;
}
void JsonSprite::deserialize() {
t1656.from_json(obj["$1656"].toObject());
t1662.from_json(obj["$1662"].toObject());
t166e.from_json(obj["$166E"].toObject());
t167a.from_json(obj["$167A"].toObject());
t1686.from_json(obj["$1686"].toObject());
t190f.from_json(obj["$190F"].toObject());
asmfile = obj["AsmFile"].toString();
actlike = obj["ActLike"].toInt();
type = obj["Type"].toInt();
extraProp1 = obj["Extra Property Byte 1"].toInt();
extraProp2 = obj["Extra Property Byte 2"].toInt();
addbcountclear = obj["Additional Byte Count (extra bit clear)"].toInt();
addbcountset = obj["Additional Byte Count (extra bit set)"].toInt();
map16 = obj["Map16"].toString();
if (obj.find("DisplayType") != obj.end())
dispType = obj["DisplayType"].toString() == "ExByte" ? DisplayType::ExtraByte : DisplayType::XY;
else
dispType = DisplayType::XY;
auto dispArr = obj["Displays"].toArray();
auto collArr = obj["Collection"].toArray();
displays.reserve(dispArr.size());
collections.reserve(collArr.size());
std::for_each(dispArr.cbegin(), dispArr.cend(), [&](auto& d) {
displays.push_back(JSONDisplay(d.toObject(), dispType));
});
std::for_each(collArr.cbegin(), collArr.cend(), [&](auto& c) {
collections.push_back(Collection(c.toObject()));
});
}
void JsonSprite::serialize() {
obj["AsmFile"] = asmfile.trimmed();
obj["ActLike"] = actlike;
obj["Type"] = type;
obj["Additional Byte Count (extra bit clear)"] = addbcountclear;
obj["Additional Byte Count (extra bit set)"] = addbcountset;
obj["Extra Property Byte 1"] = extraProp1;
obj["Extra Property Byte 2"] = extraProp2;
obj["Map16"] = map16;
QJsonArray dispArr{};
QJsonArray collArr{};
obj["DisplayType"] = dispType == DisplayType::XY ? "XY" : "ExByte";
std::for_each(displays.cbegin(), displays.cend(), [&](auto& d) {
dispArr.append(d.toJson(dispType));
});
std::for_each(collections.cbegin(), collections.cend(), [&](auto& c) {
collArr.append(c.toJson());
});
obj["Displays"] = dispArr;
obj["Collection"] = collArr;
obj["$1656"] = t1656.to_json();
obj["$1662"] = t1662.to_json();
obj["$166E"] = t166e.to_json();
obj["$167A"] = t167a.to_json();
obj["$1686"] = t1686.to_json();
obj["$190F"] = t190f.to_json();
}
QByteArray JsonSprite::to_text(const QString& filename) {
if (filename.endsWith(".cfg")) {
return serialize_cfg();
} else {
serialize();
QJsonDocument doc{obj};
return doc.toJson();
}
}
QString& JsonSprite::name() {
return m_name;
}
void JsonSprite::to_file(QString name) {
if (name.length() == 0) {
if (m_name.length() == 0)
name = QFileDialog::getSaveFileName(nullptr, "Save file", "", "JSON (*.json);;CFG (*.cfg)");
else
name = m_name;
if (name.length() == 0)
return;
QFile outFile{name};
outFile.open(QFile::OpenModeFlag::Truncate | QFile::OpenModeFlag::Text | QFile::OpenModeFlag::WriteOnly);
outFile.write(to_text(name));
} else {
QFile outFile{name};
outFile.open(QFile::OpenModeFlag::Truncate | QFile::OpenModeFlag::Text | QFile::OpenModeFlag::WriteOnly);
outFile.write(to_text(name));
}
}
void JsonSprite::addCollections(QTableView* view) {
auto model = view->model();
for (int i = 0; i < model->rowCount(); i++) {
QJsonObject obj;
obj["Name"] = model->index(i, 0).data().toString();
obj["ExtraBit"] = model->index(i, 1).data().toBool();
for (int j = 0; j < 12; j++) {
obj[QString::asprintf("Extra Property Byte %d", j + 1)] = model->index(i, j + 2).data().toString().toInt(nullptr, 16);
}
collections.append(Collection{obj});
}
}
void JsonSprite::addDisplay(const JSONDisplay& display) {
displays.append(display);
}
void JsonSprite::setMap16(const QString& mapdata) {
map16 = mapdata;
}