Преглед изворни кода

lua-compiler enabled (must be completed)

mikymod пре 12 година
родитељ
комит
b8371f236a

+ 1 - 2
third/x86_64/CMakeLists.txt

@@ -9,5 +9,4 @@ install (FILES 	luajit/include/luajit-2.0/lua.hpp
 				
 install (DIRECTORY luajit/lib/lua
 				   luajit/lib/pkgconfig DESTINATION lib/crown)
-install (FILES luajit/lib/libluajit-5.1.so.2.0.1
-			   luajit/lib/libluajit-5.1.so.2 DESTINATION lib/crown)
+install (FILES luajit/lib/libluajit-5.1.so.2.0.1 DESTINATION lib/crown)

+ 1 - 1
tools/compilers/CMakeLists.txt

@@ -22,7 +22,7 @@ target_link_libraries(resource-hash crown)
 
 add_subdirectory(txt)
 add_subdirectory(tga)
-#add_subdirectory(lua)
+add_subdirectory(lua)
 add_subdirectory(mat)
 
 install (TARGETS crown-compiler-utils DESTINATION lib/${CMAKE_PROJECT_NAME})

+ 3 - 1
tools/compilers/lua/CMakeLists.txt

@@ -10,6 +10,8 @@ set (HEADERS
 )
 
 add_executable(lua-compiler ${SRC} ${HEADERS})
-target_link_libraries(lua-compiler crown-compiler-utils crown-luajit)
+link_directories(${CROWN_THIRD}/luajit/lib)
+include_directories(${CROWN_THIRD}/luajit/include)
+target_link_libraries(lua-compiler crown-compiler-utils lua)
 
 install (TARGETS lua-compiler DESTINATION bin)

+ 22 - 0
tools/compilers/lua/LuaCompiler.cpp

@@ -17,6 +17,28 @@ LuaCompiler::LuaCompiler(const char* root_path, const char* dest_path, const cha
 //-----------------------------------------------------------------------------
 bool LuaCompiler::compile()
 {
+	int32_t status;
+    lua_State *L;
+
+    luaL_openlibs(L); // Load Lua libraries 
+
+    char* file;
+    strcpy(file, root_path());
+    strcat(file, resource_path());
+
+    /* Load the file containing the script we are going to run */
+    status = luaL_loadfile(L, file);
+
+    if (status)
+    {
+        printf("Couldn't load file: %s\n", lua_tostring(L, -1));
+        return -1;
+    }
+    else
+    {
+    	printf("yeah!\n");
+    }
+
 	return true;
 }
 

+ 42 - 35
tools/compilers/lua/main.cpp

@@ -4,14 +4,15 @@
 #include <sys/wait.h>
 #include <unistd.h>
 
-#include "lua.hpp"
 #include "Crown.h"
+#include "LuaCompiler.h"
 
 using namespace crown;
 
 const char* root_path = NULL;
+const char* dest_path = NULL;
 const char* resource_in = NULL;
-const char* resource_out = NULL;
+uint32_t hash_seed = 0;
 
 void 	parse_command_line(int argc, char** argv);
 void 	print_help_message(const char* program_name);
@@ -20,34 +21,29 @@ void	compile_script(char* tmp_out);
 /// Lua scripts compiler
 int main(int argc, char** argv)
 {
-	// parse_command_line(argc, argv);
+	parse_command_line(argc, argv);
 	
-	// // FIXME: validate input
+	if (root_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the root path with `--root-path`\n", argv[0]);
+		exit(-1);
+	}
 
-	// Filesystem fs_root(root_path);
-	
-	// if (!fs_root.exists(resource_in))
-	// {
-	// 	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_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));
+	if (dest_path == NULL)
+	{
+		printf("%s: ERROR: you have to specify the destination path with `--dest-path`\n", argv[0]);
+		exit(-1);
+	}
+
+	if (resource_in == NULL)
+	{
+		printf("%s: ERROR: you have to specify the resource name with `--resource-in`\n", argv[0]);
+		exit(-1);
+	}
 
-	// char tmp_file[256];
-	// compile_script(tmp_file);
+	LuaCompiler compiler(root_path, dest_path, resource_in, hash_seed);
+	compiler.compile();
 
-	// FileStream* src_file = (FileStream*)fs_root.open(tmp_file, SOM_READ);
-	
-	// size_t src_file_size = src_file->size();
-	
 	// ArchiveEntry archive_entry;
 	// archive_entry.name = resource_basename_hash;
 	// archive_entry.type = resource_extension_hash;
@@ -70,13 +66,11 @@ int main(int argc, char** argv)
 	// fs_root.close(dest_file);	
 
 	// printf("Resource compilation completed: %s\n", resource_out);
-	// lua_State *L;
-	 // = lua_open();
- //  	lua_close(L);
 
 	return 0;
 }
 
+//-----------------------------------------------------------------------------
 void parse_command_line(int argc, char** argv)
 {
 	// Parse arguments
@@ -84,8 +78,9 @@ void parse_command_line(int argc, char** argv)
 	{
 		"help",         AOA_NO_ARGUMENT,       NULL,        'h',
 		"root-path",    AOA_REQUIRED_ARGUMENT, NULL,        'r',
+		"dest-path",    AOA_REQUIRED_ARGUMENT, NULL,        'd',
 		"resource-in",  AOA_REQUIRED_ARGUMENT, NULL,        'i',
-		"resource-out", AOA_REQUIRED_ARGUMENT, NULL,        'o',
+		"seed",         AOA_REQUIRED_ARGUMENT, NULL,        's',
 		NULL, 0, NULL, 0
 	};
 
@@ -120,6 +115,19 @@ void parse_command_line(int argc, char** argv)
 				
 				break;
 			}
+			// Dest path
+			case 'd':
+			{
+				if (args.option_argument() == NULL)
+				{
+					printf("%s: ERROR: missing path after `--dest-path`\n", argv[0]);
+					exit(-1);
+				}
+				
+				dest_path = args.option_argument();
+				
+				break;
+			}
 			// Resource in
 			case 'i':
 			{
@@ -133,17 +141,16 @@ void parse_command_line(int argc, char** argv)
 				
 				break;
 			}
-			// Resource out
-			case 'o':
+			case 's':
 			{
 				if (args.option_argument() == NULL)
 				{
-					printf("%s: ERROR: missing path after `--resource-out`\n", argv[0]);
+					printf("%s: ERROR: missing seed value after `--seed`\n", argv[0]);
 					exit(-1);
 				}
 
-				resource_out = args.option_argument();
-				
+				hash_seed = atoi(args.option_argument());
+
 				break;
 			}
 			default:

+ 6 - 1
tools/pycrown/Compiler.py

@@ -26,6 +26,7 @@ import subprocess
 
 TXT_C = "txt-compiler"
 TGA_C = "tga-compiler"
+LUA_C = "lua-compiler"
 RES_H = "resource-hash"
 
 ROOT_P = "--root-path"
@@ -95,5 +96,9 @@ class Compiler:
 		if resource.endswith('.txt'):
 			p = subprocess.call([TXT_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
 		if resource.endswith('.tga'):
-			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);	
+			p = subprocess.call([TGA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+		if resource.endswith('.lua'):
+			p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+
+