scripting_server.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #include "scripting_server.h"
  17. #include "server.h"
  18. #include "log.h"
  19. #include "settings.h"
  20. #include "cpp_api/s_internal.h"
  21. #include "lua_api/l_areastore.h"
  22. #include "lua_api/l_auth.h"
  23. #include "lua_api/l_base.h"
  24. #include "lua_api/l_craft.h"
  25. #include "lua_api/l_env.h"
  26. #include "lua_api/l_inventory.h"
  27. #include "lua_api/l_item.h"
  28. #include "lua_api/l_itemstackmeta.h"
  29. #include "lua_api/l_mapgen.h"
  30. #include "lua_api/l_modchannels.h"
  31. #include "lua_api/l_nodemeta.h"
  32. #include "lua_api/l_nodetimer.h"
  33. #include "lua_api/l_noise.h"
  34. #include "lua_api/l_object.h"
  35. #include "lua_api/l_playermeta.h"
  36. #include "lua_api/l_particles.h"
  37. #include "lua_api/l_rollback.h"
  38. #include "lua_api/l_server.h"
  39. #include "lua_api/l_util.h"
  40. #include "lua_api/l_vmanip.h"
  41. #include "lua_api/l_settings.h"
  42. #include "lua_api/l_http.h"
  43. #include "lua_api/l_storage.h"
  44. extern "C" {
  45. #include <lualib.h>
  46. }
  47. ServerScripting::ServerScripting(Server* server):
  48. ScriptApiBase(ScriptingType::Server),
  49. asyncEngine(server)
  50. {
  51. setGameDef(server);
  52. // setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
  53. // once the environment has been created
  54. SCRIPTAPI_PRECHECKHEADER
  55. if (g_settings->getBool("secure.enable_security")) {
  56. initializeSecurity();
  57. } else {
  58. warningstream << "\\!/ Mod security should never be disabled, as it allows any mod to "
  59. << "access the host machine."
  60. << "Mods should use minetest.request_insecure_environment() instead \\!/" << std::endl;
  61. }
  62. lua_getglobal(L, "core");
  63. int top = lua_gettop(L);
  64. lua_newtable(L);
  65. lua_setfield(L, -2, "object_refs");
  66. lua_newtable(L);
  67. lua_setfield(L, -2, "luaentities");
  68. // Initialize our lua_api modules
  69. InitializeModApi(L, top);
  70. lua_pop(L, 1);
  71. // Push builtin initialization type
  72. lua_pushstring(L, "game");
  73. lua_setglobal(L, "INIT");
  74. infostream << "SCRIPTAPI: Initialized game modules" << std::endl;
  75. }
  76. void ServerScripting::initAsync()
  77. {
  78. // Save globals to transfer
  79. {
  80. lua_State *L = getStack();
  81. lua_getglobal(L, "core");
  82. luaL_checktype(L, -1, LUA_TTABLE);
  83. lua_getfield(L, -1, "get_globals_to_transfer");
  84. lua_call(L, 0, 1);
  85. auto *data = script_pack(L, -1);
  86. assert(!data->contains_userdata);
  87. getServer()->m_async_globals_data.reset(data);
  88. lua_pushnil(L);
  89. lua_setfield(L, -3, "get_globals_to_transfer"); // unset function too
  90. lua_pop(L, 2); // pop 'core', return value
  91. }
  92. infostream << "SCRIPTAPI: Initializing async engine" << std::endl;
  93. asyncEngine.registerStateInitializer(InitializeAsync);
  94. asyncEngine.registerStateInitializer(ModApiUtil::InitializeAsync);
  95. asyncEngine.registerStateInitializer(ModApiCraft::InitializeAsync);
  96. asyncEngine.registerStateInitializer(ModApiItemMod::InitializeAsync);
  97. asyncEngine.registerStateInitializer(ModApiServer::InitializeAsync);
  98. // not added: ModApiMapgen is a minefield for thread safety
  99. // not added: ModApiHttp async api can't really work together with our jobs
  100. // not added: ModApiStorage is probably not thread safe(?)
  101. asyncEngine.initialize(0);
  102. }
  103. void ServerScripting::stepAsync()
  104. {
  105. asyncEngine.step(getStack());
  106. }
  107. u32 ServerScripting::queueAsync(std::string &&serialized_func,
  108. PackedValue *param, const std::string &mod_origin)
  109. {
  110. return asyncEngine.queueAsyncJob(std::move(serialized_func),
  111. param, mod_origin);
  112. }
  113. void ServerScripting::InitializeModApi(lua_State *L, int top)
  114. {
  115. // Register reference classes (userdata)
  116. InvRef::Register(L);
  117. ItemStackMetaRef::Register(L);
  118. LuaAreaStore::Register(L);
  119. LuaItemStack::Register(L);
  120. LuaPerlinNoise::Register(L);
  121. LuaPerlinNoiseMap::Register(L);
  122. LuaPseudoRandom::Register(L);
  123. LuaPcgRandom::Register(L);
  124. LuaRaycast::Register(L);
  125. LuaSecureRandom::Register(L);
  126. LuaVoxelManip::Register(L);
  127. NodeMetaRef::Register(L);
  128. NodeTimerRef::Register(L);
  129. ObjectRef::Register(L);
  130. PlayerMetaRef::Register(L);
  131. LuaSettings::Register(L);
  132. StorageRef::Register(L);
  133. ModChannelRef::Register(L);
  134. // Initialize mod api modules
  135. ModApiAuth::Initialize(L, top);
  136. ModApiCraft::Initialize(L, top);
  137. ModApiEnvMod::Initialize(L, top);
  138. ModApiInventory::Initialize(L, top);
  139. ModApiItemMod::Initialize(L, top);
  140. ModApiMapgen::Initialize(L, top);
  141. ModApiParticles::Initialize(L, top);
  142. ModApiRollback::Initialize(L, top);
  143. ModApiServer::Initialize(L, top);
  144. ModApiUtil::Initialize(L, top);
  145. ModApiHttp::Initialize(L, top);
  146. ModApiStorage::Initialize(L, top);
  147. ModApiChannels::Initialize(L, top);
  148. }
  149. void ServerScripting::InitializeAsync(lua_State *L, int top)
  150. {
  151. // classes
  152. LuaItemStack::Register(L);
  153. LuaPerlinNoise::Register(L);
  154. LuaPerlinNoiseMap::Register(L);
  155. LuaPseudoRandom::Register(L);
  156. LuaPcgRandom::Register(L);
  157. LuaSecureRandom::Register(L);
  158. LuaVoxelManip::Register(L);
  159. LuaSettings::Register(L);
  160. // globals data
  161. lua_getglobal(L, "core");
  162. luaL_checktype(L, -1, LUA_TTABLE);
  163. auto *data = ModApiBase::getServer(L)->m_async_globals_data.get();
  164. script_unpack(L, data);
  165. lua_setfield(L, -2, "transferred_globals");
  166. lua_pop(L, 1); // pop 'core'
  167. }