LuaEnvironment.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 "Assert.h"
  25. #include "LuaEnvironment.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. //-----------------------------------------------------------------------------
  34. CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
  35. {
  36. LuaEnvironment* env = device()->lua_environment();
  37. load_int_setting(*env);
  38. load_float_setting(*env);
  39. load_string_setting(*env);
  40. load_vec2(*env);
  41. load_vec3(*env);
  42. load_mat4(*env);
  43. load_quat(*env);
  44. load_math(*env);
  45. load_mouse(*env);
  46. load_keyboard(*env);
  47. load_accelerometer(*env);
  48. load_device(*env);
  49. load_window(*env);
  50. load_resource_package(*env);
  51. return 1;
  52. }
  53. //-----------------------------------------------------------------------------
  54. static int crown_lua_require(lua_State* L)
  55. {
  56. LuaStack stack(L);
  57. const char* filename = stack.get_string(1);
  58. const ResourceId lua_res = device()->resource_manager()->load("lua", filename);
  59. device()->resource_manager()->flush();
  60. const LuaResource* lr = (LuaResource*) device()->resource_manager()->data(lua_res);
  61. luaL_loadbuffer(L, (const char*) lr->code(), lr->size(), "");
  62. device()->resource_manager()->unload(lua_res);
  63. return 1;
  64. }
  65. //-----------------------------------------------------------------------------
  66. LuaEnvironment::LuaEnvironment()
  67. : m_state(luaL_newstate()), m_is_used(false)
  68. {
  69. }
  70. //-----------------------------------------------------------------------------
  71. void LuaEnvironment::init()
  72. {
  73. // Open default libraries
  74. luaL_openlibs(m_state);
  75. // Open Crown library
  76. lua_cpcall(m_state, luaopen_libcrown, NULL);
  77. // Register custom loader
  78. lua_getfield(m_state, LUA_GLOBALSINDEX, "package");
  79. lua_getfield(m_state, -1, "loaders");
  80. lua_remove(m_state, -2);
  81. int num_loaders = 0;
  82. lua_pushnil(m_state);
  83. while (lua_next(m_state, -2) != 0)
  84. {
  85. lua_pop(m_state, 1);
  86. num_loaders++;
  87. }
  88. lua_pushinteger(m_state, num_loaders + 1);
  89. lua_pushcfunction(m_state, crown_lua_require);
  90. lua_rawset(m_state, -3);
  91. lua_pop(m_state, 1);
  92. m_is_used = true;
  93. }
  94. //-----------------------------------------------------------------------------
  95. void LuaEnvironment::shutdown()
  96. {
  97. lua_close(m_state);
  98. m_is_used = false;
  99. }
  100. //-----------------------------------------------------------------------------
  101. bool LuaEnvironment::load_and_execute(const char* res_name)
  102. {
  103. CE_ASSERT_NOT_NULL(res_name);
  104. ResourceManager* resman = device()->resource_manager();
  105. // Load the resource
  106. ResourceId res_id = resman->load("lua", res_name);
  107. resman->flush();
  108. LuaResource* lr = (LuaResource*) resman->data(res_id);
  109. if (luaL_loadbuffer(m_state, (const char*) lr->code(), lr->size(), res_name) == 0)
  110. {
  111. if (lua_pcall(m_state, 0, 0, 0) == 0)
  112. {
  113. // Unloading is OK since the script data has been copied to Lua
  114. resman->unload(res_id);
  115. return true;
  116. }
  117. }
  118. error();
  119. return false;
  120. }
  121. //-----------------------------------------------------------------------------
  122. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  123. {
  124. luaL_Reg entry[2];
  125. entry[0].name = name;
  126. entry[0].func = func;
  127. entry[1].name = NULL;
  128. entry[1].func = NULL;
  129. luaL_register(m_state, module, entry);
  130. }
  131. //-----------------------------------------------------------
  132. void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name, uint32_t value)
  133. {
  134. lua_pushinteger(m_state, value);
  135. lua_setfield(m_state, -2, name);
  136. }
  137. //-----------------------------------------------------------------------------
  138. bool LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  139. {
  140. CE_ASSERT_NOT_NULL(func);
  141. LuaStack stack(m_state);
  142. va_list vl;
  143. va_start(vl, argc);
  144. lua_getglobal(m_state, "debug");
  145. lua_getfield(m_state, -1, "traceback");
  146. lua_getglobal(m_state, func);
  147. for (uint8_t i = 0; i < argc; i++)
  148. {
  149. const int type = va_arg(vl, int);
  150. switch (type)
  151. {
  152. case ARGUMENT_FLOAT:
  153. {
  154. stack.push_float(va_arg(vl, double));
  155. break;
  156. }
  157. default:
  158. {
  159. CE_ASSERT(false, "Oops, lua argument unknown");
  160. break;
  161. }
  162. }
  163. }
  164. va_end(vl);
  165. if (lua_pcall(m_state, argc, 0, -argc - 2) != 0)
  166. {
  167. error();
  168. return false;
  169. }
  170. return true;
  171. }
  172. //-----------------------------------------------------------------------------
  173. void LuaEnvironment::error()
  174. {
  175. const char* msg = lua_tostring(m_state, -1);
  176. Log::e(msg);
  177. }
  178. } // namespace crown