2
0

LuaSystem.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include "LuaSystem.h"
  24. #include "lua.hpp"
  25. #include "Config.h"
  26. #include "LuaStack.h"
  27. #include "Assert.h"
  28. #include "Vector2.h"
  29. #include "Vector3.h"
  30. #include "Matrix4x4.h"
  31. #include "Quaternion.h"
  32. #include "Device.h"
  33. #include "ResourceManager.h"
  34. #include "LuaResource.h"
  35. #include "LuaEnvironment.h"
  36. namespace crown
  37. {
  38. extern int vector2_add(lua_State* L);
  39. extern int vector2_subtract(lua_State* L);
  40. extern int vector2_multiply(lua_State* L);
  41. extern int vector2_divide(lua_State* L);
  42. extern int vector2_negate(lua_State* L);
  43. extern int vector3_add(lua_State* L);
  44. extern int vector3_subtract(lua_State* L);
  45. extern int vector3_multiply(lua_State* L);
  46. extern int vector3_divide(lua_State* L);
  47. extern int vector3_negate(lua_State* L);
  48. extern int vector2(lua_State* L);
  49. extern int vector3(lua_State* L);
  50. extern int matrix4x4(lua_State* L);
  51. extern int quaternion(lua_State* L);
  52. //-----------------------------------------------------------------------------
  53. static int crown_lua_vector2_call(lua_State* L)
  54. {
  55. lua_remove(L, 1);
  56. return vector2(L);
  57. }
  58. //-----------------------------------------------------------------------------
  59. static int crown_lua_vector3_call(lua_State* L)
  60. {
  61. lua_remove(L, 1);
  62. return vector3(L);
  63. }
  64. //-----------------------------------------------------------------------------
  65. static int crown_lua_matrix4x4_call(lua_State* L)
  66. {
  67. lua_remove(L, 1);
  68. return matrix4x4(L);
  69. }
  70. //-----------------------------------------------------------------------------
  71. static int crown_lua_quaternion_call(lua_State* L)
  72. {
  73. lua_remove(L, 1);
  74. return quaternion(L);
  75. }
  76. //-----------------------------------------------------------------------------
  77. static int crown_lua_lightuserdata_add(lua_State* L)
  78. {
  79. LuaStack stack(L);
  80. if (lua_system::is_vector3(1))
  81. {
  82. return vector3_add(L);
  83. }
  84. return vector2_add(L);
  85. }
  86. //-----------------------------------------------------------------------------
  87. static int crown_lua_lightuserdata_sub(lua_State* L)
  88. {
  89. LuaStack stack(L);
  90. if (lua_system::is_vector3(1))
  91. {
  92. return vector3_subtract(L);
  93. }
  94. return vector2_subtract(L);
  95. }
  96. //-----------------------------------------------------------------------------
  97. static int crown_lua_lightuserdata_mul(lua_State* L)
  98. {
  99. LuaStack stack(L);
  100. if (lua_system::is_vector3(1))
  101. {
  102. return vector3_multiply(L);
  103. }
  104. return vector2_multiply(L);
  105. }
  106. //-----------------------------------------------------------------------------
  107. static int crown_lua_lightuserdata_div(lua_State* L)
  108. {
  109. LuaStack stack(L);
  110. if (lua_system::is_vector3(1))
  111. {
  112. return vector3_divide(L);
  113. }
  114. return vector2_divide(L);
  115. }
  116. //-----------------------------------------------------------------------------
  117. static int crown_lua_lightuserdata_unm(lua_State* L)
  118. {
  119. LuaStack stack(L);
  120. if (lua_system::is_vector3(1))
  121. {
  122. return vector3_negate(L);
  123. }
  124. return vector2_negate(L);
  125. }
  126. //-----------------------------------------------------------------------------
  127. static int crown_lua_require(lua_State* L)
  128. {
  129. LuaStack stack(L);
  130. const char* filename = stack.get_string(1);
  131. const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
  132. device()->resource_manager()->flush();
  133. const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
  134. luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
  135. device()->resource_manager()->unload(lua_res);
  136. return 1;
  137. }
  138. namespace lua_system
  139. {
  140. static lua_State* s_L;
  141. static uint32_t s_vec2_used = 0;
  142. static Vector2 s_vec2_buffer[CE_MAX_LUA_VECTOR2];
  143. static uint32_t s_vec3_used = 0;
  144. static Vector3 s_vec3_buffer[CE_MAX_LUA_VECTOR3];
  145. static uint32_t s_mat4_used = 0;
  146. static Matrix4x4 s_mat4_buffer[CE_MAX_LUA_MATRIX4X4];
  147. static uint32_t s_quat_used = 0;
  148. static Quaternion s_quat_buffer[CE_MAX_LUA_QUATERNION];
  149. void init()
  150. {
  151. s_L = luaL_newstate();
  152. CE_ASSERT(s_L, "Unable to create lua state");
  153. // Open default libraries
  154. luaL_openlibs(s_L);
  155. // Register crown libraries
  156. LuaEnvironment env(s_L);
  157. load_int_setting(env);
  158. load_float_setting(env);
  159. load_string_setting(env);
  160. load_vector2(env);
  161. load_vector3(env);
  162. load_matrix4x4(env);
  163. load_quaternion(env);
  164. load_math(env);
  165. load_window(env);
  166. load_mouse(env);
  167. load_keyboard(env);
  168. load_touch(env);
  169. load_accelerometer(env);
  170. load_device(env);
  171. load_resource_package(env);
  172. load_unit(env);
  173. load_camera(env);
  174. load_world(env);
  175. load_mesh(env);
  176. load_sprite(env);
  177. load_actor(env);
  178. load_controller(env);
  179. load_physics_world(env);
  180. load_sound_world(env);
  181. load_gui(env);
  182. load_debug_line(env);
  183. load_raycast(env);
  184. // Register custom loader
  185. lua_getfield(s_L, LUA_GLOBALSINDEX, "package");
  186. lua_getfield(s_L, -1, "loaders");
  187. lua_remove(s_L, -2);
  188. int num_loaders = 0;
  189. lua_pushnil(s_L);
  190. while (lua_next(s_L, -2) != 0)
  191. {
  192. lua_pop(s_L, 1);
  193. num_loaders++;
  194. }
  195. lua_pushinteger(s_L, num_loaders + 1);
  196. lua_pushcfunction(s_L, crown_lua_require);
  197. lua_rawset(s_L, -3);
  198. lua_pop(s_L, 1);
  199. // Create metatable for lightuserdata
  200. lua_pushlightuserdata(s_L, (void*)0x0); // Just dummy userdata
  201. luaL_newmetatable(s_L, "Lightuserdata_mt");
  202. lua_setmetatable(s_L, -2);
  203. lua_pop(s_L, -1);
  204. luaL_newmetatable(s_L, "Lightuserdata_mt");
  205. lua_pushstring(s_L, "__add");
  206. lua_pushcfunction(s_L, crown_lua_lightuserdata_add);
  207. lua_settable(s_L, 1);
  208. lua_pushstring(s_L, "__sub");
  209. lua_pushcfunction(s_L, crown_lua_lightuserdata_sub);
  210. lua_settable(s_L, 1);
  211. lua_pushstring(s_L, "__mul");
  212. lua_pushcfunction(s_L, crown_lua_lightuserdata_mul);
  213. lua_settable(s_L, 1);
  214. lua_pushstring(s_L, "__div");
  215. lua_pushcfunction(s_L, crown_lua_lightuserdata_div);
  216. lua_settable(s_L, 1);
  217. lua_pushstring(s_L, "__unm");
  218. lua_pushcfunction(s_L, crown_lua_lightuserdata_unm);
  219. lua_settable(s_L, 1);
  220. //lua_pop(s_L, -1); // pop Lightuserdata_mt
  221. Log::d("Stacksize = %d", lua_gettop(s_L));
  222. // Vector2 metatable
  223. luaL_newmetatable(s_L, "Vector2_mt");
  224. lua_pushvalue(s_L, -1);
  225. lua_setfield(s_L, -2, "__index");
  226. lua_pushcfunction(s_L, crown_lua_vector2_call);
  227. lua_setfield(s_L, -2, "__call");
  228. //lua_pop(s_L, -1); // pop Vector2_mt
  229. lua_getglobal(s_L, "Vector2");
  230. lua_pushvalue(s_L, -2); // Duplicate metatable
  231. lua_setmetatable(s_L, -2); // setmetatable(Vector2, Vector2_mt)
  232. //lua_pop(s_L, -1); // pop Vector2
  233. // Vector3 metatable
  234. luaL_newmetatable(s_L, "Vector3_mt");
  235. lua_pushvalue(s_L, -1);
  236. lua_setfield(s_L, -2, "__index");
  237. lua_pushcfunction(s_L, crown_lua_vector3_call);
  238. lua_setfield(s_L, -2, "__call");
  239. //lua_pop(s_L, -1);
  240. lua_getglobal(s_L, "Vector3");
  241. lua_pushvalue(s_L, -2); // Duplicate metatable
  242. lua_setmetatable(s_L, -2); // setmetatable(Vector3, Vector3_mt)
  243. //lua_pop(s_L, -1);
  244. // Matrix4x4 metatable
  245. luaL_newmetatable(s_L, "Matrix4x4_mt");
  246. lua_pushvalue(s_L, -1);
  247. lua_setfield(s_L, -2, "__index");
  248. lua_pushcfunction(s_L, crown_lua_matrix4x4_call);
  249. lua_setfield(s_L, -2, "__call");
  250. lua_pop(s_L, -1);
  251. lua_getglobal(s_L, "Matrix4x4");
  252. lua_pushvalue(s_L, -2); // Duplicate metatable
  253. lua_setmetatable(s_L, -2); // setmetatable(Matrix4x4, Matrix4x4_mt)
  254. //lua_pop(s_L, -1);
  255. // Quaternion metatable
  256. luaL_newmetatable(s_L, "Quaternion_mt");
  257. lua_pushvalue(s_L, -1);
  258. lua_setfield(s_L, -2, "__index");
  259. lua_pushcfunction(s_L, crown_lua_quaternion_call);
  260. lua_setfield(s_L, -2, "__call");
  261. //lua_pop(s_L, -1);
  262. lua_getglobal(s_L, "Quaternion");
  263. lua_pushvalue(s_L, -2); // Duplicate metatable
  264. lua_setmetatable(s_L, -2); // setmetatable(Quaternion, Quaternion_mt)
  265. //lua_pop(s_L, -1);
  266. Log::d("Stacksize = %d", lua_gettop(s_L));
  267. }
  268. void shutdown()
  269. {
  270. lua_close(s_L);
  271. }
  272. lua_State* state()
  273. {
  274. return s_L;
  275. }
  276. Vector2* next_vector2(const Vector2& v)
  277. {
  278. CE_ASSERT(s_vec2_used < CE_MAX_LUA_VECTOR2, "Maximum number of Vector2 reached");
  279. return &(s_vec2_buffer[s_vec2_used++] = v);
  280. }
  281. Vector3* next_vector3(const Vector3& v)
  282. {
  283. CE_ASSERT(s_vec3_used < CE_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  284. return &(s_vec3_buffer[s_vec3_used++] = v);
  285. }
  286. Matrix4x4* next_matrix4x4(const Matrix4x4& m)
  287. {
  288. CE_ASSERT(s_mat4_used < CE_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  289. return &(s_mat4_buffer[s_mat4_used++] = m);
  290. }
  291. Quaternion* next_quaternion(const Quaternion& q)
  292. {
  293. CE_ASSERT(s_quat_used < CE_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  294. return &(s_quat_buffer[s_quat_used++] = q);
  295. }
  296. bool is_vector2(int32_t index)
  297. {
  298. void* type = lua_touserdata(s_L, index);
  299. return (type >= &s_vec2_buffer[0] && type <= &s_vec2_buffer[CE_MAX_LUA_VECTOR2 - 1]);
  300. }
  301. bool is_vector3(int32_t index)
  302. {
  303. void* type = lua_touserdata(s_L, index);
  304. return (type >= &s_vec3_buffer[0] && type <= &s_vec3_buffer[CE_MAX_LUA_VECTOR3 - 1]);
  305. }
  306. bool is_matrix4x4(int32_t index)
  307. {
  308. void* type = lua_touserdata(s_L, index);
  309. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CE_MAX_LUA_MATRIX4X4 - 1]);
  310. }
  311. bool is_quaternion(int32_t index)
  312. {
  313. void* type = lua_touserdata(s_L, index);
  314. return (type >= &s_quat_buffer[0] && type <= &s_quat_buffer[CE_MAX_LUA_QUATERNION - 1]);
  315. }
  316. void clear_temporaries()
  317. {
  318. s_vec2_used = 0;
  319. s_vec3_used = 0;
  320. s_mat4_used = 0;
  321. s_quat_used = 0;
  322. }
  323. } // namespace lua_system
  324. } // namespace crown