Explorar el Código

Make resource compiler more C++-ish

Daniele Bartolini hace 12 años
padre
commit
10c4ebd370
Se han modificado 3 ficheros con 114 adiciones y 83 borrados
  1. 96 68
      tools/cli/resource-compiler.cpp
  2. 17 14
      tools/compilers/Compiler.cpp
  3. 1 1
      tools/compilers/Compiler.h

+ 96 - 68
tools/cli/resource-compiler.cpp

@@ -24,7 +24,9 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 OTHER DEALINGS IN THE SOFTWARE.
 */
 */
 
 
-#include <cstdio>
+#include <iostream>
+#include <string>
+#include <map>
 
 
 #include "Args.h"
 #include "Args.h"
 #include "Path.h"
 #include "Path.h"
@@ -34,69 +36,32 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "WAVCompiler.h"
 #include "WAVCompiler.h"
 
 
 using namespace crown;
 using namespace crown;
+using std::cout;
+using std::endl;
+using std::ofstream;
+using std::map;
 
 
 // Max number of requests per run
 // Max number of requests per run
-const uint32_t MAX_COMPILE_REQUESTS = 512;
-
+const uint32_t	MAX_COMPILE_REQUESTS = 512;
 const char*		root_path = NULL;
 const char*		root_path = NULL;
 const char*		dest_path = NULL;
 const char*		dest_path = NULL;
 uint32_t		hash_seed = 0;
 uint32_t		hash_seed = 0;
 
 
-// Help functions
-int32_t			parse_command_line(int argc, char* argv[]);
-void			print_help_message(const char* program_name);
-void			check_arguments(const char* root_path, const char* dest_path);
-void			compile_by_type(const char* resource);
-
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int main(int argc, char** argv)
+static void print_help_message(const char* program_name)
 {
 {
-	int32_t first_resource = parse_command_line(argc, argv);
-
-	// Check if all the mandatory options are set
-	check_arguments(root_path, dest_path);
-
-	// If there are no resources
-	if (first_resource >= argc)
-	{
-		printf("you have to specify at least one resource.");
-		exit(EXIT_FAILURE);
-	}
-
-
-	TGACompiler tga;
-	WAVCompiler wav;
-
-	char out_name[1024];
-	char resource_name[1024];
-	char resource_type[1024];
-
-	for (int32_t i = 0; i < argc - first_resource; i++)
-	{
-		path::filename_without_extension(argv[first_resource + i], resource_name, 1024);
-		path::extension(argv[first_resource + i], resource_type, 1024);
-
-		snprintf(out_name, 1024, "%.8X%.8X",
-			hash::murmur2_32(resource_name, string::strlen(resource_name), hash_seed),
-			hash::murmur2_32(resource_type, string::strlen(resource_type), 0));
-
-		printf("%s <= %s\n", out_name, argv[first_resource + i]);
-
-		if (string::strcmp(resource_type, "tga") == 0)
-		{
-			tga.compile(root_path, dest_path, argv[first_resource + i], out_name);
-		}
-		else if (string::strcmp(resource_type, "wav") == 0)
-		{
-			wav.compile(root_path, dest_path, argv[first_resource + i], out_name);
-		}
-	}
+	cout << "Usage: " << program_name << " [options] [resources]" << endl;
+	cout <<
+		"Options:\n\n"
 
 
-	return 0;
+		"  --help                  Show this help.\n"
+		"  --root-path <path>      The absolute <path> whether to look for the input resources.\n"
+		"  --dest-path <path>      The absolute <path> whether to put the compiled resources.\n"
+		"  --seed <value>          The seed to use for generating output resource hashes.\n";
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-int32_t parse_command_line(int argc, char* argv[])
+static int32_t parse_command_line(int argc, char* argv[])
 {
 {
 	// Parse arguments
 	// Parse arguments
 	static ArgsOption options[] = 
 	static ArgsOption options[] = 
@@ -147,32 +112,95 @@ int32_t parse_command_line(int argc, char* argv[])
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void print_help_message(const char* program_name)
+static void check_arguments(const char* root_path, const char* dest_path)
 {
 {
-	printf("Usage: %s [options] [resources]\n", program_name);
-	printf
-	(
-		"Options:\n\n"
+	if (root_path == NULL)
+	{
+		cout << "you have to specify the root path with `--root-path`" << endl;
+		exit(EXIT_FAILURE);
+	}
 
 
-		"  --help                  Show this help.\n"
-		"  --root-path <path>      The absolute <path> whether to look for the input resources.\n"
-		"  --dest-path <path>      The absolute <path> whether to put the compiled resources.\n"
-		"  --seed <value>          The seed to use for generating output resource hashes.\n"
-	);
+	if (dest_path == NULL)
+	{
+		cout << "you have to specify the destination path with `--dest-path`" << endl;
+		exit(EXIT_FAILURE);
+	}
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-void check_arguments(const char* root_path, const char* dest_path)
+int main(int argc, char** argv)
 {
 {
-	if (root_path == NULL)
+	int32_t first_resource = parse_command_line(argc, argv);
+
+	// Check if all the mandatory options are set
+	check_arguments(root_path, dest_path);
+
+	// If there are no resources
+	if (first_resource >= argc)
 	{
 	{
-		printf("you have to specify the root path with `--root-path`\n");
+		cout << "You have to specify at least one resource." << endl;
 		exit(EXIT_FAILURE);
 		exit(EXIT_FAILURE);
 	}
 	}
 
 
-	if (dest_path == NULL)
+	// Register compilers
+	TGACompiler tga;
+	WAVCompiler wav;
+
+	map<const char*, Compiler*> compilers;
+	compilers["tga"] = &tga;
+	compilers["wav"] = &wav;
+
+	// Open debug output file
+	ofstream debug_file("/home/dani/Desktop/compiler.json");
+	if (debug_file.is_open())
 	{
 	{
-		printf("you have to specify the destination path with `--dest-path`\n");
-		exit(EXIT_FAILURE);
+		debug_file << "{\n";
+	}
+
+	for (int32_t i = 0; i < argc - first_resource; i++)
+	{
+		const char* resource = argv[first_resource + i];
+
+		char resource_name[1024];
+		char resource_type[1024];
+		path::filename_without_extension(resource, resource_name, 1024);
+		path::extension(resource, resource_type, 1024);
+
+		uint32_t resource_name_hash = hash::murmur2_32(resource_name, string::strlen(resource_name), hash_seed);
+		uint32_t resource_type_hash = hash::murmur2_32(resource_type, string::strlen(resource_type), 0);
+
+		char out_name[1024];
+		snprintf(out_name, 1024, "%.8X%.8X", resource_name_hash, resource_type_hash);
+
+		cout << out_name << " <= " << argv[first_resource + i] << endl;
+
+		map<const char*, Compiler*>::iterator it = compilers.find(resource_type);
+		if (it != compilers.end())
+		{
+			if (!it->second->compile(root_path, dest_path, argv[first_resource + i], out_name))
+			{
+				cout << "Exiting." << endl;
+				exit(EXIT_FAILURE);
+			}
+		}
+		else
+		{
+			cout << "No compilers found for type '" << resource_type << "'." << endl;
+			exit(EXIT_FAILURE);
+		}
+
+		// Debug stuff
+		debug_file << "    \"" << out_name << "\" : " << "\"" << argv[first_resource + i] << "\"";
+		if (argc - first_resource - i != 1)
+		{
+			debug_file << ",";
+		}
+
+		debug_file << "\n";
 	}
 	}
+
+	debug_file << "}\n";
+	debug_file.close();
+
+	return 0;
 }
 }

+ 17 - 14
tools/compilers/Compiler.cpp

@@ -31,11 +31,14 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Compiler.h"
 #include "Compiler.h"
 #include "ResourceFormat.h"
 #include "ResourceFormat.h"
 
 
+using std::cout;
+using std::endl;
+
 namespace crown
 namespace crown
 {
 {
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
-size_t Compiler::compile(const char* root_path, const char* dest_path, const char* name_in, const char* name_out)
+bool Compiler::compile(const char* root_path, const char* dest_path, const char* name_in, const char* name_out)
 {
 {
 	std::string path_in = std::string(root_path) + "/" + std::string(name_in);
 	std::string path_in = std::string(root_path) + "/" + std::string(name_in);
 	std::string path_out = std::string(dest_path) + "/" + std::string(name_out);
 	std::string path_out = std::string(dest_path) + "/" + std::string(name_out);
@@ -44,8 +47,8 @@ size_t Compiler::compile(const char* root_path, const char* dest_path, const cha
 	size_t resource_size = 0;
 	size_t resource_size = 0;
 	if ((resource_size = compile_impl(path_in.c_str())) == 0)
 	if ((resource_size = compile_impl(path_in.c_str())) == 0)
 	{
 	{
-		std::cout << "Compilation failed." << std::endl;
-		return 0;
+		cout << "Compilation failed." << endl;
+		return false;
 	}
 	}
 
 
 	// Setup resource header
 	// Setup resource header
@@ -58,21 +61,21 @@ size_t Compiler::compile(const char* root_path, const char* dest_path, const cha
 	std::fstream out_file;
 	std::fstream out_file;
 	out_file.open(path_out.c_str(), std::fstream::out | std::fstream::binary);
 	out_file.open(path_out.c_str(), std::fstream::out | std::fstream::binary);
 
 
-	if (!out_file.is_open())
+	if (out_file.is_open())
 	{
 	{
-		std::cout << "Unable to write compiled file." << std::endl;
-		return 0;
-	}
+		// Write header
+		out_file.write((char*)&resource_header, sizeof(ResourceHeader));
 
 
-	out_file.write((char*)&resource_header, sizeof(ResourceHeader));
+		// Write resource-specific data
+		write_impl(out_file);
+		out_file.close();
 
 
-	// Write resource-specific data
-	write_impl(out_file);
-
-	out_file.close();
+		cleanup();
+		return true;
+	}
 
 
-	// Cleanup
-	cleanup();
+	cout << "Unable to write compiled file." << endl;
+	return false;
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------

+ 1 - 1
tools/compilers/Compiler.h

@@ -43,7 +43,7 @@ public:
 
 
 	virtual					~Compiler() {}
 	virtual					~Compiler() {}
 
 
-	size_t					compile(const char* root_path, const char* dest_path, const char* name_in, const char* name_out);
+	bool					compile(const char* root_path, const char* dest_path, const char* name_in, const char* name_out);
 	void					cleanup();
 	void					cleanup();
 
 
 protected:
 protected: