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_nodemeta.h"
33 #include "gui/mainmenumanager.h"
35 #include "util/string.h"
38 #define checkCSMRestrictionFlag(flag) \
39 ( getClient(L)->checkCSMRestrictionFlag(CSMRestrictionFlags::flag) )
41 // Not the same as FlagDesc, which contains an `u32 flag`
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
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},
62 // get_current_modname()
63 int ModApiClient::l_get_current_modname(lua_State *L)
65 lua_rawgeti(L, LUA_REGISTRYINDEX, CUSTOM_RIDX_CURRENT_MOD_NAME);
69 // get_modpath(modname)
70 int ModApiClient::l_get_modpath(lua_State *L)
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());
80 int ModApiClient::l_get_last_run_mod(lua_State *L)
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()) {
86 lua_pushstring(L, getScriptApiBase(L)->getOrigin().c_str());
91 // set_last_run_mod(modname)
92 int ModApiClient::l_set_last_run_mod(lua_State *L)
94 if (!lua_isstring(L, 1))
97 const char *mod = lua_tostring(L, 1);
98 getScriptApiBase(L)->setOriginDirect(mod);
99 lua_pushboolean(L, true);
104 int ModApiClient::l_print(lua_State *L)
106 NO_MAP_LOCK_REQUIRED;
107 std::string text = luaL_checkstring(L, 1);
108 rawstream << text << std::endl;
112 // display_chat_message(message)
113 int ModApiClient::l_display_chat_message(lua_State *L)
115 if (!lua_isstring(L, 1))
118 std::string message = luaL_checkstring(L, 1);
119 getClient(L)->pushToChatQueue(new ChatMessage(utf8_to_wide(message)));
120 lua_pushboolean(L, true);
124 // send_chat_message(message)
125 int ModApiClient::l_send_chat_message(lua_State *L)
127 if (!lua_isstring(L, 1))
130 // If server disabled this API, discard
132 if (checkCSMRestrictionFlag(CSM_RF_CHAT_MESSAGES))
135 std::string message = luaL_checkstring(L, 1);
136 getClient(L)->sendChatMessage(utf8_to_wide(message));
140 // clear_out_chat_queue()
141 int ModApiClient::l_clear_out_chat_queue(lua_State *L)
143 getClient(L)->clearOutChatQueue();
147 // get_player_names()
148 int ModApiClient::l_get_player_names(lua_State *L)
150 if (checkCSMRestrictionFlag(CSM_RF_READ_PLAYERINFO))
153 const std::list<std::string> &plist = getClient(L)->getConnectedPlayerNames();
154 lua_createtable(L, plist.size(), 0);
155 int newTable = lua_gettop(L);
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);
166 // show_formspec(formspec)
167 int ModApiClient::l_show_formspec(lua_State *L)
169 if (!lua_isstring(L, 1) || !lua_isstring(L, 2))
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);
182 int ModApiClient::l_send_respawn(lua_State *L)
184 getClient(L)->sendRespawn();
189 int ModApiClient::l_disconnect(lua_State *L)
191 // Stops badly written Lua code form causing boot loops
192 if (getClient(L)->isShutdown()) {
193 lua_pushboolean(L, false);
197 g_gamecallback->disconnect();
198 lua_pushboolean(L, true);
203 int ModApiClient::l_gettext(lua_State *L)
205 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
206 lua_pushstring(L, text.c_str());
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)
216 v3s16 pos = read_v3s16(L, 1);
220 MapNode n = getClient(L)->CSMGetNode(pos, &pos_ok);
223 pushnode(L, n, getClient(L)->ndef());
231 int ModApiClient::l_get_language(lua_State *L)
234 char *locale = setlocale(LC_ALL, NULL);
236 char *locale = setlocale(LC_MESSAGES, NULL);
238 std::string lang = gettext("LANG_CODE");
239 if (lang == "LANG_CODE")
242 lua_pushstring(L, locale);
243 lua_pushstring(L, lang.c_str());
248 int ModApiClient::l_get_meta(lua_State *L)
250 v3s16 p = read_v3s16(L, 1);
252 // check restrictions first
254 getClient(L)->CSMGetNode(p, &pos_ok);
258 NodeMetadata *meta = getEnv(L)->getMap().getNodeMetadata(p);
259 NodeMetaRef::createClient(L, meta);
263 // sound_play(spec, parameters)
264 int ModApiClient::l_sound_play(lua_State *L)
266 ISoundManager *sound = getClient(L)->getSoundManager();
268 SimpleSoundSpec spec;
269 read_soundspec(L, 1, spec);
276 if (lua_istable(L, 2)) {
277 getfloatfield(L, 2, "gain", gain);
278 getfloatfield(L, 2, "pitch", pitch);
279 getboolfield(L, 2, "loop", looped);
281 lua_getfield(L, 2, "pos");
282 if (!lua_isnil(L, -1)) {
283 v3f pos = read_v3f(L, -1) * BS;
285 handle = sound->playSoundAt(
286 spec.name, looped, gain * spec.gain, pos, pitch);
287 lua_pushinteger(L, handle);
292 handle = sound->playSound(spec.name, looped, gain * spec.gain, spec.fade, pitch);
293 lua_pushinteger(L, handle);
298 // sound_stop(handle)
299 int ModApiClient::l_sound_stop(lua_State *L)
301 s32 handle = luaL_checkinteger(L, 1);
303 getClient(L)->getSoundManager()->stopSound(handle);
308 // sound_fade(handle, step, gain)
309 int ModApiClient::l_sound_fade(lua_State *L)
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);
319 int ModApiClient::l_get_server_info(lua_State *L)
321 Client *client = getClient(L);
322 Address serverAddress = client->getServerAddress();
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");
335 // get_item_def(itemstring)
336 int ModApiClient::l_get_item_def(lua_State *L)
338 IGameDef *gdef = getGameDef(L);
341 IItemDefManager *idef = gdef->idef();
344 if (checkCSMRestrictionFlag(CSM_RF_READ_ITEMDEFS))
347 if (!lua_isstring(L, 1))
350 std::string name = readParam<std::string>(L, 1);
351 if (!idef->isKnown(name))
353 const ItemDefinition &def = idef->get(name);
355 push_item_definition_full(L, def);
360 // get_node_def(nodename)
361 int ModApiClient::l_get_node_def(lua_State *L)
363 IGameDef *gdef = getGameDef(L);
366 const NodeDefManager *ndef = gdef->ndef();
369 if (!lua_isstring(L, 1))
372 if (checkCSMRestrictionFlag(CSM_RF_READ_NODEDEFS))
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
380 push_content_features(L, cf);
385 // get_privilege_list()
386 int ModApiClient::l_get_privilege_list(lua_State *L)
388 const Client *client = getClient(L);
390 for (const std::string &priv : client->getPrivilegeList()) {
391 lua_pushboolean(L, true);
392 lua_setfield(L, -2, priv.c_str());
397 // get_builtin_path()
398 int ModApiClient::l_get_builtin_path(lua_State *L)
400 lua_pushstring(L, BUILTIN_MOD_NAME ":");
404 // get_csm_restrictions()
405 int ModApiClient::l_get_csm_restrictions(lua_State *L)
407 u64 flags = getClient(L)->getCSMRestrictionFlags();
408 const CSMFlagDesc *flagdesc = flagdesc_csm_restriction;
411 for (int i = 0; flagdesc[i].name; i++) {
412 setboolfield(L, -1, flagdesc[i].name, !!(flags & flagdesc[i].flag));
417 void ModApiClient::Initialize(lua_State *L, int top)
419 API_FCT(get_current_modname);
420 API_FCT(get_modpath);
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);
431 API_FCT(get_node_or_nil);
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);