2
0

lua_system.cpp 8.1 KB

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