]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/scripting_server.cpp
Use native packer to transfer globals into async env(s)
[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                 auto *data = script_pack(L, -1);
102                 assert(!data->contains_userdata);
103                 getServer()->m_async_globals_data.reset(data);
104                 lua_pushnil(L);
105                 lua_setfield(L, -3, "get_globals_to_transfer"); // unset function too
106                 lua_pop(L, 2); // pop 'core', return value
107         }
108
109         infostream << "SCRIPTAPI: Initializing async engine" << std::endl;
110         asyncEngine.registerStateInitializer(InitializeAsync);
111         asyncEngine.registerStateInitializer(ModApiUtil::InitializeAsync);
112         asyncEngine.registerStateInitializer(ModApiCraft::InitializeAsync);
113         asyncEngine.registerStateInitializer(ModApiItemMod::InitializeAsync);
114         asyncEngine.registerStateInitializer(ModApiServer::InitializeAsync);
115         // not added: ModApiMapgen is a minefield for thread safety
116         // not added: ModApiHttp async api can't really work together with our jobs
117         // not added: ModApiStorage is probably not thread safe(?)
118
119         asyncEngine.initialize(0);
120 }
121
122 void ServerScripting::stepAsync()
123 {
124         asyncEngine.step(getStack());
125 }
126
127 u32 ServerScripting::queueAsync(std::string &&serialized_func,
128         PackedValue *param, const std::string &mod_origin)
129 {
130         return asyncEngine.queueAsyncJob(std::move(serialized_func),
131                         param, mod_origin);
132 }
133
134 void ServerScripting::InitializeModApi(lua_State *L, int top)
135 {
136         // Register reference classes (userdata)
137         InvRef::Register(L);
138         ItemStackMetaRef::Register(L);
139         LuaAreaStore::Register(L);
140         LuaItemStack::Register(L);
141         LuaPerlinNoise::Register(L);
142         LuaPerlinNoiseMap::Register(L);
143         LuaPseudoRandom::Register(L);
144         LuaPcgRandom::Register(L);
145         LuaRaycast::Register(L);
146         LuaSecureRandom::Register(L);
147         LuaVoxelManip::Register(L);
148         NodeMetaRef::Register(L);
149         NodeTimerRef::Register(L);
150         ObjectRef::Register(L);
151         PlayerMetaRef::Register(L);
152         LuaSettings::Register(L);
153         StorageRef::Register(L);
154         ModChannelRef::Register(L);
155
156         // Initialize mod api modules
157         ModApiAuth::Initialize(L, top);
158         ModApiCraft::Initialize(L, top);
159         ModApiEnvMod::Initialize(L, top);
160         ModApiInventory::Initialize(L, top);
161         ModApiItemMod::Initialize(L, top);
162         ModApiMapgen::Initialize(L, top);
163         ModApiParticles::Initialize(L, top);
164         ModApiRollback::Initialize(L, top);
165         ModApiServer::Initialize(L, top);
166         ModApiUtil::Initialize(L, top);
167         ModApiHttp::Initialize(L, top);
168         ModApiStorage::Initialize(L, top);
169         ModApiChannels::Initialize(L, top);
170 }
171
172 void ServerScripting::InitializeAsync(lua_State *L, int top)
173 {
174         // classes
175         LuaItemStack::Register(L);
176         LuaPerlinNoise::Register(L);
177         LuaPerlinNoiseMap::Register(L);
178         LuaPseudoRandom::Register(L);
179         LuaPcgRandom::Register(L);
180         LuaSecureRandom::Register(L);
181         LuaVoxelManip::Register(L);
182         LuaSettings::Register(L);
183
184         // globals data
185         lua_getglobal(L, "core");
186         luaL_checktype(L, -1, LUA_TTABLE);
187         auto *data = ModApiBase::getServer(L)->m_async_globals_data.get();
188         script_unpack(L, data);
189         lua_setfield(L, -2, "transferred_globals");
190         lua_pop(L, 1); // pop 'core'
191 }