From 7861c872fb705294455693332ed3b92ae569b290 Mon Sep 17 00:00:00 2001 From: JackCarterSmith Date: Fri, 5 Jul 2019 14:13:57 +0200 Subject: [PATCH] Fixed some troubles with HMT_f management TODO: Change code to implement chained list instead of array, issue with data extraction --- src/HMT_Parser.c | 65 ++++++++++++++++++++++++++++------------- src/HMT_Parser.h | 7 +++-- src/Texture-Extractor.c | 33 +++++++-------------- src/Texture-Extractor.h | 1 - 4 files changed, 60 insertions(+), 46 deletions(-) diff --git a/src/HMT_Parser.c b/src/HMT_Parser.c index 82db57f..c6713cf 100644 --- a/src/HMT_Parser.c +++ b/src/HMT_Parser.c @@ -1,35 +1,43 @@ #include "HMT_Parser.h" -int parseHMTFile(FILE *hmt_src, HMT_FILE *dst) { +HMT_FILE *parseHMTFile(FILE *hmt_src) { int i; + HMT_FILE *_buff = NULL; - if (hmt_src == NULL) return EXIT_FAILURE; + if (hmt_src == NULL) return NULL; + _buff = calloc(1, sizeof(HMT_FILE)); rewind(hmt_src); //Rewind file at the start - fread(&(dst->material_count), 4, 1, hmt_src); // Extract first usefull datas - fread(&(dst->texture_offset), 4, 1, hmt_src); + fread(&(_buff->material_count), 4, 1, hmt_src); // Extract first usefull datas + fread(&(_buff->texture_offset), 4, 1, hmt_src); // Read materials - printf("\n\nMaterials detected: %d\n", dst->material_count); - dst->materials_list = calloc(dst->material_count, sizeof(HMT_MATERIAL)); - for (i=0; imaterial_count; i++) { + printf("\n\nMaterials detected: %d\n", _buff->material_count); + _buff->materials_list = calloc(_buff->material_count, sizeof(HMT_MATERIAL)); + for (i=0; i<_buff->material_count; i++) { // Extract materials datas - if (readMaterial(&(dst->materials_list[i]), hmt_src) != 0) return EXIT_FAILURE; - } - - // Read textures - fseek(hmt_src, dst->texture_offset, SEEK_SET); - fread(&(dst->texture_count), 4, 1, hmt_src); - printf("\n\nTextures detected: %d\n", dst->texture_count); - if (dst->texture_count > 0) { - dst->textures_list = calloc(dst->texture_count, sizeof(HMT_TEXTURE)); - for (i=0; itexture_count; i++) { - // Extract textures datas - if (readTexture(&(dst->textures_list[i]), hmt_src) != 0) return EXIT_FAILURE; + if (readMaterial(&(_buff->materials_list[i]), hmt_src) != 0) { + purgeHMTFromMemory(_buff); + return NULL; } } - return EXIT_SUCCESS; + // Read textures + fseek(hmt_src, _buff->texture_offset, SEEK_SET); + fread(&(_buff->texture_count), 4, 1, hmt_src); + printf("\n\nTextures detected: %d\n", _buff->texture_count); + if (_buff->texture_count > 0) { + _buff->textures_list = calloc(_buff->texture_count, sizeof(HMT_TEXTURE)); + for (i=0; i<_buff->texture_count; i++) { + // Extract textures datas + if (readTexture(&(_buff->textures_list[i]), hmt_src) != 0) { + purgeHMTFromMemory(_buff); + return NULL; + } + } + } + + return _buff; } int readMaterial(HMT_MATERIAL *mat, FILE *hmt_src) { @@ -93,3 +101,20 @@ int readTexture(HMT_TEXTURE *tex, FILE *hmt_src) { return EXIT_SUCCESS; } + +void purgeHMTFromMemory(HMT_FILE *_f) { + if (_f == NULL) return; + + if (_f->textures_list != NULL) { + if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels); + if (_f->textures_list->image.samples != NULL) free(_f->textures_list->image.samples); + } + if (_f->textures_list != NULL) { + if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels); + } + free(_f->textures_list); + + if (_f->materials_list != NULL) free(_f->materials_list); + + free(_f); +} diff --git a/src/HMT_Parser.h b/src/HMT_Parser.h index 907ae0f..c26f7ed 100644 --- a/src/HMT_Parser.h +++ b/src/HMT_Parser.h @@ -37,7 +37,7 @@ typedef struct HMTMaterial { int zero; int hex_a; char name[16]; - //HMT_MATERIAL *next_mat; + struct HMTMaterial *_next; }HMT_MATERIAL; /** @@ -76,7 +76,7 @@ typedef struct HMTTexture { unsigned long width, height; char name[16]; RS_IMAGE image; - //HMT_TEXTURE *next_text; + struct HMTTexture *_next; }HMT_TEXTURE; /** @@ -94,8 +94,9 @@ typedef struct HMTFile { ///////////////////////////// ///// Declare functions ///// ///////////////////////////// -int parseHMTFile(FILE *hmt_src, HMT_FILE *dst); +HMT_FILE *parseHMTFile(FILE *hmt_src); int readMaterial(HMT_MATERIAL *mat, FILE *hmt_src); int readTexture(HMT_TEXTURE *tex, FILE *hmt_src); +void purgeHMTFromMemory(HMT_FILE *_f); #endif diff --git a/src/Texture-Extractor.c b/src/Texture-Extractor.c index d0b4192..821fce3 100644 --- a/src/Texture-Extractor.c +++ b/src/Texture-Extractor.c @@ -23,35 +23,24 @@ int main(int argc, char *argv[]) { // Do the work hmt_fdatas = extractDatasFromHMT(argv[1]); //TODO: Manage multi inputs files + if (hmt_fdatas == NULL) return EXIT_FAILURE; exportTextures(hmt_fdatas, argv[1]); purgeHMTFromMemory(hmt_fdatas); // Clean up memory (because I'm a good boy) return EXIT_SUCCESS; } -void purgeHMTFromMemory(HMT_FILE *_f) { - if (_f == NULL) return; - - if (_f->textures_list != NULL) { - if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels); - if (_f->textures_list->image.samples != NULL) free(_f->textures_list->image.samples); - } - if (_f->textures_list != NULL) { - if (_f->textures_list->image.pixels != NULL) free(_f->textures_list->image.pixels); - } - free(_f->textures_list); - - if (_f->materials_list != NULL) free(_f->materials_list); - - free(_f); -} - HMT_FILE *extractDatasFromHMT(char *hmt_filename) { - FILE *_hmtFile = fopen(hmt_filename, "rb"); - HMT_FILE *hmt_fdatas = calloc(1, sizeof(HMT_FILE)); - - if (parseHMTFile(_hmtFile, hmt_fdatas) != EXIT_SUCCESS) purgeHMTFromMemory(hmt_fdatas); + FILE *_hmtFile = NULL; + HMT_FILE *hmt_fdatas = NULL; + _hmtFile = fopen(hmt_filename, "rb"); + if (_hmtFile != NULL) { + hmt_fdatas = parseHMTFile(_hmtFile); + if (hmt_fdatas == NULL) printf("[ERR] Failed to parse datas from %s\n", hmt_filename); + } else { + printf("[ERR] Input file not found!\n"); + } fclose(_hmtFile); return hmt_fdatas; @@ -76,7 +65,7 @@ int exportTextures(HMT_FILE *hmt_f, char *filename) { //Write to PGM break; default: - printf("[INFO] Image type %d not currently supported!", hmt_f->textures_list[i].image.type_); + printf("[INFO] Image type %d not currently supported!\n", hmt_f->textures_list[i].image.type_); } } diff --git a/src/Texture-Extractor.h b/src/Texture-Extractor.h index 6ebb323..a80d053 100644 --- a/src/Texture-Extractor.h +++ b/src/Texture-Extractor.h @@ -8,7 +8,6 @@ #include "RS_images.h" #include "Image_Exporter.h" -void purgeHMTFromMemory(HMT_FILE *_f); HMT_FILE *extractDatasFromHMT(char* hmt_filename); int exportTextures(HMT_FILE *hmt_f, char *filename);