LuaEnvironment.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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 vector3_add(lua_State* L);
  34. extern int vector3_subtract(lua_State* L);
  35. extern int vector3_multiply(lua_State* L);
  36. extern int vector3_divide(lua_State* L);
  37. extern int vector3_negate(lua_State* L);
  38. extern int vector3(lua_State* L);
  39. //-----------------------------------------------------------------------------
  40. CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
  41. {
  42. LuaEnvironment* env = device()->lua_environment();
  43. load_int_setting(*env);
  44. load_float_setting(*env);
  45. load_string_setting(*env);
  46. load_vector2(*env);
  47. load_vector3(*env);
  48. load_matrix4x4(*env);
  49. load_quaternion(*env);
  50. load_math(*env);
  51. load_window(*env);
  52. load_mouse(*env);
  53. load_keyboard(*env);
  54. load_accelerometer(*env);
  55. load_device(*env);
  56. load_resource_package(*env);
  57. load_unit(*env);
  58. load_camera(*env);
  59. load_world(*env);
  60. load_mesh(*env);
  61. load_sprite(*env);
  62. return 1;
  63. }
  64. //-----------------------------------------------------------------------------
  65. static int crown_lua_lightuserdata_add(lua_State* L)
  66. {
  67. return vector3_add(L);
  68. }
  69. //-----------------------------------------------------------------------------
  70. static int crown_lua_lightuserdata_sub(lua_State* L)
  71. {
  72. return vector3_subtract(L);
  73. }
  74. //-----------------------------------------------------------------------------
  75. static int crown_lua_lightuserdata_mul(lua_State* L)
  76. {
  77. return vector3_multiply(L);
  78. }
  79. //-----------------------------------------------------------------------------
  80. static int crown_lua_lightuserdata_div(lua_State* L)
  81. {
  82. return vector3_divide(L);
  83. }
  84. //-----------------------------------------------------------------------------
  85. static int crown_lua_lightuserdata_unm(lua_State* L)
  86. {
  87. return vector3_negate(L);
  88. }
  89. //-----------------------------------------------------------------------------
  90. static int crown_lua_require(lua_State* L)
  91. {
  92. LuaStack stack(L);
  93. const char* filename = stack.get_string(1);
  94. const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
  95. device()->resource_manager()->flush();
  96. const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
  97. luaL_loadbuffer(L, (const char*) lr->code(), lr->size(), "");
  98. device()->resource_manager()->unload(lua_res);
  99. return 1;
  100. }
  101. //-----------------------------------------------------------------------------
  102. LuaEnvironment::LuaEnvironment()
  103. : m_state(luaL_newstate())
  104. {
  105. }
  106. //-----------------------------------------------------------------------------
  107. void LuaEnvironment::init()
  108. {
  109. // Open default libraries
  110. luaL_openlibs(m_state);
  111. // Open Crown library
  112. lua_cpcall(m_state, luaopen_libcrown, NULL);
  113. // Register custom loader
  114. lua_getfield(m_state, LUA_GLOBALSINDEX, "package");
  115. lua_getfield(m_state, -1, "loaders");
  116. lua_remove(m_state, -2);
  117. int num_loaders = 0;
  118. lua_pushnil(m_state);
  119. while (lua_next(m_state, -2) != 0)
  120. {
  121. lua_pop(m_state, 1);
  122. num_loaders++;
  123. }
  124. lua_pushinteger(m_state, num_loaders + 1);
  125. lua_pushcfunction(m_state, crown_lua_require);
  126. lua_rawset(m_state, -3);
  127. lua_pop(m_state, 1);
  128. // Create metatable for lightuserdata
  129. lua_pushlightuserdata(m_state, (void*)0x0);
  130. luaL_newmetatable(m_state, "Crown.lightuserdata.metatable");
  131. lua_setmetatable(m_state, -2);
  132. lua_pop(m_state, 1);
  133. luaL_newmetatable(m_state, "Crown.lightuserdata.metatable");
  134. lua_pushstring(m_state, "__add");
  135. lua_pushcfunction(m_state, crown_lua_lightuserdata_add);
  136. lua_settable(m_state, 1);
  137. lua_pushstring(m_state, "__sub");
  138. lua_pushcfunction(m_state, crown_lua_lightuserdata_sub);
  139. lua_settable(m_state, 1);
  140. lua_pushstring(m_state, "__mul");
  141. lua_pushcfunction(m_state, crown_lua_lightuserdata_mul);
  142. lua_settable(m_state, 1);
  143. lua_pushstring(m_state, "__div");
  144. lua_pushcfunction(m_state, crown_lua_lightuserdata_div);
  145. lua_settable(m_state, 1);
  146. lua_pushstring(m_state, "__unm");
  147. lua_pushcfunction(m_state, crown_lua_lightuserdata_unm);
  148. lua_settable(m_state, 1);
  149. }
  150. //-----------------------------------------------------------------------------
  151. void LuaEnvironment::shutdown()
  152. {
  153. lua_close(m_state);
  154. }
  155. //-----------------------------------------------------------------------------
  156. bool LuaEnvironment::load_and_execute(const char* res_name)
  157. {
  158. CE_ASSERT_NOT_NULL(res_name);
  159. ResourceManager* resman = device()->resource_manager();
  160. // Load the resource
  161. ResourceId res_id = resman->load("lua", res_name);
  162. resman->flush();
  163. LuaResource* lr = (LuaResource*) resman->data(res_id);
  164. lua_getglobal(m_state, "debug");
  165. lua_getfield(m_state, -1, "traceback");
  166. if (luaL_loadbuffer(m_state, (const char*) lr->code(), lr->size(), res_name) == 0)
  167. {
  168. if (lua_pcall(m_state, 0, 0, -2) == 0)
  169. {
  170. // Unloading is OK since the script data has been copied to Lua
  171. resman->unload(res_id);
  172. lua_pop(m_state, 2); // Pop debug.traceback
  173. return true;
  174. }
  175. }
  176. error();
  177. lua_pop(m_state, 2); // Pop debug.traceback
  178. return false;
  179. }
  180. //-----------------------------------------------------------------------------
  181. bool LuaEnvironment::execute_string(const char* s)
  182. {
  183. CE_ASSERT_NOT_NULL(s);
  184. lua_getglobal(m_state, "debug");
  185. lua_getfield(m_state, -1, "traceback");
  186. if (luaL_loadstring(m_state, s) == 0)
  187. {
  188. if (lua_pcall(m_state, 0, 0, -2) == 0)
  189. {
  190. // Unloading is OK since the script data has been copied to Lua
  191. lua_pop(m_state, 2); // Pop debug.traceback
  192. return true;
  193. }
  194. }
  195. error();
  196. lua_pop(m_state, 2); // Pop debug.traceback
  197. return false;
  198. }
  199. //-----------------------------------------------------------------------------
  200. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  201. {
  202. luaL_Reg entry[2];
  203. entry[0].name = name;
  204. entry[0].func = func;
  205. entry[1].name = NULL;
  206. entry[1].func = NULL;
  207. luaL_register(m_state, module, entry);
  208. }
  209. //-----------------------------------------------------------
  210. void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name, uint32_t value)
  211. {
  212. lua_pushinteger(m_state, value);
  213. lua_setfield(m_state, -2, name);
  214. }
  215. //-----------------------------------------------------------------------------
  216. bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  217. {
  218. CE_ASSERT_NOT_NULL(func);
  219. LuaStack stack(m_state);
  220. va_list vl;
  221. va_start(vl, argc);
  222. lua_getglobal(m_state, "debug");
  223. lua_getfield(m_state, -1, "traceback");
  224. lua_getglobal(m_state, func);
  225. for (uint8_t i = 0; i < argc; i++)
  226. {
  227. const int type = va_arg(vl, int);
  228. switch (type)
  229. {
  230. case ARGUMENT_FLOAT:
  231. {
  232. stack.push_float(va_arg(vl, double));
  233. break;
  234. }
  235. default:
  236. {
  237. CE_ASSERT(false, "Oops, lua argument unknown");
  238. break;
  239. }
  240. }
  241. }
  242. va_end(vl);
  243. if (lua_pcall(m_state, argc, 0, -argc - 2) != 0)
  244. {
  245. error();
  246. lua_pop(m_state, 2); // Pop debug.traceback
  247. return false;
  248. }
  249. lua_pop(m_state, 2); // Pop debug.traceback
  250. return true;
  251. }
  252. //-----------------------------------------------------------------------------
  253. void LuaEnvironment::error()
  254. {
  255. const char* msg = lua_tostring(m_state, -1);
  256. Log::e(msg);
  257. lua_pop(m_state, 1);
  258. }
  259. } // namespace crown