]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_client.cpp
Modernize lua read (part 2 & 3): C++ templating assurance (#7410)
[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.h"
24 #include "client/clientevent.h"
25 #include "client/sound.h"
26 #include "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 int ModApiClient::l_get_current_modname(lua_State *L)
40 {
41         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
42         return 1;
43 }
44
45 // get_last_run_mod()
46 int ModApiClient::l_get_last_run_mod(lua_State *L)
47 {
48         lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
49         std::string current_mod = readParam<std::string>(L, -1, "");
50         if (current_mod.empty()) {
51                 lua_pop(L, 1);
52                 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
53         }
54         return 1;
55 }
56
57 // set_last_run_mod(modname)
58 int ModApiClient::l_set_last_run_mod(lua_State *L)
59 {
60         if (!lua_isstring(L, 1))
61                 return 0;
62
63         const char *mod = lua_tostring(L, 1);
64         getScriptApiBase(L)->setOriginDirect(mod);
65         lua_pushboolean(L, true);
66         return 1;
67 }
68
69 // print(text)
70 int ModApiClient::l_print(lua_State *L)
71 {
72         NO_MAP_LOCK_REQUIRED;
73         std::string text = luaL_checkstring(L, 1);
74         rawstream << text << std::endl;
75         return 0;
76 }
77
78 // display_chat_message(message)
79 int ModApiClient::l_display_chat_message(lua_State *L)
80 {
81         if (!lua_isstring(L, 1))
82                 return 0;
83
84         std::string message = luaL_checkstring(L, 1);
85         getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
86         lua_pushboolean(L, true);
87         return 1;
88 }
89
90 // send_chat_message(message)
91 int ModApiClient::l_send_chat_message(lua_State *L)
92 {
93         if (!lua_isstring(L, 1))
94                 return 0;
95
96         // If server disabled this API, discard
97
98         // clang-format off
99         if (getClient(L)->checkCSMRestrictionFlag(
100                         CSMRestrictionFlags::CSM_RF_CHAT_MESSAGES))
101                 return 0;
102         // clang-format on
103
104         std::string message = luaL_checkstring(L, 1);
105         getClient(L)->sendChatMessage(utf8_to_wide(message));
106         return 0;
107 }
108
109 // clear_out_chat_queue()
110 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
111 {
112         getClient(L)->clearOutChatQueue();
113         return 0;
114 }
115
116 // get_player_names()
117 int ModApiClient::l_get_player_names(lua_State *L)
118 {
119         const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
120         lua_createtable(L, plist.size(), 0);
121         int newTable = lua_gettop(L);
122         int index = 1;
123         std::list<std::string>::const_iterator iter;
124         for (iter = plist.begin(); iter != plist.end(); ++iter) {
125                 lua_pushstring(L, (*iter).c_str());
126                 lua_rawseti(L, newTable, index);
127                 index++;
128         }
129         return 1;
130 }
131
132 // show_formspec(formspec)
133 int ModApiClient::l_show_formspec(lua_State *L)
134 {
135         if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
136                 return 0;
137
138         ClientEvent *event = new ClientEvent();
139         event->type = CE_SHOW_LOCAL_FORMSPEC;
140         event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
141         event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
142         getClient(L)->pushToEventQueue(event);
143         lua_pushboolean(L, true);
144         return 1;
145 }
146
147 // send_respawn()
148 int ModApiClient::l_send_respawn(lua_State *L)
149 {
150         getClient(L)->sendRespawn();
151         return 0;
152 }
153
154 // disconnect()
155 int ModApiClient::l_disconnect(lua_State *L)
156 {
157         // Stops badly written Lua code form causing boot loops
158         if (getClient(L)->isShutdown()) {
159                 lua_pushboolean(L, false);
160                 return 1;
161         }
162
163         g_gamecallback->disconnect();
164         lua_pushboolean(L, true);
165         return 1;
166 }
167
168 // gettext(text)
169 int ModApiClient::l_gettext(lua_State *L)
170 {
171         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
172         lua_pushstring(L, text.c_str());
173
174         return 1;
175 }
176
177 // get_node(pos)
178 // pos = {x=num, y=num, z=num}
179 int ModApiClient::l_get_node_or_nil(lua_State *L)
180 {
181         // pos
182         v3s16 pos = read_v3s16(L, 1);
183
184         // Do it
185         bool pos_ok;
186         MapNode n = getClient(L)->getNode(pos, &pos_ok);
187         if (pos_ok) {
188                 // Return node
189                 pushnode(L, n, getClient(L)->ndef());
190         } else {
191                 lua_pushnil(L);
192         }
193         return 1;
194 }
195
196 int ModApiClient::l_get_language(lua_State *L)
197 {
198         char *locale = setlocale(LC_ALL, "");
199         lua_pushstring(L, locale);
200         return 1;
201 }
202
203 int ModApiClient::l_get_wielded_item(lua_State *L)
204 {
205         Client *client = getClient(L);
206
207         Inventory local_inventory(client->idef());
208         client->getLocalInventory(local_inventory);
209
210         InventoryList *mlist = local_inventory.getList("main");
211
212         if (mlist && client->getPlayerItem() < mlist->getSize()) {
213                 LuaItemStack::create(L, mlist->getItem(client->getPlayerItem()));
214         } else {
215                 LuaItemStack::create(L, ItemStack());
216         }
217         return 1;
218 }
219
220 // get_meta(pos)
221 int ModApiClient::l_get_meta(lua_State *L)
222 {
223         v3s16 p = read_v3s16(L, 1);
224         NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
225         NodeMetaRef::createClient(L, meta);
226         return 1;
227 }
228
229 int ModApiClient::l_sound_play(lua_State *L)
230 {
231         ISoundManager *sound = getClient(L)->getSoundManager();
232
233         SimpleSoundSpec spec;
234         read_soundspec(L, 1, spec);
235         float gain = 1.0f;
236         float pitch = 1.0f;
237         bool looped = false;
238         s32 handle;
239
240         if (lua_istable(L, 2)) {
241                 getfloatfield(L, 2, "gain", gain);
242                 getfloatfield(L, 2, "pitch", pitch);
243                 getboolfield(L, 2, "loop", looped);
244
245                 lua_getfield(L, 2, "pos");
246                 if (!lua_isnil(L, -1)) {
247                         v3f pos = read_v3f(L, -1) * BS;
248                         lua_pop(L, 1);
249                         handle = sound->playSoundAt(
250                                         spec.name, looped, gain * spec.gain, pos, pitch);
251                         lua_pushinteger(L, handle);
252                         return 1;
253                 }
254         }
255
256         handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
257         lua_pushinteger(L, handle);
258
259         return 1;
260 }
261
262 int ModApiClient::l_sound_stop(lua_State *L)
263 {
264         u32 handle = luaL_checkinteger(L, 1);
265
266         getClient(L)->getSoundManager()->stopSound(handle);
267
268         return 0;
269 }
270
271 // get_server_info()
272 int ModApiClient::l_get_server_info(lua_State *L)
273 {
274         Client *client = getClient(L);
275         Address serverAddress = client->getServerAddress();
276         lua_newtable(L);
277         lua_pushstring(L, client->getAddressName().c_str());
278         lua_setfield(L, -2, "address");
279         lua_pushstring(L, serverAddress.serializeString().c_str());
280         lua_setfield(L, -2, "ip");
281         lua_pushinteger(L, serverAddress.getPort());
282         lua_setfield(L, -2, "port");
283         lua_pushinteger(L, client->getProtoVersion());
284         lua_setfield(L, -2, "protocol_version");
285         return 1;
286 }
287
288 // get_item_def(itemstring)
289 int ModApiClient::l_get_item_def(lua_State *L)
290 {
291         IGameDef *gdef = getGameDef(L);
292         assert(gdef);
293
294         IItemDefManager *idef = gdef->idef();
295         assert(idef);
296
297         // clang-format off
298         if (getClient(L)->checkCSMRestrictionFlag(
299                         CSMRestrictionFlags::CSM_RF_READ_ITEMDEFS))
300                 return 0;
301         // clang-format on
302
303         if (!lua_isstring(L, 1))
304                 return 0;
305
306         std::string name = readParam<std::string>(L, 1);
307         if (!idef->isKnown(name))
308                 return 0;
309         const ItemDefinition &def = idef->get(name);
310
311         push_item_definition_full(L, def);
312
313         return 1;
314 }
315
316 // get_node_def(nodename)
317 int ModApiClient::l_get_node_def(lua_State *L)
318 {
319         IGameDef *gdef = getGameDef(L);
320         assert(gdef);
321
322         const NodeDefManager *ndef = gdef->ndef();
323         assert(ndef);
324
325         if (!lua_isstring(L, 1))
326                 return 0;
327
328         // clang-format off
329         if (getClient(L)->checkCSMRestrictionFlag(
330                         CSMRestrictionFlags::CSM_RF_READ_NODEDEFS))
331                 return 0;
332         // clang-format on
333
334         std::string name = readParam<std::string>(L, 1);
335         const ContentFeatures &cf = ndef->get(ndef->getId(name));
336         if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
337                 return 0;
338
339         push_content_features(L, cf);
340
341         return 1;
342 }
343
344 int ModApiClient::l_get_privilege_list(lua_State *L)
345 {
346         const Client *client = getClient(L);
347         lua_newtable(L);
348         for (const std::string &priv : client->getPrivilegeList()) {
349                 lua_pushboolean(L, true);
350                 lua_setfield(L, -2, priv.c_str());
351         }
352         return 1;
353 }
354
355 // get_builtin_path()
356 int ModApiClient::l_get_builtin_path(lua_State *L)
357 {
358         lua_pushstring(L, BUILTIN_MOD_NAME ":");
359         return 1;
360 }
361
362 void ModApiClient::Initialize(lua_State *L, int top)
363 {
364         API_FCT(get_current_modname);
365         API_FCT(print);
366         API_FCT(display_chat_message);
367         API_FCT(send_chat_message);
368         API_FCT(clear_out_chat_queue);
369         API_FCT(get_player_names);
370         API_FCT(set_last_run_mod);
371         API_FCT(get_last_run_mod);
372         API_FCT(show_formspec);
373         API_FCT(send_respawn);
374         API_FCT(gettext);
375         API_FCT(get_node_or_nil);
376         API_FCT(get_wielded_item);
377         API_FCT(disconnect);
378         API_FCT(get_meta);
379         API_FCT(sound_play);
380         API_FCT(sound_stop);
381         API_FCT(get_server_info);
382         API_FCT(get_item_def);
383         API_FCT(get_node_def);
384         API_FCT(get_privilege_list);
385         API_FCT(get_builtin_path);
386         API_FCT(get_language);
387 }