Adding goto failure to remove code repetitions in asset builder.

This commit is contained in:
Ulysse Cura 2026-08-31 19:19:40 +02:00
parent 99422b085e
commit c4646d67f4
4 changed files with 128 additions and 283 deletions

View File

@ -27,11 +27,18 @@
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
return NULL; \
} while (0);
#else
#define goto_failure \
do { \
fprintf(stderr, "\tin %s:%d:%s()\n", __FILE__, __LINE__, __func__); \
goto failure; \
} while (0);
#else
#define debug_printf(...)
#define return_failure_int return EXIT_FAILURE;
#define return_failure_ptr return NULL;
#define goto_failure goto failure;
#endif
#define warning_printf(...) \

View File

@ -15,35 +15,31 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
return EXIT_FAILURE;
}
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
// Check png autenticity.
png_byte sig[8];
if(file_read(sig, sizeof(png_byte), sizeof(sig), input_file)) return_failure_int;
if(file_read(sig, sizeof(png_byte), sizeof(sig), input_file)) goto_failure;
if(!png_check_sig(sig, 8))
{
error_printf("Png file is not valid.");
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return EXIT_FAILURE;
goto_failure;
}
// PNG structs.
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if(!png_ptr)
{
error_printf("Failed to allocate png ptr.");
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return EXIT_FAILURE;
goto_failure;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
info_ptr = png_create_info_struct(png_ptr);
if(!info_ptr)
{
error_printf("Failed to allocate png info.");
png_destroy_read_struct(&png_ptr, NULL, NULL);
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return EXIT_FAILURE;
goto_failure;
}
// Fill structs and load data.
@ -51,72 +47,54 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
png_set_sig_bytes(png_ptr, 8);
png_read_info(png_ptr, info_ptr);
uint16_t width = png_get_image_width(png_ptr, info_ptr);
uint16_t height = png_get_image_height(png_ptr, info_ptr);
size_t row_length = png_get_rowbytes(png_ptr, info_ptr);
// Write image size.
if(file_write(&width, sizeof(width), 1, output_file))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return_failure_int;
}
uint16_t width = png_get_image_width(png_ptr, info_ptr);
uint16_t height = png_get_image_height(png_ptr, info_ptr);
size_t row_length = png_get_rowbytes(png_ptr, info_ptr);
*asset_size += sizeof(width);
// Write image size.
if(file_write(&width, sizeof(width), 1, output_file)) goto_failure;
*asset_size += sizeof(width);
if(file_write(&height, sizeof(height), 1, output_file))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return_failure_int;
}
if(file_write(&height, sizeof(height), 1, output_file)) goto_failure;
*asset_size += sizeof(height);
*asset_size += sizeof(height);
// Prepare storage.
png_byte src_pixels[height * row_length];
png_bytep src_row_ptrs[height];
for(png_uint_32 i = 0; i < height; i++)
src_row_ptrs[i] = &src_pixels[i * row_length];
// Prepare storage.
png_byte src_pixels[height * row_length];
png_bytep src_row_ptrs[height];
for(png_uint_32 i = 0; i < height; i++)
src_row_ptrs[i] = &src_pixels[i * row_length];
// Read pixels.
png_read_image(png_ptr, src_row_ptrs);
// Read pixels.
png_read_image(png_ptr, src_row_ptrs);
uint16_t dst_pixels[width * height];
uint16_t dst_pixels[width * height];
for(uint16_t i = 0; i < height; i++)
{
for(uint16_t j = 0; j < width; j += 1)
for(uint16_t i = 0; i < height; i++)
{
if(src_pixels[i * row_length + j * 4 + 3])
for(uint16_t j = 0; j < width; j += 1)
{
uint8_t r = src_pixels[i * row_length + j * 4] >> 3;
uint8_t g = src_pixels[i * row_length + j * 4 + 1] >> 2;
uint8_t b = src_pixels[i * row_length + j * 4 + 2] >> 3;
if(src_pixels[i * row_length + j * 4 + 3])
{
uint8_t r = src_pixels[i * row_length + j * 4] >> 3;
uint8_t g = src_pixels[i * row_length + j * 4 + 1] >> 2;
uint8_t b = src_pixels[i * row_length + j * 4 + 2] >> 3;
dst_pixels[i * width + j] = ((r) << 11) | ((g) << 5) | b;
}
else
{
dst_pixels[i * width + j] = 0x0001;
dst_pixels[i * width + j] = ((r) << 11) | ((g) << 5) | b;
}
else
{
dst_pixels[i * width + j] = 0x0001;
}
}
}
}
if(file_write(&dst_pixels, sizeof(*dst_pixels), width * height, output_file))
{
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return_failure_int;
if(file_write(&dst_pixels, sizeof(*dst_pixels), width * height, output_file)) goto_failure;
*asset_size += sizeof(dst_pixels);
}
*asset_size += sizeof(dst_pixels);
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(fclose(input_file))
{
error_printf("Failed to close input file : %d", errno);
@ -124,4 +102,12 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
}
return EXIT_SUCCESS;
failure:
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
if(fclose(input_file))
error_printf("Failed to close input file : %d", errno);
return EXIT_FAILURE;
}

View File

@ -48,13 +48,7 @@ int main(int argc, char *argv[])
const char *input_file_path = argv[3];
if(build_object(input_file_path, output_file))
{
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int;
}
if(build_object(input_file_path, output_file)) goto_failure;
}
else if(build_function == BUILD_ASSET_FILE)
{
@ -62,13 +56,7 @@ int main(int argc, char *argv[])
const int nb_input_object_files = argc - 2;
if(file_write(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file))
{
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int;
}
if(file_write(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file)) goto_failure;
header_size += sizeof(nb_input_object_files);
FILE *input_object_files[nb_input_object_files];
@ -90,10 +78,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return EXIT_FAILURE;
goto_failure;
}
if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i]))
@ -102,10 +87,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int
goto_failure;
}
const char *input_object_file_slash = strrchr(input_object_file_path, '/');
@ -136,10 +118,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int;
goto_failure;
}
if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file))
@ -148,10 +127,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int;
goto_failure;
}
}
@ -165,10 +141,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int;
goto_failure;
}
if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file))
@ -177,10 +150,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return_failure_int;
goto_failure;
}
}
@ -194,10 +164,7 @@ int main(int argc, char *argv[])
if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno);
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return EXIT_FAILURE;
goto_failure;
}
}
}
@ -209,4 +176,10 @@ int main(int argc, char *argv[])
}
return EXIT_SUCCESS;
failure:
if(fclose(output_file))
error_printf("Failed to close output file : %d", errno);
return EXIT_FAILURE;
}

View File

@ -51,7 +51,8 @@ typedef enum component_type_t {
int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_size)
{
json_object *root = json_object_from_file(input_file_name);
if (!root)
if(!root)
{
error_printf("Failed to load input file.");
return EXIT_FAILURE;
@ -59,213 +60,109 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
json_object *tmp_obj;
if(json_get_object_of_type(root, "MapWidth", json_type_int, &tmp_obj))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(root, "MapWidth", json_type_int, &tmp_obj)) goto_failure;
uint16_t map_width = json_object_get_int(tmp_obj);
if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj)) goto_failure;
uint16_t map_height = json_object_get_int(tmp_obj);
if(file_write(&map_width, sizeof(map_width), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&map_width, sizeof(map_width), 1, output_file)) goto_failure;
*asset_size += sizeof(map_width);
if(file_write(&map_height, sizeof(map_height), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&map_height, sizeof(map_height), 1, output_file)) goto_failure;
*asset_size += sizeof(map_height);
// BackgroundLayers
json_object *backgroundlayers;
if(json_get_object_of_type(root, "BackgroundLayers", json_type_array, &backgroundlayers))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(root, "BackgroundLayers", json_type_array, &backgroundlayers)) goto_failure;
uint8_t layer_count = json_object_array_length(backgroundlayers);
if(file_write(&layer_count, sizeof(layer_count), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&layer_count, sizeof(layer_count), 1, output_file)) goto_failure;
*asset_size += sizeof(layer_count);
uint16_t layer_data[(size_t)map_width * (size_t)map_height];
for(uint8_t i = 0; i < layer_count; i++)
{
json_object *backgroundlayer;
if(json_get_idx_of_type(backgroundlayers, i, json_type_array, &backgroundlayer))
{
json_object_put(root);
return_failure_int;
}
uint16_t layer_data[(size_t)map_width * (size_t)map_height];
for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++)
for(uint8_t i = 0; i < layer_count; i++)
{
if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj))
json_object *backgroundlayer;
if(json_get_idx_of_type(backgroundlayers, i, json_type_array, &backgroundlayer)) goto_failure;
for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++)
{
json_object_put(root);
return_failure_int;
if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj)) goto_failure;
layer_data[j] = json_object_get_int(tmp_obj);
}
layer_data[j] = json_object_get_int(tmp_obj);
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data);
}
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file))
// Foreground
json_object *foregroundlayer;
if(json_get_object_of_type(root, "ForegroundLayer", json_type_array, &foregroundlayer)) goto_failure;
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
{
json_object_put(root);
return_failure_int;
if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj)) goto_failure;
layer_data[i] = json_object_get_int(tmp_obj);
}
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data);
}
// Foreground
json_object *foregroundlayer;
if(json_get_object_of_type(root, "ForegroundLayer", json_type_array, &foregroundlayer))
{
json_object_put(root);
return_failure_int;
}
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
{
if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj))
{
json_object_put(root);
return_failure_int;
}
layer_data[i] = json_object_get_int(tmp_obj);
}
if(file_write(&layer_data, sizeof(*layer_data), (size_t)map_width * (size_t)map_height, output_file))
{
json_object_put(root);
return_failure_int;
}
*asset_size += sizeof(layer_data);
// Hitboxes
json_object *hitboxes;
if(json_get_object_of_type(root, "Hitboxes", json_type_array, &hitboxes))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(root, "Hitboxes", json_type_array, &hitboxes)) goto_failure;
uint8_t hitboxes_layer_data[(size_t)map_width * (size_t)map_height];
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
{
if(json_get_idx_of_type(hitboxes, i, json_type_int, &tmp_obj))
uint8_t hitboxes_layer_data[(size_t)map_width * (size_t)map_height];
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++)
{
json_object_put(root);
return_failure_int;
if(json_get_idx_of_type(hitboxes, i, json_type_int, &tmp_obj)) goto_failure;
hitboxes_layer_data[i] = json_object_get_int(tmp_obj);
}
hitboxes_layer_data[i] = json_object_get_int(tmp_obj);
}
if(file_write(&hitboxes_layer_data, sizeof(*hitboxes_layer_data), (size_t)map_width * (size_t)map_height, output_file))
{
json_object_put(root);
return_failure_int;
if(file_write(&hitboxes_layer_data, sizeof(*hitboxes_layer_data), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(hitboxes_layer_data);
}
*asset_size += sizeof(hitboxes_layer_data);
// Entities
json_object *entities;
if(json_get_object_of_type(root, "Entities", json_type_array, &entities))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure;
uint16_t entities_nb = json_object_array_length(entities);
if(file_write(&entities_nb, sizeof(entities_nb), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&entities_nb, sizeof(entities_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(entities_nb);
for(uint32_t i = 0; i < entities_nb; i++)
{
json_object *entity = json_object_array_get_idx(entities, i);
if(json_get_object_of_type(entity, "ID", json_type_int, &tmp_obj))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(entity, "ID", json_type_int, &tmp_obj)) goto_failure;
uint32_t entity_id = json_object_get_int(tmp_obj);
if(file_write(&entity_id, sizeof(entity_id), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&entity_id, sizeof(entity_id), 1, output_file)) goto_failure;
*asset_size += sizeof(entity_id);
json_object *components;
if(json_get_object_of_type(entity, "Components", json_type_array, &components))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(entity, "Components", json_type_array, &components)) goto_failure;
uint16_t components_nb = json_object_array_length(components);
if(file_write(&components_nb, sizeof(components_nb), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&components_nb, sizeof(components_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(components_nb);
for(uint32_t j = 0; j < components_nb; j++)
{
json_object *component;
if(json_get_idx_of_type(components, j, json_type_object, &component))
{
json_object_put(root);
return_failure_int;
}
if(json_get_idx_of_type(components, j, json_type_object, &component)) goto_failure;
json_object *attributes;
if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj))
{
json_object_put(root);
return_failure_int;
}
if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure;
uint16_t component_type;
@ -274,19 +171,12 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
component_type = NAME;
#include "components.def"
if(file_write(&component_type, sizeof(component_type), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&component_type, sizeof(component_type), 1, output_file)) goto_failure;
*asset_size += sizeof(component_type);
if(json_get_object_of_type(component, "Attributes", json_type_array, &attributes))
{
json_object_put(root);
return_failure_int;
}
json_object *attributes;
if(json_get_object_of_type(component, "Attributes", json_type_array, &attributes)) goto_failure;
uint32_t attributes_nb = json_object_array_length(attributes);
@ -299,11 +189,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_int:
{
uint32_t value = json_object_get_int(attribute);
if(file_write(&value, sizeof(value), 1, output_file))
{
json_object_put(root);
return EXIT_FAILURE;
}
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value);
break;
}
@ -311,11 +197,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_double:
{
float value = json_object_get_double(attribute);
if(file_write(&value, sizeof(value), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value);
break;
}
@ -323,11 +205,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_boolean:
{
bool value = json_object_get_boolean(attribute);
if(file_write(&value, sizeof(value), 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value);
break;
}
@ -335,18 +213,14 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_string:
{
const char *str = json_object_get_string(attribute);
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file))
{
json_object_put(root);
return_failure_int;
}
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file)) goto_failure;
*asset_size += (strlen(str) + 1) * sizeof(*str);
break;
}
default:
error_printf("Component attribute of type %s is not handled.", json_type_to_name(json_object_get_type(attribute)));
return EXIT_FAILURE;
goto_failure;
}
}
}
@ -355,4 +229,9 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
json_object_put(root);
return EXIT_SUCCESS;
failure:
json_object_put(root);
return EXIT_FAILURE;
}