lua_system.cpp 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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_mouse(LuaEnvironment& env);
  54. extern void load_physics_world(LuaEnvironment& env);
  55. extern void load_quaternion(LuaEnvironment& env);
  56. extern void load_quaternionbox(LuaEnvironment& env);
  57. extern void load_raycast(LuaEnvironment& env);
  58. extern void load_resource_package(LuaEnvironment& env);
  59. extern void load_sound_world(LuaEnvironment& env);
  60. extern void load_sprite(LuaEnvironment& env);
  61. extern void load_string_setting(LuaEnvironment& env);
  62. extern void load_touch(LuaEnvironment& env);
  63. extern void load_unit(LuaEnvironment& env);
  64. extern void load_vector3(LuaEnvironment& env);
  65. extern void load_vector3box(LuaEnvironment& env);
  66. extern void load_window(LuaEnvironment& env);
  67. extern void load_world(LuaEnvironment& env);
  68. extern void load_color4(LuaEnvironment& env);
  69. extern void load_material(LuaEnvironment& env);
  70. namespace lua_globals
  71. {
  72. static lua_State* _L;
  73. static uint32_t _vec3_used = 0;
  74. static Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
  75. static uint32_t _mat4_used = 0;
  76. static Matrix4x4 s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
  77. static uint32_t _quat_used = 0;
  78. static Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
  79. // When an error occurs, logs the error message and pauses the engine.
  80. int error_handler(lua_State* L)
  81. {
  82. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  83. if (!lua_istable(L, -1))
  84. {
  85. lua_pop(L, 1);
  86. return 0;
  87. }
  88. lua_getfield(L, -1, "traceback");
  89. if (!lua_isfunction(L, -1))
  90. {
  91. lua_pop(L, 2);
  92. return 0;
  93. }
  94. lua_pushvalue(L, 1); // Pass error message
  95. lua_pushinteger(L, 2);
  96. lua_call(L, 2, 1); // Call debug.traceback
  97. CE_LOGE(lua_tostring(L, -1)); // Print error message
  98. lua_pop(L, 1); // Remove error message from stack
  99. lua_pop(L, 1); // Remove debug.traceback from stack
  100. device()->pause();
  101. return 0;
  102. }
  103. // Redirects require to the resource manager.
  104. static int require(lua_State* L)
  105. {
  106. using namespace lua_resource;
  107. LuaStack stack(L);
  108. const LuaResource* lr = (LuaResource*)device()->resource_manager()->get("lua", stack.get_string(1));
  109. luaL_loadbuffer(L, program(lr), size(lr), "");
  110. return 1;
  111. }
  112. static int lightuserdata_add(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. const Vector3& a = stack.get_vector3(1);
  116. const Vector3& b = stack.get_vector3(2);
  117. stack.push_vector3(a + b);
  118. return 1;
  119. }
  120. static int lightuserdata_sub(lua_State* L)
  121. {
  122. LuaStack stack(L);
  123. const Vector3& a = stack.get_vector3(1);
  124. const Vector3& b = stack.get_vector3(2);
  125. stack.push_vector3(a - b);
  126. return 1;
  127. }
  128. static int lightuserdata_mul(lua_State* L)
  129. {
  130. LuaStack stack(L);
  131. const Vector3& a = stack.get_vector3(1);
  132. const float b = stack.get_float(2);
  133. stack.push_vector3(a * b);
  134. return 1;
  135. }
  136. static int lightuserdata_div(lua_State* L)
  137. {
  138. LuaStack stack(L);
  139. const Vector3& a = stack.get_vector3(1);
  140. const float b = stack.get_float(2);
  141. stack.push_vector3(a / b);
  142. return 1;
  143. }
  144. static int lightuserdata_unm(lua_State* L)
  145. {
  146. LuaStack stack(L);
  147. stack.push_vector3(-stack.get_vector3(1));
  148. return 1;
  149. }
  150. static int lightuserdata_index(lua_State* L)
  151. {
  152. LuaStack stack(L);
  153. Vector3& v = stack.get_vector3(1);
  154. const char* s = stack.get_string(2);
  155. if (strcmp(s, "x") == 0)
  156. {
  157. stack.push_float(v.x);
  158. return 1;
  159. }
  160. else if (strcmp(s, "y") == 0)
  161. {
  162. stack.push_float(v.y);
  163. return 1;
  164. }
  165. else if (strcmp(s, "z") == 0)
  166. {
  167. stack.push_float(v.z);
  168. return 1;
  169. }
  170. return 0;
  171. }
  172. static int lightuserdata_newindex(lua_State* L)
  173. {
  174. LuaStack stack(L);
  175. Vector3& v = stack.get_vector3(1);
  176. const char* s = stack.get_string(2);
  177. const float value = stack.get_float(3);
  178. if (strcmp(s, "x") == 0) v.x = value;
  179. else if (strcmp(s, "y") == 0) v.y = value;
  180. else if (strcmp(s, "z") == 0) v.z = value;
  181. return 0;
  182. }
  183. // Initializes lua subsystem
  184. void init()
  185. {
  186. _L = luaL_newstate();
  187. CE_ASSERT(_L, "Unable to create lua state");
  188. // Open default libraries
  189. luaL_openlibs(_L);
  190. // Register crown libraries
  191. LuaEnvironment env(_L);
  192. load_actor(env);
  193. load_camera(env);
  194. load_controller(env);
  195. load_debug_line(env);
  196. load_device(env);
  197. load_float_setting(env);
  198. load_gui(env);
  199. load_int_setting(env);
  200. load_keyboard(env);
  201. load_math(env);
  202. load_matrix4x4(env);
  203. load_mouse(env);
  204. load_physics_world(env);
  205. load_quaternion(env);
  206. load_quaternionbox(env);
  207. load_raycast(env);
  208. load_resource_package(env);
  209. load_sound_world(env);
  210. load_sprite(env);
  211. load_string_setting(env);
  212. load_touch(env);
  213. load_unit(env);
  214. load_vector3(env);
  215. load_vector3box(env);
  216. load_window(env);
  217. load_world(env);
  218. load_color4(env);
  219. load_material(env);
  220. // Register custom loader
  221. lua_getfield(_L, LUA_GLOBALSINDEX, "package");
  222. lua_getfield(_L, -1, "loaders");
  223. lua_remove(_L, -2);
  224. int num_loaders = 0;
  225. lua_pushnil(_L);
  226. while (lua_next(_L, -2) != 0)
  227. {
  228. lua_pop(_L, 1);
  229. num_loaders++;
  230. }
  231. lua_pushinteger(_L, num_loaders + 1);
  232. lua_pushcfunction(_L, require);
  233. lua_rawset(_L, -3);
  234. lua_pop(_L, 1);
  235. // Create metatable for lightuserdata
  236. luaL_newmetatable(_L, "Lightuserdata_mt");
  237. lua_pushstring(_L, "__add");
  238. lua_pushcfunction(_L, lightuserdata_add);
  239. lua_settable(_L, 1);
  240. lua_pushstring(_L, "__sub");
  241. lua_pushcfunction(_L, lightuserdata_sub);
  242. lua_settable(_L, 1);
  243. lua_pushstring(_L, "__mul");
  244. lua_pushcfunction(_L, lightuserdata_mul);
  245. lua_settable(_L, 1);
  246. lua_pushstring(_L, "__div");
  247. lua_pushcfunction(_L, lightuserdata_div);
  248. lua_settable(_L, 1);
  249. lua_pushstring(_L, "__unm");
  250. lua_pushcfunction(_L, lightuserdata_unm);
  251. lua_settable(_L, 1);
  252. lua_pushstring(_L, "__index");
  253. lua_pushcfunction(_L, lightuserdata_index);
  254. lua_settable(_L, 1);
  255. lua_pushstring(_L, "__newindex");
  256. lua_pushcfunction(_L, lightuserdata_newindex);
  257. lua_settable(_L, 1);
  258. lua_pop(_L, 1); // Pop Lightuserdata_mt
  259. // Ensure stack is clean
  260. CE_ASSERT(lua_gettop(_L) == 0, "Stack not clean");
  261. }
  262. void shutdown()
  263. {
  264. lua_close(_L);
  265. }
  266. lua_State* state()
  267. {
  268. return _L;
  269. }
  270. Vector3* next_vector3(const Vector3& v)
  271. {
  272. CE_ASSERT(_vec3_used < CROWN_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  273. return &(_vec3_buffer[_vec3_used++] = v);
  274. }
  275. Matrix4x4* next_matrix4x4(const Matrix4x4& m)
  276. {
  277. CE_ASSERT(_mat4_used < CROWN_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  278. return &(s_mat4_buffer[_mat4_used++] = m);
  279. }
  280. Quaternion* next_quaternion(const Quaternion& q)
  281. {
  282. CE_ASSERT(_quat_used < CROWN_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  283. return &(_quat_buffer[_quat_used++] = q);
  284. }
  285. bool is_vector3(int32_t index)
  286. {
  287. void* type = lua_touserdata(_L, index);
  288. return (type >= &_vec3_buffer[0] && type <= &_vec3_buffer[CROWN_MAX_LUA_VECTOR3 - 1]);
  289. }
  290. bool is_matrix4x4(int32_t index)
  291. {
  292. void* type = lua_touserdata(_L, index);
  293. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4 - 1]);
  294. }
  295. bool is_quaternion(int32_t index)
  296. {
  297. void* type = lua_touserdata(_L, index);
  298. return (type >= &_quat_buffer[0] && type <= &_quat_buffer[CROWN_MAX_LUA_QUATERNION - 1]);
  299. }
  300. void clear_temporaries()
  301. {
  302. _vec3_used = 0;
  303. _mat4_used = 0;
  304. _quat_used = 0;
  305. }
  306. } // namespace lua_globals
  307. } // namespace crown