lua_environment.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. * Copyright (c) 2012-2014 Daniele Bartolini and individual contributors.
  3. * License: https://github.com/taylor001/crown/blob/master/LICENSE
  4. */
  5. #include "config.h"
  6. #include "assert.h"
  7. #include "lua_environment.h"
  8. #include "lua_stack.h"
  9. #include "lua_resource.h"
  10. #include <stdarg.h>
  11. namespace crown
  12. {
  13. namespace lua_globals { extern int error_handler(lua_State*); }
  14. LuaEnvironment::LuaEnvironment(lua_State* L)
  15. : _L(L)
  16. {
  17. }
  18. void LuaEnvironment::execute(const LuaResource* lr)
  19. {
  20. using namespace lua_resource;
  21. lua_pushcfunction(_L, lua_globals::error_handler);
  22. luaL_loadbuffer(_L, program(lr), size(lr), "<unknown>");
  23. lua_pcall(_L, 0, 0, -2);
  24. lua_pop(_L, 1);
  25. }
  26. void LuaEnvironment::execute_string(const char* s)
  27. {
  28. lua_pushcfunction(_L, lua_globals::error_handler);
  29. luaL_loadstring(_L, s);
  30. lua_pcall(_L, 0, 0, -2);
  31. lua_pop(_L, 1);
  32. }
  33. void LuaEnvironment::load_module_function(const char* module, const char* name, const lua_CFunction func)
  34. {
  35. luaL_newmetatable(_L, module);
  36. luaL_Reg entry[2];
  37. entry[0].name = name;
  38. entry[0].func = func;
  39. entry[1].name = NULL;
  40. entry[1].func = NULL;
  41. luaL_register(_L, NULL, entry);
  42. lua_setglobal(_L, module);
  43. lua_pop(_L, -1);
  44. }
  45. void LuaEnvironment::load_module_function(const char* module, const char* name, const char* value)
  46. {
  47. luaL_newmetatable(_L, module);
  48. lua_getglobal(_L, value);
  49. lua_setfield(_L, -2, name);
  50. lua_setglobal(_L, module);
  51. }
  52. void LuaEnvironment::load_module_constructor(const char* module, const lua_CFunction func)
  53. {
  54. // Create dummy tables to be used as module's metatable
  55. lua_createtable(_L, 0, 1);
  56. lua_pushstring(_L, "__call");
  57. lua_pushcfunction(_L, func);
  58. lua_settable(_L, 1); // dummy.__call = func
  59. lua_getglobal(_L, module);
  60. lua_pushvalue(_L, -2); // Duplicate dummy metatable
  61. lua_setmetatable(_L, -2); // setmetatable(module, dummy)
  62. lua_pop(_L, -1);
  63. }
  64. void LuaEnvironment::load_module_enum(const char* module, const char* name, uint32_t value)
  65. {
  66. // Checks table existance
  67. lua_pushstring(_L, module);
  68. lua_rawget(_L, LUA_GLOBALSINDEX);
  69. if (!lua_istable(_L, -1)) // If not exixts
  70. {
  71. // Creates table
  72. lua_newtable(_L);
  73. lua_setglobal(_L, module);
  74. }
  75. // Adds field to table
  76. lua_getglobal(_L, module);
  77. lua_pushinteger(_L, value);
  78. lua_setfield(_L, -2, name);
  79. lua_pop(_L, 2);
  80. }
  81. void LuaEnvironment::call_global(const char* func, uint8_t argc, ...)
  82. {
  83. CE_ASSERT_NOT_NULL(func);
  84. LuaStack stack(_L);
  85. va_list vl;
  86. va_start(vl, argc);
  87. lua_pushcfunction(_L, lua_globals::error_handler);
  88. lua_getglobal(_L, func);
  89. for (uint8_t i = 0; i < argc; i++)
  90. {
  91. const int type = va_arg(vl, int);
  92. switch (type)
  93. {
  94. case ARGUMENT_FLOAT:
  95. {
  96. stack.push_float(va_arg(vl, double));
  97. break;
  98. }
  99. default:
  100. {
  101. CE_ASSERT(false, "Oops, lua argument unknown");
  102. break;
  103. }
  104. }
  105. }
  106. va_end(vl);
  107. lua_pcall(_L, argc, 0, -argc - 2);
  108. lua_pop(_L, -1);
  109. }
  110. 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)
  111. {
  112. LuaStack stack(_L);
  113. lua_pushcfunction(_L, lua_globals::error_handler);
  114. lua_getglobal(_L, "g_physics_callback");
  115. stack.push_table();
  116. stack.push_key_begin("actor_0"); (actor_0 ? stack.push_actor(actor_0) : stack.push_nil()); stack.push_key_end();
  117. stack.push_key_begin("actor_1"); (actor_1 ? stack.push_actor(actor_1) : stack.push_nil()); stack.push_key_end();
  118. stack.push_key_begin("unit_0"); (unit_0 ? stack.push_unit(unit_0) : stack.push_nil()); stack.push_key_end();
  119. stack.push_key_begin("unit_1"); (unit_1 ? stack.push_unit(unit_1) : stack.push_nil()); stack.push_key_end();
  120. stack.push_key_begin("where"); stack.push_vector3(where); stack.push_key_end();
  121. stack.push_key_begin("normal"); stack.push_vector3(normal); stack.push_key_end();
  122. stack.push_key_begin("type"); stack.push_string(type); stack.push_key_end();
  123. lua_pcall(_L, 1, 0, -3);
  124. lua_pop(_L, -1);
  125. }
  126. void LuaEnvironment::call_trigger_callback(Actor* trigger, Actor* other, const char* type)
  127. {
  128. LuaStack stack(_L);
  129. lua_pushcfunction(_L, lua_globals::error_handler);
  130. lua_getglobal(_L, "g_trigger_callback");
  131. stack.push_table();
  132. stack.push_key_begin("trigger"); (trigger ? stack.push_actor(trigger) : stack.push_nil()); stack.push_key_end();
  133. stack.push_key_begin("other"); (other ? stack.push_actor(other) : stack.push_nil()); stack.push_key_end();
  134. stack.push_key_begin("type"); stack.push_string(type); stack.push_key_end();
  135. lua_pcall(_L, 1, 0, -3);
  136. lua_pop(_L, -1);
  137. }
  138. } // namespace crown