forked from Plethora777/mcpe_viz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmcpe_viz.nbt.cc
2725 lines (2373 loc) · 82.4 KB
/
mcpe_viz.nbt.cc
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Minecraft Pocket Edition (MCPE) World File Visualization & Reporting Tool
(c) Plethora777, 2015.9.26
GPL'ed code - see LICENSE
NBT support
*/
#include <stdio.h>
#include <fstream>
#include <algorithm>
#if defined(__APPLE__) || defined(__FreeBSD__)
#include <cmath>
#endif
#include "mcpe_viz.util.h"
#include "mcpe_viz.h"
#include "mcpe_viz.nbt.h"
namespace mcpe_viz {
std::string makeGeojsonHeader(double ix, double iy, bool adjustCoordFlag) {
char tmpstring[256];
std::string s =
"{"
"\"type\":\"Feature\","
"\"geometry\":{\"type\":\"Point\",\"coordinates\":["
;
if ( std::isnan(ix) || std::isnan(iy) ) {
// we don't put out anything because "NaN" is not valid JSON
} else {
// adjust position so that items are in the center of pixels
// todobig - play with this to see if we can make it better
if ( adjustCoordFlag ) {
ix += 0.5; //todobig - plus or minus here? hmm
iy += 0.5;
}
sprintf(tmpstring,"%.1lf,%.1lf",ix,iy);
s += tmpstring;
}
s +=
"]},"
"\"properties\":{"
;
return s;
}
std::string makeGeojsonHeader_MultiPoint(int n, double *ix, double *iy) {
char tmpstring[256];
std::string s =
"{"
"\"type\":\"Feature\","
"\"geometry\":{\"type\":\"MultiPoint\",\"coordinates\":["
;
for (int i=0; i < n; i++) {
// adjust position so that items are in the center of pixels
// todobig - play with this to see if we can make it better
if ( true ) {
ix[i] += 0.5;
iy[i] += 0.5;
}
sprintf(tmpstring,"[%.1lf,%.1lf]",ix[i],iy[i]);
s += tmpstring;
if ( i < (n-1) ) {
s += ",";
}
}
s +=
"]},"
"\"properties\":{"
;
return s;
}
// nbt parsing helpers
int32_t globalNbtListNumber=0;
int32_t globalNbtCompoundNumber=0;
int32_t parseNbtTag( const char* hdr, int& indent, const MyNbtTag& t ) {
logger.msg(kLogInfo1,"%s[%s] ", makeIndent(indent,hdr).c_str(), t.first.c_str());
nbt::tag_type tagType = t.second->get_type();
switch ( tagType ) {
case nbt::tag_type::End:
logger.msg(kLogInfo1,"TAG_END\n");
break;
case nbt::tag_type::Byte:
{
nbt::tag_byte v = t.second->as<nbt::tag_byte>();
logger.msg(kLogInfo1,"%d 0x%x (byte)\n", v.get(), v.get());
}
break;
case nbt::tag_type::Short:
{
nbt::tag_short v = t.second->as<nbt::tag_short>();
logger.msg(kLogInfo1,"%d 0x%x (short)\n", v.get(), v.get());
}
break;
case nbt::tag_type::Int:
{
nbt::tag_int v = t.second->as<nbt::tag_int>();
logger.msg(kLogInfo1,"%d 0x%x (int)\n", v.get(), v.get());
}
break;
case nbt::tag_type::Long:
{
nbt::tag_long v = t.second->as<nbt::tag_long>();
// note: silly work around for linux vs win32 weirdness
logger.msg(kLogInfo1,"%lld 0x%llx (long)\n", (long long int)v.get(), (long long int)v.get());
}
break;
case nbt::tag_type::Float:
{
nbt::tag_float v = t.second->as<nbt::tag_float>();
logger.msg(kLogInfo1,"%f (float)\n", v.get());
}
break;
case nbt::tag_type::Double:
{
nbt::tag_double v = t.second->as<nbt::tag_double>();
logger.msg(kLogInfo1,"%lf (double)\n", v.get());
}
break;
case nbt::tag_type::Byte_Array:
{
nbt::tag_byte_array v = t.second->as<nbt::tag_byte_array>();
logger.msg(kLogInfo1,"[");
int32_t i=0;
for (const auto& itt: v ) {
if ( i++ > 0 ) { logger.msg(kLogInfo1," "); }
logger.msg(kLogInfo1,"%02x", (int)itt);
}
logger.msg(kLogInfo1,"] (hex byte array)\n");
}
break;
case nbt::tag_type::String:
{
nbt::tag_string v = t.second->as<nbt::tag_string>();
logger.msg(kLogInfo1,"'%s' (string)\n", v.get().c_str());
}
break;
case nbt::tag_type::List:
{
nbt::tag_list v = t.second->as<nbt::tag_list>();
int32_t lnum = ++globalNbtListNumber;
logger.msg(kLogInfo1,"LIST-%d {\n",lnum);
indent++;
for ( const auto& it: v ) {
parseNbtTag( hdr, indent, std::make_pair(std::string(""), it.get().clone() ) );
}
if ( --indent < 0 ) { indent=0; }
logger.msg(kLogInfo1,"%s} LIST-%d\n", makeIndent(indent,hdr).c_str(), lnum);
}
break;
case nbt::tag_type::Compound:
{
nbt::tag_compound v = t.second->as<nbt::tag_compound>();
int32_t cnum = ++globalNbtCompoundNumber;
logger.msg(kLogInfo1,"COMPOUND-%d {\n",cnum);
indent++;
for ( const auto& it: v ) {
parseNbtTag( hdr, indent, std::make_pair( it.first, it.second.get().clone() ) );
}
if ( --indent < 0 ) { indent=0; }
logger.msg(kLogInfo1,"%s} COMPOUND-%d\n", makeIndent(indent,hdr).c_str(),cnum);
}
break;
case nbt::tag_type::Int_Array:
{
nbt::tag_int_array v = t.second->as<nbt::tag_int_array>();
logger.msg(kLogInfo1,"[");
int32_t i=0;
for ( const auto& itt: v ) {
if ( i++ > 0 ) { logger.msg(kLogInfo1," "); }
logger.msg(kLogInfo1,"%x", itt);
}
logger.msg(kLogInfo1,"] (hex int array)\n");
}
break;
default:
logger.msg(kLogInfo1,"[ERROR: Unknown tag type = %d]\n", (int)tagType);
break;
}
return 0;
}
int32_t parseNbt( const char* hdr, const char* buf, int32_t bufLen, MyNbtTagList& tagList ) {
int32_t indent=0;
logger.msg(kLogInfo1,"%sNBT Decode Start\n",makeIndent(indent,hdr).c_str());
// these help us look at dumped nbt data and match up LIST's and COMPOUND's
globalNbtListNumber=0;
globalNbtCompoundNumber=0;
std::istringstream is(std::string(buf,bufLen));
nbt::io::stream_reader reader(is, endian::little);
// remove all elements from taglist
tagList.clear();
// read all tags
MyNbtTag t;
bool done = false;
std::istream& pis = reader.get_istr();
while ( !done && (pis) && (!pis.eof()) ) {
try {
// todo emplace_back?
tagList.push_back(reader.read_tag());
}
catch (std::exception& e) {
// check for eof which means all is well
if ( ! pis.eof() ) {
fprintf(stderr, "NBT exception: (%s) (eof=%s) (is=%s) (tc=%d) (pos=%d) (buflen=%d) (parseNbt)\n"
, e.what()
, pis.eof() ? "true" : "false"
, (pis) ? "true" : "false"
, (int)tagList.size()
, (int)pis.tellg()
, bufLen
);
// todo - testing
//dumpBuffer("nbt-buffer", buf, bufLen);
}
done = true;
}
}
// iterate over the tags
for ( const auto& itt: tagList ) {
parseNbtTag( hdr, indent, itt );
}
logger.msg(kLogInfo1,"%sNBT Decode End (%d tags)\n",makeIndent(indent,hdr).c_str(), (int)tagList.size());
return 0;
}
int32_t parseNbtQuiet( const char* buf, int32_t bufLen, int32_t numToRead, MyNbtTagList& tagList ) {
std::istringstream is(std::string(buf,bufLen));
nbt::io::stream_reader reader(is, endian::little);
// remove all elements from taglist
tagList.clear();
// read all tags
MyNbtTag t;
bool done = false;
int32_t numRead = 0;
std::istream& pis = reader.get_istr();
while ( !done && (pis) && (!pis.eof()) ) {
if ( numToRead > 0 && ++numRead >= numToRead ) {
done = true;
}
try {
// todo emplace_back?
tagList.push_back(reader.read_tag());
}
catch (std::exception& e) {
// check for eof which means all is well
if ( ! pis.eof() ) {
fprintf(stderr, "NBT exception: (%s) (eof=%s) (is=%s) (tc=%d) (pos=%d) (buflen=%d) (parseNbtQuiet)\n"
, e.what()
, pis.eof() ? "true" : "false"
, (pis) ? "true" : "false"
, (int)tagList.size()
, (int)pis.tellg()
, bufLen
);
// todo - testing
//dumpBuffer("nbt-buffer", buf, bufLen);
}
done = true;
}
}
return 0;
}
// todo - use this throughout parsing or is it madness? :)
template<class T>
class MyValue {
public:
T value;
bool valid;
MyValue() {
clear();
}
void clear() {
value=(T)0;
valid=false;
}
void set(T nvalue) {
value = nvalue;
valid = true;
}
std::string toString() {
if ( valid ) {
return std::string(""+value);
} else {
return std::string("*Invalid Value*");
}
}
};
template<class T>
class Point2d {
public:
T x, y;
bool valid;
Point2d() {
clear();
}
void set(T nx, T ny) {
x=nx;
y=ny;
valid=true;
}
void clear(){
x=(T)0;
y=(T)0;
valid=false;
}
bool isValid() {
return ! ( std::isnan(x) || std::isnan(y) );
}
std::string toGeoJSON() {
// todo - how to report invalid in geojson?
// if ( valid ) {
std::ostringstream str;
if ( std::isnan(x) || std::isnan(y) ) {
// we don't put out anything because "NaN" is not valid JSON
str << "";
} else {
str << x << "," << y;
}
return str.str();
}
std::string toString() {
if ( valid ) {
std::ostringstream str;
str << x << "," << y;
return str.str();
} else {
return std::string("*Invalid-Point2d*");
}
}
};
template<class T>
class Point3d {
public:
T x, y, z;
bool valid;
Point3d() {
clear();
}
void set(T nx, T ny, T nz) {
x=nx;
y=ny;
z=nz;
valid=true;
}
void clear(){
x=(T)0;
y=(T)0;
z=(T)0;
valid=false;
}
bool isValid() {
return ! ( std::isnan(x) || std::isnan(y) || std::isnan(z) );
}
std::string toGeoJSON() {
// todo - how to report invalid in geojson?
// if ( valid ) {
std::ostringstream str;
if ( std::isnan(x) || std::isnan(y) || std::isnan(z) ) {
// we don't put out anything because "NaN" is not valid JSON
str << "";
} else {
str << x << "," << y << "," << z;
}
return str.str();
}
std::string toString() {
if ( valid ) {
std::ostringstream str;
str << x << ", " << y << ", " << z;
return str.str();
} else {
return std::string("*Invalid-Point2d*");
}
}
std::string toStringImageCoords(int32_t dimId) {
if ( valid ) {
double tix, tiy;
worldPointToImagePoint(dimId, x,z, tix,tiy, false);
int32_t ix = tix;
int32_t iy = tiy;
std::ostringstream str;
str << ix << ", " << iy;
return str.str();
} else {
return std::string("*Invalid-Point2d*");
}
}
std::string toStringWithImageCoords(int32_t dimId) {
return std::string("(" + toString() + " @ image " + toStringImageCoords(dimId) + ")");
}
};
// todo - each value in these structs should be an object that does "valid" checking
class ParsedEnchantment {
public:
MyValue<int32_t> id;
MyValue<int32_t> level;
ParsedEnchantment() {
clear();
}
void clear() {
id.clear();
level.clear();
}
int32_t parse(nbt::tag_compound& iench) {
if ( iench.has_key("id", nbt::tag_type::Short) ) {
id.set( iench["id"].as<nbt::tag_short>().get() );
}
if ( iench.has_key("lvl", nbt::tag_type::Short) ) {
level.set( iench["lvl"].as<nbt::tag_short>().get() );
}
return 0;
}
std::string toGeoJSON() {
char tmpstring[1025];
std::string s = "";
if ( id.valid ) {
s += "\"Name\":\"";
if ( has_key(enchantmentInfoList, id.value) ) {
s += enchantmentInfoList[id.value]->name;
} else {
sprintf(tmpstring,"(UNKNOWN: id=%d 0x%x)",id.value,id.value);
s += tmpstring;
}
//s += "\"Level\":\"";
sprintf(tmpstring," (%d)\"", level.value);
s += tmpstring;
} else {
s += "\"valid\":\"false\"";
}
return s;
}
std::string toString() {
char tmpstring[1025];
std::string s = "";
if ( id.valid ) {
if ( has_key(enchantmentInfoList, id.value) ) {
s += enchantmentInfoList[id.value]->name;
} else {
sprintf(tmpstring,"(UNKNOWN: id=%d 0x%x)",id.value,id.value);
s += tmpstring;
}
sprintf(tmpstring," (%d)", level.value);
s += tmpstring;
} else {
s += "*Invalid id*";
}
return s;
}
};
class ParsedItem {
public:
bool valid;
bool armorFlag;
bool blockFlag;
bool nameBasedFlag;
int32_t id;
int32_t slot;
int32_t damage;
int32_t count;
int32_t repairCost;
std::vector< std::unique_ptr<ParsedEnchantment> > enchantmentList;
ParsedItem() {
clear();
}
void clear() {
valid = false;
armorFlag = false;
blockFlag = false;
nameBasedFlag = false;
id = -1;
slot = -1;
damage = -1;
count = -1;
repairCost = -1;
enchantmentList.clear();
}
int32_t parse(nbt::tag_compound& iitem) {
if ( iitem.has_key("Count", nbt::tag_type::Byte) ) {
count = iitem["Count"].as<nbt::tag_byte>().get();
}
if ( iitem.has_key("Damage", nbt::tag_type::Short) ) {
damage = iitem["Damage"].as<nbt::tag_short>().get();
}
if ( iitem.has_key("Slot", nbt::tag_type::Byte) ) {
slot = iitem["Slot"].as<nbt::tag_byte>().get();
}
// pre-0.1.8? id-based instead of name-based
if ( iitem.has_key("id", nbt::tag_type::Short) ) {
id = iitem["id"].as<nbt::tag_short>().get();
}
//todozooz - "Block" stores info on blocks - is it interesting?
if ( iitem.has_key("Block", nbt::tag_type::Compound) ) {
blockFlag = true;
}
// post-0.1.8? name-based instead of id-based
if ( iitem.has_key("Name", nbt::tag_type::String) ) {
nameBasedFlag = true;
std::string name = iitem["Name"].as<nbt::tag_string>().get();
if ( name.length() > 0 ) {
if ( blockFlag ) {
// try block first
id = findIdByBlockName(name);
if ( id < 0 ) {
id = findIdByItemName(name);
if ( id >= 0 ) {
blockFlag = false;
}
}
} else {
// try item first
id = findIdByItemName(name);
if ( id < 0 ) {
id = findIdByBlockName(name);
if ( id >= 0 ) {
blockFlag = true;
}
}
}
if (id < 0 ) {
slogger.msg(kLogWarning, "Did not find uname '%s' (item parse)\n", name.c_str());
}
}
}
// todo - other fields?
// look for item enchantment
if ( iitem.has_key("tag", nbt::tag_type::Compound) ) {
nbt::tag_compound etag = iitem["tag"].as<nbt::tag_compound>();
if ( etag.has_key("RepairCost", nbt::tag_type::Int) ) {
repairCost = etag["RepairCost"].as<nbt::tag_int>().get();
}
if ( etag.has_key("ench", nbt::tag_type::List) ) {
nbt::tag_list elist = etag["ench"].as<nbt::tag_list>();
for ( const auto& it: elist ) {
nbt::tag_compound ench = it.as<nbt::tag_compound>();
std::unique_ptr<ParsedEnchantment> e(new ParsedEnchantment());
int32_t ret = e->parse(ench);
if ( ret == 0 ) {
enchantmentList.push_back( std::move(e) );
} else {
// todo err?
}
}
}
}
if ( id >= 0 ) {
valid = true;
}
return 0;
}
int32_t parseArmor(nbt::tag_compound& iarmor) {
armorFlag = true;
return parse(iarmor);
}
std::string toGeoJSON(bool swallowFlag=false, int32_t swallowValue=0, bool showCountFlag=false) {
std::vector<std::string> list;
std::string s;
char tmpstring[1025];
if ( ! valid ) { return std::string(""); }
if ( swallowFlag ) {
if ( swallowValue == id ) {
return std::string("");
}
}
s = "\"Name\":";
if ( nameBasedFlag ) {
if ( blockFlag ) {
s += "\"" + getBlockName(id,damage) + "\"";
} else {
std::string iname = getItemName(id, damage, nameBasedFlag);
s += "\"" + iname + "\"";
}
} else {
if ( id >= 0 && id < 256 ) {
s += "\"" + getBlockName(id,damage) + "\"";
} else {
std::string iname = getItemName(id, damage, nameBasedFlag);
s += "\"" + iname + "\"";
}
}
list.push_back(s);
// todo - not useful?
if ( false ) {
if ( damage >= 0 ) {
sprintf(tmpstring,"\"Damage\":\"%d\"", damage);
list.push_back(std::string(tmpstring));
}
if ( slot >= 0 ) {
sprintf(tmpstring,"\"Slot\":\"%d\"", slot);
list.push_back(std::string(tmpstring));
}
}
if ( showCountFlag && count >= 0 ) {
sprintf(tmpstring,"\"Count\":\"%d\"", count);
list.push_back(std::string(tmpstring));
}
if ( enchantmentList.size() > 0 ) {
s = "\"Enchantments\":[";
int32_t i = enchantmentList.size();
for ( const auto& it: enchantmentList ) {
s+="{" + it->toGeoJSON() + "}";
if ( --i > 0 ) {
s += ",";
}
}
s += "]";
list.push_back(s);
}
// check for icon image
char urlImage[1025];
char urlImageGeneric[1025];
if ( nameBasedFlag ) {
if ( blockFlag ) {
sprintf(urlImage,"images/mcpe_viz.block.%d.%d.png", id, damage);
sprintf(urlImageGeneric,"images/mcpe_viz.block.%d.%d.png", id, 0);
} else {
sprintf(urlImage,"images/mcpe_viz.item.%d.%d.png", id, damage);
sprintf(urlImageGeneric,"images/mcpe_viz.item.%d.%d.png", id, 0);
}
} else {
if ( id < 256 ) {
sprintf(urlImage,"images/mcpe_viz.block.%d.%d.png", id, damage);
sprintf(urlImageGeneric,"images/mcpe_viz.block.%d.%d.png", id, 0);
} else {
sprintf(urlImage,"images/mcpe_viz.item.%d.%d.png", id, damage);
sprintf(urlImageGeneric,"images/mcpe_viz.item.%d.%d.png", id, 0);
}
}
if ( ! file_exists(urlImage) ) {
// check for non-variant
strcpy(urlImage, urlImageGeneric);
}
if ( file_exists(dirExec + "/" + urlImage) ) {
std::string fImage(urlImage);
int32_t imgId = -1;
if ( has_key(imageFileMap, fImage) ) {
imgId = imageFileMap[fImage];
} else {
imgId = globalIconImageId++;
imageFileMap.insert( std::make_pair(fImage,imgId) );
}
sprintf(tmpstring,"\"imgid\":%d",imgId);
list.push_back(tmpstring);
}
// combine the list and put the commas in the right spots (stupid json)
s = "";
int32_t i=list.size();
for ( const auto& iter: list ) {
s += iter;
if ( --i > 0 ) {
s += ",";
}
}
return s;
}
std::string toString(bool swallowFlag=false, int32_t swallowValue=0) {
char tmpstring[1025];
if ( ! valid ) { return std::string("*Invalid Item*"); }
if ( swallowFlag ) {
if ( swallowValue == id ) {
return std::string("");
}
}
std::string s = "[";
if ( nameBasedFlag ) {
if ( blockFlag ) {
s += "Block:" + getBlockName(id,damage);
} else {
s += "Item:" + getItemName(id, damage, nameBasedFlag);
}
} else {
if ( id >= 0 && id < 256 ) {
s += "Block:" + getBlockName(id,damage);
} else {
s += "Item:" + getItemName(id, damage, nameBasedFlag);
}
}
if ( damage >= 0 ) {
sprintf(tmpstring," Damage=%d", damage);
s += tmpstring;
}
if ( count >= 0 ) {
sprintf(tmpstring," Count=%d", count);
s += tmpstring;
}
if ( slot >= 0 ) {
sprintf(tmpstring," Slot=%d", slot);
s += tmpstring;
}
if ( enchantmentList.size() > 0 ) {
s += " Enchantments=[";
int32_t i=enchantmentList.size();
for ( const auto& it: enchantmentList ) {
s += it->toString();
if ( --i > 0 ) {
s += "; ";
}
}
s += "]";
}
s += "]";
return s;
}
};
class ParsedEntity {
public:
Point3d<int> bedPosition;
Point3d<int> spawn;
Point3d<double> pos;
Point2d<double> rotation;
int32_t idShort;
int32_t idFull;
int32_t tileId;
int32_t dimensionId;
bool playerLocalFlag;
bool playerRemoteFlag;
std::string playerType;
std::string playerId;
std::vector< std::unique_ptr<ParsedItem> > inventory;
std::vector< std::unique_ptr<ParsedItem> > enderchest;
std::vector< std::unique_ptr<ParsedItem> > armorList;
ParsedItem itemInHand;
ParsedItem item;
// todobig - this is very handy + powerful - use it for other Parsed classes?
std::vector< std::pair<std::string,std::string> > otherProps;
bool otherPropsSortedFlag;
ParsedEntity() {
clear();
}
void clear() {
bedPosition.clear();
spawn.clear();
pos.clear();
rotation.clear();
idShort = 0;
idFull = 0;
tileId = -1;
dimensionId = -1;
playerLocalFlag = false;
playerRemoteFlag = false;
playerType = "";
playerId = "";
inventory.clear();
enderchest.clear();
armorList.clear();
itemInHand.clear();
item.clear();
otherProps.clear();
otherPropsSortedFlag = false;
}
int32_t addInventoryItem ( nbt::tag_compound &iitem ) {
std::unique_ptr<ParsedItem> it(new ParsedItem());
int32_t ret = it->parse(iitem);
inventory.push_back( std::move(it) );
return ret;
}
int32_t addEnderChestItem ( nbt::tag_compound &iitem ) {
std::unique_ptr<ParsedItem> it(new ParsedItem());
int32_t ret = it->parse(iitem);
enderchest.push_back( std::move(it) );
return ret;
}
int32_t doItemInHand ( nbt::tag_compound &iitem ) {
itemInHand.clear();
return itemInHand.parse(iitem);
}
int32_t doItem ( nbt::tag_compound &iitem ) {
item.clear();
return item.parse(iitem);
}
int32_t doTile ( int32_t ntileId ) {
tileId = ntileId;
return 0;
}
int32_t addArmor ( nbt::tag_compound &iarmor ) {
std::unique_ptr<ParsedItem> armor(new ParsedItem());
int32_t ret = armor->parseArmor(iarmor);
if ( ret == 0 ) {
armorList.push_back( std::move(armor) );
}
return 0;
}
int32_t addOtherProp(const std::string& key, const std::string& value) {
otherProps.push_back( std::make_pair(key,value) );
return 0;
}
int32_t checkOtherProp(nbt::tag_compound& tc, const std::string& key) {
char tmpstring[1025];
if ( tc.has_key(key)) {
// seems silly that we have to do this:
const nbt::tag_type tagType = tc[key].get_type();
switch ( tagType ) {
case nbt::tag_type::Byte:
{
int8_t v = tc[key].as<nbt::tag_byte>().get();
sprintf(tmpstring,"%d (0x%x)", v, v);
addOtherProp(key, std::string(tmpstring));
}
break;
case nbt::tag_type::Short:
{
int16_t v = tc[key].as<nbt::tag_short>().get();
sprintf(tmpstring,"%d (0x%x)", v, v);
addOtherProp(key, std::string(tmpstring));
}
break;
case nbt::tag_type::Int:
{
int32_t v = tc[key].as<nbt::tag_int>().get();
sprintf(tmpstring,"%d (0x%x)", v, v);
addOtherProp(key, std::string(tmpstring));
}
break;
case nbt::tag_type::Long:
{
int64_t v = tc[key].as<nbt::tag_long>().get();
sprintf(tmpstring,"%lld (0x%llx)", (long long int)v, (long long int)v);
addOtherProp(key, std::string(tmpstring));
}
break;
case nbt::tag_type::Float:
{
sprintf(tmpstring,"%f",tc[key].as<nbt::tag_float>().get());
addOtherProp(key, std::string(tmpstring));
}
break;
case nbt::tag_type::Double:
{
sprintf(tmpstring,"%lf",tc[key].as<nbt::tag_double>().get());
addOtherProp(key, std::string(tmpstring));
}
break;
case nbt::tag_type::String:
{
addOtherProp(key, std::string(tc[key].as<nbt::tag_string>().get()));
}
break;
default:
// todo - err?
// items we don't parse (yet)
//Byte_Array = 7,
//List = 9,
//Compound = 10,
//Int_Array = 11,
fprintf(stderr,"WARNING: Unable to capture entity other prop key=%s\n", key.c_str());
break;
}
}
return 0;
}
std::string toGeoJSON(int32_t forceDimensionId) {
std::vector<std::string> list;
std::string s = "";
char tmpstring[1025];
// we make sure that the entity is valid -- have seen rare occurances of mobs with "nan" in pos et al
if ( ! pos.isValid() || ! rotation.isValid() ) {
logger.msg(kLogInfo1,"WARNING: Not outputting geojson for mob with invalid position/rotation\n");
return "";
}
double ix, iy;
worldPointToGeoJSONPoint(forceDimensionId, pos.x,pos.z, ix,iy);
s += makeGeojsonHeader(ix,iy);
if ( has_key(entityInfoList, idShort) ) {
sprintf(tmpstring,"\"Name\":\"%s\"", entityInfoList[idShort]->name.c_str());
list.push_back(std::string(tmpstring));
sprintf(tmpstring,"\"etype\":\"%s\"", entityInfoList[idShort]->etype.c_str());
list.push_back(std::string(tmpstring));
} else {
sprintf(tmpstring,"\"Name\":\"*UNKNOWN: id=%d 0x%x\"", idShort,idShort);
list.push_back(std::string(tmpstring));
}
sprintf(tmpstring,"\"id\":\"%d\"", idShort);
list.push_back(std::string(tmpstring));
sprintf(tmpstring,"\"idFull\":\"%d\"", idFull);
list.push_back(std::string(tmpstring));
// todo - needed?
if ( playerLocalFlag || playerRemoteFlag ) {
list.push_back(std::string("\"player\":\"true\""));
sprintf(tmpstring,"\"playerType\":\"%s\"", playerType.c_str());
list.push_back(std::string(tmpstring));
sprintf(tmpstring,"\"playerId\":\"%s\"", playerId.c_str());
list.push_back(std::string(tmpstring));
if ( has_key(playerIdToName, playerId) ) {
sprintf(tmpstring,"\"playerName\":\"%s\"", playerIdToName[playerId].c_str());
list.push_back(std::string(tmpstring));
} else {
sprintf(tmpstring,"\"playerName\":\"*UNKNOWN*\"");
list.push_back(std::string(tmpstring));
// we log it to screen so that people have an easier time adding new player name mappings
if ( playerId.length() > 0 ) {
slogger.msg(kLogInfo1,"INFO: Unmapped remote player: %s\n", playerId.c_str());
}
}
} else {
// list.push_back(std::string("\"player\":\"false\""));
}
if ( forceDimensionId >= 0 ) {
// getting dimension name from myWorld is more trouble than it's worth here :)
sprintf(tmpstring,"\"Dimension\":\"%d\"", forceDimensionId);
list.push_back(std::string(tmpstring));
}
sprintf(tmpstring, "\"Pos\":[%s]", pos.toGeoJSON().c_str());
list.push_back(std::string(tmpstring));
sprintf(tmpstring, "\"Rotation\":[%s]", rotation.toGeoJSON().c_str());
list.push_back(std::string(tmpstring));
if ( playerLocalFlag || playerRemoteFlag ) {
sprintf(tmpstring,"\"BedPos\":[%s]", bedPosition.toGeoJSON().c_str());