127 lines
3.5 KiB
C
127 lines
3.5 KiB
C
/*
|
|
================================================================================
|
|
Name : Texture-Extractor.c
|
|
Author : JackCarterSmith
|
|
Version : 1.0
|
|
License : GPL-v3.0
|
|
Description : DAT textures extractor to PNG format with enhanced function in C
|
|
================================================================================
|
|
*/
|
|
|
|
#include "Texture-Extractor.h"
|
|
|
|
|
|
int _options = 0x0000; // Global options settings variable
|
|
|
|
int main(int argc, char *argv[]) {
|
|
// Init buffer vars
|
|
HMT_FILE *hmt_fdatas = NULL;
|
|
|
|
// Check if filenames arguments exist
|
|
if (argc < 2) {
|
|
printf("\nNo input file/commands specified!\n\nCorrect syntax is:\n$> Texture-Extractor <texture_hmt_file>\n"); //TODO: add help function
|
|
return EXCEPTION_NONCONTINUABLE; //TODO: implement own error codes system
|
|
}
|
|
_options = checkArgs(argv, argc); // Analyse program arguments
|
|
if (_options == -1) return EXIT_SUCCESS;
|
|
|
|
// Do the work
|
|
hmt_fdatas = extractDatasFromHMT(argv[(_options >> 8) + 0]); //TODO: Manage multi inputs files
|
|
if (hmt_fdatas == NULL) return EXIT_FAILURE;
|
|
if (exportTextures(hmt_fdatas, argv[(_options >> 8) + 0]) == 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) {
|
|
int _o = 0x0000;
|
|
char test[256];
|
|
int i;
|
|
|
|
if (arg_nbr > 1) {
|
|
for (i=1; i<arg_nbr; i++) {
|
|
strcpy(test, args[i]);
|
|
if (args[i][0] != '-') break;
|
|
if (strcmp(args[i], "-h") == 0) {
|
|
dispHelp();
|
|
return -1;
|
|
} else if (strcmp(args[i], "-v") == 0) {
|
|
_o |= VERBOSE_ENABLED;
|
|
printf("[INFO] Verbose enabled.\n");
|
|
} else if (strcmp(args[i], "-no-subdir") == 0) {
|
|
_o |= NO_OUTPUT_DIR;
|
|
printf("[INFO] Extract to current directory.\n");
|
|
} else {
|
|
printf("[INFO] Unknow option: %s\n", args[i]);
|
|
}
|
|
}
|
|
_o = (i << 8) | (_o & 0x00FF);
|
|
}
|
|
|
|
return _o;
|
|
}
|
|
|
|
void createSubDir(char *dirName) {
|
|
if (dirName == NULL) return;
|
|
char _dir[50];
|
|
strcpy(_dir, dirName);
|
|
strcat(_dir, "-out");
|
|
|
|
#ifdef _WIN32
|
|
CreateDirectory(_dir, NULL);
|
|
#else
|
|
mkdir(_dir, 755);
|
|
#endif
|
|
}
|
|
|
|
HMT_FILE *extractDatasFromHMT(char *hmt_filename) {
|
|
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 %s not found!\n", hmt_filename);
|
|
}
|
|
fclose(_hmtFile);
|
|
|
|
return hmt_fdatas;
|
|
}
|
|
|
|
int exportTextures(HMT_FILE *hmt_f, char *filename) {
|
|
int i;
|
|
|
|
if(hmt_f->texture_count > 0) {
|
|
createSubDir(filename);
|
|
for (i=0; i<hmt_f->texture_count; i++) {
|
|
switch (hmt_f->textures_list[i].image.type_) {
|
|
case 0:
|
|
case 1:
|
|
case 3:
|
|
case 4:
|
|
case 5:
|
|
if (saveToPNG(&(hmt_f->textures_list[i].image), hmt_f->textures_list[i].name, filename)) return EXIT_FAILURE;
|
|
break;
|
|
default:
|
|
printf("[INFO] Image type %d not currently supported!\n", hmt_f->textures_list[i].image.type_);
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
void dispHelp() {
|
|
printf("\n");
|
|
printf("RogueSquadron Data Extractor - TEXTURE module\n");
|
|
printf("\n");
|
|
printf("Options:\n -h Print this message\n -v Activate verbose output\n -no-subdir Extract textures directly inside current folder\n");
|
|
printf("\n");
|
|
printf("Usage: Texture-Extractor_\"version\" [options] <hmt files...>\n");
|
|
printf("\n");
|
|
}
|