lua_environment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. /*
  2. * Copyright (c) 2012-2015 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "error.h"
  7. #include "lua_environment.h"
  8. #include "lua_stack.h"
  9. #include "lua_resource.h"
  10. #include "device.h"
  11. #include "lua_assert.h"
  12. #include "resource_manager.h"
  13. #include "log.h"
  14. #include <stdarg.h>
  15. namespace crown
  16. {
  17. // Lua modules
  18. extern void load_actor(LuaEnvironment& env);
  19. extern void load_camera(LuaEnvironment& env);
  20. extern void load_controller(LuaEnvironment& env);
  21. extern void load_debug_line(LuaEnvironment& env);
  22. extern void load_device(LuaEnvironment& env);
  23. extern void load_float_setting(LuaEnvironment& env);
  24. extern void load_gui(LuaEnvironment& env);
  25. extern void load_int_setting(LuaEnvironment& env);
  26. extern void load_keyboard(LuaEnvironment& env);
  27. extern void load_math(LuaEnvironment& env);
  28. extern void load_matrix4x4(LuaEnvironment& env);
  29. extern void load_mouse(LuaEnvironment& env);
  30. extern void load_physics_world(LuaEnvironment& env);
  31. extern void load_quaternion(LuaEnvironment& env);
  32. extern void load_raycast(LuaEnvironment& env);
  33. extern void load_resource_package(LuaEnvironment& env);
  34. extern void load_sound_world(LuaEnvironment& env);
  35. extern void load_sprite(LuaEnvironment& env);
  36. extern void load_string_setting(LuaEnvironment& env);
  37. extern void load_touch(LuaEnvironment& env);
  38. extern void load_unit(LuaEnvironment& env);
  39. extern void load_vector3(LuaEnvironment& env);
  40. extern void load_window(LuaEnvironment& env);
  41. extern void load_world(LuaEnvironment& env);
  42. extern void load_color4(LuaEnvironment& env);
  43. extern void load_material(LuaEnvironment& env);
  44. // When an error occurs, logs the error message and pauses the engine.
  45. static int error_handler(lua_State* L)
  46. {
  47. lua_getfield(L, LUA_GLOBALSINDEX, "debug");
  48. if (!lua_istable(L, -1))
  49. {
  50. lua_pop(L, 1);
  51. return 0;
  52. }
  53. lua_getfield(L, -1, "traceback");
  54. if (!lua_isfunction(L, -1))
  55. {
  56. lua_pop(L, 2);
  57. return 0;
  58. }
  59. lua_pushvalue(L, 1); // Pass error message
  60. lua_pushinteger(L, 2);
  61. lua_call(L, 2, 1); // Call debug.traceback
  62. CE_LOGE(lua_tostring(L, -1)); // Print error message
  63. lua_pop(L, 1); // Remove error message from stack
  64. lua_pop(L, 1); // Remove debug.traceback from stack
  65. device()->pause();
  66. return 0;
  67. }
  68. // Redirects require to the resource manager.
  69. static int require(lua_State* L)
  70. {
  71. using namespace lua_resource;
  72. LuaStack stack(L);
  73. const LuaResource* lr = (LuaResource*)device()->resource_manager()->get(LUA_TYPE, stack.get_resource_id(1));
  74. luaL_loadbuffer(L, program(lr), size(lr), "");
  75. return 1;
  76. }
  77. static int lightuserdata_add(lua_State* L)
  78. {
  79. LuaStack stack(L);
  80. const Vector3& a = stack.get_vector3(1);
  81. const Vector3& b = stack.get_vector3(2);
  82. stack.push_vector3(a + b);
  83. return 1;
  84. }
  85. static int lightuserdata_sub(lua_State* L)
  86. {
  87. LuaStack stack(L);
  88. const Vector3& a = stack.get_vector3(1);
  89. const Vector3& b = stack.get_vector3(2);
  90. stack.push_vector3(a - b);
  91. return 1;
  92. }
  93. static int lightuserdata_mul(lua_State* L)
  94. {
  95. LuaStack stack(L);
  96. const Vector3& a = stack.get_vector3(1);
  97. const float b = stack.get_float(2);
  98. stack.push_vector3(a * b);
  99. return 1;
  100. }
  101. static int lightuserdata_div(lua_State* L)
  102. {
  103. LuaStack stack(L);
  104. const Vector3& a = stack.get_vector3(1);
  105. const float b = stack.get_float(2);
  106. stack.push_vector3(a / b);
  107. return 1;
  108. }
  109. static int lightuserdata_unm(lua_State* L)
  110. {
  111. LuaStack stack(L);
  112. stack.push_vector3(-stack.get_vector3(1));
  113. return 1;
  114. }
  115. static int lightuserdata_index(lua_State* L)
  116. {
  117. LuaStack stack(L);
  118. Vector3& v = stack.get_vector3(1);
  119. const char* s = stack.get_string(2);
  120. switch (s[0])
  121. {
  122. case 'x': stack.push_float(v.x); return 1;
  123. case 'y': stack.push_float(v.y); return 1;
  124. case 'z': stack.push_float(v.z); return 1;
  125. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  126. }
  127. return 0;
  128. }
  129. static int lightuserdata_newindex(lua_State* L)
  130. {
  131. LuaStack stack(L);
  132. Vector3& v = stack.get_vector3(1);
  133. const char* s = stack.get_string(2);
  134. const float value = stack.get_float(3);
  135. switch (s[0])
  136. {
  137. case 'x': v.x = value; break;
  138. case 'y': v.y = value; break;
  139. case 'z': v.z = value; break;
  140. default: LUA_ASSERT(false, stack, "Bad index: '%c'", s[0]); break;
  141. }
  142. return 0;
  143. }
  144. LuaEnvironment::LuaEnvironment()
  145. : L(NULL)
  146. , _vec3_used(0)
  147. , _quat_used(0)
  148. , _mat4_used(0)
  149. {
  150. L = luaL_newstate();
  151. CE_ASSERT(L, "Unable to create lua state");
  152. }
  153. LuaEnvironment::~LuaEnvironment()
  154. {
  155. lua_close(L);
  156. }
  157. void LuaEnvironment::load_libs()
  158. {
  159. // Open default libraries
  160. luaL_openlibs(L);
  161. // Register crown libraries
  162. load_actor(*this);
  163. load_camera(*this);
  164. load_controller(*this);
  165. load_debug_line(*this);
  166. load_device(*this);
  167. load_float_setting(*this);
  168. load_gui(*this);
  169. load_int_setting(*this);
  170. load_keyboard(*this);
  171. load_math(*this);
  172. load_matrix4x4(*this);
  173. load_mouse(*this);
  174. load_physics_world(*this);
  175. load_quaternion(*this);
  176. load_raycast(*this);
  177. load_resource_package(*this);
  178. load_sound_world(*this);
  179. load_sprite(*this);
  180. load_string_setting(*this);
  181. load_touch(*this);
  182. load_unit(*this);
  183. load_vector3(*this);
  184. load_window(*this);
  185. load_world(*this);
  186. load_color4(*this);
  187. load_material(*this);
  188. // Register custom loader
  189. lua_getfield(L, LUA_GLOBALSINDEX, "package");
  190. lua_getfield(L, -1, "loaders");
  191. lua_remove(L, -2);
  192. int num_loaders = 0;
  193. lua_pushnil(L);
  194. while (lua_next(L, -2) != 0)
  195. {
  196. lua_pop(L, 1);
  197. num_loaders++;
  198. }
  199. lua_pushinteger(L, num_loaders + 1);
  200. lua_pushcfunction(L, require);
  201. lua_rawset(L, -3);
  202. lua_pop(L, 1);
  203. // Create metatable for lightuserdata
  204. luaL_newmetatable(L, "Lightuserdata_mt");
  205. lua_pushstring(L, "__add");
  206. lua_pushcfunction(L, lightuserdata_add);
  207. lua_settable(L, 1);
  208. lua_pushstring(L, "__sub");
  209. lua_pushcfunction(L, lightuserdata_sub);
  210. lua_settable(L, 1);
  211. lua_pushstring(L, "__mul");
  212. lua_pushcfunction(L, lightuserdata_mul);
  213. lua_settable(L, 1);
  214. lua_pushstring(L, "__div");
  215. lua_pushcfunction(L, lightuserdata_div);
  216. lua_settable(L, 1);
  217. lua_pushstring(L, "__unm");
  218. lua_pushcfunction(L, lightuserdata_unm);
  219. lua_settable(L, 1);
  220. lua_pushstring(L, "__index");
  221. lua_pushcfunction(L, lightuserdata_index);
  222. lua_settable(L, 1);
  223. lua_pushstring(L, "__newindex");
  224. lua_pushcfunction(L, lightuserdata_newindex);
  225. lua_settable(L, 1);
  226. lua_pop(L, 1); // Pop Lightuserdata_mt
  227. // Ensure stack is clean
  228. CE_ASSERT(lua_gettop(L) == 0, "Stack not clean");
  229. }
  230. void LuaEnvironment::execute(const LuaResource* lr)
  231. {
  232. using namespace lua_resource;
  233. lua_pushcfunction(L, error_handler);
  234. luaL_loadbuffer(L, program(lr), size(lr), "<unknown>");
  235. lua_pcall(L, 0, 0, -2);
  236. lua_pop(L, 1);
  237. }
  238. void LuaEnvironment::execute_string(const char* s)
  239. {
  240. lua_pushcfunction(L, error_handler);
  241. luaL_loadstring(L, s);
  242. lua_pcall(L, 0, 0, -2);
  243. lua_pop(L, 1);
  244. }
  245. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  246. {
  247. luaL_newmetatable(L, module);
  248. luaL_Reg entry[2];
  249. entry[0].name = name;
  250. entry[0].func = func;
  251. entry[1].name = NULL;
  252. entry[1].func = NULL;
  253. luaL_register(L, NULL, entry);
  254. lua_setglobal(L, module);
  255. lua_pop(L, -1);
  256. }
  257. void LuaEnvironment::load_module_function(const char* module, const char* name, const char* value)
  258. {
  259. luaL_newmetatable(L, module);
  260. lua_getglobal(L, value);
  261. lua_setfield(L, -2, name);
  262. lua_setglobal(L, module);
  263. }
  264. void LuaEnvironment::load_module_constructor(const char* module, const lua_CFunction func)
  265. {
  266. // Create dummy tables to be used as module's metatable
  267. lua_createtable(L, 0, 1);
  268. lua_pushstring(L, "__call");
  269. lua_pushcfunction(L, func);
  270. lua_settable(L, 1); // dummy.__call = func
  271. lua_getglobal(L, module);
  272. lua_pushvalue(L, -2); // Duplicate dummy metatable
  273. lua_setmetatable(L, -2); // setmetatable(module, dummy)
  274. lua_pop(L, -1);
  275. }
  276. void LuaEnvironment::load_module_enum(const char* module, const char* name, uint32_t value)
  277. {
  278. // Checks table existance
  279. lua_pushstring(L, module);
  280. lua_rawget(L, LUA_GLOBALSINDEX);
  281. if (!lua_istable(L, -1)) // If not exixts
  282. {
  283. // Creates table
  284. lua_newtable(L);
  285. lua_setglobal(L, module);
  286. }
  287. // Adds field to table
  288. lua_getglobal(L, module);
  289. lua_pushinteger(L, value);
  290. lua_setfield(L, -2, name);
  291. lua_pop(L, 2);
  292. }
  293. void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  294. {
  295. CE_ASSERT_NOT_NULL(func);
  296. LuaStack stack(L);
  297. va_list vl;
  298. va_start(vl, argc);
  299. lua_pushcfunction(L, error_handler);
  300. lua_getglobal(L, func);
  301. for (uint8_t i = 0; i < argc; i++)
  302. {
  303. const int type = va_arg(vl, int);
  304. switch (type)
  305. {
  306. case ARGUMENT_FLOAT:
  307. {
  308. stack.push_float(va_arg(vl, double));
  309. break;
  310. }
  311. default:
  312. {
  313. CE_ASSERT(false, "Oops, lua argument unknown");
  314. break;
  315. }
  316. }
  317. }
  318. va_end(vl);
  319. lua_pcall(L, argc, 0, -argc - 2);
  320. lua_pop(L, -1);
  321. }
  322. void LuaEnvironment::call_physics_callback(Actor* actor_0, Actor* actor_1, Unit* unit_0, Unit* unit_1, const Vector3& where, const Vector3& normal, const char* type)
  323. {
  324. LuaStack stack(L);
  325. lua_pushcfunction(L, error_handler);
  326. lua_getglobal(L, "g_physics_callback");
  327. stack.push_table();
  328. stack.push_key_begin("actor_0"); (actor_0 ? stack.push_actor(actor_0) : stack.push_nil()); stack.push_key_end();
  329. stack.push_key_begin("actor_1"); (actor_1 ? stack.push_actor(actor_1) : stack.push_nil()); stack.push_key_end();
  330. stack.push_key_begin("unit_0"); (unit_0 ? stack.push_unit(unit_0) : stack.push_nil()); stack.push_key_end();
  331. stack.push_key_begin("unit_1"); (unit_1 ? stack.push_unit(unit_1) : stack.push_nil()); stack.push_key_end();
  332. stack.push_key_begin("where"); stack.push_vector3(where); stack.push_key_end();
  333. stack.push_key_begin("normal"); stack.push_vector3(normal); stack.push_key_end();
  334. stack.push_key_begin("type"); stack.push_string(type); stack.push_key_end();
  335. lua_pcall(L, 1, 0, -3);
  336. lua_pop(L, -1);
  337. }
  338. void LuaEnvironment::call_trigger_callback(Actor* trigger, Actor* other, const char* type)
  339. {
  340. LuaStack stack(L);
  341. lua_pushcfunction(L, error_handler);
  342. lua_getglobal(L, "g_trigger_callback");
  343. stack.push_table();
  344. stack.push_key_begin("trigger"); (trigger ? stack.push_actor(trigger) : stack.push_nil()); stack.push_key_end();
  345. stack.push_key_begin("other"); (other ? stack.push_actor(other) : stack.push_nil()); stack.push_key_end();
  346. stack.push_key_begin("type"); stack.push_string(type); stack.push_key_end();
  347. lua_pcall(L, 1, 0, -3);
  348. lua_pop(L, -1);
  349. }
  350. Vector3* LuaEnvironment::next_vector3(const Vector3& v)
  351. {
  352. CE_ASSERT(_vec3_used < CROWN_MAX_LUA_VECTOR3, "Maximum number of Vector3 reached");
  353. return &(_vec3_buffer[_vec3_used++] = v);
  354. }
  355. Matrix4x4* LuaEnvironment::next_matrix4x4(const Matrix4x4& m)
  356. {
  357. CE_ASSERT(_mat4_used < CROWN_MAX_LUA_MATRIX4X4, "Maximum number of Matrix4x4 reached");
  358. return &(s_mat4_buffer[_mat4_used++] = m);
  359. }
  360. Quaternion* LuaEnvironment::next_quaternion(const Quaternion& q)
  361. {
  362. CE_ASSERT(_quat_used < CROWN_MAX_LUA_QUATERNION, "Maximum number of Quaternion reached");
  363. return &(_quat_buffer[_quat_used++] = q);
  364. }
  365. bool LuaEnvironment::is_vector3(int index)
  366. {
  367. void* type = lua_touserdata(L, index);
  368. return (type >= &_vec3_buffer[0] && type <= &_vec3_buffer[CROWN_MAX_LUA_VECTOR3 - 1]);
  369. }
  370. bool LuaEnvironment::is_matrix4x4(int index)
  371. {
  372. void* type = lua_touserdata(L, index);
  373. return (type >= &s_mat4_buffer[0] && type <= &s_mat4_buffer[CROWN_MAX_LUA_MATRIX4X4 - 1]);
  374. }
  375. bool LuaEnvironment::is_quaternion(int index)
  376. {
  377. void* type = lua_touserdata(L, index);
  378. return (type >= &_quat_buffer[0] && type <= &_quat_buffer[CROWN_MAX_LUA_QUATERNION - 1]);
  379. }
  380. void LuaEnvironment::clear_temporaries()
  381. {
  382. _vec3_used = 0;
  383. _mat4_used = 0;
  384. _quat_used = 0;
  385. }
  386. } // namespace crown