14 if (properties == NULL) {
18 if (!json_is_array(properties)) {
26 size_t property_count = json_array_size(properties);
39 json_t*
property = NULL;
41 json_array_foreach(properties, idx, property) {
42 int unpk = json_unpack_ex(property,
45 "{s:s, s:s, s?s, s:o}",
51 &ret[idx].propertytype,
56 logmsg(
TMJ_LOG_ERR,
"Unable to unpack properties, %s at line %d column %d", error.text, error.line, error.column);
63 if (strcmp(ret[idx].type,
"string") == 0) {
64 unpk = json_unpack_ex(value, &error, 0,
"s", &ret[idx].value_string);
67 logmsg(
TMJ_LOG_ERR,
"Unable to unpack string value from property, %s at line %d column %d", error.text, error.line, error.column);
75 if (strcmp(ret[idx].type,
"int") == 0) {
76 unpk = json_unpack_ex(value, &error, 0,
"i", &ret[idx].value_int);
79 logmsg(
TMJ_LOG_ERR,
"Unable to unpack integer value from property, %s at line %d column %d", error.text, error.line, error.column);
87 if (strcmp(ret[idx].type,
"float") == 0) {
88 unpk = json_unpack_ex(value, &error, 0,
"F", &ret[idx].value_float);
91 logmsg(
TMJ_LOG_ERR,
"Unable to unpack float value from property, %s at line %d column %d", error.text, error.line, error.column);
99 if (strcmp(ret[idx].type,
"bool") == 0) {
100 unpk = json_unpack_ex(value, &error, 0,
"b", &ret[idx].value_bool);
103 logmsg(
TMJ_LOG_ERR,
"Unable to unpack bool value from property, %s at line %d column %d", error.text, error.line, error.column);
111 if (strcmp(ret[idx].type,
"color") == 0) {
112 unpk = json_unpack_ex(value, &error, 0,
"s", &ret[idx].value_color);
115 logmsg(
TMJ_LOG_ERR,
"Unable to unpack color value from property, %s at line %d column %d", error.text, error.line, error.column);
123 if (strcmp(ret[idx].type,
"file") == 0) {
124 unpk = json_unpack_ex(value, &error, 0,
"s", &ret[idx].value_file);
127 logmsg(
TMJ_LOG_ERR,
"Unable to unpack file value from property, %s at line %d column %d", error.text, error.line, error.column);
135 if (strcmp(ret[idx].type,
"object") == 0) {
136 unpk = json_unpack_ex(value, &error, 0,
"i", &ret[idx].value_object);
139 logmsg(
TMJ_LOG_ERR,
"Unable to unpack object value from property, %s at line %d column %d", error.text, error.line, error.column);
256 if (objects == NULL) {
260 if (!json_is_array(objects)) {
268 size_t object_count = json_array_size(objects);
279 json_t*
object = NULL;
281 json_array_foreach(objects, idx,
object) {
282 json_t* properties = NULL;
284 json_t* polygon = NULL;
285 json_t* polyline = NULL;
288 int unpk = json_unpack_ex(
object,
295 "s:F, s:F, s:F, s:F, s:F,"
334 logmsg(
TMJ_LOG_ERR,
"Unable to unpack object, %s at line %d column %d", error.text, error.line, error.column);
342 if (properties != NULL) {
343 if (!json_is_array(properties)) {
346 goto fail_properties;
351 if (ret[idx].properties == NULL) {
354 goto fail_properties;
364 if (ret[idx].text == NULL) {
372 if (ret[idx].ellipse || ret[idx].point || ret[idx].gid != 0 || ret[idx].text != NULL) {
377 if (polygon != NULL) {
380 if (ret[idx].polygon == NULL) {
391 if (polyline != NULL) {
394 if (ret[idx].polyline == NULL) {
406 for (
size_t i = 0; i < object_count; i++) {
411 for (
size_t i = 0; i < object_count; i++) {
412 free(ret[i].properties);
437 if (chunks == NULL) {
441 if (!json_is_array(chunks)) {
449 *chunk_count = json_array_size(chunks);
451 Chunk* ret = calloc(*chunk_count,
sizeof(
Chunk));
462 json_array_foreach(chunks, idx, chunk) {
465 int unpk = json_unpack_ex(chunk,
469 "s:i, s:i, s:i, s:i,"
484 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk, %s at line %d column %d", error.text, error.line, error.column);
489 if (json_is_string(data)) {
490 unpk = json_unpack_ex(data, &error, 0,
"s", &ret[idx].data_str);
493 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk data, %s at line %d column %d", error.text, error.line, error.column);
499 }
else if (json_is_array(data)) {
500 size_t datum_count = json_array_size(data);
502 ret[idx].
data_uint = calloc(datum_count,
sizeof(
unsigned int));
504 if (ret[idx].data_uint == NULL) {
513 json_array_foreach(data, idx2, datum) {
514 unpk = json_unpack_ex(datum, &error, 0,
"i", &ret[idx].data_uint[idx2]);
517 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk datum, %s at line %d column %d", error.text, error.line, error.column);
523 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk, chunk data must be a string or an array of uint");
532 for (
size_t i = 0; i < *chunk_count; i++) {
533 if (!ret[i].data_is_str) {
534 free(ret[i].data_uint);
559 if (!json_is_array(layers)) {
565 size_t layer_count = json_array_size(layers);
567 if (layer_count < 1) {
568 logmsg(
TMJ_LOG_ERR,
"Unable to load layers, 'layers' array must have at least one element");
573 Layer* ret = calloc(layer_count,
sizeof(
Layer));
585 json_array_foreach(layers, idx, layer) {
587 int unpk = json_unpack_ex(layer, &error, 0,
"{s:i}",
"id", &ret[idx].
id);
590 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer ID, %s at line %d column %d", error.text, error.line, error.column);
596 unpk = json_unpack_ex(layer, &error, 0,
"{s:s}",
"type", &ret[idx].type);
599 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
605 unpk = json_unpack_ex(layer,
611 "s?i, s?i, s:i, s:i,"
612 "s?F, s?F, s:F, s?F, s?F"
641 &ret[idx].parallaxy);
644 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
650 if (strcmp(ret[idx].type,
"imagelayer") == 0) {
651 unpk = json_unpack_ex(layer,
660 &ret[idx].imageheight,
662 &ret[idx].imagewidth,
670 &ret[idx].transparentcolor);
673 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
677 }
else if (strcmp(ret[idx].type,
"tilelayer") == 0) {
678 unpk = json_unpack_ex(layer,
686 &ret[idx].compression,
695 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
699 }
else if (strcmp(ret[idx].type,
"objectgroup") == 0) {
700 unpk = json_unpack_ex(layer, &error, 0,
"{s?s}",
"draworder", &ret[idx].draworder);
703 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
710 if (strcmp(ret[idx].type,
"tilelayer") == 0) {
711 if (!json_object_get(layer,
"data") && !json_object_get(layer,
"chunks")) {
712 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], missing 'data' and 'chunks' fields", ret[idx].
id);
719 if (strcmp(ret[idx].type,
"tilelayer") == 0 && json_object_get(layer,
"data")) {
722 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"data", &data);
725 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
730 if (json_is_string(data)) {
733 unpk = json_unpack_ex(layer, &error, 0,
"{s:s}",
"data", &ret[idx].data_str);
736 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
740 }
else if (json_is_array(data)) {
745 ret[idx].
data_uint = calloc(ret[idx].data_count,
sizeof(
unsigned int));
747 if (ret[idx].data_uint == NULL) {
748 logmsg(
TMJ_LOG_ERR,
"Unable to unpack layer[%d]->data, the system is out of memory", ret[idx].
id);
757 json_array_foreach(data, idx2, datum) {
758 unpk = json_unpack_ex(datum, &error, 0,
"i", &ret[idx].data_uint[idx2]);
765 logmsg(
TMJ_LOG_ERR,
"Unable to unpack layer[%d]->data, data must be a string or an array", ret[idx].
id);
772 json_t* properties = NULL;
774 unpk = json_unpack_ex(layer, &error, 0,
"{s?o}",
"properties", &properties);
780 if (properties != NULL) {
783 if (ret[idx].properties == NULL) {
791 if (strcmp(ret[idx].type,
"tilelayer") == 0) {
792 json_t* chunks = NULL;
794 unpk = json_unpack_ex(layer, &error, 0,
"{s?o}",
"chunks", &chunks);
797 goto fail_properties;
800 if (chunks != NULL) {
803 if (ret[idx].chunks == NULL) {
806 goto fail_properties;
812 if (strcmp(ret[idx].type,
"objectgroup") == 0) {
813 json_t* objects = NULL;
815 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"objects", &objects);
821 if (objects != NULL) {
824 if (ret[idx].objects == NULL) {
835 if (strcmp(ret[idx].type,
"group") == 0) {
836 json_t* nested_layers = NULL;
838 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"layers", &nested_layers);
844 if (json_is_array(nested_layers) && json_array_size(nested_layers) > 0) {
847 if (ret[idx].layers == NULL) {
859 for (
size_t i = 0; i < layer_count; i++) {
863 for (
size_t i = 0; i < layer_count; i++) {
868 for (
size_t i = 0; i < layer_count; i++) {
869 free(ret[i].properties);
873 for (
size_t i = 0; i < layer_count; i++) {
874 if (!ret[i].data_is_str) {
875 free(ret[i].data_uint);
908 Map* map = calloc(1,
sizeof(
Map));
911 logmsg(
TMJ_LOG_ERR,
"Could not load map '%s', the system is out of memory", path);
919 int unpk = json_unpack_ex(root, &error, 0,
"{s:s}",
"type", &map->
type);
922 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->type, %s at line %d, column %d", path, error.text, error.line, error.column);
927 if (strcmp(map->
type,
"map") != 0) {
933 json_t* tilesets = NULL;
934 json_t* layers = NULL;
935 json_t* properties = NULL;
938 unpk = json_unpack_ex(root,
943 "s?s, s?s, s:s, s:s, s:s, s:s,"
944 "s:i, s:i, s:i, s:i, s:i, s:i, s:i,"
988 logmsg(
TMJ_LOG_ERR,
"Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
995 unpk = json_unpack_ex(root, &error, 0,
"{s:s, s:s}",
"staggeraxis", &map->
staggeraxis,
"staggerindex", &map->
staggerindex);
998 logmsg(
TMJ_LOG_ERR,
"Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
1005 unpk = json_unpack_ex(root, &error, 0,
"{s:i}",
"hexsidelength", &map->
hexsidelength);
1008 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
1015 if (properties != NULL) {
1028 if (map->
layers == NULL) {
1031 goto fail_properties;
1037 if (!json_is_array(tilesets)) {
1038 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, tilesets must be an array of Tilesets", path);
1043 size_t tileset_count = json_array_size(tilesets);
1048 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, the system is out of memory", path);
1054 json_t* tileset = NULL;
1056 json_array_foreach(tilesets, idx, tileset) {
1057 char* source = NULL;
1060 unpk = json_unpack_ex(tileset, &error, 0,
"{s?s, s?i}",
"source", &source,
"firstgid", &firstgid);
1063 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, %s at line %d column %d", path, error.text, error.line, error.column);
1078 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, could not unpack embedded tileset", path);
https://doc.mapeditor.org/en/stable/reference/json-map-format/#text