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

LuaCompiler fixed, it works

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

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

@@ -10,9 +10,7 @@ set (HEADERS
 )
 
 add_executable(lua-compiler ${SRC} ${HEADERS})
-link_directories(${CROWN_THIRD}/luajit/lib)
-include_directories(${CROWN_THIRD}/luajit/include/luajit-2.0)
-target_link_libraries(lua-compiler crown-compiler-utils luajit-5.1)
+target_link_libraries(lua-compiler crown-compiler-utils)
 
 install (TARGETS lua-compiler DESTINATION bin)
 install (FILES bytecode-generator.lua DESTINATION bin)

+ 28 - 16
tools/compilers/lua/LuaCompiler.cpp

@@ -1,6 +1,5 @@
 #include "LuaCompiler.h"
 #include "FileStream.h"
-#include "lua.hpp"
 
 namespace crown
 {
@@ -17,35 +16,48 @@ LuaCompiler::LuaCompiler(const char* root_path, const char* dest_path, const cha
 //-----------------------------------------------------------------------------
 bool LuaCompiler::compile()
 {
-	int32_t status;
-    lua_State *L;
+    Filesystem fs(root_path());
 
-    luaL_openlibs(L); // Load Lua libraries 
+    char tmp_resource[os::MAX_PATH_LENGTH];
 
-    char* file;
-    strcpy(file, root_path());
-    strcat(file, resource_path());
+    string::strcpy(tmp_resource, resource_path());
+    string::strcat(tmp_resource, ".script");
 
-    /* Load the file containing the script we are going to run */
-    status = luaL_loadfile(L, file);
-
-    if (status)
+    if (!fs.exists(tmp_resource))
     {
-        printf("Couldn't load file: %s\n", lua_tostring(L, -1));
-        return -1;
+        os::printf("Resource cannot be found.\n");
+        return false;
     }
-    else
+
+    FileStream* file = (FileStream*)fs.open(tmp_resource, SOM_READ);
+
+    m_file_size = file->size();
+
+    if (m_file_size == 0)
     {
-    	printf("yeah!\n");
+        return false;
     }
 
-	return true;
+    m_file_data = new char[m_file_size];
+    
+    // Copy the entire file into the buffer
+    file->read(m_file_data, m_file_size);
+
+    // Prepare for writing
+    Compiler::prepare_header(m_file_size);
+
+    return true;
 }
 
 //-----------------------------------------------------------------------------
 void LuaCompiler::write()
 {
+    Compiler::write_header();
+
+    FileStream* file = Compiler::destination_file();
 
+    file->write(&m_file_size, sizeof(uint32_t));
+    file->write(m_file_data, m_file_size);
 }
 
 } // namespace crown

+ 1 - 1
tools/compilers/lua/bytecode-generator.lua

@@ -1,5 +1,5 @@
 local src = arg[1]
-local tmp = src .. ".tmp"
+local tmp = src .. ".script"
 
 
 local chunk = string.dump(loadfile(src), true)

+ 6 - 22
tools/compilers/lua/main.cpp

@@ -42,30 +42,14 @@ int main(int argc, char** argv)
 	}
 
 	LuaCompiler compiler(root_path, dest_path, resource_in, hash_seed);
-	compiler.compile();
 
-	// ArchiveEntry archive_entry;
-	// archive_entry.name = resource_basename_hash;
-	// archive_entry.type = resource_extension_hash;
-	// archive_entry.offset = sizeof (ArchiveEntry);
-	// archive_entry.size = src_file_size + sizeof(uint32_t);
-	
-	// void* buffer = new uint8_t[src_file_size];
-	
-	// src_file->read(buffer, src_file_size);
-	
-	// fs_root.close(src_file);
-	
-	// FileStream* dest_file = (FileStream*)fs_root.open(resource_out, SOM_WRITE);
-
-	// dest_file->write(&archive_entry, sizeof(ArchiveEntry));
-	// dest_file->write(&src_file_size, sizeof(uint32_t));
-	// dest_file->write(buffer, src_file_size);
-
-	// fs_root.delete_file(tmp_file);
-	// fs_root.close(dest_file);	
+	if (compiler.compile() == false)
+	{
+		printf("%s: ERROR: compilation failed for resource %s\n", argv[0], compiler.resource_path());
+		exit(-1);
+	}
 
-	// printf("Resource compilation completed: %s\n", resource_out);
+	compiler.write();
 
 	return 0;
 }

+ 1 - 1
tools/pycrown/Compiler.py

@@ -103,7 +103,7 @@ class Compiler:
 		if resource.endswith('.lua'):
 			path = os.path.normpath(root_path + "/" + resource)
 			f = subprocess.call([LUAJIT, BC_G, path]);
-			# p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);
+			p = subprocess.call([LUA_C, ROOT_P, root_path, DEST_P, self.m_dest_path, RES_IN, resource, SEED, str(self.m_perfect_seed)]);