Naïve target host endianness implementation in asset builder.

This commit is contained in:
Ulysse Cura 2026-11-02 22:29:46 +01:00
parent 6cc9e6a5bb
commit c458cb72ce
6 changed files with 76 additions and 46 deletions

View File

@ -20,7 +20,12 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
// Check png autenticity. // Check png autenticity.
uint8_t sig[8]; uint8_t sig[8];
if(file_read(sig, sizeof(*sig), sizeof(sig), input_file)) goto_failure; if(fread(sig, sizeof(sig[0]), ARRAY_SIZE(sig), input_file) != ARRAY_SIZE(sig))
{
error_printf("Failed to read output file : %d", errno);
goto failure;
}
if(!png_check_sig(sig, 8)) if(!png_check_sig(sig, 8))
{ {
error_printf("Png file is not valid."); error_printf("Png file is not valid.");
@ -53,10 +58,10 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
size_t row_length = png_get_rowbytes(img, img_info); size_t row_length = png_get_rowbytes(img, img_info);
// Write image size. // Write image size.
if(file_write(&width, sizeof(width), 1, output_file)) goto_failure; if(file_write(&target_host_16b(width), sizeof(width), 1, output_file)) goto_failure;
*asset_size += sizeof(width); *asset_size += sizeof(width);
if(file_write(&height, sizeof(height), 1, output_file)) goto_failure; if(file_write(&target_host_16b(height), sizeof(height), 1, output_file)) goto_failure;
*asset_size += sizeof(height); *asset_size += sizeof(height);
// Prepare storage. // Prepare storage.
@ -108,12 +113,14 @@ int build_image(const char *input_file_name, FILE *output_file, uint32_t *asset_
{ {
dst_pixels[i * width + j] = 0x0001; dst_pixels[i * width + j] = 0x0001;
} }
dst_pixels[i * width + j] = target_host_16b(dst_pixels[i * width + j]);
} }
} }
free(src_pixels); free(src_pixels);
if(file_write(dst_pixels, sizeof(*dst_pixels), width * height, output_file)) if(file_write(dst_pixels, sizeof(dst_pixels[0]), width * height, output_file))
{ {
free(dst_pixels); free(dst_pixels);
goto_failure; goto_failure;

View File

@ -54,9 +54,9 @@ int main(int argc, char *argv[])
{ {
uint32_t header_size = 0; uint32_t header_size = 0;
const int nb_input_object_files = argc - 2; const uint32_t nb_input_object_files = argc - 2;
if(file_write(&nb_input_object_files, sizeof(nb_input_object_files), 1, output_file)) goto_failure; if(file_write(&target_host_32b(nb_input_object_files), sizeof(nb_input_object_files), 1, output_file)) goto_failure;
header_size += sizeof(nb_input_object_files); header_size += sizeof(nb_input_object_files);
FILE *input_object_files[nb_input_object_files]; FILE *input_object_files[nb_input_object_files];
@ -81,8 +81,10 @@ int main(int argc, char *argv[])
goto_failure; goto_failure;
} }
if(file_read(&input_object_files_sizes[i], sizeof(*input_object_files_sizes), 1, input_object_files[i])) if(fread(&input_object_files_sizes[i], sizeof(input_object_files_sizes[0]), 1, input_object_files[i]) != 1)
{ {
error_printf("Failed to read output file : %d", errno);
for(; i >= 0; i--) for(; i >= 0; i--)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno); error_printf("Failed to close input object file : %d", errno);
@ -112,7 +114,7 @@ int main(int argc, char *argv[])
input_object_file_ext[0] = '\00'; input_object_file_ext[0] = '\00';
uint32_t input_object_file_base_name_length = strlen(input_object_file_name) - strlen(input_object_file_ext) + 1; uint32_t input_object_file_base_name_length = strlen(input_object_file_name) - strlen(input_object_file_ext) + 1;
if(file_write(input_object_file_name, sizeof(*input_object_file_name), input_object_file_base_name_length, output_file)) if(file_write(input_object_file_name, sizeof(input_object_file_name[0]), input_object_file_base_name_length, output_file))
{ {
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
@ -121,7 +123,7 @@ int main(int argc, char *argv[])
goto_failure; goto_failure;
} }
if(file_write(&input_object_files_offsets[i], sizeof(*input_object_files_offsets), 1, output_file)) if(file_write(&target_host_32b(input_object_files_offsets[i]), sizeof(input_object_files_offsets[0]), 1, output_file))
{ {
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
@ -131,12 +133,14 @@ int main(int argc, char *argv[])
} }
} }
for(int i = 0; i < nb_input_object_files; i++) for(uint32_t i = 0; i < nb_input_object_files; i++)
{ {
char buffer[input_object_files_sizes[i]]; char buffer[input_object_files_sizes[i]];
if(file_read(buffer, sizeof(*buffer), input_object_files_sizes[i], input_object_files[i])) if(fread(buffer, sizeof(buffer[0]), input_object_files_sizes[i], input_object_files[i]) != input_object_files_sizes[i])
{ {
error_printf("Failed to read input file : %d", errno);
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno); error_printf("Failed to close input object file : %d", errno);
@ -144,8 +148,10 @@ int main(int argc, char *argv[])
goto_failure; goto_failure;
} }
if(file_write(buffer, sizeof(*buffer), input_object_files_sizes[i], output_file)) if(fwrite(buffer, sizeof(buffer[0]), input_object_files_sizes[i], output_file) != input_object_files_sizes[i])
{ {
error_printf("Failed to write output file : %d", errno);
for(i = 0; i < nb_input_object_files; i++) for(i = 0; i < nb_input_object_files; i++)
if(fclose(input_object_files[i])) if(fclose(input_object_files[i]))
error_printf("Failed to close input object file : %d", errno); error_printf("Failed to close input object file : %d", errno);

View File

@ -66,10 +66,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
if(json_get_object_of_type(root, "MapHeight", json_type_int, &tmp_obj)) goto_failure; 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); uint16_t map_height = json_object_get_int(tmp_obj);
if(file_write(&map_width, sizeof(map_width), 1, output_file)) goto_failure; if(file_write(&target_host_16b(map_width), sizeof(map_width), 1, output_file)) goto_failure;
*asset_size += sizeof(map_width); *asset_size += sizeof(map_width);
if(file_write(&map_height, sizeof(map_height), 1, output_file)) goto_failure; if(file_write(&target_host_16b(map_height), sizeof(map_height), 1, output_file)) goto_failure;
*asset_size += sizeof(map_height); *asset_size += sizeof(map_height);
// BackgroundLayers // BackgroundLayers
@ -91,10 +91,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++) for(size_t j = 0; j < (size_t)map_width * (size_t)map_height; j++)
{ {
if(json_get_idx_of_type(backgroundlayer, j, json_type_int, &tmp_obj)) goto_failure; 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] = target_host_16b((uint16_t){ 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; if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data); *asset_size += sizeof(layer_data);
} }
@ -105,10 +105,10 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
for(size_t i = 0; i < (size_t)map_width * (size_t)map_height; i++) 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)) goto_failure; if(json_get_idx_of_type(foregroundlayer, i, json_type_int, &tmp_obj)) goto_failure;
layer_data[i] = json_object_get_int(tmp_obj); layer_data[i] = target_host_16b((uint16_t){ 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; if(file_write(&layer_data, sizeof(layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(layer_data); *asset_size += sizeof(layer_data);
} }
// Hitboxes // Hitboxes
@ -124,7 +124,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
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)) goto_failure; if(file_write(&hitboxes_layer_data, sizeof(hitboxes_layer_data[0]), (size_t)map_width * (size_t)map_height, output_file)) goto_failure;
*asset_size += sizeof(hitboxes_layer_data); *asset_size += sizeof(hitboxes_layer_data);
} }
@ -134,7 +134,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure; if(json_get_object_of_type(root, "Entities", json_type_array, &entities)) goto_failure;
uint16_t entities_nb = json_object_array_length(entities); uint16_t entities_nb = json_object_array_length(entities);
if(file_write(&entities_nb, sizeof(entities_nb), 1, output_file)) goto_failure; if(file_write(&target_host_16b(entities_nb), sizeof(entities_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(entities_nb); *asset_size += sizeof(entities_nb);
for(uint32_t i = 0; i < entities_nb; i++) for(uint32_t i = 0; i < entities_nb; i++)
@ -145,7 +145,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
uint32_t entity_id = json_object_get_int(tmp_obj); uint32_t entity_id = json_object_get_int(tmp_obj);
if(file_write(&entity_id, sizeof(entity_id), 1, output_file)) goto_failure; if(file_write(&target_host_32b(entity_id), sizeof(entity_id), 1, output_file)) goto_failure;
*asset_size += sizeof(entity_id); *asset_size += sizeof(entity_id);
json_object *components; json_object *components;
@ -154,7 +154,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
uint16_t components_nb = json_object_array_length(components); uint16_t components_nb = json_object_array_length(components);
if(file_write(&components_nb, sizeof(components_nb), 1, output_file)) goto_failure; if(file_write(&target_host_16b(components_nb), sizeof(components_nb), 1, output_file)) goto_failure;
*asset_size += sizeof(components_nb); *asset_size += sizeof(components_nb);
for(uint32_t j = 0; j < components_nb; j++) for(uint32_t j = 0; j < components_nb; j++)
@ -164,14 +164,14 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure; if(json_get_object_of_type(component, "Type", json_type_string, &tmp_obj)) goto_failure;
uint16_t component_type; uint16_t component_type = -1;
#define COMPONENT(NAME, ...) \ #define COMPONENT(NAME, ...) \
if(!strcmp(#NAME, json_object_get_string(tmp_obj))) \ if(!strcmp(#NAME, json_object_get_string(tmp_obj))) \
component_type = NAME; component_type = NAME;
#include "components.def" #include "components.def"
if(file_write(&component_type, sizeof(component_type), 1, output_file)) goto_failure; if(file_write(&target_host_16b(component_type), sizeof(component_type), 1, output_file)) goto_failure;
*asset_size += sizeof(component_type); *asset_size += sizeof(component_type);
@ -188,8 +188,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
{ {
case json_type_int: case json_type_int:
{ {
uint32_t value = json_object_get_int(attribute); int32_t value = json_object_get_int(attribute);
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure; if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value); *asset_size += sizeof(value);
break; break;
} }
@ -197,7 +197,7 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_double: case json_type_double:
{ {
float value = json_object_get_double(attribute); float value = json_object_get_double(attribute);
if(file_write(&value, sizeof(value), 1, output_file)) goto_failure; if(file_write(&target_host_32b(value), sizeof(value), 1, output_file)) goto_failure;
*asset_size += sizeof(value); *asset_size += sizeof(value);
break; break;
} }
@ -213,8 +213,8 @@ int build_map(const char *input_file_name, FILE *output_file, uint32_t *asset_si
case json_type_string: case json_type_string:
{ {
const char *str = json_object_get_string(attribute); const char *str = json_object_get_string(attribute);
if(file_write(str, sizeof(*str), strlen(str) + 1, output_file)) goto_failure; if(file_write(str, sizeof(str[0]), strlen(str) + 1, output_file)) goto_failure;
*asset_size += (strlen(str) + 1) * sizeof(*str); *asset_size += (strlen(str) + 1) * sizeof(str[0]);
break; break;
} }

View File

@ -4,18 +4,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "errors.h" #include "errors.h"
inline int file_read(void *ptr, size_t size, size_t nb, FILE *file) int file_write(const void *ptr, size_t size, size_t nb, FILE *file)
{
if(fread(ptr, size, nb, file) != nb)
{
error_printf("Failed to read output file : %d", errno);
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
inline int file_write(const void *ptr, size_t size, size_t nb, FILE *file)
{ {
if(fwrite(ptr, size, nb, file) != nb) if(fwrite(ptr, size, nb, file) != nb)
{ {

View File

@ -3,8 +3,19 @@
#include <stdio.h> #include <stdio.h>
int file_read(void *ptr, size_t size, size_t nb, FILE *file); #define ARRAY_SIZE(A) (sizeof(A) / sizeof(A[0]))
int file_write(const void *ptr, size_t size, size_t nb, FILE *file); int file_write(const void *ptr, size_t size, size_t nb, FILE *file);
#define file_write_stack_array(A, F) file_write(A, sizeof(A[0]), sizeof(A) / sizeof(A[0]), F)
#ifdef BIGENDIAN
#include <endian.h>
#define target_host_16b(V) (uint16_t){ htobe16(*(uint16_t *)&V) }
#define target_host_32b(V) (uint32_t){ htobe32(*(uint32_t *)&V) }
#else
#define target_host_16b(V) (*(uint16_t *)&V)
#define target_host_32b(V) (*(uint32_t *)&V)
#endif
#endif // _UTILS_H #endif // _UTILS_H

25
configure vendored
View File

@ -234,13 +234,30 @@ case "${TARGET_HOST}" in
TARGET_HOST_CFLAGS=( TARGET_HOST_CFLAGS=(
"--std=c17" "-Os" "--std=c17" "-Os"
"-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\"" "-DFXCG50" "-DTARGET_FXCG50" "-DFXCG50" "-DTARGET_FXCG50"
"-mb" "-m4-nofpu" "-DASSET_FILE_NAME=\\\"\$(notdir \$(ASSET_OUTPUT))\\\""
"-ffreestanding" "-nostdlib" "-fstrict-volatile-bitfields" "-mb"
"-m4-nofpu"
"-ffreestanding" "-nostdlib"
"-fstrict-volatile-bitfields"
) )
TARGET_HOST_LDFLAGS="-T fxcg50.ld" TARGET_HOST_LDFLAGS=(
"-Wl,--no-warn-rwx-segments"
"-T $(fxsdk path sysroot)/sh3eb-elf/lib/fxcg50.ld" "-lc"
"-lgint-cg" "-lgcc" "-lc"
"-lopenlibm"
"-flto"
)
OUTPUT_EXT="" OUTPUT_EXT=""
STATIC_LIB_OUTPUT_EXT=".a" STATIC_LIB_OUTPUT_EXT=".a"
POST_BUILD_COMMAND="sh-elf-objcopy -O binary -R .bss -R .gint_bss \$(OUTPUT) \$(OUTPUT).bin && fxgxa --g3a -n 2D_Engine --icon-uns=\$(ROOT)/assets/icons/fxcg50-icon-uns.png --icon-sel=\$(ROOT)/assets/icons/fxcg50-icon-sel.png \$(OUTPUT).bin -o \$(OUTPUT).g3a"
RUN_COMMAND="fxlink -isw \$(OUTPUT).g3a \$(ASSET_OUTPUT)"
ASSET_BUILDER_CFLAGS+=(
"-DBIGENDIAN" "-D_DEFAULT_SOURCE"
)
;; ;;
*) *)