소스 검색

Improve CLI of tga-compiler

Daniele Bartolini 13 년 전
부모
커밋
a2fe3485d1
1개의 변경된 파일112개의 추가작업 그리고 31개의 파일을 삭제
  1. 112 31
      tools/resource-compilers/tga-compiler.cpp

+ 112 - 31
tools/resource-compilers/tga-compiler.cpp

@@ -1,15 +1,14 @@
 #include <stdio.h>
-#include "Filesystem.h"
-#include "Stream.h"
-#include "Path.h"
-#include "String.h"
-#include "Hash.h"
-#include "Resource.h"
-#include "ResourceArchive.h"
-#include "FileStream.h"
-#include "Pixel.h"
-#include "TextureResource.h"
-#include <cstring>
+#include <stdlib.h>
+
+#include "Crown.h"
+
+const char* root_path = NULL;
+const char* resource_in = NULL;
+const char* resource_out = NULL;
+
+void print_help_message(const char* program_name);
+void parse_command_line(int argc, char** argv);
 
 using namespace crown;
 
@@ -28,6 +27,9 @@ struct TGAHeader
 	char		image_descriptor;	// 11h  Image descriptor byte
 };
 
+void print_help_message(const char* program_name);
+void parse_command_line(int argc, char** argv);
+
 void load_uncompressed(void* dest, Stream* stream, uint32_t width, uint32_t height, uint32_t channels);
 void load_compressed(void* dest, Stream* stream, uint32_t width, uint32_t height, uint32_t channels);
 void swap_red_blue(uint8_t* data, uint64_t size, uint32_t channels);
@@ -36,39 +38,28 @@ void swap_red_blue(uint8_t* data, uint64_t size, uint32_t channels);
 /// TODO: Explain supported formats, usage etc.
 int main(int argc, char** argv)
 {
-	if (argc != 4)
-	{
-		printf("Usage: %s /path/to/resources resource_in.tga resource_out.tga\n", argv[0]);
-		return -1;
-	}
-
-	Filesystem fs_root(argv[1]);
+	parse_command_line(argc, argv);
 	
-	const char* resource = argv[2];
-	const char* resource_out = argv[3];
+	// FIXME: validate input
+
+	Filesystem fs_root(root_path);
 	
-	if (!fs_root.exists(resource))
+	if (!fs_root.exists(resource_in))
 	{
-		printf("Fatal: resource %s does not exists. Aborting.\n", resource);
+		printf("%s: ERROR: %s does not exist. Aborting.\n", argv[0], resource_in);
 		return -1;
 	}
 	
 	char resource_basename[256];
 	char resource_extension[256];
 	
-	path::filename_without_extension(resource, resource_basename, 256);
-	path::extension(resource, resource_extension, 256);
-	
-	printf("Resource basename  : %s\n", resource_basename);
-	printf("Resource extension : %s\n", resource_extension);
+	path::filename_without_extension(resource_in, resource_basename, 256);
+	path::extension(resource_in, resource_extension, 256);
 	
 	uint32_t resource_basename_hash = hash::fnv1a_32(resource_basename, string::strlen(resource_basename));
 	uint32_t resource_extension_hash = hash::fnv1a_32(resource_extension, string::strlen(resource_extension));
-	
-	printf("Resource basename  (hash) : %X\n", resource_basename_hash);
-	printf("Resource extension (hash) : %X\n", resource_extension_hash);
 
-	FileStream* src_file = (FileStream*)fs_root.open(resource, SOM_READ);
+	FileStream* src_file = (FileStream*)fs_root.open(resource_in, SOM_READ);
 	
 	//-------------------------------------------------------------------------
 	// Read TGA Header
@@ -191,6 +182,96 @@ int main(int argc, char** argv)
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
+void parse_command_line(int argc, char** argv)
+{
+	// Parse arguments
+	ArgsOption options[] = 
+	{
+		"help",         AOA_NO_ARGUMENT,       NULL,        'h',
+		"root-path",    AOA_REQUIRED_ARGUMENT, NULL,        'r',
+		"resource-in",  AOA_REQUIRED_ARGUMENT, NULL,        'i',
+		"resource-out", AOA_REQUIRED_ARGUMENT, NULL,        'o',
+		NULL, 0, NULL, 0
+	};
+
+	Args args(argc, argv, "", options);
+
+	while (1)
+	{
+		int32_t ret = args.next_option();
+		
+		switch (ret)
+		{
+			case -1:
+			{
+				return;
+			}
+			// Help message
+			case 'h':
+			{
+				print_help_message(argv[0]);
+				exit(0);
+			}
+			// Root path
+			case 'r':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--root-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				root_path = args.option_argument();
+				
+				break;
+			}
+			// Resource in
+			case 'i':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--resource-in`\n", argv[0]);
+					exit(-1);
+				}
+				
+				resource_in = args.option_argument();
+				
+				break;
+			}
+			// Resource out
+			case 'o':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--resource-out`\n", argv[0]);
+					exit(-1);
+				}
+
+				resource_out = args.option_argument();
+				
+				break;
+			}
+			default:
+			{
+				break;
+			}
+		}
+	}
+}
+
+//-----------------------------------------------------------------------------
+void print_help_message(const char* program_name)
+{
+	printf("Usage: %s [options]\n", program_name);
+	printf("Options:\n\n");
+
+	printf("  --help                  Show this help.\n");
+	printf("  --root-path <path>      The _absolute_ <path> whether to look for the input resource.\n");
+	printf("  --resource-in <path>    The _relative_ <path> of the input resource.\n");
+	printf("  --resource-out <width>  The _relative_ <path> of the output resource.\n");
+}
+
 //-----------------------------------------------------------------------------
 void load_uncompressed(void* dest, Stream* stream, uint32_t width, uint32_t height, uint32_t channels)
 {