Facegroup parser

This commit is contained in:
JackCarterSmith 2022-07-26 22:35:24 +02:00
parent b3827b8820
commit f983930e15
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
3 changed files with 171 additions and 6 deletions

View File

@ -31,6 +31,7 @@
unsigned int mainProcess(int args_cnt, char *args_value[]); unsigned int mainProcess(int args_cnt, char *args_value[]);
void createSubDir(char *dirName); void createSubDir(char *dirName);
int checkInputArgs(int arg_nbr, char *args[]); int checkInputArgs(int arg_nbr, char *args[]);
void cleanUpMemory(T_HOB* hobStruct);
//int exportTextures(HMT_FILE *hmt_f, char *filename); //int exportTextures(HMT_FILE *hmt_f, char *filename);
void dispHelp(); void dispHelp();
@ -85,7 +86,7 @@ unsigned int mainProcess(int args_cnt, char *args_value[]) {
} }
} }
free(hobStruct); cleanUpMemory(hobStruct);
/* /*
@ -137,6 +138,22 @@ void createSubDir(char *dirName) {
#endif #endif
} }
void cleanUpMemory(T_HOB* hobStruct) {
int i,j,k;
for ( i=0; i<hobStruct->obj_count; i++ ) {
for ( j=0; j<hobStruct->objects[i].face_group_count; j++ ) {
free(&(hobStruct->objects[i].face_groups[j]));
}
free(&(hobStruct->objects[i]));
}
free(hobStruct);
}
void dispHelp() { void dispHelp() {
printf("\n"); printf("\n");
printf("Options:\n -h Print this message\n -v Activate verbose console output\n -no-subdir Extract textures inside current folder\n"); printf("Options:\n -h Print this message\n -v Activate verbose console output\n -no-subdir Extract textures inside current folder\n");

View File

@ -7,6 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include "errors_types.h" #include "errors_types.h"
#include "options.h" #include "options.h"
#include "hob_struct.h" #include "hob_struct.h"
@ -18,6 +19,8 @@ unsigned char parseHOBFile(const char* fileName, T_HOB* hob_struct) {
long fileSize; long fileSize;
FILE* fStream = NULL; FILE* fStream = NULL;
char* memFile = NULL; char* memFile = NULL;
int i,j;
int* offset_index = NULL;
if (hob_struct != NULL && fileName != NULL) { if (hob_struct != NULL && fileName != NULL) {
// Open file // Open file
@ -28,23 +31,81 @@ unsigned char parseHOBFile(const char* fileName, T_HOB* hob_struct) {
fseek(fStream, 0, SEEK_END); fseek(fStream, 0, SEEK_END);
fileSize = ftell(fStream); fileSize = ftell(fStream);
fseek(fStream, 0, SEEK_SET); fseek(fStream, 0, SEEK_SET);
if (_options & VERBOSE_ENABLED) printf("\n[DBG] Input file size: %ld byte(s)\n", fileSize); if (_options & VERBOSE_ENABLED) printf("[DBG] > Input file size: %ld bytes\n", fileSize);
memFile = malloc(fileSize + 1); memFile = malloc(fileSize + 1);
if (memFile != NULL) { if (memFile != NULL) {
// Copy file in RAM // Copy file in RAM
fread(memFile, fileSize, 1, fStream); fread(memFile, fileSize, 1, fStream);
fclose(fStream); fclose(fStream);
// Retrieve object count from the header
hob_struct->obj_count = ((T_HOBFILE_HEADER *)memFile)->obj_count; hob_struct->obj_count = ((T_HOBFILE_HEADER *)memFile)->obj_count;
printf("[INFO] - Object(s) number: %d ...\n", hob_struct->obj_count); printf("[INFO] - Object(s) quantity: %d\n", hob_struct->obj_count);
if (hob_struct->obj_count > 0) {
// Populate HOB structure with object descriptor
hob_struct->objects = calloc(hob_struct->obj_count, sizeof(T_HOB_OBJECT));
for ( i = 0; i < hob_struct->obj_count; i++ ) {
// Get object name
memcpy(hob_struct->objects[i].name, ((T_HOBFILE_OBJ_DESCRIPTOR *)(memFile
+ sizeof(T_HOBFILE_HEADER)
+ sizeof(T_HOBFILE_OBJ_DESCRIPTOR) * i))->object_name, 16);
printf("[INFO] - Process %s object...\n", hob_struct->objects[i].name);
// Get offsets
hob_struct->objects[i].face_group_offset = ((T_HOBFILE_OBJ_DESCRIPTOR *)(memFile
+ sizeof(T_HOBFILE_HEADER)
+ sizeof(T_HOBFILE_OBJ_DESCRIPTOR) * i))->facegroup_offset;
if (_options & VERBOSE_ENABLED) printf("[DBG] > Face group offset: 0x%X\n", hob_struct->objects[i].face_group_offset);
hob_struct->objects[i].face_group_header_offset = ((T_HOBFILE_OBJ_DESCRIPTOR *)(memFile
+ sizeof(T_HOBFILE_HEADER)
+ sizeof(T_HOBFILE_OBJ_DESCRIPTOR) * i))->object_parts_offset;
if (_options & VERBOSE_ENABLED) printf("[DBG] > Face group header/object parts offset: 0x%X\n", hob_struct->objects[i].face_group_header_offset);
hob_struct->objects[i].face_group_header2_offset = ((T_HOBFILE_OBJ_DESCRIPTOR *)(memFile
+ sizeof(T_HOBFILE_HEADER)
+ sizeof(T_HOBFILE_OBJ_DESCRIPTOR) * i))->facegroup_header_2_offset;
if (_options & VERBOSE_ENABLED) printf("[DBG] > Face group header2 offset: 0x%X\n", hob_struct->objects[i].face_group_header2_offset);
// Get count and offsets from the facegroup header
hob_struct->objects[i].face_group_count = ((T_HOBFILE_FACEGROUP_HEADER *)(memFile
+ hob_struct->objects[i].face_group_header_offset))->facegroup_count;
if (_options & VERBOSE_ENABLED) printf("[DBG] > Face group count: %d\n", hob_struct->objects[i].face_group_count);
hob_struct->objects[i].face_group_count0 = ((T_HOBFILE_FACEGROUP_HEADER *)(memFile
+ hob_struct->objects[i].face_group_header_offset))->facegroup_count0;
if (_options & VERBOSE_ENABLED) printf("[DBG] > Face group count0: %d\n", hob_struct->objects[i].face_group_count0);
if (hob_struct->objects[i].face_group_count != hob_struct->objects[i].face_group_count0 && (_options & VERBOSE_ENABLED)) printf("[DBG] > Facegroup count are different!\n");
// Get facegroup datas
offset_index = calloc(hob_struct->objects[i].face_group_count, sizeof(int));
hob_struct->objects[i].face_groups = calloc(hob_struct->objects[i].face_group_count, sizeof(T_HOB_FACE_GROUP));
for ( j = 0; j < hob_struct->objects[i].face_group_count; j++ ) {
offset_index[j] = ((T_HOBFILE_FACEGROUP_OFFSET *)(memFile + hob_struct->objects[i].face_group_header_offset
+ sizeof(T_HOBFILE_FACEGROUP_HEADER)
+ sizeof(T_HOBFILE_FACEGROUP_OFFSET) * j))->facegroup_offset;
if (_options & VERBOSE_ENABLED) printf("[DBG] > Face group meshdef0 offset: 0x%X\n", offset_index[j]);
// TODO: READ FACEGROUP MESHDEF!!!
}
}
free(offset_index);
} else {
err = ERROR_GENERIC;
printf("[INFO] Can't process empty file!\n");
}
free(memFile);
} else { } else {
fclose(fStream); fclose(fStream);
err = ERROR_MEMORY; err = ERROR_MEMORY;
if (_options & VERBOSE_ENABLED) printf("\n[ERR] Can't allocate enough memory for file processing!\n"); if (_options & VERBOSE_ENABLED) printf("[ERR] Can't allocate enough memory for file processing!\n");
} }
} else { } else {
err = ERROR_IO; err = ERROR_IO;
if (_options & VERBOSE_ENABLED) printf("\n[ERR] Input file %s not found!\n", fileName); if (_options & VERBOSE_ENABLED) printf("[ERR] Input file %s not found!\n", fileName);
} }
} else err = ERROR_ARGS_NULL; } else err = ERROR_ARGS_NULL;

View File

@ -9,6 +9,13 @@
#define SRC_HOB_STRUCT_H_ #define SRC_HOB_STRUCT_H_
/*
* long = 64bits???
* int = 32bits
* short = 16bits
* car = 8bits
*/
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// HOB file structure // HOB file structure
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -79,7 +86,7 @@ typedef struct __attribute__((packed)) hobfile_header {
typedef struct __attribute__((packed)) hobfile_obj_descriptor { typedef struct __attribute__((packed)) hobfile_obj_descriptor {
unsigned char object_name[16]; unsigned char object_name[16];
unsigned int facegroup_header_offset; unsigned int facegroup_offset;
unsigned int object_parts_offset; unsigned int object_parts_offset;
unsigned int facegroup_header_2_offset; unsigned int facegroup_header_2_offset;
@ -110,4 +117,84 @@ typedef struct __attribute__((packed)) hobfile_obj_descriptor {
float reserved17; float reserved17;
} T_HOBFILE_OBJ_DESCRIPTOR; } T_HOBFILE_OBJ_DESCRIPTOR;
typedef struct __attribute__((packed)) hobfile_facegroup_header {
unsigned short facegroup_count;
unsigned short facegroup_count0;
} T_HOBFILE_FACEGROUP_HEADER;
typedef struct __attribute__((packed)) hobfile_facegroup_offset {
unsigned int unknow1;
unsigned int facegroup_offset;
} T_HOBFILE_FACEGROUP_OFFSET;
typedef struct __attribute__((packed)) hobfile_meshdef0_0 {
unsigned int next_meshdef0_offset;
unsigned int prev_meshdef0_offset_unsure;
unsigned int offset_beginning_if_not_first;
unsigned int offset_end_if_next_equal_0;
unsigned int meshdef1_offset_plus_4;
unsigned int reserved1; // 8B of zeros
unsigned int reserved2;
} T_HOBFILE_MESHDEF0_0;
typedef struct __attribute__((packed)) hobfile_meshdef0_1 {
float unknow1;
unsigned int reserved1; // 12B of zeros
unsigned int reserved2;
unsigned int reserved3;
} T_HOBFILE_MESHDEF0_1;
typedef struct __attribute__((packed)) hobfile_meshdef0_2 {
unsigned int unknow1;
float unknow2; // Can be a vector???
float unknow3;
float unknow4;
float unknow5; // Can be a vector???
float unknow6;
float unknow7;
float unknow8; // Can be a matrix???
float unknow9;
float unknow10;
float unknow11;
float unknow12; // Can be a vector???
float unknow13;
float unknow14;
} T_HOBFILE_MESHDEF0_2;
typedef struct __attribute__((packed)) hobfile_meshdef1 {
unsigned int facedef_end_offset;
unsigned int reserved1; // 20B of zeros
unsigned int reserved2;
unsigned int reserved3;
unsigned int reserved4;
unsigned int reserved5;
unsigned int vertex_count;
unsigned int unknow1;
unsigned int reserved6;
unsigned int faceblock_offset;
unsigned int vertexblocks_offset;
unsigned int reserved7; // 52B of zeros
unsigned int reserved8;
unsigned int reserved9;
unsigned int reserved10;
unsigned int reserved11;
unsigned int reserved12;
unsigned int reserved13;
unsigned int reserved14;
unsigned int reserved15;
unsigned int reserved16;
unsigned int reserved17;
unsigned int reserved18;
unsigned int reserved19;
} T_HOBFILE_MESHDEF1;
#endif /* SRC_HOB_STRUCT_H_ */ #endif /* SRC_HOB_STRUCT_H_ */