lua_system.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "lua_system.h"
  6. #include "config.h"
  7. #include "lua_stack.h"
  8. #include "lua_assert.h"
  9. #include "assert.h"
  10. #include "vector2.h"
  11. #include "vector3.h"
  12. #include "matrix4x4.h"
  13. #include "quaternion.h"
  14. #include "device.h"
  15. #include "resource_manager.h"
  16. #include "lua_resource.h"
  17. #include "lua_environment.h"
  18. #include "lua_stack.h"
  19. #include "log.h"
  20. #include "lua.hpp"
  21. namespace crown
  22. {
  23. // Lua modules
  24. extern void load_actor(LuaEnvironment& env);
  25. extern void load_camera(LuaEnvironment& env);
  26. extern void load_controller(LuaEnvironment& env);
  27. extern void load_debug_line(LuaEnvironment& env);
  28. extern void load_device(LuaEnvironment& env);
  29. extern void load_float_setting(LuaEnvironment& env);
  30. extern void load_gui(LuaEnvironment& env);
  31. extern void load_int_setting(LuaEnvironment& env);
  32. extern void load_keyboard(LuaEnvironment& env);
  33. extern void load_math(LuaEnvironment& env);
  34. extern void load_matrix4x4(LuaEnvironment& env);
  35. extern void load_matrix4x4box(LuaEnvironment& env);
  36. extern void load_mouse(LuaEnvironment& env);
  37. extern void load_physics_world(LuaEnvironment& env);
  38. extern void load_quaternion(LuaEnvironment& env);
  39. extern void load_quaternionbox(LuaEnvironment& env);
  40. extern void load_raycast(LuaEnvironment& env);
  41. extern void load_resource_package(LuaEnvironment& env);
  42. extern void load_sound_world(LuaEnvironment& env);
  43. extern void load_sprite(LuaEnvironment& env);
  44. extern void load_string_setting(LuaEnvironment& env);
  45. extern void load_touch(LuaEnvironment& env);
  46. extern void load_unit(LuaEnvironment& env);
  47. extern void load_vector3(LuaEnvironment& env);
  48. extern void load_vector3box(LuaEnvironment& env);
  49. extern void load_window(LuaEnvironment& env);
  50. extern void load_world(LuaEnvironment& env);
  51. extern void load_color4(LuaEnvironment& env);
  52. extern void load_material(LuaEnvironment& env);
  53. namespace lua_globals
  54. {
  55. static lua_State* L;
  56. static uint32_t _vec3_used = 0;
  57. static Vector3 _vec3_buffer[CROWN_MAX_LUA_VECTOR3];
  58. static uint32_t _mat4_used = 0;
  59. static Matrix4x4 s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4];
  60. static uint32_t _quat_used = 0;
  61. static Quaternion _quat_buffer[CROWN_MAX_LUA_QUATERNION];
  62. // When an error occurs, logs the error message and pauses the engine.
  63. int error_handler(lua_State* L)
  64. {
  65. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  66. if (!lua_istable(L, -1))
  67. {
  68. lua_pop(L, 1);
  69. return 0;
  70. }
  71. lua_getfield(L, -1, "traceback");
  72. if (!lua_isfunction(L, -1))
  73. {
  74. lua_pop(L, 2);
  75. return 0;
  76. }
  77. lua_pushvalue(L, 1); // Pass error message
  78. lua_pushinteger(L, 2);
  79. lua_call(L, 2, 1); // Call debug.traceback
  80. CE_LOGE(lua_tostring(L, -1)); // Print error message
  81. lua_pop(L, 1); // Remove error message from stack
  82. lua_pop(L, 1); // Remove debug.traceback from stack
  83. device()->pause();
  84. return 0;
  85. }
  86. // Redirects require to the resource manager.
  87. static int require(lua_State* L)
  88. {
  89. using namespace lua_resource;
  90. LuaStack stack(L);
  91. const LuaResource* lr = (LuaResource*)device()->resource_manager()->get("lua", stack.get_string(1));
  92. luaL_loadbuffer(L, program(lr), size(lr), "");
  93. return 1;
  94. }
  95. static int lightuserdata_add(lua_State* L)
  96. {
  97. LuaStack stack(L);
  98. const Vector3& a = stack.get_vector3(1);
  99. const Vector3& b = stack.get_vector3(2);
  100. stack.push_vector3(a + b);
  101. return 1;
  102. }
  103. static int lightuserdata_sub(lua_State* L)
  104. {
  105. LuaStack stack(L);
  106. const Vector3& a = stack.get_vector3(1);
  107. const Vector3& b = stack.get_vector3(2);
  108. stack.push_vector3(a - b);
  109. return 1;
  110. }
  111. static int lightuserdata_mul(lua_State* L)
  112. {
  113. LuaStack stack(L);
  114. const Vector3& a = stack.get_vector3(1);
  115. const float b = stack.get_float(2);
  116. stack.push_vector3(a * b);
  117. return 1;
  118. }
  119. static int lightuserdata_div(lua_State* L)
  120. {
  121. LuaStack stack(L);
  122. const Vector3& a = stack.get_vector3(1);
  123. const float b = stack.get_float(2);
  124. stack.push_vector3(a / b);
  125. return 1;
  126. }
  127. static int lightuserdata_unm(lua_State* L)
  128. {
  129. LuaStack stack(L);
  130. stack.push_vector3(-stack.get_vector3(1));
  131. return 1;
  132. }
  133. static int lightuserdata_index(lua_State* L)
  134. {
  135. LuaStack stack(L);
  136. Vector3& v = stack.get_vector3(1);
  137. const char* s = stack.get_string(2);
  138. switch (s[0])
  139. {
  140. case 'x': stack.push_float(v.x); return 1;
  141. case 'y': stack.push_float(v.y); return 1;
  142. case 'z': stack.push_float(v.z); return 1;
  143. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  144. }
  145. return 0;
  146. }
  147. static int lightuserdata_newindex(lua_State* L)
  148. {
  149. LuaStack stack(L);
  150. Vector3& v = stack.get_vector3(1);
  151. const char* s = stack.get_string(2);
  152. const float value = stack.get_float(3);
  153. switch (s[0])
  154. {
  155. case 'x': v.x = value; break;
  156. case 'y': v.y = value; break;
  157. case 'z': v.z = value; break;
  158. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  159. }
  160. return 0;
  161. }
  162. // Initializes lua subsystem
  163. void init()
  164. {
  165. L = luaL_newstate();
  166. CE_ASSERT(L, "Unable to create lua state");
  167. // Open default libraries
  168. luaL_openlibs(L);
  169. // Register crown libraries
  170. LuaEnvironment env(L);
  171. load_actor(env);
  172. load_camera(env);
  173. load_controller(env);
  174. load_debug_line(env);
  175. load_device(env);
  176. load_float_setting(env);
  177. load_gui(env);
  178. load_int_setting(env);
  179. load_keyboard(env);
  180. load_math(env);
  181. load_matrix4x4(env);
  182. load_mouse(env);
  183. load_physics_world(env);
  184. load_quaternion(env);
  185. load_quaternionbox(env);
  186. load_raycast(env);
  187. load_resource_package(env);
  188. load_sound_world(env);
  189. load_sprite(env);
  190. load_string_setting(env);
  191. load_touch(env);
  192. load_unit(env);
  193. load_vector3(env);
  194. load_vector3box(env);
  195. load_window(env);
  196. load_world(env);
  197. load_color4(env);
  198. load_material(env);
  199. // Register custom loader
  200. lua_getfield(L, LUA_GLOBALSINDEX, "package");
  201. lua_getfield(L, -1, "loaders");
  202. lua_remove(L, -2);
  203. int num_loaders = 0;
  204. lua_pushnil(L);
  205. while (lua_next(L, -2) != 0)
  206. {
  207. lua_pop(L, 1);
  208. num_loaders++;
  209. }
  210. lua_pushinteger(L, num_loaders + 1);
  211. lua_pushcfunction(L, require);
  212. lua_rawset(L, -3);
  213. lua_pop(L, 1);
  214. // Create metatable for lightuserdata
  215. luaL_newmetatable(L, "Lightuserdata_mt");
  216. lua_pushstring(L, "__add");
  217. lua_pushcfunction(L, lightuserdata_add);
  218. lua_settable(L, 1);
  219. lua_pushstring(L, "__sub");
  220. lua_pushcfunction(L, lightuserdata_sub);
  221. lua_settable(L, 1);
  222. lua_pushstring(L, "__mul");
  223. lua_pushcfunction(L, lightuserdata_mul);
  224. lua_settable(L, 1);
  225. lua_pushstring(L, "__div");
  226. lua_pushcfunction(L, lightuserdata_div);
  227. lua_settable(L, 1);
  228. lua_pushstring(L, "__unm");
  229. lua_pushcfunction(L, lightuserdata_unm);
  230. lua_settable(L, 1);
  231. lua_pushstring(L, "__index");
  232. lua_pushcfunction(L, lightuserdata_index);
  233. lua_settable(L, 1);
  234. lua_pushstring(L, "__newindex");
  235. lua_pushcfunction(L, lightuserdata_newindex);
  236. lua_settable(L, 1);
  237. lua_pop(L, 1); // Pop Lightuserdata_mt
  238. // Ensure stack is clean
  239. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  240. }
  241. void shutdown()
  242. {
  243. lua_close(L);
  244. }
  245. lua_State* state()
  246. {
  247. return L;
  248. }
  249. Vector3* next_vector3(const Vector3& v)
  250. {
  251. CE_ASSERT(_vec3_used < CROWN_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  252. return &(_vec3_buffer[_vec3_used++] = v);
  253. }
  254. Matrix4x4* next_matrix4x4(const Matrix4x4& m)
  255. {
  256. CE_ASSERT(_mat4_used < CROWN_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  257. return &(s_mat4_buffer[_mat4_used++] = m);
  258. }
  259. Quaternion* next_quaternion(const Quaternion& q)
  260. {
  261. CE_ASSERT(_quat_used < CROWN_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  262. return &(_quat_buffer[_quat_used++] = q);
  263. }
  264. bool is_vector3(int32_t index)
  265. {
  266. void* type = lua_touserdata(L, index);
  267. return (type >= &_vec3_buffer[0] && type <= &_vec3_buffer[CROWN_MAX_LUA_VECTOR3 - 1]);
  268. }
  269. bool is_matrix4x4(int32_t index)
  270. {
  271. void* type = lua_touserdata(L, index);
  272. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4 - 1]);
  273. }
  274. bool is_quaternion(int32_t index)
  275. {
  276. void* type = lua_touserdata(L, index);
  277. return (type >= &_quat_buffer[0] && type <= &_quat_buffer[CROWN_MAX_LUA_QUATERNION - 1]);
  278. }
  279. void clear_temporaries()
  280. {
  281. _vec3_used = 0;
  282. _mat4_used = 0;
  283. _quat_used = 0;
  284. }
  285. } // namespace lua_globals
  286. } // namespace crown