lua_system.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 "lua_system.h"
  24. #include "lua.hpp"
  25. #include "config.h"
  26. #include "lua_stack.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 "resource_manager.h"
  34. #include "lua_resource.h"
  35. #include "lua_environment.h"
  36. #include "lua_stack.h"
  37. #include "log.h"
  38. namespace crown
  39. {
  40. // Lua modules
  41. extern void load_actor(LuaEnvironment& env);
  42. extern void load_camera(LuaEnvironment& env);
  43. extern void load_controller(LuaEnvironment& env);
  44. extern void load_debug_line(LuaEnvironment& env);
  45. extern void load_device(LuaEnvironment& env);
  46. extern void load_float_setting(LuaEnvironment& env);
  47. extern void load_gui(LuaEnvironment& env);
  48. extern void load_int_setting(LuaEnvironment& env);
  49. extern void load_keyboard(LuaEnvironment& env);
  50. extern void load_math(LuaEnvironment& env);
  51. extern void load_matrix4x4(LuaEnvironment& env);
  52. extern void load_matrix4x4box(LuaEnvironment& env);
  53. extern void load_mesh(LuaEnvironment& env);
  54. extern void load_mouse(LuaEnvironment& env);
  55. extern void load_physics_world(LuaEnvironment& env);
  56. extern void load_quaternion(LuaEnvironment& env);
  57. extern void load_quaternionbox(LuaEnvironment& env);
  58. extern void load_raycast(LuaEnvironment& env);
  59. extern void load_resource_package(LuaEnvironment& env);
  60. extern void load_sound_world(LuaEnvironment& env);
  61. extern void load_sprite(LuaEnvironment& env);
  62. extern void load_string_setting(LuaEnvironment& env);
  63. extern void load_touch(LuaEnvironment& env);
  64. extern void load_unit(LuaEnvironment& env);
  65. extern void load_vector2(LuaEnvironment& env);
  66. extern void load_vector2box(LuaEnvironment& env);
  67. extern void load_vector3(LuaEnvironment& env);
  68. extern void load_vector3box(LuaEnvironment& env);
  69. extern void load_window(LuaEnvironment& env);
  70. extern void load_world(LuaEnvironment& env);
  71. extern void load_color4(LuaEnvironment& env);
  72. extern void load_material(LuaEnvironment& env);
  73. namespace lua_system
  74. {
  75. static lua_State* s_L;
  76. static uint32_t s_vec2_used = 0;
  77. static Vector2 s_vec2_buffer[CE_MAX_LUA_VECTOR2];
  78. static uint32_t s_vec3_used = 0;
  79. static Vector3 s_vec3_buffer[CE_MAX_LUA_VECTOR3];
  80. static uint32_t s_mat4_used = 0;
  81. static Matrix4x4 s_mat4_buffer[CE_MAX_LUA_MATRIX4X4];
  82. static uint32_t s_quat_used = 0;
  83. static Quaternion s_quat_buffer[CE_MAX_LUA_QUATERNION];
  84. // When an error occurs, logs the error message and pauses the engine.
  85. int error_handler(lua_State* L)
  86. {
  87. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  88. if (!lua_istable(L, -1))
  89. {
  90. lua_pop(L, 1);
  91. return 0;
  92. }
  93. lua_getfield(L, -1, "traceback");
  94. if (!lua_isfunction(L, -1))
  95. {
  96. lua_pop(L, 2);
  97. return 0;
  98. }
  99. lua_pushvalue(L, 1); // Pass error message
  100. lua_pushinteger(L, 2);
  101. lua_call(L, 2, 1); // Call debug.traceback
  102. CE_LOGE(lua_tostring(L, -1)); // Print error message
  103. lua_pop(L, 1); // Remove error message from stack
  104. lua_pop(L, 1); // Remove debug.traceback from stack
  105. device()->pause();
  106. return 0;
  107. }
  108. // Redirects require to the resource manager.
  109. static int require(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. const char* filename = stack.get_string(1);
  113. const ResourceId lua_res("lua", filename);
  114. device()->resource_manager()->load(lua_res);
  115. device()->resource_manager()->flush();
  116. const LuaResource* lr = (LuaResource*) device()->resource_manager()->get(lua_res);
  117. luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
  118. device()->resource_manager()->unload(lua_res);
  119. return 1;
  120. }
  121. //-----------------------------------------------------------------------------
  122. static int lightuserdata_add(lua_State* L)
  123. {
  124. LuaStack stack(L);
  125. if (lua_system::is_vector3(1))
  126. {
  127. const Vector3& a = stack.get_vector3(1);
  128. const Vector3& b = stack.get_vector3(2);
  129. stack.push_vector3(a + b);
  130. return 1;
  131. }
  132. const Vector2& a = stack.get_vector2(1);
  133. const Vector2& b = stack.get_vector2(2);
  134. stack.push_vector2(a + b);
  135. return 1;
  136. }
  137. //-----------------------------------------------------------------------------
  138. static int lightuserdata_sub(lua_State* L)
  139. {
  140. LuaStack stack(L);
  141. if (lua_system::is_vector3(1))
  142. {
  143. const Vector3& a = stack.get_vector3(1);
  144. const Vector3& b = stack.get_vector3(2);
  145. stack.push_vector3(a - b);
  146. return 1;
  147. }
  148. const Vector2& a = stack.get_vector2(1);
  149. const Vector2& b = stack.get_vector2(2);
  150. stack.push_vector2(a - b);
  151. return 1;
  152. }
  153. //-----------------------------------------------------------------------------
  154. static int lightuserdata_mul(lua_State* L)
  155. {
  156. LuaStack stack(L);
  157. if (lua_system::is_vector3(1))
  158. {
  159. const Vector3& a = stack.get_vector3(1);
  160. const float b = stack.get_float(2);
  161. stack.push_vector3(a * b);
  162. return 1;
  163. }
  164. const Vector2& a = stack.get_vector2(1);
  165. const float b = stack.get_float(2);
  166. stack.push_vector2(a * b);
  167. return 1;
  168. }
  169. //-----------------------------------------------------------------------------
  170. static int lightuserdata_div(lua_State* L)
  171. {
  172. LuaStack stack(L);
  173. if (lua_system::is_vector3(1))
  174. {
  175. const Vector3& a = stack.get_vector3(1);
  176. const float b = stack.get_float(2);
  177. stack.push_vector3(a / b);
  178. return 1;
  179. }
  180. const Vector2& a = stack.get_vector2(1);
  181. const float b = stack.get_float(2);
  182. stack.push_vector2(a / b);
  183. return 1;
  184. }
  185. //-----------------------------------------------------------------------------
  186. static int lightuserdata_unm(lua_State* L)
  187. {
  188. LuaStack stack(L);
  189. if (lua_system::is_vector3(1))
  190. {
  191. stack.push_vector3(-stack.get_vector3(1));
  192. return 1;
  193. }
  194. stack.push_vector2(-stack.get_vector2(1));
  195. return 1;
  196. }
  197. //-----------------------------------------------------------------------------
  198. static int lightuserdata_index(lua_State* L)
  199. {
  200. LuaStack stack(L);
  201. if (lua_system::is_vector3(1))
  202. {
  203. Vector3& v = stack.get_vector3(1);
  204. const char* s = stack.get_string(2);
  205. if (string::strcmp(s, "x") == 0)
  206. {
  207. stack.push_float(v.x);
  208. return 1;
  209. }
  210. else if (string::strcmp(s, "y") == 0)
  211. {
  212. stack.push_float(v.y);
  213. return 1;
  214. }
  215. else if (string::strcmp(s, "z") == 0)
  216. {
  217. stack.push_float(v.z);
  218. return 1;
  219. }
  220. }
  221. else if (lua_system::is_vector2(1))
  222. {
  223. Vector2& v = stack.get_vector2(1);
  224. const char* s = stack.get_string(2);
  225. if (string::strcmp(s, "x") == 0)
  226. {
  227. stack.push_float(v.x);
  228. return 1;
  229. }
  230. else if (string::strcmp(s, "y") == 0)
  231. {
  232. stack.push_float(v.y);
  233. return 1;
  234. }
  235. }
  236. return 0;
  237. }
  238. //-----------------------------------------------------------------------------
  239. static int lightuserdata_newindex(lua_State* L)
  240. {
  241. LuaStack stack(L);
  242. if (lua_system::is_vector3(1))
  243. {
  244. Vector3& v = stack.get_vector3(1);
  245. const char* s = stack.get_string(2);
  246. const float value = stack.get_float(3);
  247. if (string::strcmp(s, "x") == 0) v.x = value;
  248. else if (string::strcmp(s, "y") == 0) v.y = value;
  249. else if (string::strcmp(s, "z") == 0) v.z = value;
  250. }
  251. else if (lua_system::is_vector2(1))
  252. {
  253. Vector2& v = stack.get_vector2(1);
  254. const char* s = stack.get_string(2);
  255. const float value = stack.get_float(3);
  256. if (string::strcmp(s, "x") == 0) v.x = value;
  257. else if (string::strcmp(s, "y") == 0) v.y = value;
  258. }
  259. return 0;
  260. }
  261. // Initializes lua subsystem
  262. void init()
  263. {
  264. s_L = luaL_newstate();
  265. CE_ASSERT(s_L, "Unable to create lua state");
  266. // Open default libraries
  267. luaL_openlibs(s_L);
  268. // Register crown libraries
  269. LuaEnvironment env(s_L);
  270. load_actor(env);
  271. load_camera(env);
  272. load_controller(env);
  273. load_debug_line(env);
  274. load_device(env);
  275. load_float_setting(env);
  276. load_gui(env);
  277. load_int_setting(env);
  278. load_keyboard(env);
  279. load_math(env);
  280. load_matrix4x4(env);
  281. load_mesh(env);
  282. load_mouse(env);
  283. load_physics_world(env);
  284. load_quaternion(env);
  285. load_quaternionbox(env);
  286. load_raycast(env);
  287. load_resource_package(env);
  288. load_sound_world(env);
  289. load_sprite(env);
  290. load_string_setting(env);
  291. load_touch(env);
  292. load_unit(env);
  293. load_vector2(env);
  294. load_vector2box(env);
  295. load_vector3(env);
  296. load_vector3box(env);
  297. load_window(env);
  298. load_world(env);
  299. load_color4(env);
  300. load_material(env);
  301. // Register custom loader
  302. lua_getfield(s_L, LUA_GLOBALSINDEX, "package");
  303. lua_getfield(s_L, -1, "loaders");
  304. lua_remove(s_L, -2);
  305. int num_loaders = 0;
  306. lua_pushnil(s_L);
  307. while (lua_next(s_L, -2) != 0)
  308. {
  309. lua_pop(s_L, 1);
  310. num_loaders++;
  311. }
  312. lua_pushinteger(s_L, num_loaders + 1);
  313. lua_pushcfunction(s_L, require);
  314. lua_rawset(s_L, -3);
  315. lua_pop(s_L, 1);
  316. // Create metatable for lightuserdata
  317. luaL_newmetatable(s_L, "Lightuserdata_mt");
  318. lua_pushstring(s_L, "__add");
  319. lua_pushcfunction(s_L, lightuserdata_add);
  320. lua_settable(s_L, 1);
  321. lua_pushstring(s_L, "__sub");
  322. lua_pushcfunction(s_L, lightuserdata_sub);
  323. lua_settable(s_L, 1);
  324. lua_pushstring(s_L, "__mul");
  325. lua_pushcfunction(s_L, lightuserdata_mul);
  326. lua_settable(s_L, 1);
  327. lua_pushstring(s_L, "__div");
  328. lua_pushcfunction(s_L, lightuserdata_div);
  329. lua_settable(s_L, 1);
  330. lua_pushstring(s_L, "__unm");
  331. lua_pushcfunction(s_L, lightuserdata_unm);
  332. lua_settable(s_L, 1);
  333. lua_pushstring(s_L, "__index");
  334. lua_pushcfunction(s_L, lightuserdata_index);
  335. lua_settable(s_L, 1);
  336. lua_pushstring(s_L, "__newindex");
  337. lua_pushcfunction(s_L, lightuserdata_newindex);
  338. lua_settable(s_L, 1);
  339. lua_pop(s_L, 1); // Pop Lightuserdata_mt
  340. // Ensure stack is clean
  341. CE_ASSERT(lua_gettop(s_L) == 0, "Stack not clean");
  342. }
  343. //-----------------------------------------------------------------------------
  344. void shutdown()
  345. {
  346. lua_close(s_L);
  347. }
  348. //-----------------------------------------------------------------------------
  349. lua_State* state()
  350. {
  351. return s_L;
  352. }
  353. //-----------------------------------------------------------------------------
  354. Vector2* next_vector2(const Vector2& v)
  355. {
  356. CE_ASSERT(s_vec2_used < CE_MAX_LUA_VECTOR2, "Maximum number of Vector2 reached");
  357. return &(s_vec2_buffer[s_vec2_used++] = v);
  358. }
  359. //-----------------------------------------------------------------------------
  360. Vector3* next_vector3(const Vector3& v)
  361. {
  362. CE_ASSERT(s_vec3_used < CE_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  363. return &(s_vec3_buffer[s_vec3_used++] = v);
  364. }
  365. //-----------------------------------------------------------------------------
  366. Matrix4x4* next_matrix4x4(const Matrix4x4& m)
  367. {
  368. CE_ASSERT(s_mat4_used < CE_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  369. return &(s_mat4_buffer[s_mat4_used++] = m);
  370. }
  371. //-----------------------------------------------------------------------------
  372. Quaternion* next_quaternion(const Quaternion& q)
  373. {
  374. CE_ASSERT(s_quat_used < CE_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  375. return &(s_quat_buffer[s_quat_used++] = q);
  376. }
  377. //-----------------------------------------------------------------------------
  378. bool is_vector2(int32_t index)
  379. {
  380. void* type = lua_touserdata(s_L, index);
  381. return (type >= &s_vec2_buffer[0] && type <= &s_vec2_buffer[CE_MAX_LUA_VECTOR2 - 1]);
  382. }
  383. //-----------------------------------------------------------------------------
  384. bool is_vector3(int32_t index)
  385. {
  386. void* type = lua_touserdata(s_L, index);
  387. return (type >= &s_vec3_buffer[0] && type <= &s_vec3_buffer[CE_MAX_LUA_VECTOR3 - 1]);
  388. }
  389. //-----------------------------------------------------------------------------
  390. bool is_matrix4x4(int32_t index)
  391. {
  392. void* type = lua_touserdata(s_L, index);
  393. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CE_MAX_LUA_MATRIX4X4 - 1]);
  394. }
  395. //-----------------------------------------------------------------------------
  396. bool is_quaternion(int32_t index)
  397. {
  398. void* type = lua_touserdata(s_L, index);
  399. return (type >= &s_quat_buffer[0] && type <= &s_quat_buffer[CE_MAX_LUA_QUATERNION - 1]);
  400. }
  401. //-----------------------------------------------------------------------------
  402. void clear_temporaries()
  403. {
  404. s_vec2_used = 0;
  405. s_vec3_used = 0;
  406. s_mat4_used = 0;
  407. s_quat_used = 0;
  408. }
  409. } // namespace lua_system
  410. } // namespace crown