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