2
0

LuaEnvironment.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. namespace crown
  31. {
  32. //-----------------------------------------------------------------------------
  33. CE_EXPORT int luaopen_libcrown(lua_State* /*L*/)
  34. {
  35. LuaEnvironment* env = device()->lua_environment();
  36. load_int_setting(*env);
  37. load_float_setting(*env);
  38. load_string_setting(*env);
  39. load_vec2(*env);
  40. load_vec3(*env);
  41. load_mat4(*env);
  42. load_quat(*env);
  43. load_math(*env);
  44. load_mouse(*env);
  45. load_keyboard(*env);
  46. load_accelerometer(*env);
  47. load_device(*env);
  48. load_window(*env);
  49. return 1;
  50. }
  51. //-----------------------------------------------------------------------------
  52. LuaEnvironment::LuaEnvironment() :
  53. m_state(luaL_newstate()),
  54. m_is_used(false)
  55. {
  56. // Open Lua default libraries
  57. string::strncpy(m_error_buffer, "", 1024);
  58. string::strncpy(m_tmp_buffer, "", 1024);
  59. }
  60. //-----------------------------------------------------------------------------
  61. void LuaEnvironment::init()
  62. {
  63. // Open default libraries
  64. luaL_openlibs(m_state);
  65. // Open Crown library
  66. lua_cpcall(m_state, luaopen_libcrown, NULL);
  67. // load_buffer(class_system, string::strlen(class_system));
  68. // execute(0, 0);
  69. // load_buffer(commands_list, string::strlen(commands_list));
  70. // execute(0, 0);
  71. // load_buffer(get_cmd_by_name, string::strlen(get_cmd_by_name));
  72. // execute(0, 0);
  73. // load_buffer(tmp_print_table, string::strlen(tmp_print_table));
  74. // execute(0, 0);
  75. // load_buffer(count_all, string::strlen(count_all));
  76. // execute(0, 0);
  77. m_is_used = true;
  78. }
  79. //-----------------------------------------------------------------------------
  80. void LuaEnvironment::shutdown()
  81. {
  82. lua_close(m_state);
  83. m_is_used = false;
  84. }
  85. //-----------------------------------------------------------------------------
  86. const char* LuaEnvironment::error()
  87. {
  88. string::strncpy(m_tmp_buffer, m_error_buffer, 1024);
  89. string::strncpy(m_error_buffer, "", 1024);
  90. return m_tmp_buffer;
  91. }
  92. //-----------------------------------------------------------------------------
  93. void LuaEnvironment::load(const LuaResource* lr)
  94. {
  95. CE_ASSERT_NOT_NULL(lr);
  96. if (luaL_loadbuffer(m_state, (const char*) lr->code(), lr->size(), "") != 0)
  97. {
  98. lua_error();
  99. }
  100. if (lua_pcall(m_state, 0, 0, 0) != 0)
  101. {
  102. lua_error();
  103. }
  104. }
  105. //-----------------------------------------------------------------------------
  106. void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  107. {
  108. CE_ASSERT_NOT_NULL(func);
  109. LuaStack stack(m_state);
  110. va_list vl;
  111. va_start(vl, argc);
  112. lua_getglobal(m_state, func);
  113. for (uint8_t i = 0; i < argc; i++)
  114. {
  115. const int type = va_arg(vl, int);
  116. switch (type)
  117. {
  118. case ARGUMENT_FLOAT:
  119. {
  120. stack.push_float(va_arg(vl, double));
  121. break;
  122. }
  123. default:
  124. {
  125. CE_ASSERT(false, "Oops, lua argument unknown");
  126. break;
  127. }
  128. }
  129. }
  130. va_end(vl);
  131. if (lua_pcall(m_state, argc, 0, 0) != 0)
  132. {
  133. lua_error();
  134. }
  135. }
  136. //-----------------------------------------------------------------------------
  137. void LuaEnvironment::lua_error()
  138. {
  139. string::strncpy(m_error_buffer, "", 1024);
  140. string::strncpy(m_error_buffer, lua_tostring(m_state, -1), 1024);
  141. }
  142. //-----------------------------------------------------------------------------
  143. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  144. {
  145. luaL_Reg entry[2];
  146. entry[0].name = name;
  147. entry[0].func = func;
  148. entry[1].name = NULL;
  149. entry[1].func = NULL;
  150. luaL_register(m_state, module, entry);
  151. }
  152. //-----------------------------------------------------------
  153. void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name, uint32_t value)
  154. {
  155. lua_pushinteger(m_state, value);
  156. lua_setfield(m_state, -2, name);
  157. }
  158. const char* LuaEnvironment::class_system = "function class(klass, super) "
  159. " if not klass then "
  160. " klass = {} "
  161. " local meta = {} "
  162. " meta.__call = function(self, ...) "
  163. " local object = {} "
  164. " setmetatable(object, klass) "
  165. " if object.init then object:init(...) end "
  166. " return object "
  167. " end "
  168. " setmetatable(klass, meta) "
  169. " end "
  170. " if super then "
  171. " for k,v in pairs(super) do "
  172. " klass[k] = v "
  173. " end "
  174. " end "
  175. " klass.__index = klass "
  176. " return klass "
  177. "end";
  178. const char* LuaEnvironment::commands_list = "function get_all_commands() "
  179. " local cmds = {}; "
  180. " for class_name,class in pairs(_G) do "
  181. " if type(class) == 'table' then "
  182. " for func_name,func in pairs(class) do "
  183. " if type(func) == 'function' then "
  184. " cmds[#cmds+1] = class_name .. '.' .. func_name "
  185. " end "
  186. " end "
  187. " end "
  188. " end "
  189. " return cmds "
  190. "end";
  191. const char* LuaEnvironment::get_cmd_by_name = "function get_command_by_name(text) "
  192. " local cmds = get_all_commands() "
  193. " local results = {} "
  194. " local index = 0 "
  195. " for i,cmd in pairs(cmds) do "
  196. " if string.find(cmd, text) then "
  197. " results[index] = cmds[i] "
  198. " index = index + 1 "
  199. " end "
  200. " end "
  201. " return results "
  202. "end";
  203. const char* LuaEnvironment::tmp_print_table = "function print_table(table) "
  204. " for k,v in pairs(table) do "
  205. " print(v) "
  206. " end "
  207. "end";
  208. const char* LuaEnvironment::count_all = "function count_all(f) "
  209. " local seen = {} "
  210. " local count_table "
  211. " count_table = function(t) "
  212. " if seen[t] then return end "
  213. " f(t) "
  214. " seen[t] = true "
  215. " for k,v in pairs(t) do "
  216. " if type(v) == 'table' then "
  217. " count_table(v) "
  218. " elseif type(v) == 'userdata' then "
  219. " f(v) "
  220. " end "
  221. " end "
  222. " end "
  223. " count_table(_G) "
  224. "end";
  225. } // namespace crown