Program structure

This commit is contained in:
JackCarterSmith 2022-07-26 20:57:50 +02:00
parent 93b0bcb335
commit b3827b8820
Signed by: JackCarterSmith
GPG Key ID: 832E52F4E23F8F24
6 changed files with 280 additions and 43 deletions

View File

@ -1,47 +1,102 @@
/* /*
================================================================================ ================================================================================
Name : Map-Extractor.c Name : Model-Extractor.c
Author : JackCarterSmith Author : JackCarterSmith
License : GPL-v3.0 License : GPL-v3.0
Description : DAT textures extractor to PNG format with enhanced function in C Description : HOB model parser and export to Waveform OBJ format.
================================================================================ ================================================================================
*/ */
#include "Model-Extractor.h" #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include "errors_types.h"
#include "config.h"
#include "options.h"
#include "hob_struct.h"
#include "hob_parser.h"
#include "rlk/obj.h" #include "rlk/obj.h"
/*
* Internal functions declarations
*/
unsigned int mainProcess(int args_cnt, char *args_value[]);
void createSubDir(char *dirName);
int checkInputArgs(int arg_nbr, char *args[]);
//int exportTextures(HMT_FILE *hmt_f, char *filename);
void dispHelp();
/*
* Global variables declaration
*/
int _options; // Global options settings variable int _options; // Global options settings variable
/*
* - MAIN -
*/
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
// Init buffer vars // Init buffer vars
int file_index;
printf("\n*** RogueSquadron Extractor (RSE) - MAP module - v%s ***\n", VERSION);
printf("\n*** RogueSquadron Extractor (RSE) - MODEL module - v%s ***\n", VERSION);
// Check if filenames arguments exist // Check if filenames arguments exist
if (argc < 2) { if (argc < 2) {
printf("\n[ERR] No input file/commands specified!\n"); printf("\n[ERR] No input file/commands specified!\n");
dispHelp(); dispHelp();
return EXIT_FAILURE; //TODO: implement own error codes system return ERROR_ARGS_NULL;
} }
_options = checkArgs(argv, argc); // Analyse program arguments _options = checkInputArgs(argc, argv); // Analyse program arguments
if (_options == -1) return EXIT_SUCCESS; if (_options == -1) return NO_ERROR;
// Do the work return mainProcess(argc, argv);
for (file_index=(_options >> 8) & 0xFF; file_index<argc; file_index++) { // Manage multiple inputs files
/*
hmt_fdatas = extractDatasFromHMT(argv[file_index]);
if (hmt_fdatas == NULL) return EXIT_FAILURE;
if (exportTextures(hmt_fdatas, argv[file_index]) == EXIT_FAILURE) return EXIT_FAILURE;
purgeHMTFromMemory(hmt_fdatas); // Clean up memory (because I'm a good boy)
*/
}
return EXIT_SUCCESS;
} }
int checkArgs(char *args[], int arg_nbr) {
/*
* Private functions definition
*/
unsigned int mainProcess(int args_cnt, char *args_value[]) {
unsigned short file_index;
T_HOB* hobStruct = NULL;
// Manage multiple inputs files
for (file_index=(_options >> 8) & 0xFF; file_index < args_cnt; file_index++)
{
printf("\n=============================================\n[INFO] - Parsing file: %s ...\n", args_value[file_index]);
hobStruct = calloc(1, sizeof(T_HOB));
// Parse data from HOB file and put in T_HOB structure.
if (parseHOBFile(args_value[file_index], hobStruct) != NO_ERROR) {
printf("[ERR] Failed to parse datas from %s\n", args_value[file_index]);
free(hobStruct);
return ERROR_PROCESS;
}
}
free(hobStruct);
/*
if (exportTextures(hmt_fdatas, argv[file_index]) == EXIT_FAILURE) return EXIT_FAILURE;
purgeHMTFromMemory(hmt_fdatas); // Clean up memory (because I'm a good boy)
*/
return NO_ERROR;
}
int checkInputArgs(int arg_nbr, char *args[]) {
int _o = 0x0002; // Default options parameters int _o = 0x0002; // Default options parameters
char test[256]; char test[256];
int i; int i;
@ -86,6 +141,6 @@ 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");
printf("\n"); printf("\n");
printf("Usage: RSE-Texture_%s [options] <hmt files...>\n", VERSION); printf("Usage: RSE-Model_%s [options] <hob files...>\n", VERSION);
printf("\n"); printf("\n");
} }

View File

@ -1,22 +0,0 @@
#ifndef MAP_EXTRACTOR_H_
#define MAP_EXTRACTOR_H_
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(_WIN32)
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/stat.h>
#endif
#include "config.h"
#include "options.h"
void createSubDir(char *dirName);
int checkArgs(char *args[], int arg_nbr);
//int exportTextures(HMT_FILE *hmt_f, char *filename);
void dispHelp();
#endif

25
src/errors_types.h Normal file
View File

@ -0,0 +1,25 @@
/*
* errors_types.h
*
* Created on: 26 juil. 2022
* Author: JackCarterSmith
*/
#include "error.h" //TODO: use it as base for error ID
#ifndef SRC_ERRORS_TYPES_H_
#define SRC_ERRORS_TYPES_H_
#define NO_ERROR 0
#define ERROR_GENERIC 1
#define ERROR_MEMORY 2
#define ERROR_IO 3
#define ERROR_PROCESS 4
#define ERROR_ARGS_NULL 10
#define ERROR_ARGS_RANGE 11
#define ERROR_REALITY_BROKED -1
#endif /* SRC_ERRORS_TYPES_H_ */

52
src/hob_parser.c Normal file
View File

@ -0,0 +1,52 @@
/*
* hob_parser.c
*
* Created on: 26 juil. 2022
* Author: JackCarterSmith
*/
#include <stdlib.h>
#include <stdio.h>
#include "errors_types.h"
#include "options.h"
#include "hob_struct.h"
#include "hob_parser.h"
unsigned char parseHOBFile(const char* fileName, T_HOB* hob_struct) {
unsigned char err = NO_ERROR;
long fileSize;
FILE* fStream = NULL;
char* memFile = NULL;
if (hob_struct != NULL && fileName != NULL) {
// Open file
fStream = fopen(fileName, "rb");
if (fStream != NULL) {
// Determine file size in bytes
fseek(fStream, 0, SEEK_END);
fileSize = ftell(fStream);
fseek(fStream, 0, SEEK_SET);
if (_options & VERBOSE_ENABLED) printf("\n[DBG] Input file size: %ld byte(s)\n", fileSize);
memFile = malloc(fileSize + 1);
if (memFile != NULL) {
// Copy file in RAM
fread(memFile, fileSize, 1, fStream);
fclose(fStream);
hob_struct->obj_count = ((T_HOBFILE_HEADER *)memFile)->obj_count;
printf("[INFO] - Object(s) number: %d ...\n", hob_struct->obj_count);
} else {
fclose(fStream);
err = ERROR_MEMORY;
if (_options & VERBOSE_ENABLED) printf("\n[ERR] Can't allocate enough memory for file processing!\n");
}
} else {
err = ERROR_IO;
if (_options & VERBOSE_ENABLED) printf("\n[ERR] Input file %s not found!\n", fileName);
}
} else err = ERROR_ARGS_NULL;
return err;
}

14
src/hob_parser.h Normal file
View File

@ -0,0 +1,14 @@
/*
* hob_parser.h
*
* Created on: 26 juil. 2022
* Author: JackCarterSmith
*/
#ifndef SRC_HOB_PARSER_H_
#define SRC_HOB_PARSER_H_
unsigned char parseHOBFile(const char* fileName, T_HOB* hob_struct);
#endif /* SRC_HOB_PARSER_H_ */

113
src/hob_struct.h Normal file
View File

@ -0,0 +1,113 @@
/*
* hob_struct.h
*
* Created on: 26 juil. 2022
* Author: JackCarterSmith
*/
#ifndef SRC_HOB_STRUCT_H_
#define SRC_HOB_STRUCT_H_
///////////////////////////////////////////////////////////////////////////////
// HOB file structure
///////////////////////////////////////////////////////////////////////////////
typedef unsigned int T_RGBA;
typedef struct tex_coord { unsigned short u,v; } T_TEXCOORD;
typedef struct hob_face {
unsigned int flags;
unsigned char b1;
unsigned char b2;
unsigned char b3;
unsigned char bsize;
unsigned char ftype; // 3-Tri / 4-Quad
unsigned __int64 material_index; //TODO: verify for 64bits width and adapt MinGW dependancy to __int64
unsigned __int64 indices[4]; //TODO: verify for 64bits width and adapt MinGW dependancy to __int64
T_RGBA vertex_colors[4]; //TODO: convert in R:8_G:8_B:8_A:8 format? Caution with BE/LE conversion
T_TEXCOORD tex_coords[4];
} T_HOB_FACE;
typedef struct hob_face_group {
unsigned int meshdef1_offset;
unsigned int face_block_end_offset;
unsigned int face_block_offset;
unsigned int vertex_block_offset;
unsigned int face_count;
T_HOB_FACE* faces;
unsigned int vertex_count;
unsigned short* vertices;
} T_HOB_FACE_GROUP;
typedef struct hob_object {
unsigned char name[16];
unsigned int face_group_offset;
unsigned int face_group_header_offset;
unsigned int face_group_header2_offset;
unsigned int face_group_count;
unsigned int face_group_count0;
T_HOB_FACE_GROUP* face_groups;
} T_HOB_OBJECT;
/**
* \brief Model-Extractor HOB structure of an HOB file content.
* \details Used with malloc to create a clean method of bufferized
* model datas before saving it.
* \todo Export format to use it directly in other program.
*/
typedef struct hob {
unsigned int obj_count;
T_HOB_OBJECT* objects;
} T_HOB;
///////////////////////////////////////////////////////////////////////////////
// Declaration of Memory Mapped Structure
///////////////////////////////////////////////////////////////////////////////
typedef struct __attribute__((packed)) hobfile_header {
unsigned int obj_count;
unsigned int vertices_offset;
} T_HOBFILE_HEADER;
typedef struct __attribute__((packed)) hobfile_obj_descriptor {
unsigned char object_name[16];
unsigned int facegroup_header_offset;
unsigned int object_parts_offset;
unsigned int facegroup_header_2_offset;
unsigned int reserved1; // 12B of zeros
unsigned int reserved2;
unsigned int reserved3;
unsigned int unknow1;
unsigned int unknow2;
unsigned int unknow3;
float unknow4;
unsigned int reserved4; // 12B of zeros
unsigned int reserved5;
unsigned int reserved6;
float reserved7;
float reserved8;
float reserved9;
float reserved10;
float reserved11;
unsigned int end_mask; // Always equal to 0xFFFFFFFF
float reserved12;
float reserved13;
float reserved14;
float reserved15;
float reserved16;
float reserved17;
} T_HOBFILE_OBJ_DESCRIPTOR;
#endif /* SRC_HOB_STRUCT_H_ */