]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/scripting_server.cpp
5b99468dcc28c7d7b9d6f93a992c1eef0257e6f7
[dragonfireclient.git] / src / script / scripting_server.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
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.
9
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.
14
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.
18 */
19
20 #include "scripting_server.h"
21 #include "server.h"
22 #include "log.h"
23 #include "settings.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"
48
49 extern "C" {
50 #include <lualib.h>
51 }
52
53 ServerScripting::ServerScripting(Server* server):
54                 ScriptApiBase(ScriptingType::Server),
55                 asyncEngine(server)
56 {
57         setGameDef(server);
58
59         // setEnv(env) is called by ScriptApiEnv::initializeEnvironment()
60         // once the environment has been created
61
62         SCRIPTAPI_PRECHECKHEADER
63
64         if (g_settings->getBool("secure.enable_security")) {
65                 initializeSecurity();
66         } else {
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;
70         }
71
72         lua_getglobal(L, "core");
73         int top = lua_gettop(L);
74
75         lua_newtable(L);
76         lua_setfield(L, -2, "object_refs");
77
78         lua_newtable(L);
79         lua_setfield(L, -2, "luaentities");
80
81         // Initialize our lua_api modules
82         InitializeModApi(L, top);
83         lua_pop(L, 1);
84
85         // Push builtin initialization type
86         lua_pushstring(L, "game");
87         lua_setglobal(L, "INIT");
88
89         infostream << "SCRIPTAPI: Initialized game modules" << std::endl;
90 }
91
92 void ServerScripting::initAsync()
93 {
94         // Save globals to transfer
95         {
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");
100                 lua_call(L, 0, 1);
101                 luaL_checktype(L, -1, LUA_TSTRING);
102                 getServer()->m_async_globals_data.set(readParam<std::string>(L, -1));
103                 lua_pushnil(L);
104                 lua_setfield(L, -3, "get_globals_to_transfer"); // unset function too
105                 lua_pop(L, 2); // pop 'core', return value
106         }
107
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(?)
117
118         asyncEngine.initialize(0);
119 }
120
121 void ServerScripting::stepAsync()
122 {
123         asyncEngine.step(getStack());
124 }
125
126 u32 ServerScripting::queueAsync(std::string &&serialized_func,
127         PackedValue *param, const std::string &mod_origin)
128 {
129         return asyncEngine.queueAsyncJob(std::move(serialized_func),
130                         param, mod_origin);
131 }
132
133 void ServerScripting::InitializeModApi(lua_State *L, int top)
134 {
135         // Register reference classes (userdata)
136         InvRef::Register(L);
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);
154
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);
169 }
170
171 void ServerScripting::InitializeAsync(lua_State *L, int top)
172 {
173         // classes
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);
182
183         // globals data
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'
190 }