LuaEnvironment.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 "Device.h"
  24. #include "OS.h"
  25. #include "Assert.h"
  26. #include "Log.h"
  27. #include "LuaEnvironment.h"
  28. #include "StringSetting.h"
  29. #include "Filesystem.h"
  30. namespace crown
  31. {
  32. StringSetting g_boot("boot_file", "lua main file", "lua/game.raw");
  33. /*
  34. *N.B: Lua garbage collection is actually disabled
  35. */
  36. //-----------------------------------------------------------------------------
  37. LuaEnvironment::LuaEnvironment() :
  38. m_state(luaL_newstate()),
  39. m_is_used(false)
  40. //m_thread(LuaEnvironment::background_thread, (void*)this, "lua-environment-thread")
  41. {
  42. // Open Lua default libraries
  43. string::strncpy(m_error_buffer, "", 1024);
  44. string::strncpy(m_tmp_buffer, "", 1024);
  45. }
  46. //-----------------------------------------------------------------------------
  47. void LuaEnvironment::init()
  48. {
  49. // Open default libraries
  50. luaL_openlibs(m_state);
  51. // Open Crown library
  52. lua_cpcall(m_state, luaopen_libcrown, NULL);
  53. load_buffer(class_system, string::strlen(class_system));
  54. execute(0, 0);
  55. load_buffer(commands_list, string::strlen(commands_list));
  56. execute(0, 0);
  57. load_buffer(get_cmd_by_name, string::strlen(get_cmd_by_name));
  58. execute(0, 0);
  59. load_buffer(tmp_print_table, string::strlen(tmp_print_table));
  60. execute(0, 0);
  61. load_buffer(count_all, string::strlen(count_all));
  62. execute(0, 0);
  63. m_is_used = true;
  64. }
  65. //-----------------------------------------------------------------------------
  66. void LuaEnvironment::shutdown()
  67. {
  68. lua_close(m_state);
  69. m_is_used = false;
  70. }
  71. //-----------------------------------------------------------------------------
  72. lua_State* LuaEnvironment::state()
  73. {
  74. return m_state;
  75. }
  76. //-----------------------------------------------------------------------------
  77. const char* LuaEnvironment::error()
  78. {
  79. string::strncpy(m_tmp_buffer, m_error_buffer, 1024);
  80. string::strncpy(m_error_buffer, "", 1024);
  81. return m_tmp_buffer;
  82. }
  83. //-----------------------------------------------------------------------------
  84. void LuaEnvironment::load_buffer(const char* buffer, size_t len)
  85. {
  86. int32_t loaded = luaL_loadbuffer(m_state, buffer, len, "");
  87. if (loaded != 0)
  88. {
  89. lua_error();
  90. }
  91. }
  92. //-----------------------------------------------------------------------------
  93. void LuaEnvironment::load_file(const char* file)
  94. {
  95. int32_t loaded = luaL_loadfile(m_state, file);
  96. if (loaded != 0)
  97. {
  98. lua_error();
  99. }
  100. }
  101. //-----------------------------------------------------------------------------
  102. void LuaEnvironment::load_string(const char* str)
  103. {
  104. int32_t loaded = luaL_loadstring(m_state, str);
  105. if (loaded != 0)
  106. {
  107. lua_error();
  108. }
  109. }
  110. //-----------------------------------------------------------------------------
  111. void LuaEnvironment::get_global_symbol(const char* symbol)
  112. {
  113. lua_getglobal(m_state, symbol);
  114. }
  115. //-----------------------------------------------------------------------------
  116. void LuaEnvironment::execute(int32_t args, int32_t results)
  117. {
  118. int32_t executed = lua_pcall(m_state, args, results, 0);
  119. if (executed != 0)
  120. {
  121. lua_error();
  122. }
  123. }
  124. // //-----------------------------------------------------------------------------
  125. // void LuaEnvironment::collect_garbage()
  126. // {
  127. // uint64_t start = os::milliseconds();
  128. // while ((os::milliseconds() - start) < device()->last_delta_time() && !m_is_used)
  129. // {
  130. // lua_gc(m_state, LUA_GCSTEP, 0);
  131. // }
  132. // }
  133. // //-----------------------------------------------------------------------------
  134. // void* LuaEnvironment::background_thread(void* thiz)
  135. // {
  136. // ((LuaEnvironment*)thiz)->collect_garbage();
  137. // }
  138. //-----------------------------------------------------------------------------
  139. void LuaEnvironment::game_init()
  140. {
  141. // const char* path = device()->filesystem()->os_path("disk", g_boot.value());
  142. // load_file(path);
  143. // execute(0, 0);
  144. // get_global_symbol("init");
  145. // execute(0, 0);
  146. }
  147. //-----------------------------------------------------------------------------
  148. void LuaEnvironment::game_shutdown()
  149. {
  150. // get_global_symbol("shutdown");
  151. // execute(0, 0);
  152. }
  153. //-----------------------------------------------------------------------------
  154. void LuaEnvironment::game_frame(float dt)
  155. {
  156. // LuaStack stack(m_state);
  157. // get_global_symbol("frame");
  158. // stack.push_float(dt);
  159. // execute(1, 0);
  160. (void)dt;
  161. }
  162. //-----------------------------------------------------------------------------
  163. void LuaEnvironment::lua_error()
  164. {
  165. string::strncpy(m_error_buffer, "", 1024);
  166. string::strncpy(m_error_buffer, lua_tostring(m_state, -1), 1024);
  167. }
  168. //-----------------------------------------------------------------------------
  169. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  170. {
  171. luaL_Reg entry[2];
  172. entry[0].name = name;
  173. entry[0].func = func;
  174. entry[1].name = NULL;
  175. entry[1].func = NULL;
  176. luaL_register(m_state, module, entry);
  177. }
  178. //-----------------------------------------------------------
  179. void LuaEnvironment::load_module_enum(const char* /*module*/, const char* name, uint32_t value)
  180. {
  181. lua_pushinteger(m_state, value);
  182. lua_setfield(m_state, -2, name);
  183. }
  184. //-----------------------------------------------------------------------------
  185. CE_EXPORT int32_t luaopen_libcrown(lua_State* /*L*/)
  186. {
  187. LuaEnvironment* env = device()->lua_environment();
  188. load_int_setting(*env);
  189. load_float_setting(*env);
  190. load_string_setting(*env);
  191. load_vec2(*env);
  192. load_vec3(*env);
  193. load_mat4(*env);
  194. load_quat(*env);
  195. load_math(*env);
  196. load_mouse(*env);
  197. load_keyboard(*env);
  198. load_accelerometer(*env);
  199. load_device(*env);
  200. load_window(*env);
  201. return 1;
  202. }
  203. const char* LuaEnvironment::class_system = "function class(klass, super) "
  204. " if not klass then "
  205. " klass = {} "
  206. " local meta = {} "
  207. " meta.__call = function(self, ...) "
  208. " local object = {} "
  209. " setmetatable(object, klass) "
  210. " if object.init then object:init(...) end "
  211. " return object "
  212. " end "
  213. " setmetatable(klass, meta) "
  214. " end "
  215. " if super then "
  216. " for k,v in pairs(super) do "
  217. " klass[k] = v "
  218. " end "
  219. " end "
  220. " klass.__index = klass "
  221. " return klass "
  222. "end";
  223. const char* LuaEnvironment::commands_list = "function get_all_commands() "
  224. " local cmds = {}; "
  225. " for class_name,class in pairs(_G) do "
  226. " if type(class) == 'table' then "
  227. " for func_name,func in pairs(class) do "
  228. " if type(func) == 'function' then "
  229. " cmds[#cmds+1] = class_name .. '.' .. func_name "
  230. " end "
  231. " end "
  232. " end "
  233. " end "
  234. " return cmds "
  235. "end";
  236. const char* LuaEnvironment::get_cmd_by_name = "function get_command_by_name(text) "
  237. " local cmds = get_all_commands() "
  238. " local results = {} "
  239. " local index = 0 "
  240. " for i,cmd in pairs(cmds) do "
  241. " if string.find(cmd, text) then "
  242. " results[index] = cmds[i] "
  243. " index = index + 1 "
  244. " end "
  245. " end "
  246. " return results "
  247. "end";
  248. const char* LuaEnvironment::tmp_print_table = "function print_table(table) "
  249. " for k,v in pairs(table) do "
  250. " print(v) "
  251. " end "
  252. "end";
  253. const char* LuaEnvironment::count_all = "function count_all(f) "
  254. " local seen = {} "
  255. " local count_table "
  256. " count_table = function(t) "
  257. " if seen[t] then return end "
  258. " f(t) "
  259. " seen[t] = true "
  260. " for k,v in pairs(t) do "
  261. " if type(v) == 'table' then "
  262. " count_table(v) "
  263. " elseif type(v) == 'userdata' then "
  264. " f(v) "
  265. " end "
  266. " end "
  267. " end "
  268. " count_table(_G) "
  269. "end";
  270. } // namespace crown