Implement options flag and help menu

This commit is contained in:
JackCarterSmith 2019-07-21 16:40:52 +02:00
parent 716d7ff496
commit 00be994dd5
2 changed files with 67 additions and 18 deletions

View File

@ -11,25 +11,57 @@
#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("No input file specified!\nCorrect syntax is:\n HMT-Extractor <texture_file_hmt>\n");
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[1]); //TODO: Manage multi inputs files
hmt_fdatas = extractDatasFromHMT(argv[(_options >> 8) + 0]); //TODO: Manage multi inputs files
if (hmt_fdatas == NULL) return EXIT_FAILURE;
if (exportTextures(hmt_fdatas, argv[1]) == EXIT_FAILURE) 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];
@ -39,7 +71,7 @@ void createSubDir(char *dirName) {
#ifdef _WIN32
CreateDirectory(_dir, NULL);
#else
mkdir(dirName, 755);
mkdir(_dir, 755);
#endif
}
@ -52,7 +84,7 @@ HMT_FILE *extractDatasFromHMT(char *hmt_filename) {
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");
printf("[ERR] Input file %s not found!\n", hmt_filename);
}
fclose(_hmtFile);
@ -62,21 +94,33 @@ HMT_FILE *extractDatasFromHMT(char *hmt_filename) {
int exportTextures(HMT_FILE *hmt_f, char *filename) {
int i;
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_);
}
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");
}

View File

@ -14,9 +14,14 @@
#include "RS_images.h"
#include "Image_Exporter.h"
#define VERBOSE_ENABLED 0x0001
#define NO_OUTPUT_DIR 0x0002
void createSubDir(char *dirName);
int checkArgs(char *args[], int arg_nbr);
HMT_FILE *extractDatasFromHMT(char* hmt_filename);
int exportTextures(HMT_FILE *hmt_f, char *filename);
void dispHelp();
#endif