2
0

LuaEnvironment.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  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_touch(*env);
  63. load_accelerometer(*env);
  64. load_device(*env);
  65. load_resource_package(*env);
  66. load_unit(*env);
  67. load_camera(*env);
  68. load_world(*env);
  69. load_mesh(*env);
  70. load_sprite(*env);
  71. load_actor(*env);
  72. load_controller(*env);
  73. load_physics_world(*env);
  74. load_gui(*env);
  75. return 1;
  76. }
  77. //-----------------------------------------------------------------------------
  78. static int crown_lua_vector2_call(lua_State* L)
  79. {
  80. lua_remove(L, 1);
  81. return vector2(L);
  82. }
  83. //-----------------------------------------------------------------------------
  84. static int crown_lua_vector3_call(lua_State* L)
  85. {
  86. lua_remove(L, 1);
  87. return vector3(L);
  88. }
  89. //-----------------------------------------------------------------------------
  90. static int crown_lua_matrix4x4_call(lua_State* L)
  91. {
  92. lua_remove(L, 1);
  93. return matrix4x4(L);
  94. }
  95. //-----------------------------------------------------------------------------
  96. static int crown_lua_quaternion_call(lua_State* L)
  97. {
  98. lua_remove(L, 1);
  99. return quaternion(L);
  100. }
  101. //-----------------------------------------------------------------------------
  102. static int crown_lua_lightuserdata_add(lua_State* L)
  103. {
  104. LuaStack stack(L);
  105. if (stack.is_vector3(1))
  106. {
  107. return vector3_add(L);
  108. }
  109. return vector2_add(L);
  110. }
  111. //-----------------------------------------------------------------------------
  112. static int crown_lua_lightuserdata_sub(lua_State* L)
  113. {
  114. LuaStack stack(L);
  115. if (stack.is_vector3(1))
  116. {
  117. return vector3_subtract(L);
  118. }
  119. return vector2_subtract(L);
  120. }
  121. //-----------------------------------------------------------------------------
  122. static int crown_lua_lightuserdata_mul(lua_State* L)
  123. {
  124. LuaStack stack(L);
  125. if (stack.is_vector3(1))
  126. {
  127. return vector3_multiply(L);
  128. }
  129. return vector2_multiply(L);
  130. }
  131. //-----------------------------------------------------------------------------
  132. static int crown_lua_lightuserdata_div(lua_State* L)
  133. {
  134. LuaStack stack(L);
  135. if (stack.is_vector3(1))
  136. {
  137. return vector3_divide(L);
  138. }
  139. return vector2_divide(L);
  140. }
  141. //-----------------------------------------------------------------------------
  142. static int crown_lua_lightuserdata_unm(lua_State* L)
  143. {
  144. LuaStack stack(L);
  145. if (stack.is_vector3(1))
  146. {
  147. return vector3_negate(L);
  148. }
  149. return vector2_negate(L);
  150. }
  151. //-----------------------------------------------------------------------------
  152. static int crown_lua_require(lua_State* L)
  153. {
  154. LuaStack stack(L);
  155. const char* filename = stack.get_string(1);
  156. const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
  157. device()->resource_manager()->flush();
  158. const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
  159. luaL_loadbuffer(L, (const char*) lr->program(), lr->size(), "");
  160. device()->resource_manager()->unload(lua_res);
  161. return 1;
  162. }
  163. //-----------------------------------------------------------------------------
  164. LuaEnvironment::LuaEnvironment()
  165. : m_state(luaL_newstate())
  166. {
  167. }
  168. //-----------------------------------------------------------------------------
  169. void LuaEnvironment::init()
  170. {
  171. // Open default libraries
  172. luaL_openlibs(m_state);
  173. // Open Crown library
  174. lua_cpcall(m_state, luaopen_libcrown, NULL);
  175. // Register custom loader
  176. lua_getfield(m_state, LUA_GLOBALSINDEX, "package");
  177. lua_getfield(m_state, -1, "loaders");
  178. lua_remove(m_state, -2);
  179. int num_loaders = 0;
  180. lua_pushnil(m_state);
  181. while (lua_next(m_state, -2) != 0)
  182. {
  183. lua_pop(m_state, 1);
  184. num_loaders++;
  185. }
  186. lua_pushinteger(m_state, num_loaders + 1);
  187. lua_pushcfunction(m_state, crown_lua_require);
  188. lua_rawset(m_state, -3);
  189. lua_pop(m_state, 1);
  190. // Create metatable for lightuserdata
  191. lua_pushlightuserdata(m_state, (void*)0x0); // Just dummy userdata
  192. luaL_newmetatable(m_state, "Lightuserdata_mt");
  193. lua_setmetatable(m_state, -2);
  194. lua_pop(m_state, 1);
  195. luaL_newmetatable(m_state, "Lightuserdata_mt");
  196. lua_pushstring(m_state, "__add");
  197. lua_pushcfunction(m_state, crown_lua_lightuserdata_add);
  198. lua_settable(m_state, 1);
  199. lua_pushstring(m_state, "__sub");
  200. lua_pushcfunction(m_state, crown_lua_lightuserdata_sub);
  201. lua_settable(m_state, 1);
  202. lua_pushstring(m_state, "__mul");
  203. lua_pushcfunction(m_state, crown_lua_lightuserdata_mul);
  204. lua_settable(m_state, 1);
  205. lua_pushstring(m_state, "__div");
  206. lua_pushcfunction(m_state, crown_lua_lightuserdata_div);
  207. lua_settable(m_state, 1);
  208. lua_pushstring(m_state, "__unm");
  209. lua_pushcfunction(m_state, crown_lua_lightuserdata_unm);
  210. lua_settable(m_state, 1);
  211. // Vector2 metatable
  212. luaL_newmetatable(m_state, "Vector2_mt");
  213. lua_pushvalue(m_state, -1);
  214. lua_setfield(m_state, -2, "__index");
  215. lua_pushcfunction(m_state, crown_lua_vector2_call);
  216. lua_setfield(m_state, -2, "__call");
  217. lua_getglobal(m_state, "Vector2");
  218. lua_pushvalue(m_state, -2); // Duplicate metatable
  219. lua_setmetatable(m_state, -2); // setmetatable(Vector2, Vector2_mt)
  220. // Vector3 metatable
  221. luaL_newmetatable(m_state, "Vector3_mt");
  222. lua_pushvalue(m_state, -1);
  223. lua_setfield(m_state, -2, "__index");
  224. lua_pushcfunction(m_state, crown_lua_vector3_call);
  225. lua_setfield(m_state, -2, "__call");
  226. lua_getglobal(m_state, "Vector3");
  227. lua_pushvalue(m_state, -2); // Duplicate metatable
  228. lua_setmetatable(m_state, -2); // setmetatable(Vector3, Vector3_mt)
  229. // Matrix4x4 metatable
  230. luaL_newmetatable(m_state, "Matrix4x4_mt");
  231. lua_pushvalue(m_state, -1);
  232. lua_setfield(m_state, -2, "__index");
  233. lua_pushcfunction(m_state, crown_lua_matrix4x4_call);
  234. lua_setfield(m_state, -2, "__call");
  235. lua_getglobal(m_state, "Matrix4x4");
  236. lua_pushvalue(m_state, -2); // Duplicate metatable
  237. lua_setmetatable(m_state, -2); // setmetatable(Matrix4x4, Matrix4x4_mt)
  238. // Quaternion metatable
  239. luaL_newmetatable(m_state, "Quaternion_mt");
  240. lua_pushvalue(m_state, -1);
  241. lua_setfield(m_state, -2, "__index");
  242. lua_pushcfunction(m_state, crown_lua_quaternion_call);
  243. lua_setfield(m_state, -2, "__call");
  244. lua_getglobal(m_state, "Quaternion");
  245. lua_pushvalue(m_state, -2); // Duplicate metatable
  246. lua_setmetatable(m_state, -2); // setmetatable(Quaternion, Quaternion_mt)
  247. }
  248. //-----------------------------------------------------------------------------
  249. void LuaEnvironment::shutdown()
  250. {
  251. lua_close(m_state);
  252. }
  253. //-----------------------------------------------------------------------------
  254. bool LuaEnvironment::load_and_execute(const char* res_name)
  255. {
  256. CE_ASSERT_NOT_NULL(res_name);
  257. ResourceManager* resman = device()->resource_manager();
  258. // Load the resource
  259. ResourceId res_id = resman->load("lua", res_name);
  260. resman->flush();
  261. LuaResource* lr = (LuaResource*) resman->data(res_id);
  262. lua_getglobal(m_state, "debug");
  263. lua_getfield(m_state, -1, "traceback");
  264. if (luaL_loadbuffer(m_state, (const char*) lr->program(), lr->size(), res_name) == 0)
  265. {
  266. if (lua_pcall(m_state, 0, 0, -2) == 0)
  267. {
  268. // Unloading is OK since the script data has been copied to Lua
  269. resman->unload(res_id);
  270. lua_pop(m_state, 2); // Pop debug.traceback
  271. return true;
  272. }
  273. }
  274. error();
  275. lua_pop(m_state, 2); // Pop debug.traceback
  276. return false;
  277. }
  278. //-----------------------------------------------------------------------------
  279. bool LuaEnvironment::execute_string(const char* s)
  280. {
  281. CE_ASSERT_NOT_NULL(s);
  282. lua_getglobal(m_state, "debug");
  283. lua_getfield(m_state, -1, "traceback");
  284. if (luaL_loadstring(m_state, s) == 0)
  285. {
  286. if (lua_pcall(m_state, 0, 0, -2) == 0)
  287. {
  288. // Unloading is OK since the script data has been copied to Lua
  289. lua_pop(m_state, 2); // Pop debug.traceback
  290. return true;
  291. }
  292. }
  293. error();
  294. lua_pop(m_state, 2); // Pop debug.traceback
  295. return false;
  296. }
  297. //-----------------------------------------------------------------------------
  298. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  299. {
  300. luaL_Reg entry[2];
  301. entry[0].name = name;
  302. entry[0].func = func;
  303. entry[1].name = NULL;
  304. entry[1].func = NULL;
  305. luaL_register(m_state, module, entry);
  306. }
  307. //-----------------------------------------------------------
  308. void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name, uint32_t value)
  309. {
  310. lua_pushinteger(m_state, value);
  311. lua_setfield(m_state, -2, name);
  312. }
  313. //-----------------------------------------------------------------------------
  314. bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  315. {
  316. CE_ASSERT_NOT_NULL(func);
  317. LuaStack stack(m_state);
  318. va_list vl;
  319. va_start(vl, argc);
  320. lua_getglobal(m_state, "debug");
  321. lua_getfield(m_state, -1, "traceback");
  322. lua_getglobal(m_state, func);
  323. for (uint8_t i = 0; i < argc; i++)
  324. {
  325. const int type = va_arg(vl, int);
  326. switch (type)
  327. {
  328. case ARGUMENT_FLOAT:
  329. {
  330. stack.push_float(va_arg(vl, double));
  331. break;
  332. }
  333. default:
  334. {
  335. CE_ASSERT(false, "Oops, lua argument unknown");
  336. break;
  337. }
  338. }
  339. }
  340. va_end(vl);
  341. if (lua_pcall(m_state, argc, 0, -argc - 2) != 0)
  342. {
  343. error();
  344. lua_pop(m_state, 2); // Pop debug.traceback
  345. return false;
  346. }
  347. lua_pop(m_state, 2); // Pop debug.traceback
  348. return true;
  349. }
  350. //-----------------------------------------------------------------------------
  351. void LuaEnvironment::error()
  352. {
  353. const char* msg = lua_tostring(m_state, -1);
  354. Log::e(msg);
  355. lua_pop(m_state, 1);
  356. }
  357. } // namespace crown