LuaEnvironment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. Copyright (c) 2013 Daniele Bartolini, Michele Rossi
  3. Copyright (c) 2012 Daniele Bartolini, Simone Boscaratto
  4. Permission is hereby granted, free of charge, to any person
  5. obtaining a copy of this software and associated documentation
  6. files (the "Software"), to deal in the Software without
  7. restriction, including without limitation the rights to use,
  8. copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the
  10. Software is furnished to do so, subject to the following
  11. conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  16. OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  18. HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  19. WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  20. FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  21. OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <stdarg.h>
  24. #include "LuaEnvironment.h"
  25. #include "Assert.h"
  26. #include "StringUtils.h"
  27. #include "LuaStack.h"
  28. #include "Device.h"
  29. #include "LuaResource.h"
  30. #include "ResourceManager.h"
  31. namespace crown
  32. {
  33. extern int vector2_add(lua_State* L);
  34. extern int vector2_subtract(lua_State* L);
  35. extern int vector2_multiply(lua_State* L);
  36. extern int vector2_divide(lua_State* L);
  37. extern int vector2_negate(lua_State* L);
  38. extern int vector3_add(lua_State* L);
  39. extern int vector3_subtract(lua_State* L);
  40. extern int vector3_multiply(lua_State* L);
  41. extern int vector3_divide(lua_State* L);
  42. extern int vector3_negate(lua_State* L);
  43. extern int vector2(lua_State* L);
  44. extern int vector3(lua_State* L);
  45. extern int matrix4x4(lua_State* L);
  46. extern int quaternion(lua_State* L);
  47. //-----------------------------------------------------------------------------
  48. CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
  49. {
  50. LuaEnvironment* env = device()->lua_environment();
  51. load_int_setting(*env);
  52. load_float_setting(*env);
  53. load_string_setting(*env);
  54. load_vector2(*env);
  55. load_vector3(*env);
  56. load_matrix4x4(*env);
  57. load_quaternion(*env);
  58. load_math(*env);
  59. load_window(*env);
  60. load_mouse(*env);
  61. load_keyboard(*env);
  62. load_accelerometer(*env);
  63. load_device(*env);
  64. load_resource_package(*env);
  65. load_unit(*env);
  66. load_camera(*env);
  67. load_world(*env);
  68. load_mesh(*env);
  69. load_sprite(*env);
  70. load_actor(*env);
  71. load_controller(*env);
  72. load_physics_world(*env);
  73. return 1;
  74. }
  75. //-----------------------------------------------------------------------------
  76. static int crown_lua_vector2_call(lua_State* L)
  77. {
  78. lua_remove(L, 1);
  79. return vector2(L);
  80. }
  81. //-----------------------------------------------------------------------------
  82. static int crown_lua_vector3_call(lua_State* L)
  83. {
  84. lua_remove(L, 1);
  85. return vector3(L);
  86. }
  87. //-----------------------------------------------------------------------------
  88. static int crown_lua_matrix4x4_call(lua_State* L)
  89. {
  90. lua_remove(L, 1);
  91. return matrix4x4(L);
  92. }
  93. //-----------------------------------------------------------------------------
  94. static int crown_lua_quaternion_call(lua_State* L)
  95. {
  96. lua_remove(L, 1);
  97. return quaternion(L);
  98. }
  99. //-----------------------------------------------------------------------------
  100. static int crown_lua_lightuserdata_add(lua_State* L)
  101. {
  102. LuaStack stack(L);
  103. if (stack.is_vector3(1))
  104. {
  105. return vector3_add(L);
  106. }
  107. return vector2_add(L);
  108. }
  109. //-----------------------------------------------------------------------------
  110. static int crown_lua_lightuserdata_sub(lua_State* L)
  111. {
  112. LuaStack stack(L);
  113. if (stack.is_vector3(1))
  114. {
  115. return vector3_subtract(L);
  116. }
  117. return vector2_subtract(L);
  118. }
  119. //-----------------------------------------------------------------------------
  120. static int crown_lua_lightuserdata_mul(lua_State* L)
  121. {
  122. LuaStack stack(L);
  123. if (stack.is_vector3(1))
  124. {
  125. return vector3_multiply(L);
  126. }
  127. return vector2_multiply(L);
  128. }
  129. //-----------------------------------------------------------------------------
  130. static int crown_lua_lightuserdata_div(lua_State* L)
  131. {
  132. LuaStack stack(L);
  133. if (stack.is_vector3(1))
  134. {
  135. return vector3_divide(L);
  136. }
  137. return vector2_divide(L);
  138. }
  139. //-----------------------------------------------------------------------------
  140. static int crown_lua_lightuserdata_unm(lua_State* L)
  141. {
  142. LuaStack stack(L);
  143. if (stack.is_vector3(1))
  144. {
  145. return vector3_negate(L);
  146. }
  147. return vector2_negate(L);
  148. }
  149. //-----------------------------------------------------------------------------
  150. static int crown_lua_require(lua_State* L)
  151. {
  152. LuaStack stack(L);
  153. const char* filename = stack.get_string(1);
  154. const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
  155. device()->resource_manager()->flush();
  156. const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
  157. luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
  158. device()->resource_manager()->unload(lua_res);
  159. return 1;
  160. }
  161. //-----------------------------------------------------------------------------
  162. LuaEnvironment::LuaEnvironment()
  163. : m_state(luaL_newstate())
  164. {
  165. }
  166. //-----------------------------------------------------------------------------
  167. void LuaEnvironment::init()
  168. {
  169. // Open default libraries
  170. luaL_openlibs(m_state);
  171. // Open Crown library
  172. lua_cpcall(m_state, luaopen_libcrown, NULL);
  173. // Register custom loader
  174. lua_getfield(m_state, LUA_GLOBALSINDEX, "package");
  175. lua_getfield(m_state, -1, "loaders");
  176. lua_remove(m_state, -2);
  177. int num_loaders = 0;
  178. lua_pushnil(m_state);
  179. while (lua_next(m_state, -2) != 0)
  180. {
  181. lua_pop(m_state, 1);
  182. num_loaders++;
  183. }
  184. lua_pushinteger(m_state, num_loaders + 1);
  185. lua_pushcfunction(m_state, crown_lua_require);
  186. lua_rawset(m_state, -3);
  187. lua_pop(m_state, 1);
  188. // Create metatable for lightuserdata
  189. lua_pushlightuserdata(m_state, (void*)0x0); // Just dummy userdata
  190. luaL_newmetatable(m_state, "Lightuserdata_mt");
  191. lua_setmetatable(m_state, -2);
  192. lua_pop(m_state, 1);
  193. luaL_newmetatable(m_state, "Lightuserdata_mt");
  194. lua_pushstring(m_state, "__add");
  195. lua_pushcfunction(m_state, crown_lua_lightuserdata_add);
  196. lua_settable(m_state, 1);
  197. lua_pushstring(m_state, "__sub");
  198. lua_pushcfunction(m_state, crown_lua_lightuserdata_sub);
  199. lua_settable(m_state, 1);
  200. lua_pushstring(m_state, "__mul");
  201. lua_pushcfunction(m_state, crown_lua_lightuserdata_mul);
  202. lua_settable(m_state, 1);
  203. lua_pushstring(m_state, "__div");
  204. lua_pushcfunction(m_state, crown_lua_lightuserdata_div);
  205. lua_settable(m_state, 1);
  206. lua_pushstring(m_state, "__unm");
  207. lua_pushcfunction(m_state, crown_lua_lightuserdata_unm);
  208. lua_settable(m_state, 1);
  209. // Vector2 metatable
  210. luaL_newmetatable(m_state, "Vector2_mt");
  211. lua_pushvalue(m_state, -1);
  212. lua_setfield(m_state, -2, "__index");
  213. lua_pushcfunction(m_state, crown_lua_vector2_call);
  214. lua_setfield(m_state, -2, "__call");
  215. lua_getglobal(m_state, "Vector2");
  216. lua_pushvalue(m_state, -2); // Duplicate metatable
  217. lua_setmetatable(m_state, -2); // setmetatable(Vector2, Vector2_mt)
  218. // Vector3 metatable
  219. luaL_newmetatable(m_state, "Vector3_mt");
  220. lua_pushvalue(m_state, -1);
  221. lua_setfield(m_state, -2, "__index");
  222. lua_pushcfunction(m_state, crown_lua_vector3_call);
  223. lua_setfield(m_state, -2, "__call");
  224. lua_getglobal(m_state, "Vector3");
  225. lua_pushvalue(m_state, -2); // Duplicate metatable
  226. lua_setmetatable(m_state, -2); // setmetatable(Vector3, Vector3_mt)
  227. // Matrix4x4 metatable
  228. luaL_newmetatable(m_state, "Matrix4x4_mt");
  229. lua_pushvalue(m_state, -1);
  230. lua_setfield(m_state, -2, "__index");
  231. lua_pushcfunction(m_state, crown_lua_matrix4x4_call);
  232. lua_setfield(m_state, -2, "__call");
  233. lua_getglobal(m_state, "Matrix4x4");
  234. lua_pushvalue(m_state, -2); // Duplicate metatable
  235. lua_setmetatable(m_state, -2); // setmetatable(Matrix4x4, Matrix4x4_mt)
  236. // Quaternion metatable
  237. luaL_newmetatable(m_state, "Quaternion_mt");
  238. lua_pushvalue(m_state, -1);
  239. lua_setfield(m_state, -2, "__index");
  240. lua_pushcfunction(m_state, crown_lua_quaternion_call);
  241. lua_setfield(m_state, -2, "__call");
  242. lua_getglobal(m_state, "Quaternion");
  243. lua_pushvalue(m_state, -2); // Duplicate metatable
  244. lua_setmetatable(m_state, -2); // setmetatable(Quaternion, Quaternion_mt)
  245. }
  246. //-----------------------------------------------------------------------------
  247. void LuaEnvironment::shutdown()
  248. {
  249. lua_close(m_state);
  250. }
  251. //-----------------------------------------------------------------------------
  252. bool LuaEnvironment::load_and_execute(const char* res_name)
  253. {
  254. CE_ASSERT_NOT_NULL(res_name);
  255. ResourceManager* resman = device()->resource_manager();
  256. // Load the resource
  257. ResourceId res_id = resman->load("lua", res_name);
  258. resman->flush();
  259. LuaResource* lr = (LuaResource*) resman->data(res_id);
  260. lua_getglobal(m_state, "debug");
  261. lua_getfield(m_state, -1, "traceback");
  262. if (luaL_loadbuffer(m_state, (const char*) lr->program(), lr->size(), res_name) == 0)
  263. {
  264. if (lua_pcall(m_state, 0, 0, -2) == 0)
  265. {
  266. // Unloading is OK since the script data has been copied to Lua
  267. resman->unload(res_id);
  268. lua_pop(m_state, 2); // Pop debug.traceback
  269. return true;
  270. }
  271. }
  272. error();
  273. lua_pop(m_state, 2); // Pop debug.traceback
  274. return false;
  275. }
  276. //-----------------------------------------------------------------------------
  277. bool LuaEnvironment::execute_string(const char* s)
  278. {
  279. CE_ASSERT_NOT_NULL(s);
  280. lua_getglobal(m_state, "debug");
  281. lua_getfield(m_state, -1, "traceback");
  282. if (luaL_loadstring(m_state, s) == 0)
  283. {
  284. if (lua_pcall(m_state, 0, 0, -2) == 0)
  285. {
  286. // Unloading is OK since the script data has been copied to Lua
  287. lua_pop(m_state, 2); // Pop debug.traceback
  288. return true;
  289. }
  290. }
  291. error();
  292. lua_pop(m_state, 2); // Pop debug.traceback
  293. return false;
  294. }
  295. //-----------------------------------------------------------------------------
  296. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  297. {
  298. luaL_Reg entry[2];
  299. entry[0].name = name;
  300. entry[0].func = func;
  301. entry[1].name = NULL;
  302. entry[1].func = NULL;
  303. luaL_register(m_state, module, entry);
  304. }
  305. //-----------------------------------------------------------
  306. void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name, uint32_t value)
  307. {
  308. lua_pushinteger(m_state, value);
  309. lua_setfield(m_state, -2, name);
  310. }
  311. //-----------------------------------------------------------------------------
  312. bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  313. {
  314. CE_ASSERT_NOT_NULL(func);
  315. LuaStack stack(m_state);
  316. va_list vl;
  317. va_start(vl, argc);
  318. lua_getglobal(m_state, "debug");
  319. lua_getfield(m_state, -1, "traceback");
  320. lua_getglobal(m_state, func);
  321. for (uint8_t i = 0; i < argc; i++)
  322. {
  323. const int type = va_arg(vl, int);
  324. switch (type)
  325. {
  326. case ARGUMENT_FLOAT:
  327. {
  328. stack.push_float(va_arg(vl, double));
  329. break;
  330. }
  331. default:
  332. {
  333. CE_ASSERT(false, "Oops, lua argument unknown");
  334. break;
  335. }
  336. }
  337. }
  338. va_end(vl);
  339. if (lua_pcall(m_state, argc, 0, -argc - 2) != 0)
  340. {
  341. error();
  342. lua_pop(m_state, 2); // Pop debug.traceback
  343. return false;
  344. }
  345. lua_pop(m_state, 2); // Pop debug.traceback
  346. return true;
  347. }
  348. //-----------------------------------------------------------------------------
  349. void LuaEnvironment::error()
  350. {
  351. const char* msg = lua_tostring(m_state, -1);
  352. Log::e(msg);
  353. lua_pop(m_state, 1);
  354. }
  355. } // namespace crown