lua_system.cpp 7.9 KB

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