lua_environment.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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 "lua_environment.h"
  25. #include "assert.h"
  26. #include "string_utils.h"
  27. #include "lua_stack.h"
  28. #include "device.h"
  29. #include "lua_resource.h"
  30. #include "resource_manager.h"
  31. #include "config.h"
  32. namespace crown
  33. {
  34. namespace lua_system { extern int error_handler(lua_State*); }
  35. //-----------------------------------------------------------------------------
  36. LuaEnvironment::LuaEnvironment(lua_State* L)
  37. : m_L(L)
  38. {
  39. }
  40. //-----------------------------------------------------------------------------
  41. void LuaEnvironment::load_and_execute(const char* res_name)
  42. {
  43. ResourceManager* resman = device()->resource_manager();
  44. // Load the resource
  45. const ResourceId res_id("lua", res_name);
  46. resman->load(res_id);
  47. resman->flush();
  48. LuaResource* lr = (LuaResource*) resman->get(res_id);
  49. lua_pushcfunction(m_L, lua_system::error_handler);
  50. luaL_loadbuffer(m_L, (const char*) lr->program(), lr->size(), res_name);
  51. lua_pcall(m_L, 0, 0, -2);
  52. // Unloading is OK since the script data has been copied to Lua
  53. resman->unload(res_id);
  54. }
  55. //-----------------------------------------------------------------------------
  56. void LuaEnvironment::execute_string(const char* s)
  57. {
  58. lua_pushcfunction(m_L, lua_system::error_handler);
  59. luaL_loadstring(m_L, s);
  60. lua_pcall(m_L, 0, 0, -2);
  61. }
  62. //-----------------------------------------------------------------------------
  63. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  64. {
  65. luaL_newmetatable(m_L, module);
  66. luaL_Reg entry[2];
  67. entry[0].name = name;
  68. entry[0].func = func;
  69. entry[1].name = NULL;
  70. entry[1].func = NULL;
  71. luaL_register(m_L, NULL, entry);
  72. lua_setglobal(m_L, module);
  73. lua_pop(m_L, -1);
  74. }
  75. //-----------------------------------------------------------------------------
  76. void LuaEnvironment::load_module_function(const char* module, const char* name, const char* value)
  77. {
  78. luaL_newmetatable(m_L, module);
  79. lua_getglobal(m_L, value);
  80. lua_setfield(m_L, -2, name);
  81. lua_setglobal(m_L, module);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void LuaEnvironment::load_module_constructor(const char* module, const lua_CFunction func)
  85. {
  86. // Create dummy tables to be used as module's metatable
  87. lua_createtable(m_L, 0, 1);
  88. lua_pushstring(m_L, "__call");
  89. lua_pushcfunction(m_L, func);
  90. lua_settable(m_L, 1); // dummy.__call = func
  91. lua_getglobal(m_L, module);
  92. lua_pushvalue(m_L, -2); // Duplicate dummy metatable
  93. lua_setmetatable(m_L, -2); // setmetatable(module, dummy)
  94. lua_pop(m_L, -1);
  95. }
  96. //-----------------------------------------------------------------------------
  97. void LuaEnvironment::load_module_enum(const char* module, const char* name, uint32_t value)
  98. {
  99. // Checks table existance
  100. lua_pushstring(m_L, module);
  101. lua_rawget(m_L, LUA_GLOBALSINDEX);
  102. if (!lua_istable(m_L, -1)) // If not exixts
  103. {
  104. // Creates table
  105. lua_newtable(m_L);
  106. lua_setglobal(m_L, module);
  107. }
  108. // Adds field to table
  109. lua_getglobal(m_L, module);
  110. lua_pushinteger(m_L, value);
  111. lua_setfield(m_L, -2, name);
  112. }
  113. //-----------------------------------------------------------------------------
  114. void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  115. {
  116. CE_ASSERT_NOT_NULL(func);
  117. LuaStack stack(m_L);
  118. va_list vl;
  119. va_start(vl, argc);
  120. lua_pushcfunction(m_L, lua_system::error_handler);
  121. lua_getglobal(m_L, func);
  122. for (uint8_t i = 0; i < argc; i++)
  123. {
  124. const int type = va_arg(vl, int);
  125. switch (type)
  126. {
  127. case ARGUMENT_FLOAT:
  128. {
  129. stack.push_float(va_arg(vl, double));
  130. break;
  131. }
  132. default:
  133. {
  134. CE_ASSERT(false, "Oops, lua argument unknown");
  135. break;
  136. }
  137. }
  138. }
  139. va_end(vl);
  140. lua_pcall(m_L, argc, 0, -argc - 2);
  141. }
  142. //-----------------------------------------------------------------------------
  143. void LuaEnvironment::call_physics_callback(Actor* actor_0, Actor* actor_1, Unit* unit_0, Unit* unit_1, const Vector3& where, const Vector3& normal, const char* type)
  144. {
  145. LuaStack stack(m_L);
  146. lua_pushcfunction(m_L, lua_system::error_handler);
  147. lua_getglobal(m_L, "g_physics_callback");
  148. stack.push_table();
  149. stack.push_key_begin("actor_0"); (actor_0 ? stack.push_actor(actor_0) : stack.push_nil()); stack.push_key_end();
  150. stack.push_key_begin("actor_1"); (actor_1 ? stack.push_actor(actor_1) : stack.push_nil()); stack.push_key_end();
  151. stack.push_key_begin("unit_0"); (unit_0 ? stack.push_unit(unit_0) : stack.push_nil()); stack.push_key_end();
  152. stack.push_key_begin("unit_1"); (unit_1 ? stack.push_unit(unit_1) : stack.push_nil()); stack.push_key_end();
  153. stack.push_key_begin("where"); stack.push_vector3(where); stack.push_key_end();
  154. stack.push_key_begin("normal"); stack.push_vector3(normal); stack.push_key_end();
  155. stack.push_key_begin("type"); stack.push_string(type); stack.push_key_end();
  156. lua_pcall(m_L, 1, 0, -3);
  157. }
  158. //-----------------------------------------------------------------------------
  159. void LuaEnvironment::call_trigger_callback(Actor* trigger, Actor* other, const char* type)
  160. {
  161. LuaStack stack(m_L);
  162. lua_pushcfunction(m_L, lua_system::error_handler);
  163. lua_getglobal(m_L, "g_trigger_callback");
  164. stack.push_table();
  165. stack.push_key_begin("trigger"); (trigger ? stack.push_actor(trigger) : stack.push_nil()); stack.push_key_end();
  166. stack.push_key_begin("other"); (other ? stack.push_actor(other) : stack.push_nil()); stack.push_key_end();
  167. stack.push_key_begin("type"); stack.push_string(type); stack.push_key_end();
  168. lua_pcall(m_L, 1, 0, -3);
  169. }
  170. } // namespace crown