3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
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 "client/game.h"
28 #include "common/c_content.h"
29 #include "common/c_converter.h"
30 #include "cpp_api/s_base.h"
32 #include "l_internal.h"
33 #include "lua_api/l_nodemeta.h"
34 #include "gui/mainmenumanager.h"
36 #include "util/string.h"
38 #include "client/keycode.h"
40 #define checkCSMRestrictionFlag(flag) \
41 ( getClient(L)->checkCSMRestrictionFlag(CSMRestrictionFlags::flag) )
43 // Not the same as FlagDesc, which contains an `u32 flag`
50 FIXME: This should eventually be moved somewhere else
51 It also needs to be kept in sync with the definition of CSMRestrictionFlags
52 in network/networkprotocol.h
54 const static CSMFlagDesc flagdesc_csm_restriction[] = {
55 {"load_client_mods", CSM_RF_LOAD_CLIENT_MODS},
56 {"chat_messages", CSM_RF_CHAT_MESSAGES},
57 {"read_itemdefs", CSM_RF_READ_ITEMDEFS},
58 {"read_nodedefs", CSM_RF_READ_NODEDEFS},
59 {"lookup_nodes", CSM_RF_LOOKUP_NODES},
60 {"read_playerinfo", CSM_RF_READ_PLAYERINFO},
64 // get_current_modname()
65 int ModApiClient::l_get_current_modname(lua_State *L)
67 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
71 // get_modpath(modname)
72 int ModApiClient::l_get_modpath(lua_State *L)
74 std::string modname = readParam<std::string>(L, 1);
75 // Client mods use a virtual filesystem, see Client::scanModSubfolder()
76 std::string path = modname + ":";
77 lua_pushstring(L, path.c_str());
82 int ModApiClient::l_get_last_run_mod(lua_State *L)
84 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
85 std::string current_mod = readParam<std::string>(L, -1, "");
86 if (current_mod.empty()) {
88 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
93 // set_last_run_mod(modname)
94 int ModApiClient::l_set_last_run_mod(lua_State *L)
96 if (!lua_isstring(L, 1))
99 const char *mod = lua_tostring(L, 1);
100 getScriptApiBase(L)->setOriginDirect(mod);
101 lua_pushboolean(L, true);
106 int ModApiClient::l_print(lua_State *L)
108 NO_MAP_LOCK_REQUIRED;
109 std::string text = luaL_checkstring(L, 1);
110 rawstream << text << std::endl;
114 // display_chat_message(message)
115 int ModApiClient::l_display_chat_message(lua_State *L)
117 if (!lua_isstring(L, 1))
120 std::string message = luaL_checkstring(L, 1);
121 getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
122 lua_pushboolean(L, true);
126 // send_chat_message(message)
127 int ModApiClient::l_send_chat_message(lua_State *L)
129 if (!lua_isstring(L, 1))
132 // If server disabled this API, discard
134 if (checkCSMRestrictionFlag(CSM_RF_CHAT_MESSAGES))
137 std::string message = luaL_checkstring(L, 1);
138 getClient(L)->sendChatMessage(utf8_to_wide(message));
142 // clear_out_chat_queue()
143 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
145 getClient(L)->clearOutChatQueue();
149 // get_player_names()
150 int ModApiClient::l_get_player_names(lua_State *L)
152 if (checkCSMRestrictionFlag(CSM_RF_READ_PLAYERINFO))
155 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
156 lua_createtable(L, plist.size(), 0);
157 int newTable = lua_gettop(L);
159 std::list<std::string>::const_iterator iter;
160 for (iter = plist.begin(); iter != plist.end(); ++iter) {
161 lua_pushstring(L, (*iter).c_str());
162 lua_rawseti(L, newTable, index);
168 // show_formspec(formspec)
169 int ModApiClient::l_show_formspec(lua_State *L)
171 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
174 ClientEvent *event = new ClientEvent();
175 event->type = CE_SHOW_LOCAL_FORMSPEC;
176 event->show_formspec.formname = new std::string(luaL_checkstring(L, 1));
177 event->show_formspec.formspec = new std::string(luaL_checkstring(L, 2));
178 getClient(L)->pushToEventQueue(event);
179 lua_pushboolean(L, true);
184 int ModApiClient::l_send_respawn(lua_State *L)
186 getClient(L)->sendRespawn();
191 int ModApiClient::l_disconnect(lua_State *L)
193 // Stops badly written Lua code form causing boot loops
194 if (getClient(L)->isShutdown()) {
195 lua_pushboolean(L, false);
199 g_gamecallback->disconnect();
200 lua_pushboolean(L, true);
205 int ModApiClient::l_gettext(lua_State *L)
207 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
208 lua_pushstring(L, text.c_str());
213 // get_node_or_nil(pos)
214 // pos = {x=num, y=num, z=num}
215 int ModApiClient::l_get_node_or_nil(lua_State *L)
218 v3s16 pos = read_v3s16(L, 1);
222 MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok);
225 pushnode(L, n, getClient(L)->ndef());
233 int ModApiClient::l_get_language(lua_State *L)
236 char *locale = setlocale(LC_ALL, NULL);
238 char *locale = setlocale(LC_MESSAGES, NULL);
240 std::string lang = gettext("LANG_CODE");
241 if (lang == "LANG_CODE")
244 lua_pushstring(L, locale);
245 lua_pushstring(L, lang.c_str());
250 int ModApiClient::l_get_meta(lua_State *L)
252 v3s16 p = read_v3s16(L, 1);
254 // check restrictions first
256 getClient(L)->CSMGetNode(p, &pos_ok);
260 NodeMetadata *meta = getEnv(L)->getMap().getNodeMetadata(p);
261 NodeMetaRef::createClient(L, meta);
265 // sound_play(spec, parameters)
266 int ModApiClient::l_sound_play(lua_State *L)
268 ISoundManager *sound = getClient(L)->getSoundManager();
270 SimpleSoundSpec spec;
271 read_soundspec(L, 1, spec);
278 if (lua_istable(L, 2)) {
279 getfloatfield(L, 2, "gain", gain);
280 getfloatfield(L, 2, "pitch", pitch);
281 getboolfield(L, 2, "loop", looped);
283 lua_getfield(L, 2, "pos");
284 if (!lua_isnil(L, -1)) {
285 v3f pos = read_v3f(L, -1) * BS;
287 handle = sound->playSoundAt(
288 spec.name, looped, gain * spec.gain, pos, pitch);
289 lua_pushinteger(L, handle);
294 handle = sound->playSound(spec.name, looped, gain * spec.gain, spec.fade, pitch);
295 lua_pushinteger(L, handle);
300 // sound_stop(handle)
301 int ModApiClient::l_sound_stop(lua_State *L)
303 s32 handle = luaL_checkinteger(L, 1);
305 getClient(L)->getSoundManager()->stopSound(handle);
310 // sound_fade(handle, step, gain)
311 int ModApiClient::l_sound_fade(lua_State *L)
313 s32 handle = luaL_checkinteger(L, 1);
314 float step = readParam<float>(L, 2);
315 float gain = readParam<float>(L, 3);
316 getClient(L)->getSoundManager()->fadeSound(handle, step, gain);
321 int ModApiClient::l_get_server_info(lua_State *L)
323 Client *client = getClient(L);
324 Address serverAddress = client->getServerAddress();
326 lua_pushstring(L, client->getAddressName().c_str());
327 lua_setfield(L, -2, "address");
328 lua_pushstring(L, serverAddress.serializeString().c_str());
329 lua_setfield(L, -2, "ip");
330 lua_pushinteger(L, serverAddress.getPort());
331 lua_setfield(L, -2, "port");
332 lua_pushinteger(L, client->getProtoVersion());
333 lua_setfield(L, -2, "protocol_version");
337 // get_item_def(itemstring)
338 int ModApiClient::l_get_item_def(lua_State *L)
340 IGameDef *gdef = getGameDef(L);
343 IItemDefManager *idef = gdef->idef();
346 if (checkCSMRestrictionFlag(CSM_RF_READ_ITEMDEFS))
349 if (!lua_isstring(L, 1))
352 std::string name = readParam<std::string>(L, 1);
353 if (!idef->isKnown(name))
355 const ItemDefinition &def = idef->get(name);
357 push_item_definition_full(L, def);
362 // get_node_def(nodename)
363 int ModApiClient::l_get_node_def(lua_State *L)
365 IGameDef *gdef = getGameDef(L);
368 const NodeDefManager *ndef = gdef->ndef();
371 if (!lua_isstring(L, 1))
374 if (checkCSMRestrictionFlag(CSM_RF_READ_NODEDEFS))
377 std::string name = readParam<std::string>(L, 1);
378 const ContentFeatures &cf = ndef->get(ndef->getId(name));
379 if (cf.name != name) // Unknown node. | name = <whatever>, cf.name = ignore
382 push_content_features(L, cf);
387 // get_privilege_list()
388 int ModApiClient::l_get_privilege_list(lua_State *L)
390 const Client *client = getClient(L);
392 for (const std::string &priv : client->getPrivilegeList()) {
393 lua_pushboolean(L, true);
394 lua_setfield(L, -2, priv.c_str());
399 // get_builtin_path()
400 int ModApiClient::l_get_builtin_path(lua_State *L)
402 lua_pushstring(L, BUILTIN_MOD_NAME ":");
406 // get_csm_restrictions()
407 int ModApiClient::l_get_csm_restrictions(lua_State *L)
409 u64 flags = getClient(L)->getCSMRestrictionFlags();
410 const CSMFlagDesc *flagdesc = flagdesc_csm_restriction;
413 for (int i = 0; flagdesc[i].name; i++) {
414 setboolfield(L, -1, flagdesc[i].name, !!(flags & flagdesc[i].flag));
419 // send_damage(damage)
420 int ModApiClient::l_send_damage(lua_State *L)
422 u16 damage = luaL_checknumber(L, 1);
423 getClient(L)->sendDamage(damage);
428 int ModApiClient::l_place_node(lua_State *L)
430 Client *client = getClient(L);
431 ClientMap &map = client->getEnv().getClientMap();
432 LocalPlayer *player = client->getEnv().getLocalPlayer();
433 ItemStack selected_item, hand_item;
434 player->getWieldedItem(&selected_item, &hand_item);
435 const ItemDefinition &selected_def = selected_item.getDefinition(getGameDef(L)->idef());
436 v3s16 pos = read_v3s16(L, 1);
437 PointedThing pointed;
438 pointed.type = POINTEDTHING_NODE;
439 pointed.node_abovesurface = pos;
440 pointed.node_undersurface = pos;
441 NodeMetadata *meta = map.getNodeMetadata(pos);
442 g_game->nodePlacement(selected_def, selected_item, pos, pos, pointed, meta);
447 int ModApiClient::l_dig_node(lua_State *L)
449 Client *client = getClient(L);
450 v3s16 pos = read_v3s16(L, 1);
451 PointedThing pointed;
452 pointed.type = POINTEDTHING_NODE;
453 pointed.node_abovesurface = pos;
454 pointed.node_undersurface = pos;
455 client->interact(INTERACT_START_DIGGING, pointed);
456 client->interact(INTERACT_DIGGING_COMPLETED, pointed);
460 // get_inventory(location)
461 int ModApiClient::l_get_inventory(lua_State *L)
463 Client *client = getClient(L);
464 InventoryLocation inventory_location;
465 Inventory *inventory;
466 std::string location;
468 location = readParam<std::string>(L, 1);
471 inventory_location.deSerialize(location);
472 inventory = client->getInventory(inventory_location);
473 push_inventory(L, inventory);
474 } catch (SerializationError &) {
481 // set_keypress(key_setting, pressed) -> returns true on success
482 int ModApiClient::l_set_keypress(lua_State *L)
484 std::string setting_name = "keymap_" + readParam<std::string>(L, 1);
485 bool pressed = lua_isboolean(L, 2) && readParam<bool>(L, 2);
487 KeyPress keyCode = getKeySetting(setting_name.c_str());
489 g_game->input->setKeypress(keyCode);
491 g_game->input->unsetKeypress(keyCode);
492 lua_pushboolean(L, true);
493 } catch (SettingNotFoundException &) {
494 lua_pushboolean(L, false);
499 void ModApiClient::Initialize(lua_State *L, int top)
501 API_FCT(get_current_modname);
502 API_FCT(get_modpath);
504 API_FCT(display_chat_message);
505 API_FCT(send_chat_message);
506 API_FCT(clear_out_chat_queue);
507 API_FCT(get_player_names);
508 API_FCT(set_last_run_mod);
509 API_FCT(get_last_run_mod);
510 API_FCT(show_formspec);
511 API_FCT(send_respawn);
513 API_FCT(get_node_or_nil);
519 API_FCT(get_server_info);
520 API_FCT(get_item_def);
521 API_FCT(get_node_def);
522 API_FCT(get_privilege_list);
523 API_FCT(get_builtin_path);
524 API_FCT(get_language);
525 API_FCT(get_csm_restrictions);
526 API_FCT(send_damage);
529 API_FCT(get_inventory);
530 API_FCT(set_keypress);