Bläddra i källkod

Simplify A LOT LuaEnvironment

Daniele Bartolini 12 år sedan
förälder
incheckning
bb2fe7d730
2 ändrade filer med 89 tillägg och 172 borttagningar
  1. 69 131
      engine/lua/LuaEnvironment.cpp
  2. 20 41
      engine/lua/LuaEnvironment.h

+ 69 - 131
engine/lua/LuaEnvironment.cpp

@@ -24,35 +24,50 @@ FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
 
-
-#include "Device.h"
-#include "OS.h"
+#include <stdarg.h>
 #include "Assert.h"
-#include "Log.h"
 #include "LuaEnvironment.h"
-#include "StringSetting.h"
-#include "Filesystem.h"
+#include "StringUtils.h"
+#include "LuaStack.h"
+#include "Device.h"
+#include "LuaResource.h"
 
 namespace crown
 {
 
-extern StringSetting g_default_mountpoint;
+//-----------------------------------------------------------------------------
+CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
+{
+	LuaEnvironment* env = device()->lua_environment();
 
-StringSetting g_boot("boot_file", "lua main file", "lua/game.raw");
+	load_int_setting(*env);
+	load_float_setting(*env);
+	load_string_setting(*env);
 
-/*
-*N.B: Lua garbage collection is actually disabled
-*/
+	load_vec2(*env);
+	load_vec3(*env);
+	load_mat4(*env);
+	load_quat(*env);
+	load_math(*env);
+
+	load_mouse(*env);
+	load_keyboard(*env);
+	load_accelerometer(*env);
+
+	load_device(*env);
+
+	load_window(*env);
+
+	return 1;
+}
 
 //-----------------------------------------------------------------------------
 LuaEnvironment::LuaEnvironment() :
 	m_state(luaL_newstate()),
 	m_is_used(false)
-	//m_thread(LuaEnvironment::background_thread, (void*)this, "lua-environment-thread")
 {
 	// Open Lua default libraries
 	string::strncpy(m_error_buffer, "", 1024);
-
 	string::strncpy(m_tmp_buffer, "", 1024);
 }
 
@@ -64,16 +79,16 @@ void LuaEnvironment::init()
 	// Open Crown library
 	lua_cpcall(m_state, luaopen_libcrown, NULL);
 
-	load_buffer(class_system, string::strlen(class_system));
-	execute(0, 0);
-	load_buffer(commands_list, string::strlen(commands_list));
-	execute(0, 0);
-	load_buffer(get_cmd_by_name, string::strlen(get_cmd_by_name));
-	execute(0, 0);
-	load_buffer(tmp_print_table, string::strlen(tmp_print_table));
-	execute(0, 0);
-	load_buffer(count_all, string::strlen(count_all));
-	execute(0, 0);
+	// load_buffer(class_system, string::strlen(class_system));
+	// execute(0, 0);
+	// load_buffer(commands_list, string::strlen(commands_list));
+	// execute(0, 0);
+	// load_buffer(get_cmd_by_name, string::strlen(get_cmd_by_name));
+	// execute(0, 0);
+	// load_buffer(tmp_print_table, string::strlen(tmp_print_table));
+	// execute(0, 0);
+	// load_buffer(count_all, string::strlen(count_all));
+	// execute(0, 0);
 
 	m_is_used = true;
 }
@@ -86,123 +101,73 @@ void LuaEnvironment::shutdown()
 	m_is_used = false;
 }
 
-//-----------------------------------------------------------------------------
-lua_State* LuaEnvironment::state()
-{
-	return m_state;
-}
-
 //-----------------------------------------------------------------------------
 const char* LuaEnvironment::error()
 {	
 	string::strncpy(m_tmp_buffer, m_error_buffer, 1024);
-
 	string::strncpy(m_error_buffer, "", 1024);
 
 	return m_tmp_buffer;
 }
 
 //-----------------------------------------------------------------------------
-void LuaEnvironment::load_buffer(const char* buffer, size_t len)
+void LuaEnvironment::load(const LuaResource* lr)
 {
-	int32_t loaded = luaL_loadbuffer(m_state, buffer, len, "");
+	CE_ASSERT_NOT_NULL(lr);
 
-	if (loaded != 0)
+	if (luaL_loadbuffer(m_state, (const char*) lr->code(), lr->size(), "") != 0)
 	{
-		lua_error();
+		lua_error();		
 	}
-}
-
-//-----------------------------------------------------------------------------
-void LuaEnvironment::load_file(const char* file)
-{
-	int32_t loaded = luaL_loadfile(m_state, file);
 
-	if (loaded != 0)
+	if (lua_pcall(m_state, 0, 0, 0) != 0)
 	{
 		lua_error();
 	}
 }
 
 //-----------------------------------------------------------------------------
-void LuaEnvironment::load_string(const char* str)
+void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
 {
-	int32_t loaded = luaL_loadstring(m_state, str);
+	CE_ASSERT_NOT_NULL(func);
 
-	if (loaded != 0)
-	{
-		lua_error();
-	}
-}
+	LuaStack stack(m_state);
 
-//-----------------------------------------------------------------------------
-void LuaEnvironment::get_global_symbol(const char* symbol)
-{
-	lua_getglobal(m_state, symbol);
-}
+	va_list vl;
+	va_start(vl, argc);
 
-//-----------------------------------------------------------------------------
-void LuaEnvironment::execute(int32_t args, int32_t results)
-{
-	int32_t executed = lua_pcall(m_state, args, results, 0);
+	lua_getglobal(m_state, func);
 
-	if (executed != 0)
+	for (uint8_t i = 0; i < argc; i++)
 	{
-		lua_error();
+		const int type = va_arg(vl, int);
+		switch (type)
+		{
+			case ARGUMENT_FLOAT:
+			{
+				stack.push_float(va_arg(vl, double));
+				break;
+			}
+			default:
+			{
+				CE_ASSERT(false, "Oops, lua argument unknown");
+				break;
+			}
+		}
 	}
-}
-
-// //-----------------------------------------------------------------------------
-// void LuaEnvironment::collect_garbage()
-// {
-// 	uint64_t start = os::milliseconds();
-
-// 	while ((os::milliseconds() - start) < device()->last_delta_time() && !m_is_used)
-// 	{
-// 		lua_gc(m_state, LUA_GCSTEP, 0);
-// 	}
-// }
-
-// //-----------------------------------------------------------------------------
-// void* LuaEnvironment::background_thread(void* thiz)
-// {
-// 	((LuaEnvironment*)thiz)->collect_garbage();	
-// }
-
-//-----------------------------------------------------------------------------
-void LuaEnvironment::game_init()
-{
-	const char* path = device()->filesystem()->os_path(g_default_mountpoint.value(), g_boot.value());
-
-	load_file(path);
-	execute(0, 0);
 
-	get_global_symbol("init");
-	execute(0, 0);
-}
-
-//-----------------------------------------------------------------------------
-void LuaEnvironment::game_shutdown()
-{
-	get_global_symbol("shutdown");
-	execute(0, 0);
-}
+	va_end(vl);
 
-//-----------------------------------------------------------------------------
-void LuaEnvironment::game_frame(float dt)
-{
-	LuaStack stack(m_state);
-	
-	get_global_symbol("frame");
-	stack.push_float(dt);
-	execute(1, 0);
+	if (lua_pcall(m_state, argc, 0, 0) != 0)
+	{
+		lua_error();
+	}
 }
 
 //-----------------------------------------------------------------------------
 void LuaEnvironment::lua_error()
 {
 	string::strncpy(m_error_buffer, "", 1024);
-
 	string::strncpy(m_error_buffer, lua_tostring(m_state, -1), 1024);
 }
 
@@ -213,7 +178,6 @@ void LuaEnvironment::load_module_function(const char* module, const char* name,
 
 	entry[0].name = name;
 	entry[0].func = func;
-
 	entry[1].name = NULL;
 	entry[1].func = NULL;
 
@@ -227,32 +191,6 @@ void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name,
 	lua_setfield(m_state, -2, name);
 }
 
-//-----------------------------------------------------------------------------
-CE_EXPORT int32_t luaopen_libcrown(lua_State* /*L*/)
-{
-	LuaEnvironment* env = device()->lua_environment();
-
-	load_int_setting(*env);
-	load_float_setting(*env);
-	load_string_setting(*env);
-
-	load_vec2(*env);
-	load_vec3(*env);
-	load_mat4(*env);
-	load_quat(*env);
-	load_math(*env);
-
-	load_mouse(*env);
-	load_keyboard(*env);
-	load_accelerometer(*env);
-
-	load_device(*env);
-
-	load_window(*env);
-
-	return 1;
-}
-
 const char* LuaEnvironment::class_system =  "function class(klass, super) "
     										"	if not klass then "
         									"		klass = {} "

+ 20 - 41
engine/lua/LuaEnvironment.h

@@ -27,15 +27,19 @@ OTHER DEALINGS IN THE SOFTWARE.
 #pragma once
 
 #include "lua.hpp"
-#include "Types.h"
 #include "Config.h"
-#include "LuaStack.h"
-#include "LinearAllocator.h"
-#include "Thread.h"
+#include "Types.h"
 
 namespace crown
 {
 
+enum LuaArgumentType
+{
+	ARGUMENT_FLOAT
+};
+
+class LuaResource;
+
 /// LuaEnvironment is a wrapper of a subset of Lua functions and 
 /// provides utilities for extending Lua
 class LuaEnvironment
@@ -48,41 +52,29 @@ public:
 	void					init();
 	/// Close Lua state and shutdown LuaEnvironment
 	void					shutdown();
-	/// Return the __lua_State__ pointer required by each Lua function
-	lua_State*				state();
 
 	const char*				error();
-	/// Load Lua chunk as buffer. @a buffer contains lua chunk, @len is length of buffer
-	void					load_buffer(const char* buffer, size_t len);
-	/// Load Lua chunk as file. @a file is path to lua file
-	void					load_file(const char* file);
-	/// Load Lua chunk as string
-	void 					load_string(const char* str);
-	/// Get global symbol from Lua stack. @symbol is the name of required symbol
-	void					get_global_symbol(const char* symbol);
-	/// Execute Lua chunk. @a args is the number of arguments required
-	/// @a results is the number of results returned
-	void					execute(int32_t args, int32_t results);
-	/// Collect garbage generated by Lua
-	void					collect_garbage();
-	/// Call init function in the Lua game file
-	void					game_init();
-	/// Call shutdown function in the Lua game file
-	void					game_shutdown();
-	/// Call frame function in the Lua game file
-	void					game_frame(float dt);
+
+	/// Loads and execute the given @a lr lua script resource.
+	void					load(const LuaResource* lr);
+
+	/// Calls the global function @a func with @a argc argument number.
+	/// Each argument is a pair (type, value).
+	/// Example call:
+	/// call_global("myfunc", 1, ARGUMENT_FLOAT, 3.14f)
+	void					call_global(const char* func, uint8_t argc, ...);
+
 	/// Load a function which will be used in Lua. @a module is the name of table contenitor,
 	/// @a name is the name of function in module and @func is the pointer to the function.
 	/// _func_ must be a C/lua function (__int32_t function_name(lua_State* L)__)
 	void					load_module_function(const char* module, const char* name, const lua_CFunction func);
+
 	/// Load a enum's value which will be used in Lua. 
 	/// @a module is the name of table contenitor, generally take  enum's name
 	/// @a name is module's name that refears _value_ and @value is an unsigned integer
 	void					load_module_enum(const char* module, const char* name, uint32_t value);
 
 private:
-	/// Thread used for garbage collection. It calls __collect_garbage()__
-	static void*			background_thread(void* thiz);
 
 	void					lua_error();
 	// Disable copying
@@ -92,28 +84,17 @@ private:
 private:
 	/// Required by each Lua function
 	lua_State*				m_state;
+
 	/// LuaEnvironment is used right now?
 	bool					m_is_used;
-	/// Thread used for garbage collection
-	// os::Thread 				m_thread;
 
 	char					m_error_buffer[1024];
-
 	char					m_tmp_buffer[1024];
-
 	static const char* 		class_system;
-
 	static const char*		commands_list;
-
 	static const char*		get_cmd_by_name;
-
 	static const char*		tmp_print_table;
-
 	static const char*		count_all;
-
-	// static const char*		type_name;
-
-	// static const char*		type_count;
 };
 
 
@@ -134,6 +115,4 @@ void load_camera(LuaEnvironment& env);
 void load_device(LuaEnvironment& env);
 void load_window(LuaEnvironment& env);
 
-CE_EXPORT int32_t luaopen_libcrown(lua_State* L);
-
 } // namespace crown