2
0
Эх сурвалжийг харах

Better organize lua stuff

Daniele Bartolini 12 жил өмнө
parent
commit
5476b6a9fc

+ 1 - 1
engine/Android.mk

@@ -185,8 +185,8 @@ LOCAL_SRC_FILES :=\
 	lua/LuaResourcePackage.cpp\
 	lua/LuaSoundWorld.cpp\
 	lua/LuaSprite.cpp\
-	lua/LuaStack.cpp\
 	lua/LuaStringSetting.cpp\
+	lua/LuaSystem.cpp\
 	lua/LuaTouch.cpp\
 	lua/LuaUnit.cpp\
 	lua/LuaVector2.cpp\

+ 3 - 2
engine/CMakeLists.txt

@@ -372,8 +372,8 @@ set (LUA_SRC
 	lua/LuaResourcePackage.cpp
 	lua/LuaSoundWorld.cpp
 	lua/LuaSprite.cpp
-	lua/LuaStack.cpp
 	lua/LuaStringSetting.cpp
+	lua/LuaSystem.cpp
 	lua/LuaTouch.cpp
 	lua/LuaUnit.cpp
 	lua/LuaVector2.cpp
@@ -383,8 +383,9 @@ set (LUA_SRC
 )
 
 set (LUA_HEADERS
-	lua/LuaStack.h
 	lua/LuaEnvironment.h
+	lua/LuaStack.h
+	lua/LuaSystem.h
 )
 
 set (AUDIO_SRC)

+ 5 - 0
engine/Config.h.in

@@ -64,3 +64,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #define CE_MAX_GUI_TEXTS 					64					// Per Gui
 
 #define CE_MAX_DEBUG_LINES					2 * 1024			// Per DebugLine
+
+#define CE_MAX_LUA_VECTOR2					4096
+#define CE_MAX_LUA_VECTOR3					4096
+#define CE_MAX_LUA_MATRIX4X4				4096
+#define CE_MAX_LUA_QUATERNION				4096

+ 7 - 7
engine/Device.cpp

@@ -56,6 +56,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "LuaStack.h"
 #include "WorldManager.h"
 #include "NetworkFilesystem.h"
+#include "LuaSystem.h"
 
 #if defined(LINUX) || defined(WINDOWS)
 	#include "BundleCompiler.h"
@@ -171,9 +172,9 @@ void Device::init()
 	m_renderer = CE_NEW(m_allocator, Renderer)(m_allocator);
 	m_renderer->init();
 
-	Log::d("Creating lua environment...");
-	m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)();
-	m_lua_environment->init();
+	Log::d("Creating lua system...");
+	lua_system::init();
+	m_lua_environment = CE_NEW(m_allocator, LuaEnvironment)(lua_system::state());
 
 	Log::d("Creating physics...");
 	physics_system::init();
@@ -222,11 +223,10 @@ void Device::shutdown()
 	Log::d("Releasing physics...");
 	physics_system::shutdown();
 
-	Log::d("Releasing lua environment...");
+	Log::d("Releasing lua system...");
+	lua_system::shutdown();
 	if (m_lua_environment)
 	{
-		m_lua_environment->shutdown();
-		
 		CE_DELETE(m_allocator, m_lua_environment);
 	}
 
@@ -413,7 +413,7 @@ void Device::frame()
 		m_renderer->frame();
 	}
 
-	clear_lua_temporaries();
+	lua_system::clear_temporaries();
 
 	m_frame_count++;
 }

+ 3 - 268
engine/lua/LuaEnvironment.cpp

@@ -33,280 +33,15 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Device.h"
 #include "LuaResource.h"
 #include "ResourceManager.h"
+#include "Config.h"
 
 namespace crown
 {
 
-extern int vector2_add(lua_State* L);
-extern int vector2_subtract(lua_State* L);
-extern int vector2_multiply(lua_State* L);
-extern int vector2_divide(lua_State* L);
-extern int vector2_negate(lua_State* L);
-extern int vector3_add(lua_State* L);
-extern int vector3_subtract(lua_State* L);
-extern int vector3_multiply(lua_State* L);
-extern int vector3_divide(lua_State* L);
-extern int vector3_negate(lua_State* L);
-extern int vector2(lua_State* L);
-extern int vector3(lua_State* L);
-extern int matrix4x4(lua_State* L);
-extern int quaternion(lua_State* L);
-
-//-----------------------------------------------------------------------------
-CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
-{
-	LuaEnvironment* env = device()->lua_environment();
-
-	load_int_setting(*env);
-	load_float_setting(*env);
-	load_string_setting(*env);
-
-	load_vector2(*env);
-	load_vector3(*env);
-	load_matrix4x4(*env);
-	load_quaternion(*env);
-	load_math(*env);
-	load_window(*env);
-	load_mouse(*env);
-	load_keyboard(*env);
-	load_touch(*env);
-	load_accelerometer(*env);
-	load_device(*env);
-	load_resource_package(*env);
-
-	load_unit(*env);
-	load_camera(*env);
-	load_world(*env);
-	load_mesh(*env);
-	load_sprite(*env);
-	load_actor(*env);
-	load_controller(*env);
-	load_physics_world(*env);
-	load_sound_world(*env);
-
-	load_gui(*env);
-
-	load_debug_line(*env);
-
-	return 1;
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_vector2_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return vector2(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_vector3_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return vector3(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_matrix4x4_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return matrix4x4(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_quaternion_call(lua_State* L)
-{
-	lua_remove(L, 1);
-	return quaternion(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_add(lua_State* L)
-{
-	LuaStack stack(L);
-
-	if (stack.is_vector3(1))
-	{
-		return vector3_add(L);
-	}
-	return vector2_add(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_sub(lua_State* L)
-{
-	LuaStack stack(L);
-
-	if (stack.is_vector3(1))
-	{
-		return vector3_subtract(L);
-	}
-	return vector2_subtract(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_mul(lua_State* L)
-{
-	LuaStack stack(L);
-
-	if (stack.is_vector3(1))
-	{
-		return vector3_multiply(L);
-	}
-	return vector2_multiply(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_div(lua_State* L)
-{
-	LuaStack stack(L);
-
-	if (stack.is_vector3(1))
-	{
-		return vector3_divide(L);
-	}
-	return vector2_divide(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_lightuserdata_unm(lua_State* L)
-{
-	LuaStack stack(L);
-
-	if (stack.is_vector3(1))
-	{
-		return vector3_negate(L);
-	}
-	return vector2_negate(L);
-}
-
-//-----------------------------------------------------------------------------
-static int crown_lua_require(lua_State* L)
-{
-	LuaStack stack(L);
-
-	const char* filename = stack.get_string(1);
-
-	const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
-	device()->resource_manager()->flush();
-
-	const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
-	luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
-
-	device()->resource_manager()->unload(lua_res);
-
-	return 1;
-}
-
-//-----------------------------------------------------------------------------
-LuaEnvironment::LuaEnvironment()
-	: m_state(luaL_newstate())
-{
-}
-
-//-----------------------------------------------------------------------------
-void LuaEnvironment::init()
-{
-	// Open default libraries
-	luaL_openlibs(m_state);
-	// Open Crown library
-	lua_cpcall(m_state, luaopen_libcrown, NULL);
-
-	// Register custom loader
-	lua_getfield(m_state, LUA_GLOBALSINDEX, "package");
-	lua_getfield(m_state, -1, "loaders");
-	lua_remove(m_state, -2);
-
-	int num_loaders = 0;
-	lua_pushnil(m_state);
-	while (lua_next(m_state, -2) != 0)
-	{
-		lua_pop(m_state, 1);
-		num_loaders++;
-	}
-
-	lua_pushinteger(m_state, num_loaders + 1);
-	lua_pushcfunction(m_state, crown_lua_require);
-	lua_rawset(m_state, -3);
-
-	lua_pop(m_state, 1);
-
-	// Create metatable for lightuserdata
-	lua_pushlightuserdata(m_state, (void*)0x0); // Just dummy userdata
-	luaL_newmetatable(m_state, "Lightuserdata_mt");
-	lua_setmetatable(m_state, -2);
-	lua_pop(m_state, 1);
-
-	luaL_newmetatable(m_state, "Lightuserdata_mt");
-
-	lua_pushstring(m_state, "__add");
-	lua_pushcfunction(m_state, crown_lua_lightuserdata_add);
-	lua_settable(m_state, 1);
-
-	lua_pushstring(m_state, "__sub");
-	lua_pushcfunction(m_state, crown_lua_lightuserdata_sub);
-	lua_settable(m_state, 1);
-
-	lua_pushstring(m_state, "__mul");
-	lua_pushcfunction(m_state, crown_lua_lightuserdata_mul);
-	lua_settable(m_state, 1);
-
-	lua_pushstring(m_state, "__div");
-	lua_pushcfunction(m_state, crown_lua_lightuserdata_div);
-	lua_settable(m_state, 1);
-
-	lua_pushstring(m_state, "__unm");
-	lua_pushcfunction(m_state, crown_lua_lightuserdata_unm);
-	lua_settable(m_state, 1);
-
-	// Vector2 metatable
-	luaL_newmetatable(m_state, "Vector2_mt");
-	lua_pushvalue(m_state, -1);
-	lua_setfield(m_state, -2, "__index");
-	lua_pushcfunction(m_state, crown_lua_vector2_call);
-	lua_setfield(m_state, -2, "__call");
-
-	lua_getglobal(m_state, "Vector2");
-	lua_pushvalue(m_state, -2); // Duplicate metatable
-	lua_setmetatable(m_state, -2); // setmetatable(Vector2, Vector2_mt)
-
-	// Vector3 metatable
-	luaL_newmetatable(m_state, "Vector3_mt");
-	lua_pushvalue(m_state, -1);
-	lua_setfield(m_state, -2, "__index");
-	lua_pushcfunction(m_state, crown_lua_vector3_call);
-	lua_setfield(m_state, -2, "__call");
-
-	lua_getglobal(m_state, "Vector3");
-	lua_pushvalue(m_state, -2); // Duplicate metatable
-	lua_setmetatable(m_state, -2); // setmetatable(Vector3, Vector3_mt)
-
-	// Matrix4x4 metatable
-	luaL_newmetatable(m_state, "Matrix4x4_mt");
-	lua_pushvalue(m_state, -1);
-	lua_setfield(m_state, -2, "__index");
-	lua_pushcfunction(m_state, crown_lua_matrix4x4_call);
-	lua_setfield(m_state, -2, "__call");
-
-	lua_getglobal(m_state, "Matrix4x4");
-	lua_pushvalue(m_state, -2); // Duplicate metatable
-	lua_setmetatable(m_state, -2); // setmetatable(Matrix4x4, Matrix4x4_mt)
-
-	// Quaternion metatable
-	luaL_newmetatable(m_state, "Quaternion_mt");
-	lua_pushvalue(m_state, -1);
-	lua_setfield(m_state, -2, "__index");
-	lua_pushcfunction(m_state, crown_lua_quaternion_call);
-	lua_setfield(m_state, -2, "__call");
-
-	lua_getglobal(m_state, "Quaternion");
-	lua_pushvalue(m_state, -2); // Duplicate metatable
-	lua_setmetatable(m_state, -2); // setmetatable(Quaternion, Quaternion_mt)
-}
-
 //-----------------------------------------------------------------------------
-void LuaEnvironment::shutdown()
+LuaEnvironment::LuaEnvironment(lua_State* L)
+	: m_state(L)
 {
-	lua_close(m_state);
 }
 
 //-----------------------------------------------------------------------------

+ 2 - 8
engine/lua/LuaEnvironment.h

@@ -44,16 +44,10 @@ struct LuaResource;
 /// provides utilities for extending Lua
 class LuaEnvironment
 {
-
 public:
-
-	LuaEnvironment();
 	
-	/// Init Lua state and open libraries. Must be called first
-	void init();
-	/// Close Lua state and shutdown LuaEnvironment
-	void shutdown();
-
+	LuaEnvironment(lua_State* L);
+	
 	/// Loads and execute the given @a res_name Lua resource, returns
 	/// true if success, false otherwise.
 	bool load_and_execute(const char* res_name);

+ 0 - 202
engine/lua/LuaStack.cpp

@@ -1,202 +0,0 @@
-/*
-Copyright (c) 2013 Daniele Bartolini, Michele Rossi
-Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
-
-Permission is hereby granted, free of charge, to any person
-obtaining a copy of this software and associated documentation
-files (the "Software"), to deal in the Software without
-restriction, including without limitation the rights to use,
-copy, modify, merge, publish, distribute, sublicense, and/or sell
-copies of the Software, and to permit persons to whom the
-Software is furnished to do so, subject to the following
-conditions:
-
-The above copyright notice and this permission notice shall be
-included in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
-EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
-OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
-NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
-HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
-WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-*/
-
-#include "LuaStack.h"
-#include "Assert.h"
-#include "Vector2.h"
-#include "Vector3.h"
-#include "Matrix4x4.h"
-#include "Quaternion.h"
-
-namespace crown
-{
-
-static const uint32_t 	LUA_VEC2_BUFFER_SIZE = 4096;
-static Vector2 			g_vec2_buffer[LUA_VEC2_BUFFER_SIZE];
-static uint32_t 		g_vec2_used = 0;
-
-static const uint32_t 	LUA_VEC3_BUFFER_SIZE = 4096;
-static Vector3 			g_vec3_buffer[LUA_VEC3_BUFFER_SIZE];
-static uint32_t 		g_vec3_used = 0;
-
-static const uint32_t 	LUA_MAT4_BUFFER_SIZE = 4096;
-static Matrix4x4 		g_mat4_buffer[LUA_MAT4_BUFFER_SIZE];
-static uint32_t 		g_mat4_used = 0;
-
-static const uint32_t 	LUA_QUAT_BUFFER_SIZE = 4096;
-static Quaternion 		g_quat_buffer[LUA_QUAT_BUFFER_SIZE];
-static uint32_t 		g_quat_used = 0;
-
-//-----------------------------------------------------------------------------
-static Vector2* next_vec2(const Vector2& v)
-{
-	CE_ASSERT(g_vec2_used < LUA_VEC2_BUFFER_SIZE, "Maximum number of Vector2 reached");
-
-	return &(g_vec2_buffer[g_vec2_used++] = v);
-}
-
-//-----------------------------------------------------------------------------
-static Vector3* next_vec3(const Vector3& v)
-{
-	CE_ASSERT(g_vec3_used < LUA_VEC3_BUFFER_SIZE, "Maximum number of Vector3 reached");
-
-	return &(g_vec3_buffer[g_vec3_used++] = v);
-}
-
-//-----------------------------------------------------------------------------
-static Matrix4x4* next_mat4(const Matrix4x4& m)
-{
-	CE_ASSERT(g_mat4_used < LUA_MAT4_BUFFER_SIZE, "Maximum number of Matrix4x4 reached");
-
-	return &(g_mat4_buffer[g_mat4_used++] = m);
-}
-
-//-----------------------------------------------------------------------------
-static Quaternion* next_quat(const Quaternion& q)
-{
-	CE_ASSERT(g_quat_used < LUA_QUAT_BUFFER_SIZE, "Maximum number of Quaternion reached");
-
-	return &(g_quat_buffer[g_quat_used++] = q);
-}
-
-//-----------------------------------------------------------------------------
-void clear_lua_temporaries()
-{
-	g_vec2_used = 0;
-	g_vec3_used = 0;
-	g_mat4_used = 0;
-	g_quat_used = 0;
-}
-
-//-----------------------------------------------------------------------------
-Vector2& LuaStack::get_vector2(int32_t index)
-{
-	void* v = lua_touserdata(m_state, index);
-
-	if (!is_vector2(index))
-	{
-		luaL_typerror(m_state, index, "Vector2");
-	}
-
-	return *(Vector2*)v;
-}
-
-//-----------------------------------------------------------------------------
-Vector3& LuaStack::get_vector3(int32_t index)
-{
-	void* v = lua_touserdata(m_state, index);
-
-	if (!is_vector3(index))
-	{
-		luaL_typerror(m_state, index, "Vector3");
-	}
-
-	return *(Vector3*)v;
-}
-
-//-----------------------------------------------------------------------------
-Matrix4x4& LuaStack::get_matrix4x4(int32_t index)
-{
-	void* m = lua_touserdata(m_state, index);
-
-	if (!is_matrix4x4(index))
-	{
-		luaL_typerror(m_state, index, "Matrix4x4");
-	}
-
-	return *(Matrix4x4*)m;
-}
-
-//-----------------------------------------------------------------------------
-Quaternion& LuaStack::get_quaternion(int32_t index)
-{
-	void* q = lua_touserdata(m_state, index);
-
-	if (!is_quaternion(index))
-	{
-		luaL_typerror(m_state, index, "Quaternion");
-	}
-
-	return *(Quaternion*)q;
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_vector2(const Vector2& v)
-{
-	lua_pushlightuserdata(m_state, next_vec2(v));
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_vector3(const Vector3& v)
-{
-	lua_pushlightuserdata(m_state, next_vec3(v));
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_matrix4x4(const Matrix4x4& m)
-{
-	lua_pushlightuserdata(m_state, next_mat4(m));
-}
-
-//-----------------------------------------------------------------------------
-void LuaStack::push_quaternion(const Quaternion& q)
-{
-	lua_pushlightuserdata(m_state, next_quat(q));
-}
-
-//-----------------------------------------------------------------------------
-bool LuaStack::is_vector2(int32_t index)
-{
-	void* type = lua_touserdata(m_state, index);
-
-	return (type >= &g_vec2_buffer[0] && type <= &g_vec2_buffer[LUA_VEC2_BUFFER_SIZE - 1]);
-}
-
-//-----------------------------------------------------------------------------
-bool LuaStack::is_vector3(int32_t index)
-{
-	void* type = lua_touserdata(m_state, index);
-
-	return (type >= &g_vec3_buffer[0] && type <= &g_vec3_buffer[LUA_VEC3_BUFFER_SIZE - 1]);
-}
-
-//-----------------------------------------------------------------------------
-bool LuaStack::is_matrix4x4(int32_t index)
-{
-	void* type = lua_touserdata(m_state, index);
-
-	return (type >= &g_mat4_buffer[0] && type <= &g_mat4_buffer[LUA_MAT4_BUFFER_SIZE - 1]);
-}
-
-//-----------------------------------------------------------------------------
-bool LuaStack::is_quaternion(int32_t index)
-{
-	void* type = lua_touserdata(m_state, index);
-
-	return (type >= &g_quat_buffer[0] && type <= &g_quat_buffer[LUA_QUAT_BUFFER_SIZE - 1]);
-}
-
-} // namespace crown

+ 76 - 12
engine/lua/LuaStack.h

@@ -28,6 +28,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 
 #include "lua.hpp"
 #include "Types.h"
+#include "LuaSystem.h"
 
 namespace crown
 {
@@ -381,18 +382,81 @@ public:
 		return (DebugLine*) lua_touserdata(m_state, index);
 	}
 
-	bool is_vector2(int32_t index);
-	bool is_vector3(int32_t index);
-	bool is_matrix4x4(int32_t index);
-	bool is_quaternion(int32_t index);
-	Vector2& get_vector2(int32_t index);
-	Vector3& get_vector3(int32_t index);
-	Matrix4x4& get_matrix4x4(int32_t index);
-	Quaternion& get_quaternion(int32_t index);
-	void push_vector2(const Vector2& v);
-	void push_vector3(const Vector3& v);
-	void push_matrix4x4(const Matrix4x4& m);
-	void push_quaternion(const Quaternion& q);
+	//-----------------------------------------------------------------------------
+	Vector2& get_vector2(int32_t index)
+	{
+		void* v = lua_touserdata(m_state, index);
+
+		if (!lua_system::is_vector2(index))
+		{
+			luaL_typerror(m_state, index, "Vector2");
+		}
+
+		return *(Vector2*)v;
+	}
+
+	//-----------------------------------------------------------------------------
+	Vector3& get_vector3(int32_t index)
+	{
+		void* v = lua_touserdata(m_state, index);
+
+		if (!lua_system::is_vector3(index))
+		{
+			luaL_typerror(m_state, index, "Vector3");
+		}
+
+		return *(Vector3*)v;
+	}
+
+	//-----------------------------------------------------------------------------
+	Matrix4x4& get_matrix4x4(int32_t index)
+	{
+		void* m = lua_touserdata(m_state, index);
+
+		if (!lua_system::is_matrix4x4(index))
+		{
+			luaL_typerror(m_state, index, "Matrix4x4");
+		}
+
+		return *(Matrix4x4*)m;
+	}
+
+	//-----------------------------------------------------------------------------
+	Quaternion& get_quaternion(int32_t index)
+	{
+		void* q = lua_touserdata(m_state, index);
+
+		if (!lua_system::is_quaternion(index))
+		{
+			luaL_typerror(m_state, index, "Quaternion");
+		}
+
+		return *(Quaternion*)q;
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_vector2(const Vector2& v)
+	{
+		lua_pushlightuserdata(m_state, lua_system::next_vector2(v));
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_vector3(const Vector3& v)
+	{
+		lua_pushlightuserdata(m_state, lua_system::next_vector3(v));
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_matrix4x4(const Matrix4x4& m)
+	{
+		lua_pushlightuserdata(m_state, lua_system::next_matrix4x4(m));
+	}
+
+	//-----------------------------------------------------------------------------
+	void push_quaternion(const Quaternion& q)
+	{
+		lua_pushlightuserdata(m_state, lua_system::next_quaternion(q));
+	}
 
 private: