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);
64 if (ret[idx].type == NULL || strcmp(ret[idx].type,
"string") == 0) {
65 unpk = json_unpack_ex(value, &error, 0,
"s", &ret[idx].value_string);
68 logmsg(
TMJ_LOG_ERR,
"Unable to unpack string value from property, %s at line %d column %d", error.text, error.line, error.column);
76 if (strcmp(ret[idx].type,
"int") == 0) {
77 unpk = json_unpack_ex(value, &error, 0,
"i", &ret[idx].value_int);
80 logmsg(
TMJ_LOG_ERR,
"Unable to unpack integer value from property, %s at line %d column %d", error.text, error.line, error.column);
88 if (strcmp(ret[idx].type,
"float") == 0) {
89 unpk = json_unpack_ex(value, &error, 0,
"i", &ret[idx].value_float);
92 logmsg(
TMJ_LOG_ERR,
"Unable to unpack float value from property, %s at line %d column %d", error.text, error.line, error.column);
100 if (strcmp(ret[idx].type,
"bool") == 0) {
101 unpk = json_unpack_ex(value, &error, 0,
"b", &ret[idx].value_bool);
104 logmsg(
TMJ_LOG_ERR,
"Unable to unpack bool value from property, %s at line %d column %d", error.text, error.line, error.column);
112 if (strcmp(ret[idx].type,
"color") == 0) {
113 unpk = json_unpack_ex(value, &error, 0,
"s", &ret[idx].value_color);
116 logmsg(
TMJ_LOG_ERR,
"Unable to unpack color value from property, %s at line %d column %d", error.text, error.line, error.column);
124 if (strcmp(ret[idx].type,
"file") == 0) {
125 unpk = json_unpack_ex(value, &error, 0,
"s", &ret[idx].value_file);
128 logmsg(
TMJ_LOG_ERR,
"Unable to unpack file value from property, %s at line %d column %d", error.text, error.line, error.column);
136 if (strcmp(ret[idx].type,
"object") == 0) {
137 unpk = json_unpack_ex(value, &error, 0,
"i", &ret[idx].value_object);
140 logmsg(
TMJ_LOG_ERR,
"Unable to unpack object value from property, %s at line %d column %d", error.text, error.line, error.column);
257 if (objects == NULL) {
261 if (!json_is_array(objects)) {
267 size_t object_count = json_array_size(objects);
278 json_t*
object = NULL;
280 json_array_foreach(objects, idx,
object) {
281 json_t* properties = NULL;
283 json_t* polygon = NULL;
284 json_t* polyline = NULL;
287 int unpk = json_unpack_ex(
object,
294 "s:F, s:F, s:F, s:F, s:F,"
333 logmsg(
TMJ_LOG_ERR,
"Unable to unpack object, %s at line %d column %d", error.text, error.line, error.column);
339 if (properties != NULL) {
340 if (!json_is_array(properties)) {
343 goto fail_properties;
348 if (ret[idx].properties == NULL) {
351 goto fail_properties;
361 if (ret[idx].text == NULL) {
369 if (ret[idx].ellipse || ret[idx].point || ret[idx].gid != 0 || ret[idx].text != NULL) {
374 if (polygon != NULL) {
377 if (ret[idx].polygon == NULL) {
388 if (polyline != NULL) {
391 if (ret[idx].polyline == NULL) {
403 for (
size_t i = 0; i < object_count; i++) {
408 for (
size_t i = 0; i < object_count; i++) {
409 free(ret[i].properties);
434 if (chunks == NULL) {
438 if (!json_is_array(chunks)) {
446 *chunk_count = json_array_size(chunks);
448 Chunk* ret = calloc(*chunk_count,
sizeof(
Chunk));
459 json_array_foreach(chunks, idx, chunk) {
462 int unpk = json_unpack_ex(chunk,
466 "s:i, s:i, s:i, s:i,"
481 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk, %s at line %d column %d", error.text, error.line, error.column);
486 if (json_is_string(data)) {
487 unpk = json_unpack_ex(data, &error, 0,
"s", &ret[idx].data_str);
490 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk data, %s at line %d column %d", error.text, error.line, error.column);
496 }
else if (json_is_array(data)) {
497 size_t datum_count = json_array_size(data);
499 ret[idx].
data_uint = calloc(datum_count,
sizeof(
unsigned int));
501 if (ret[idx].data_uint == NULL) {
510 json_array_foreach(data, idx2, datum) {
511 unpk = json_unpack_ex(datum, &error, 0,
"i", &ret[idx].data_uint[idx2]);
514 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk datum, %s at line %d column %d", error.text, error.line, error.column);
520 logmsg(
TMJ_LOG_ERR,
"Unable to unpack chunk, chunk data must be a string or an array of uint");
529 for (
size_t i = 0; i < *chunk_count; i++) {
530 if (!ret[i].data_is_str) {
531 free(ret[i].data_uint);
556 if (!json_is_array(layers)) {
562 size_t layer_count = json_array_size(layers);
564 if (layer_count < 1) {
565 logmsg(
TMJ_LOG_ERR,
"Unable to load layers, 'layers' array must have at least one element");
570 Layer* ret = calloc(layer_count,
sizeof(
Layer));
582 json_array_foreach(layers, idx, layer) {
584 int unpk = json_unpack_ex(layer, &error, 0,
"{s:i}",
"id", &ret[idx].
id);
587 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer ID, %s at line %d column %d", error.text, error.line, error.column);
593 unpk = json_unpack_ex(layer, &error, 0,
"{s:s}",
"type", &ret[idx].type);
596 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
602 unpk = json_unpack_ex(layer,
608 "s?i, s?i, s:i, s:i,"
609 "s?F, s?F, s:F, s?F, s?F"
638 &ret[idx].parallaxy);
641 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
647 if (strcmp(ret[idx].type,
"imagelayer") == 0) {
648 unpk = json_unpack_ex(layer,
657 &ret[idx].imageheight,
659 &ret[idx].imagewidth,
667 &ret[idx].transparentcolor);
670 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
674 }
else if (strcmp(ret[idx].type,
"tilelayer") == 0) {
675 unpk = json_unpack_ex(layer,
683 &ret[idx].compression,
692 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
696 }
else if (strcmp(ret[idx].type,
"objectgroup") == 0) {
697 unpk = json_unpack_ex(layer, &error, 0,
"{s?s}",
"draworder", &ret[idx].draworder);
700 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
707 if (strcmp(ret[idx].type,
"tilelayer") == 0) {
708 if (!json_object_get(layer,
"data") && !json_object_get(layer,
"chunks")) {
709 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], missing 'data' and 'chunks' fields", ret[idx].
id);
716 if (strcmp(ret[idx].type,
"tilelayer") == 0 && json_object_get(layer,
"data")) {
719 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"data", &data);
722 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
727 if (json_is_string(data)) {
730 unpk = json_unpack_ex(layer, &error, 0,
"{s:s}",
"data", &ret[idx].data_str);
733 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
737 }
else if (json_is_array(data)) {
742 ret[idx].
data_uint = calloc(ret[idx].data_count,
sizeof(
unsigned int));
744 if (ret[idx].data_uint == NULL) {
745 logmsg(
TMJ_LOG_ERR,
"Unable to unpack layer[%d]->data, the system is out of memory", ret[idx].
id);
754 json_array_foreach(data, idx2, datum) {
755 unpk = json_unpack_ex(datum, &error, 0,
"i", &ret[idx].data_uint[idx2]);
762 logmsg(
TMJ_LOG_ERR,
"Unable to unpack layer[%d]->data, data must be a string or an array", ret[idx].
id);
769 json_t* properties = NULL;
771 unpk = json_unpack_ex(layer, &error, 0,
"{s?o}",
"properties", &properties);
777 if (properties != NULL) {
780 if (ret[idx].properties == NULL) {
788 if (strcmp(ret[idx].type,
"tilelayer") == 0) {
789 json_t* chunks = NULL;
791 unpk = json_unpack_ex(layer, &error, 0,
"{s?o}",
"chunks", &chunks);
794 goto fail_properties;
797 if (chunks != NULL) {
800 if (ret[idx].chunks == NULL) {
803 goto fail_properties;
809 if (strcmp(ret[idx].type,
"objectgroup") == 0) {
810 json_t* objects = NULL;
812 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"objects", &objects);
818 if (objects != NULL) {
821 if (ret[idx].objects == NULL) {
832 if (strcmp(ret[idx].type,
"group") == 0) {
833 json_t* nested_layers = NULL;
835 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"layers", &nested_layers);
841 if (json_is_array(nested_layers) && json_array_size(nested_layers) > 0) {
844 if (ret[idx].layers == NULL) {
856 for (
size_t i = 0; i < layer_count; i++) {
860 for (
size_t i = 0; i < layer_count; i++) {
865 for (
size_t i = 0; i < layer_count; i++) {
866 free(ret[i].properties);
870 for (
size_t i = 0; i < layer_count; i++) {
871 if (!ret[i].data_is_str) {
872 free(ret[i].data_uint);
905 Map* map = calloc(1,
sizeof(
Map));
908 logmsg(
TMJ_LOG_ERR,
"Could not load map '%s', the system is out of memory", path);
916 int unpk = json_unpack_ex(root, &error, 0,
"{s:s}",
"type", &map->
type);
919 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->type, %s at line %d, column %d", path, error.text, error.line, error.column);
924 if (strcmp(map->
type,
"map") != 0) {
930 json_t* tilesets = NULL;
931 json_t* layers = NULL;
932 json_t* properties = NULL;
935 unpk = json_unpack_ex(root,
940 "s?s, s?s, s:s, s:s, s:s, s:s,"
941 "s:i, s:i, s:i, s:i, s:i, s:i, s:i,"
985 logmsg(
TMJ_LOG_ERR,
"Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
992 unpk = json_unpack_ex(root, &error, 0,
"{s:s, s:s}",
"staggeraxis", &map->
staggeraxis,
"staggerindex", &map->
staggerindex);
995 logmsg(
TMJ_LOG_ERR,
"Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
1002 unpk = json_unpack_ex(root, &error, 0,
"{s:i}",
"hexsidelength", &map->
hexsidelength);
1005 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
1012 if (properties != NULL) {
1025 if (map->
layers == NULL) {
1028 goto fail_properties;
1034 if (!json_is_array(tilesets)) {
1035 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, tilesets must be an array of Tilesets", path);
1040 size_t tileset_count = json_array_size(tilesets);
1045 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, the system is out of memory", path);
1051 json_t* tileset = NULL;
1053 json_array_foreach(tilesets, idx, tileset) {
1054 char* source = NULL;
1057 unpk = json_unpack_ex(tileset, &error, 0,
"{s?s, s?i}",
"source", &source,
"firstgid", &firstgid);
1060 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, %s at line %d column %d", path, error.text, error.line, error.column);
1075 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