Daniele Bartolini 13 лет назад
Родитель
Сommit
239d212ec3
1 измененных файлов с 108 добавлено и 29 удалено
  1. 108 29
      tools/resource-compilers/txt-compiler.cpp

+ 108 - 29
tools/resource-compilers/txt-compiler.cpp

@@ -1,51 +1,42 @@
 #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 <stdlib.h>
+
+#include "Crown.h"
 
 using namespace crown;
 
+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);
+
 /// UTF-8 compiler for "txt" resource types
 int main(int argc, char** argv)
 {
-	if (argc != 4)
-	{
-		printf("Usage: %s /path/to/resources resource_in.txt resource_out.txt\n", argv[0]);
-		return -1;
-	}
-
-	Filesystem fs_root(argv[1]);
+	parse_command_line(argc, argv);
+	
+	// FIXME: validate input
 
-	const char* resource = argv[2];
-	const char* resource_out = argv[3];
+	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", 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);
 	
 	size_t src_file_size = src_file->size();
 	
@@ -73,3 +64,91 @@ 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");
+}
+