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 "common/c_content.h"
28 #include "common/c_converter.h"
29 #include "cpp_api/s_base.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"
36 #include "util/string.h"
39 #define checkCSMRestrictionFlag(flag) \
40 ( getClient(L)->checkCSMRestrictionFlag(CSMRestrictionFlags::flag) )
42 // Not the same as FlagDesc, which contains an `u32 flag`
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
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},
63 // get_current_modname()
64 int ModApiClient::l_get_current_modname(lua_State *L)
66 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
70 // get_modpath(modname)
71 int ModApiClient::l_get_modpath(lua_State *L)
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());
81 int ModApiClient::l_get_last_run_mod(lua_State *L)
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()) {
87 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
92 // set_last_run_mod(modname)
93 int ModApiClient::l_set_last_run_mod(lua_State *L)
95 if (!lua_isstring(L, 1))
98 const char *mod = lua_tostring(L, 1);
99 getScriptApiBase(L)->setOriginDirect(mod);
100 lua_pushboolean(L, true);
105 int ModApiClient::l_print(lua_State *L)
107 NO_MAP_LOCK_REQUIRED;
108 std::string text = luaL_checkstring(L, 1);
109 rawstream << text << std::endl;
113 // display_chat_message(message)
114 int ModApiClient::l_display_chat_message(lua_State *L)
116 if (!lua_isstring(L, 1))
119 std::string message = luaL_checkstring(L, 1);
120 getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
121 lua_pushboolean(L, true);
125 // send_chat_message(message)
126 int ModApiClient::l_send_chat_message(lua_State *L)
128 if (!lua_isstring(L, 1))
131 // If server disabled this API, discard
133 if (checkCSMRestrictionFlag(CSM_RF_CHAT_MESSAGES))
136 std::string message = luaL_checkstring(L, 1);
137 getClient(L)->sendChatMessage(utf8_to_wide(message));
141 // clear_out_chat_queue()
142 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
144 getClient(L)->clearOutChatQueue();
148 // get_player_names()
149 int ModApiClient::l_get_player_names(lua_State *L)
151 if (checkCSMRestrictionFlag(CSM_RF_READ_PLAYERINFO))
154 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
155 lua_createtable(L, plist.size(), 0);
156 int newTable = lua_gettop(L);
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);
167 // show_formspec(formspec)
168 int ModApiClient::l_show_formspec(lua_State *L)
170 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
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);
183 int ModApiClient::l_send_respawn(lua_State *L)
185 getClient(L)->sendRespawn();
190 int ModApiClient::l_disconnect(lua_State *L)
192 // Stops badly written Lua code form causing boot loops
193 if (getClient(L)->isShutdown()) {
194 lua_pushboolean(L, false);
198 g_gamecallback->disconnect();
199 lua_pushboolean(L, true);
204 int ModApiClient::l_gettext(lua_State *L)
206 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
207 lua_pushstring(L, text.c_str());
213 // pos = {x=num, y=num, z=num}
214 int ModApiClient::l_get_node_or_nil(lua_State *L)
217 v3s16 pos = read_v3s16(L, 1);
221 MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok);
224 pushnode(L, n, getClient(L)->ndef());
231 int ModApiClient::l_get_language(lua_State *L)
233 char *locale = setlocale(LC_ALL, "");
234 lua_pushstring(L, locale);
238 int ModApiClient::l_get_wielded_item(lua_State *L)
240 Client *client = getClient(L);
241 LocalPlayer *player = client->getEnv().getLocalPlayer();
245 ItemStack selected_item;
246 player->getWieldedItem(&selected_item, nullptr);
247 LuaItemStack::create(L, selected_item);
252 int ModApiClient::l_get_meta(lua_State *L)
254 v3s16 p = read_v3s16(L, 1);
255 NodeMetadata *meta = getClient(L)->getEnv().getMap().getNodeMetadata(p);
256 NodeMetaRef::createClient(L, meta);
260 int ModApiClient::l_sound_play(lua_State *L)
262 ISoundManager *sound = getClient(L)->getSoundManager();
264 SimpleSoundSpec spec;
265 read_soundspec(L, 1, spec);
271 if (lua_istable(L, 2)) {
272 getfloatfield(L, 2, "gain", gain);
273 getfloatfield(L, 2, "pitch", pitch);
274 getboolfield(L, 2, "loop", looped);
276 lua_getfield(L, 2, "pos");
277 if (!lua_isnil(L, -1)) {
278 v3f pos = read_v3f(L, -1) * BS;
280 handle = sound->playSoundAt(
281 spec.name, looped, gain * spec.gain, pos, pitch);
282 lua_pushinteger(L, handle);
287 handle = sound->playSound(spec.name, looped, gain * spec.gain, 0.0f, pitch);
288 lua_pushinteger(L, handle);
293 int ModApiClient::l_sound_stop(lua_State *L)
295 u32 handle = luaL_checkinteger(L, 1);
297 getClient(L)->getSoundManager()->stopSound(handle);
303 int ModApiClient::l_get_server_info(lua_State *L)
305 Client *client = getClient(L);
306 Address serverAddress = client->getServerAddress();
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");
319 // get_item_def(itemstring)
320 int ModApiClient::l_get_item_def(lua_State *L)
322 IGameDef *gdef = getGameDef(L);
325 IItemDefManager *idef = gdef->idef();
328 if (checkCSMRestrictionFlag(CSM_RF_READ_ITEMDEFS))
331 if (!lua_isstring(L, 1))
334 std::string name = readParam<std::string>(L, 1);
335 if (!idef->isKnown(name))
337 const ItemDefinition &def = idef->get(name);
339 push_item_definition_full(L, def);
344 // get_node_def(nodename)
345 int ModApiClient::l_get_node_def(lua_State *L)
347 IGameDef *gdef = getGameDef(L);
350 const NodeDefManager *ndef = gdef->ndef();
353 if (!lua_isstring(L, 1))
356 if (checkCSMRestrictionFlag(CSM_RF_READ_NODEDEFS))
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
364 push_content_features(L, cf);
369 int ModApiClient::l_get_privilege_list(lua_State *L)
371 const Client *client = getClient(L);
373 for (const std::string &priv : client->getPrivilegeList()) {
374 lua_pushboolean(L, true);
375 lua_setfield(L, -2, priv.c_str());
380 // get_builtin_path()
381 int ModApiClient::l_get_builtin_path(lua_State *L)
383 lua_pushstring(L, BUILTIN_MOD_NAME ":");
387 // get_csm_restrictions()
388 int ModApiClient::l_get_csm_restrictions(lua_State *L)
390 u64 flags = getClient(L)->getCSMRestrictionFlags();
391 const CSMFlagDesc *flagdesc = flagdesc_csm_restriction;
394 for (int i = 0; flagdesc[i].name; i++) {
395 setboolfield(L, -1, flagdesc[i].name, !!(flags & flagdesc[i].flag));
400 void ModApiClient::Initialize(lua_State *L, int top)
402 API_FCT(get_current_modname);
403 API_FCT(get_modpath);
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);
414 API_FCT(get_node_or_nil);
415 API_FCT(get_wielded_item);
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);