]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
fa369a360b53a445c65e9081df2deed1ba2f32d8
[dragonfireclient.git] / src / script / lua_api / l_client.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 GNU Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21 #include "l_client.h"
22 #include "chatmessage.h"
23 #include "client/client.h"
24 #include "client/clientevent.h"
25 #include "client/sound.h"
26 #include "client/clientenvironment.h"
27 #include "common/c_content.h"
28 #include "common/c_converter.h"
29 #include "cpp_api/s_base.h"
30 #include "gettext.h"
31 #include "l_internal.h"
32 #include "lua_api/l_item.h"
33 #include "lua_api/l_nodemeta.h"
34 #include "gui/mainmenumanager.h"
35 #include "map.h"
36 #include "util/string.h"
37 #include "nodedef.h"
38
39 #define checkCSMRestrictionFlag(flag) \
40         ( getClient(L)->checkCSMRestrictionFlag(CSMRestrictionFlags::flag) )
41
42 // Not the same as FlagDesc, which contains an `u32 flag`
43 struct CSMFlagDesc {
44         const char *name;
45         u64 flag;
46 };
47
48 /*
49         FIXME: This should eventually be moved somewhere else
50         It also needs to be kept in sync with the definition of CSMRestrictionFlags
51         in network/networkprotocol.h
52 */
53 const static CSMFlagDesc flagdesc_csm_restriction[] = {
54         {"load_client_mods",  CSM_RF_LOAD_CLIENT_MODS},
55         {"chat_messages",     CSM_RF_CHAT_MESSAGES},
56         {"read_itemdefs",     CSM_RF_READ_ITEMDEFS},
57         {"read_nodedefs",     CSM_RF_READ_NODEDEFS},
58         {"lookup_nodes",      CSM_RF_LOOKUP_NODES},
59         {"read_playerinfo",   CSM_RF_READ_PLAYERINFO},
60         {NULL,      0}
61 };
62
63 // get_current_modname()
64 int ModApiClient::l_get_current_modname(lua_State *L)
65 {
66         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
67         return 1;
68 }
69
70 // get_modpath(modname)
71 int ModApiClient::l_get_modpath(lua_State *L)
72 {
73         std::string modname = readParam<std::string>(L, 1);
74         // Client mods use a virtual filesystem, see Client::scanModSubfolder()
75         std::string path = modname + ":";
76         lua_pushstring(L, path.c_str());
77         return 1;
78 }
79
80 // get_last_run_mod()
81 int ModApiClient::l_get_last_run_mod(lua_State *L)
82 {
83         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
84         std::string current_mod = readParam<std::string>(L, -1, "");
85         if (current_mod.empty()) {
86                 lua_pop(L, 1);
87                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
88         }
89         return 1;
90 }
91
92 // set_last_run_mod(modname)
93 int ModApiClient::l_set_last_run_mod(lua_State *L)
94 {
95         if (!lua_isstring(L, 1))
96                 return 0;
97
98         const char *mod = lua_tostring(L, 1);
99         getScriptApiBase(L)->setOriginDirect(mod);
100         lua_pushboolean(L, true);
101         return 1;
102 }
103
104 // print(text)
105 int ModApiClient::l_print(lua_State *L)
106 {
107         NO_MAP_LOCK_REQUIRED;
108         std::string text = luaL_checkstring(L, 1);
109         rawstream << text << std::endl;
110         return 0;
111 }
112
113 // display_chat_message(message)
114 int ModApiClient::l_display_chat_message(lua_State *L)
115 {
116         if (!lua_isstring(L, 1))
117                 return 0;
118
119         std::string message = luaL_checkstring(L, 1);
120         getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
121         lua_pushboolean(L, true);
122         return 1;
123 }
124
125 // send_chat_message(message)
126 int ModApiClient::l_send_chat_message(lua_State *L)
127 {
128         if (!lua_isstring(L, 1))
129                 return 0;
130
131         // If server disabled this API, discard
132
133         if (checkCSMRestrictionFlag(CSM_RF_CHAT_MESSAGES))
134                 return 0;
135
136         std::string message = luaL_checkstring(L, 1);
137         getClient(L)->sendChatMessage(utf8_to_wide(message));
138         return 0;
139 }
140
141 // clear_out_chat_queue()
142 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
143 {
144         getClient(L)->clearOutChatQueue();
145         return 0;
146 }
147
148 // get_player_names()
149 int ModApiClient::l_get_player_names(lua_State *L)
150 {
151         if (checkCSMRestrictionFlag(CSM_RF_READ_PLAYERINFO))
152                 return 0;
153
154         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
155         lua_createtable(L, plist.size(), 0);
156         int newTable = lua_gettop(L);
157         int index = 1;
158         std::list<std::string>::const_iterator iter;
159         for (iter = plist.begin(); iter != plist.end(); ++iter) {
160                 lua_pushstring(L, (*iter).c_str());
161                 lua_rawseti(L, newTable, index);
162                 index++;
163         }
164         return 1;
165 }
166
167 // show_formspec(formspec)
168 int ModApiClient::l_show_formspec(lua_State *L)
169 {
170         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
171                 return 0;
172
173         ClientEvent *event = new ClientEvent();
174         event->type = CE_SHOW_LOCAL_FORMSPEC;
175         event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
176         event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
177         getClient(L)->pushToEventQueue(event);
178         lua_pushboolean(L, true);
179         return 1;
180 }
181
182 // send_respawn()
183 int ModApiClient::l_send_respawn(lua_State *L)
184 {
185         getClient(L)->sendRespawn();
186         return 0;
187 }
188
189 // disconnect()
190 int ModApiClient::l_disconnect(lua_State *L)
191 {
192         // Stops badly written Lua code form causing boot loops
193         if (getClient(L)->isShutdown()) {
194                 lua_pushboolean(L, false);
195                 return 1;
196         }
197
198         g_gamecallback->disconnect();
199         lua_pushboolean(L, true);
200         return 1;
201 }
202
203 // gettext(text)
204 int ModApiClient::l_gettext(lua_State *L)
205 {
206         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
207         lua_pushstring(L, text.c_str());
208
209         return 1;
210 }
211
212 // get_node(pos)
213 // pos = {x=num, y=num, z=num}
214 int ModApiClient::l_get_node_or_nil(lua_State *L)
215 {
216         // pos
217         v3s16 pos = read_v3s16(L, 1);
218
219         // Do it
220         bool pos_ok;
221         MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok);
222         if (pos_ok) {
223                 // Return node
224                 pushnode(L, n, getClient(L)->ndef());
225         } else {
226                 lua_pushnil(L);
227         }
228         return 1;
229 }
230
231 int ModApiClient::l_get_language(lua_State *L)
232 {
233         char *locale = setlocale(LC_ALL, "");
234         lua_pushstring(L, locale);
235         return 1;
236 }
237
238 int ModApiClient::l_get_wielded_item(lua_State *L)
239 {
240         Client *client = getClient(L);
241         LocalPlayer *player = client->getEnv().getLocalPlayer();
242         if (!player)
243                 return 0;
244
245         ItemStack selected_item;
246         player->getWieldedItem(&selected_item, nullptr);
247         LuaItemStack::create(L, selected_item);
248         return 1;
249 }
250
251 // get_meta(pos)
252 int ModApiClient::l_get_meta(lua_State *L)
253 {
254         v3s16 p = read_v3s16(L, 1);
255         NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
256         NodeMetaRef::createClient(L, meta);
257         return 1;
258 }
259
260 int ModApiClient::l_sound_play(lua_State *L)
261 {
262         ISoundManager *sound = getClient(L)->getSoundManager();
263
264         SimpleSoundSpec spec;
265         read_soundspec(L, 1, spec);
266         float gain = 1.0f;
267         float pitch = 1.0f;
268         bool looped = false;
269         s32 handle;
270
271         if (lua_istable(L, 2)) {
272                 getfloatfield(L, 2, "gain", gain);
273                 getfloatfield(L, 2, "pitch", pitch);
274                 getboolfield(L, 2, "loop", looped);
275
276                 lua_getfield(L, 2, "pos");
277                 if (!lua_isnil(L, -1)) {
278                         v3f pos = read_v3f(L, -1) * BS;
279                         lua_pop(L, 1);
280                         handle = sound->playSoundAt(
281                                         spec.name, looped, gain * spec.gain, pos, pitch);
282                         lua_pushinteger(L, handle);
283                         return 1;
284                 }
285         }
286
287         handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
288         lua_pushinteger(L, handle);
289
290         return 1;
291 }
292
293 int ModApiClient::l_sound_stop(lua_State *L)
294 {
295         u32 handle = luaL_checkinteger(L, 1);
296
297         getClient(L)->getSoundManager()->stopSound(handle);
298
299         return 0;
300 }
301
302 // get_server_info()
303 int ModApiClient::l_get_server_info(lua_State *L)
304 {
305         Client *client = getClient(L);
306         Address serverAddress = client->getServerAddress();
307         lua_newtable(L);
308         lua_pushstring(L, client->getAddressName().c_str());
309         lua_setfield(L, -2, "address");
310         lua_pushstring(L, serverAddress.serializeString().c_str());
311         lua_setfield(L, -2, "ip");
312         lua_pushinteger(L, serverAddress.getPort());
313         lua_setfield(L, -2, "port");
314         lua_pushinteger(L, client->getProtoVersion());
315         lua_setfield(L, -2, "protocol_version");
316         return 1;
317 }
318
319 // get_item_def(itemstring)
320 int ModApiClient::l_get_item_def(lua_State *L)
321 {
322         IGameDef *gdef = getGameDef(L);
323         assert(gdef);
324
325         IItemDefManager *idef = gdef->idef();
326         assert(idef);
327
328         if (checkCSMRestrictionFlag(CSM_RF_READ_ITEMDEFS))
329                 return 0;
330
331         if (!lua_isstring(L, 1))
332                 return 0;
333
334         std::string name = readParam<std::string>(L, 1);
335         if (!idef->isKnown(name))
336                 return 0;
337         const ItemDefinition &def = idef->get(name);
338
339         push_item_definition_full(L, def);
340
341         return 1;
342 }
343
344 // get_node_def(nodename)
345 int ModApiClient::l_get_node_def(lua_State *L)
346 {
347         IGameDef *gdef = getGameDef(L);
348         assert(gdef);
349
350         const NodeDefManager *ndef = gdef->ndef();
351         assert(ndef);
352
353         if (!lua_isstring(L, 1))
354                 return 0;
355
356         if (checkCSMRestrictionFlag(CSM_RF_READ_NODEDEFS))
357                 return 0;
358
359         std::string name = readParam<std::string>(L, 1);
360         const ContentFeatures &cf = ndef->get(ndef->getId(name));
361         if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
362                 return 0;
363
364         push_content_features(L, cf);
365
366         return 1;
367 }
368
369 int ModApiClient::l_get_privilege_list(lua_State *L)
370 {
371         const Client *client = getClient(L);
372         lua_newtable(L);
373         for (const std::string &priv : client->getPrivilegeList()) {
374                 lua_pushboolean(L, true);
375                 lua_setfield(L, -2, priv.c_str());
376         }
377         return 1;
378 }
379
380 // get_builtin_path()
381 int ModApiClient::l_get_builtin_path(lua_State *L)
382 {
383         lua_pushstring(L, BUILTIN_MOD_NAME ":");
384         return 1;
385 }
386
387 // get_csm_restrictions()
388 int ModApiClient::l_get_csm_restrictions(lua_State *L)
389 {
390         u64 flags = getClient(L)->getCSMRestrictionFlags();
391         const CSMFlagDesc *flagdesc = flagdesc_csm_restriction;
392
393         lua_newtable(L);
394         for (int i = 0; flagdesc[i].name; i++) {
395                 setboolfield(L, -1, flagdesc[i].name, !!(flags & flagdesc[i].flag));
396         }
397         return 1;
398 }
399
400 void ModApiClient::Initialize(lua_State *L, int top)
401 {
402         API_FCT(get_current_modname);
403         API_FCT(get_modpath);
404         API_FCT(print);
405         API_FCT(display_chat_message);
406         API_FCT(send_chat_message);
407         API_FCT(clear_out_chat_queue);
408         API_FCT(get_player_names);
409         API_FCT(set_last_run_mod);
410         API_FCT(get_last_run_mod);
411         API_FCT(show_formspec);
412         API_FCT(send_respawn);
413         API_FCT(gettext);
414         API_FCT(get_node_or_nil);
415         API_FCT(get_wielded_item);
416         API_FCT(disconnect);
417         API_FCT(get_meta);
418         API_FCT(sound_play);
419         API_FCT(sound_stop);
420         API_FCT(get_server_info);
421         API_FCT(get_item_def);
422         API_FCT(get_node_def);
423         API_FCT(get_privilege_list);
424         API_FCT(get_builtin_path);
425         API_FCT(get_language);
426         API_FCT(get_csm_restrictions);
427 }