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 size_t 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) {
710 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"data", &data);
713 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
718 if (json_is_string(data)) {
721 unpk = json_unpack_ex(layer, &error, 0,
"{s:s}",
"data", &ret[idx].data_str);
724 logmsg(
TMJ_LOG_ERR,
"Could not unpack layer[%d], %s at line %d column %d", ret[idx].
id, error.text, error.line, error.column);
728 }
else if (json_is_array(data)) {
733 ret[idx].
data_uint = calloc(ret[idx].data_count,
sizeof(
unsigned int));
735 if (ret[idx].data_uint == NULL) {
736 logmsg(
TMJ_LOG_ERR,
"Unable to unpack layer[%d]->data, the system is out of memory", ret[idx].
id);
745 json_array_foreach(data, idx2, datum) {
746 unpk = json_unpack_ex(datum, &error, 0,
"i", &ret[idx].data_uint[idx2]);
753 logmsg(
TMJ_LOG_ERR,
"Unable to unpack layer[%d]->data, data must be a string or an array", ret[idx].
id);
760 json_t* properties = NULL;
762 unpk = json_unpack_ex(layer, &error, 0,
"{s?o}",
"properties", &properties);
768 if (properties != NULL) {
771 if (ret[idx].properties == NULL) {
779 if (strcmp(ret[idx].type,
"tilelayer") == 0) {
780 json_t* chunks = NULL;
782 unpk = json_unpack_ex(layer, &error, 0,
"{s?o}",
"chunks", &chunks);
785 goto fail_properties;
788 if (chunks != NULL) {
791 if (ret[idx].chunks == NULL) {
794 goto fail_properties;
800 if (strcmp(ret[idx].type,
"objectgroup") == 0) {
801 json_t* objects = NULL;
803 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"objects", &objects);
809 if (objects != NULL) {
812 if (ret[idx].objects == NULL) {
823 if (strcmp(ret[idx].type,
"group") == 0) {
824 json_t* nested_layers = NULL;
826 unpk = json_unpack_ex(layer, &error, 0,
"{s:o}",
"layers", &nested_layers);
832 if (json_is_array(nested_layers) && json_array_size(nested_layers) > 0) {
835 if (ret[idx].layers == NULL) {
847 for (
size_t i = 0; i < layer_count; i++) {
851 for (
size_t i = 0; i < layer_count; i++) {
856 for (
size_t i = 0; i < layer_count; i++) {
857 free(ret[i].properties);
861 for (
size_t i = 0; i < layer_count; i++) {
862 if (!ret[i].data_is_str) {
863 free(ret[i].data_uint);
896 Map* map = calloc(1,
sizeof(
Map));
899 logmsg(
TMJ_LOG_ERR,
"Could not load map '%s', the system is out of memory", path);
907 int unpk = json_unpack_ex(root, &error, 0,
"{s:s}",
"type", &map->
type);
910 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->type, %s at line %d, column %d", path, error.text, error.line, error.column);
915 if (strcmp(map->
type,
"map") != 0) {
921 json_t* tilesets = NULL;
922 json_t* layers = NULL;
923 json_t* properties = NULL;
926 unpk = json_unpack_ex(root,
931 "s?s, s?s, s:s, s:s, s:s, s:s,"
932 "s:i, s:i, s:i, s:i, s:i, s:i, s:i,"
976 logmsg(
TMJ_LOG_ERR,
"Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
983 unpk = json_unpack_ex(root, &error, 0,
"{s:s, s:s}",
"staggeraxis", &map->
staggeraxis,
"staggerindex", &map->
staggerindex);
986 logmsg(
TMJ_LOG_ERR,
"Could not unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
993 unpk = json_unpack_ex(root, &error, 0,
"{s:i}",
"hexsidelength", &map->
hexsidelength);
996 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s], %s at line %d, column %d", path, error.text, error.line, error.column);
1003 if (properties != NULL) {
1016 if (map->
layers == NULL) {
1019 goto fail_properties;
1025 if (!json_is_array(tilesets)) {
1026 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, tilesets must be an array of Tilesets", path);
1031 size_t tileset_count = json_array_size(tilesets);
1036 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, the system is out of memory", path);
1042 json_t* tileset = NULL;
1044 json_array_foreach(tilesets, idx, tileset) {
1045 char* source = NULL;
1048 unpk = json_unpack_ex(tileset, &error, 0,
"{s?s, s?i}",
"source", &source,
"firstgid", &firstgid);
1051 logmsg(
TMJ_LOG_ERR,
"Unable to unpack map[%s]->tilesets, %s at line %d column %d", path, error.text, error.line, error.column);
1066 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