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