lua_environment.cpp 11 KB

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