3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "scriptapi.h"
30 #include "settings.h" // For accessing g_settings
31 #include "main.h" // For g_settings
36 #include "scriptapi_types.h"
37 #include "scriptapi_env.h"
38 #include "scriptapi_nodetimer.h"
39 #include "scriptapi_inventory.h"
40 #include "scriptapi_nodemeta.h"
41 #include "scriptapi_object.h"
42 #include "scriptapi_noise.h"
43 #include "scriptapi_common.h"
44 #include "scriptapi_item.h"
45 #include "scriptapi_content.h"
46 #include "scriptapi_craft.h"
47 #include "scriptapi_particles.h"
49 /*****************************************************************************/
51 /*****************************************************************************/
58 ModNameStorer(lua_State *L_, const std::string modname):
61 // Store current modname in registry
62 lua_pushstring(L, modname.c_str());
63 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
67 // Clear current modname in registry
69 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
73 bool scriptapi_loadmod(lua_State *L, const std::string &scriptpath,
74 const std::string &modname)
76 ModNameStorer modnamestorer(L, modname);
78 if(!string_allowed(modname, "abcdefghijklmnopqrstuvwxyz"
80 errorstream<<"Error loading mod \""<<modname
81 <<"\": modname does not follow naming conventions: "
82 <<"Only chararacters [a-z0-9_] are allowed."<<std::endl;
89 success = script_load(L, scriptpath.c_str());
92 errorstream<<"Error loading mod \""<<modname
93 <<"\": "<<e.what()<<std::endl;
100 /*****************************************************************************/
102 /*****************************************************************************/
107 static void read_privileges(lua_State *L, int index,
108 std::set<std::string> &result)
114 while(lua_next(L, index) != 0){
115 // key at index -2 and value at index -1
116 std::string key = luaL_checkstring(L, -2);
117 bool value = lua_toboolean(L, -1);
120 // removes value, keeps key for next iteration
125 static void get_auth_handler(lua_State *L)
127 lua_getglobal(L, "minetest");
128 lua_getfield(L, -1, "registered_auth_handler");
129 if(lua_isnil(L, -1)){
131 lua_getfield(L, -1, "builtin_auth_handler");
133 if(lua_type(L, -1) != LUA_TTABLE)
134 throw LuaError(L, "Authentication handler table not valid");
137 bool scriptapi_get_auth(lua_State *L, const std::string &playername,
138 std::string *dst_password, std::set<std::string> *dst_privs)
141 assert(lua_checkstack(L, 20));
142 StackUnroller stack_unroller(L);
145 lua_getfield(L, -1, "get_auth");
146 if(lua_type(L, -1) != LUA_TFUNCTION)
147 throw LuaError(L, "Authentication handler missing get_auth");
148 lua_pushstring(L, playername.c_str());
149 if(lua_pcall(L, 1, 1, 0))
150 script_error(L, "error: %s", lua_tostring(L, -1));
152 // nil = login not allowed
155 luaL_checktype(L, -1, LUA_TTABLE);
157 std::string password;
158 bool found = getstringfield(L, -1, "password", password);
160 throw LuaError(L, "Authentication handler didn't return password");
162 *dst_password = password;
164 lua_getfield(L, -1, "privileges");
165 if(!lua_istable(L, -1))
167 "Authentication handler didn't return privilege table");
169 read_privileges(L, -1, *dst_privs);
175 void scriptapi_create_auth(lua_State *L, const std::string &playername,
176 const std::string &password)
179 assert(lua_checkstack(L, 20));
180 StackUnroller stack_unroller(L);
183 lua_getfield(L, -1, "create_auth");
184 if(lua_type(L, -1) != LUA_TFUNCTION)
185 throw LuaError(L, "Authentication handler missing create_auth");
186 lua_pushstring(L, playername.c_str());
187 lua_pushstring(L, password.c_str());
188 if(lua_pcall(L, 2, 0, 0))
189 script_error(L, "error: %s", lua_tostring(L, -1));
192 bool scriptapi_set_password(lua_State *L, const std::string &playername,
193 const std::string &password)
196 assert(lua_checkstack(L, 20));
197 StackUnroller stack_unroller(L);
200 lua_getfield(L, -1, "set_password");
201 if(lua_type(L, -1) != LUA_TFUNCTION)
202 throw LuaError(L, "Authentication handler missing set_password");
203 lua_pushstring(L, playername.c_str());
204 lua_pushstring(L, password.c_str());
205 if(lua_pcall(L, 2, 1, 0))
206 script_error(L, "error: %s", lua_tostring(L, -1));
207 return lua_toboolean(L, -1);
210 /*****************************************************************************/
212 /*****************************************************************************/
216 void read_groups(lua_State *L, int index,
217 std::map<std::string, int> &result)
219 if (!lua_istable(L,index))
225 while(lua_next(L, index) != 0){
226 // key at index -2 and value at index -1
227 std::string name = luaL_checkstring(L, -2);
228 int rating = luaL_checkinteger(L, -1);
229 result[name] = rating;
230 // removes value, keeps key for next iteration
235 struct EnumString es_BiomeTerrainType[] =
237 {BIOME_TERRAIN_NORMAL, "normal"},
238 {BIOME_TERRAIN_LIQUID, "liquid"},
239 {BIOME_TERRAIN_NETHER, "nether"},
240 {BIOME_TERRAIN_AETHER, "aether"},
241 {BIOME_TERRAIN_FLAT, "flat"},
245 /*****************************************************************************/
247 /*****************************************************************************/
252 static void set_dig_params(lua_State *L, int table,
253 const DigParams ¶ms)
255 setboolfield(L, table, "diggable", params.diggable);
256 setfloatfield(L, table, "time", params.time);
257 setintfield(L, table, "wear", params.wear);
260 static void push_dig_params(lua_State *L,
261 const DigParams ¶ms)
264 set_dig_params(L, -1, params);
271 static void set_hit_params(lua_State *L, int table,
272 const HitParams ¶ms)
274 setintfield(L, table, "hp", params.hp);
275 setintfield(L, table, "wear", params.wear);
278 static void push_hit_params(lua_State *L,
279 const HitParams ¶ms)
282 set_hit_params(L, -1, params);
289 static void read_server_sound_params(lua_State *L, int index,
290 ServerSoundParams ¶ms)
293 index = lua_gettop(L) + 1 + index;
295 params = ServerSoundParams();
296 if(lua_istable(L, index)){
297 getfloatfield(L, index, "gain", params.gain);
298 getstringfield(L, index, "to_player", params.to_player);
299 lua_getfield(L, index, "pos");
300 if(!lua_isnil(L, -1)){
301 v3f p = read_v3f(L, -1)*BS;
303 params.type = ServerSoundParams::SSP_POSITIONAL;
306 lua_getfield(L, index, "object");
307 if(!lua_isnil(L, -1)){
308 ObjectRef *ref = ObjectRef::checkobject(L, -1);
309 ServerActiveObject *sao = ObjectRef::getobject(ref);
311 params.object = sao->getId();
312 params.type = ServerSoundParams::SSP_OBJECT;
316 params.max_hear_distance = BS*getfloatfield_default(L, index,
317 "max_hear_distance", params.max_hear_distance/BS);
318 getboolfield(L, index, "loop", params.loop);
322 /*****************************************************************************/
324 /*****************************************************************************/
326 // Push the list of callbacks (a lua table).
327 // Then push nargs arguments.
328 // Then call this function, which
329 // - runs the callbacks
330 // - removes the table and arguments from the lua stack
331 // - pushes the return value, computed depending on mode
332 void scriptapi_run_callbacks(lua_State *L, int nargs,
333 RunCallbacksMode mode)
335 // Insert the return value into the lua stack, below the table
336 assert(lua_gettop(L) >= nargs + 1);
338 lua_insert(L, -(nargs + 1) - 1);
339 // Stack now looks like this:
340 // ... <return value = nil> <table> <arg#1> <arg#2> ... <arg#n>
342 int rv = lua_gettop(L) - nargs - 1;
346 luaL_checktype(L, table, LUA_TTABLE);
350 bool first_loop = true;
351 while(lua_next(L, table) != 0){
352 // key at index -2 and value at index -1
353 luaL_checktype(L, -1, LUA_TFUNCTION);
355 for(int i = 0; i < nargs; i++)
356 lua_pushvalue(L, arg+i);
357 if(lua_pcall(L, nargs, 1, 0))
358 script_error(L, "error: %s", lua_tostring(L, -1));
360 // Move return value to designated space in stack
363 // Result of first callback is always moved
367 // Otherwise, what happens depends on the mode
368 if(mode == RUN_CALLBACKS_MODE_FIRST)
370 else if(mode == RUN_CALLBACKS_MODE_LAST)
372 else if(mode == RUN_CALLBACKS_MODE_AND ||
373 mode == RUN_CALLBACKS_MODE_AND_SC){
374 if((bool)lua_toboolean(L, rv) == true &&
375 (bool)lua_toboolean(L, -1) == false)
380 else if(mode == RUN_CALLBACKS_MODE_OR ||
381 mode == RUN_CALLBACKS_MODE_OR_SC){
382 if((bool)lua_toboolean(L, rv) == false &&
383 (bool)lua_toboolean(L, -1) == true)
392 // Handle short circuit modes
393 if(mode == RUN_CALLBACKS_MODE_AND_SC &&
394 (bool)lua_toboolean(L, rv) == false)
396 else if(mode == RUN_CALLBACKS_MODE_OR_SC &&
397 (bool)lua_toboolean(L, rv) == true)
400 // value removed, keep key for next iteration
403 // Remove stuff from stack, leaving only the return value
406 // Fix return value in case no callbacks were called
408 if(mode == RUN_CALLBACKS_MODE_AND ||
409 mode == RUN_CALLBACKS_MODE_AND_SC){
411 lua_pushboolean(L, true);
413 else if(mode == RUN_CALLBACKS_MODE_OR ||
414 mode == RUN_CALLBACKS_MODE_OR_SC){
416 lua_pushboolean(L, false);
421 bool scriptapi_on_chat_message(lua_State *L, const std::string &name,
422 const std::string &message)
425 assert(lua_checkstack(L, 20));
426 StackUnroller stack_unroller(L);
428 // Get minetest.registered_on_chat_messages
429 lua_getglobal(L, "minetest");
430 lua_getfield(L, -1, "registered_on_chat_messages");
432 lua_pushstring(L, name.c_str());
433 lua_pushstring(L, message.c_str());
434 scriptapi_run_callbacks(L, 2, RUN_CALLBACKS_MODE_OR_SC);
435 bool ate = lua_toboolean(L, -1);
439 void scriptapi_on_shutdown(lua_State *L)
442 assert(lua_checkstack(L, 20));
443 StackUnroller stack_unroller(L);
445 // Get registered shutdown hooks
446 lua_getglobal(L, "minetest");
447 lua_getfield(L, -1, "registered_on_shutdown");
449 scriptapi_run_callbacks(L, 0, RUN_CALLBACKS_MODE_FIRST);
452 void scriptapi_on_newplayer(lua_State *L, ServerActiveObject *player)
455 assert(lua_checkstack(L, 20));
456 StackUnroller stack_unroller(L);
458 // Get minetest.registered_on_newplayers
459 lua_getglobal(L, "minetest");
460 lua_getfield(L, -1, "registered_on_newplayers");
462 objectref_get_or_create(L, player);
463 scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
466 void scriptapi_on_dieplayer(lua_State *L, ServerActiveObject *player)
469 assert(lua_checkstack(L, 20));
470 StackUnroller stack_unroller(L);
472 // Get minetest.registered_on_dieplayers
473 lua_getglobal(L, "minetest");
474 lua_getfield(L, -1, "registered_on_dieplayers");
476 objectref_get_or_create(L, player);
477 scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
480 bool scriptapi_on_respawnplayer(lua_State *L, ServerActiveObject *player)
483 assert(lua_checkstack(L, 20));
484 StackUnroller stack_unroller(L);
486 // Get minetest.registered_on_respawnplayers
487 lua_getglobal(L, "minetest");
488 lua_getfield(L, -1, "registered_on_respawnplayers");
490 objectref_get_or_create(L, player);
491 scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_OR);
492 bool positioning_handled_by_some = lua_toboolean(L, -1);
493 return positioning_handled_by_some;
496 void scriptapi_on_joinplayer(lua_State *L, ServerActiveObject *player)
499 assert(lua_checkstack(L, 20));
500 StackUnroller stack_unroller(L);
502 // Get minetest.registered_on_joinplayers
503 lua_getglobal(L, "minetest");
504 lua_getfield(L, -1, "registered_on_joinplayers");
506 objectref_get_or_create(L, player);
507 scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
510 void scriptapi_on_leaveplayer(lua_State *L, ServerActiveObject *player)
513 assert(lua_checkstack(L, 20));
514 StackUnroller stack_unroller(L);
516 // Get minetest.registered_on_leaveplayers
517 lua_getglobal(L, "minetest");
518 lua_getfield(L, -1, "registered_on_leaveplayers");
520 objectref_get_or_create(L, player);
521 scriptapi_run_callbacks(L, 1, RUN_CALLBACKS_MODE_FIRST);
527 void scriptapi_on_player_receive_fields(lua_State *L,
528 ServerActiveObject *player,
529 const std::string &formname,
530 const std::map<std::string, std::string> &fields)
533 assert(lua_checkstack(L, 20));
534 StackUnroller stack_unroller(L);
536 // Get minetest.registered_on_chat_messages
537 lua_getglobal(L, "minetest");
538 lua_getfield(L, -1, "registered_on_player_receive_fields");
541 objectref_get_or_create(L, player);
543 lua_pushstring(L, formname.c_str());
546 for(std::map<std::string, std::string>::const_iterator
547 i = fields.begin(); i != fields.end(); i++){
548 const std::string &name = i->first;
549 const std::string &value = i->second;
550 lua_pushstring(L, name.c_str());
551 lua_pushlstring(L, value.c_str(), value.size());
554 scriptapi_run_callbacks(L, 3, RUN_CALLBACKS_MODE_OR_SC);
556 /*****************************************************************************/
558 /*****************************************************************************/
561 // Writes a line to dstream
562 static int l_debug(lua_State *L)
564 std::string text = lua_tostring(L, 1);
565 dstream << text << std::endl;
569 // log([level,] text)
570 // Writes a line to the logger.
571 // The one-argument version logs to infostream.
572 // The two-argument version accept a log level: error, action, info, or verbose.
573 static int l_log(lua_State *L)
576 LogMessageLevel level = LMT_INFO;
579 text = lua_tostring(L, 1);
583 std::string levelname = luaL_checkstring(L, 1);
584 text = luaL_checkstring(L, 2);
585 if(levelname == "error")
587 else if(levelname == "action")
589 else if(levelname == "verbose")
592 log_printline(level, text);
596 // request_shutdown()
597 static int l_request_shutdown(lua_State *L)
599 get_server(L)->requestShutdown();
603 // get_server_status()
604 static int l_get_server_status(lua_State *L)
606 lua_pushstring(L, wide_to_narrow(get_server(L)->getStatusString()).c_str());
610 // register_biome_groups({frequencies})
611 static int l_register_biome_groups(lua_State *L)
613 luaL_checktype(L, 1, LUA_TTABLE);
615 if (!lua_istable(L, index))
616 throw LuaError(L, "register_biome_groups: parameter is not a table");
618 BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
620 verbosestream << "register_biome_groups: BiomeDefManager not active" << std::endl;
625 for (int i = 1; lua_next(L, index) != 0; i++) {
626 bmgr->addBiomeGroup(lua_tonumber(L, -1));
634 // register_biome({lots of stuff})
635 static int l_register_biome(lua_State *L)
637 luaL_checktype(L, 1, LUA_TTABLE);
638 int index = 1, groupid;
639 std::string nodename;
641 IWritableNodeDefManager *ndef = get_server(L)->getWritableNodeDefManager();
642 BiomeDefManager *bmgr = get_server(L)->getBiomeDef();
644 verbosestream << "register_biome: BiomeDefManager not active" << std::endl;
648 groupid = getintfield_default(L, index, "group_id", 0);
650 enum BiomeTerrainType terrain = (BiomeTerrainType)getenumfield(L, index,
651 "terrain_type", es_BiomeTerrainType, BIOME_TERRAIN_NORMAL);
652 Biome *b = bmgr->createBiome(terrain);
654 b->name = getstringfield_default(L, index, "name", "");
656 if (getstringfield(L, index, "node_top", nodename))
657 b->n_top = MapNode(ndef->getId(nodename));
659 b->n_top = MapNode(CONTENT_IGNORE);
661 if (getstringfield(L, index, "node_filler", nodename))
662 b->n_filler = MapNode(ndef->getId(nodename));
664 b->n_filler = b->n_top;
666 b->ntopnodes = getintfield_default(L, index, "num_top_nodes", 0);
668 b->height_min = getintfield_default(L, index, "height_min", 0);
669 b->height_max = getintfield_default(L, index, "height_max", 0);
670 b->heat_min = getfloatfield_default(L, index, "heat_min", 0.);
671 b->heat_max = getfloatfield_default(L, index, "heat_max", 0.);
672 b->humidity_min = getfloatfield_default(L, index, "humidity_min", 0.);
673 b->humidity_max = getfloatfield_default(L, index, "humidity_max", 0.);
675 b->np = new NoiseParams; // should read an entire NoiseParams later on...
676 getfloatfield(L, index, "scale", b->np->scale);
677 getfloatfield(L, index, "offset", b->np->offset);
679 b->groupid = (s8)groupid;
680 b->flags = 0; //reserved
684 verbosestream << "register_biome: " << b->name << std::endl;
690 // setting_set(name, value)
691 static int l_setting_set(lua_State *L)
693 const char *name = luaL_checkstring(L, 1);
694 const char *value = luaL_checkstring(L, 2);
695 g_settings->set(name, value);
700 static int l_setting_get(lua_State *L)
702 const char *name = luaL_checkstring(L, 1);
704 std::string value = g_settings->get(name);
705 lua_pushstring(L, value.c_str());
706 } catch(SettingNotFoundException &e){
712 // setting_getbool(name)
713 static int l_setting_getbool(lua_State *L)
715 const char *name = luaL_checkstring(L, 1);
717 bool value = g_settings->getBool(name);
718 lua_pushboolean(L, value);
719 } catch(SettingNotFoundException &e){
726 static int l_setting_save(lua_State *L)
728 get_server(L)->saveConfig();
732 // chat_send_all(text)
733 static int l_chat_send_all(lua_State *L)
735 const char *text = luaL_checkstring(L, 1);
736 // Get server from registry
737 Server *server = get_server(L);
739 server->notifyPlayers(narrow_to_wide(text));
743 // chat_send_player(name, text)
744 static int l_chat_send_player(lua_State *L)
746 const char *name = luaL_checkstring(L, 1);
747 const char *text = luaL_checkstring(L, 2);
748 // Get server from registry
749 Server *server = get_server(L);
751 server->notifyPlayer(name, narrow_to_wide(text));
755 // get_player_privs(name, text)
756 static int l_get_player_privs(lua_State *L)
758 const char *name = luaL_checkstring(L, 1);
759 // Get server from registry
760 Server *server = get_server(L);
763 int table = lua_gettop(L);
764 std::set<std::string> privs_s = server->getPlayerEffectivePrivs(name);
765 for(std::set<std::string>::const_iterator
766 i = privs_s.begin(); i != privs_s.end(); i++){
767 lua_pushboolean(L, true);
768 lua_setfield(L, table, i->c_str());
770 lua_pushvalue(L, table);
775 static int l_get_ban_list(lua_State *L)
777 lua_pushstring(L, get_server(L)->getBanDescription("").c_str());
781 // get_ban_description()
782 static int l_get_ban_description(lua_State *L)
784 const char * ip_or_name = luaL_checkstring(L, 1);
785 lua_pushstring(L, get_server(L)->getBanDescription(std::string(ip_or_name)).c_str());
790 static int l_ban_player(lua_State *L)
792 const char * name = luaL_checkstring(L, 1);
793 Player *player = get_env(L)->getPlayer(name);
796 lua_pushboolean(L, false); // no such player
801 Address addr = get_server(L)->getPeerAddress(get_env(L)->getPlayer(name)->peer_id);
802 std::string ip_str = addr.serializeString();
803 get_server(L)->setIpBanned(ip_str, name);
805 catch(con::PeerNotFoundException) // unlikely
807 dstream << __FUNCTION_NAME << ": peer was not found" << std::endl;
808 lua_pushboolean(L, false); // error
811 lua_pushboolean(L, true);
815 // unban_player_or_ip()
816 static int l_unban_player_of_ip(lua_State *L)
818 const char * ip_or_name = luaL_checkstring(L, 1);
819 get_server(L)->unsetIpBanned(ip_or_name);
820 lua_pushboolean(L, true);
824 // show_formspec(playername,formname,formspec)
825 static int l_show_formspec(lua_State *L)
827 const char *playername = luaL_checkstring(L, 1);
828 const char *formname = luaL_checkstring(L, 2);
829 const char *formspec = luaL_checkstring(L, 3);
831 if(get_server(L)->showFormspec(playername,formspec,formname))
833 lua_pushboolean(L, true);
835 lua_pushboolean(L, false);
840 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
841 static int l_get_dig_params(lua_State *L)
843 std::map<std::string, int> groups;
844 read_groups(L, 1, groups);
845 ToolCapabilities tp = read_tool_capabilities(L, 2);
846 if(lua_isnoneornil(L, 3))
847 push_dig_params(L, getDigParams(groups, &tp));
849 push_dig_params(L, getDigParams(groups, &tp,
850 luaL_checknumber(L, 3)));
854 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
855 static int l_get_hit_params(lua_State *L)
857 std::map<std::string, int> groups;
858 read_groups(L, 1, groups);
859 ToolCapabilities tp = read_tool_capabilities(L, 2);
860 if(lua_isnoneornil(L, 3))
861 push_hit_params(L, getHitParams(groups, &tp));
863 push_hit_params(L, getHitParams(groups, &tp,
864 luaL_checknumber(L, 3)));
868 // get_current_modname()
869 static int l_get_current_modname(lua_State *L)
871 lua_getfield(L, LUA_REGISTRYINDEX, "minetest_current_modname");
875 // get_modpath(modname)
876 static int l_get_modpath(lua_State *L)
878 std::string modname = luaL_checkstring(L, 1);
880 if(modname == "__builtin"){
881 std::string path = get_server(L)->getBuiltinLuaPath();
882 lua_pushstring(L, path.c_str());
885 const ModSpec *mod = get_server(L)->getModSpec(modname);
890 lua_pushstring(L, mod->path.c_str());
895 // the returned list is sorted alphabetically for you
896 static int l_get_modnames(lua_State *L)
898 // Get a list of mods
899 std::list<std::string> mods_unsorted, mods_sorted;
900 get_server(L)->getModNames(mods_unsorted);
902 // Take unsorted items from mods_unsorted and sort them into
903 // mods_sorted; not great performance but the number of mods on a
904 // server will likely be small.
905 for(std::list<std::string>::iterator i = mods_unsorted.begin();
906 i != mods_unsorted.end(); ++i)
909 for(std::list<std::string>::iterator x = mods_sorted.begin();
910 x != mods_sorted.end(); ++x)
912 // I doubt anybody using Minetest will be using
913 // anything not ASCII based :)
914 if((*i).compare(*x) <= 0)
916 mods_sorted.insert(x, *i);
922 mods_sorted.push_back(*i);
925 // Get the table insertion function from Lua.
926 lua_getglobal(L, "table");
927 lua_getfield(L, -1, "insert");
928 int insertion_func = lua_gettop(L);
930 // Package them up for Lua
932 int new_table = lua_gettop(L);
933 std::list<std::string>::iterator i = mods_sorted.begin();
934 while(i != mods_sorted.end())
936 lua_pushvalue(L, insertion_func);
937 lua_pushvalue(L, new_table);
938 lua_pushstring(L, (*i).c_str());
939 if(lua_pcall(L, 2, 0, 0) != 0)
941 script_error(L, "error: %s", lua_tostring(L, -1));
949 static int l_get_worldpath(lua_State *L)
951 std::string worldpath = get_server(L)->getWorldPath();
952 lua_pushstring(L, worldpath.c_str());
956 // sound_play(spec, parameters)
957 static int l_sound_play(lua_State *L)
959 SimpleSoundSpec spec;
960 read_soundspec(L, 1, spec);
961 ServerSoundParams params;
962 read_server_sound_params(L, 2, params);
963 s32 handle = get_server(L)->playSound(spec, params);
964 lua_pushinteger(L, handle);
968 // sound_stop(handle)
969 static int l_sound_stop(lua_State *L)
971 int handle = luaL_checkinteger(L, 1);
972 get_server(L)->stopSound(handle);
977 static int l_is_singleplayer(lua_State *L)
979 lua_pushboolean(L, get_server(L)->isSingleplayer());
983 // get_password_hash(name, raw_password)
984 static int l_get_password_hash(lua_State *L)
986 std::string name = luaL_checkstring(L, 1);
987 std::string raw_password = luaL_checkstring(L, 2);
988 std::string hash = translatePassword(name,
989 narrow_to_wide(raw_password));
990 lua_pushstring(L, hash.c_str());
994 // notify_authentication_modified(name)
995 static int l_notify_authentication_modified(lua_State *L)
997 std::string name = "";
998 if(lua_isstring(L, 1))
999 name = lua_tostring(L, 1);
1000 get_server(L)->reportPrivsModified(name);
1004 // rollback_get_last_node_actor(p, range, seconds) -> actor, p, seconds
1005 static int l_rollback_get_last_node_actor(lua_State *L)
1007 v3s16 p = read_v3s16(L, 1);
1008 int range = luaL_checknumber(L, 2);
1009 int seconds = luaL_checknumber(L, 3);
1010 Server *server = get_server(L);
1011 IRollbackManager *rollback = server->getRollbackManager();
1013 int act_seconds = 0;
1014 std::string actor = rollback->getLastNodeActor(p, range, seconds, &act_p, &act_seconds);
1015 lua_pushstring(L, actor.c_str());
1016 push_v3s16(L, act_p);
1017 lua_pushnumber(L, act_seconds);
1021 // rollback_revert_actions_by(actor, seconds) -> bool, log messages
1022 static int l_rollback_revert_actions_by(lua_State *L)
1024 std::string actor = luaL_checkstring(L, 1);
1025 int seconds = luaL_checknumber(L, 2);
1026 Server *server = get_server(L);
1027 IRollbackManager *rollback = server->getRollbackManager();
1028 std::list<RollbackAction> actions = rollback->getRevertActions(actor, seconds);
1029 std::list<std::string> log;
1030 bool success = server->rollbackRevertActions(actions, &log);
1031 // Push boolean result
1032 lua_pushboolean(L, success);
1033 // Get the table insert function and push the log table
1034 lua_getglobal(L, "table");
1035 lua_getfield(L, -1, "insert");
1036 int table_insert = lua_gettop(L);
1038 int table = lua_gettop(L);
1039 for(std::list<std::string>::const_iterator i = log.begin();
1040 i != log.end(); i++)
1042 lua_pushvalue(L, table_insert);
1043 lua_pushvalue(L, table);
1044 lua_pushstring(L, i->c_str());
1045 if(lua_pcall(L, 2, 0, 0))
1046 script_error(L, "error: %s", lua_tostring(L, -1));
1048 lua_remove(L, -2); // Remove table
1049 lua_remove(L, -2); // Remove insert
1053 static const struct luaL_Reg minetest_f [] = {
1056 {"request_shutdown", l_request_shutdown},
1057 {"get_server_status", l_get_server_status},
1058 {"register_item_raw", l_register_item_raw},
1059 {"register_alias_raw", l_register_alias_raw},
1060 {"register_craft", l_register_craft},
1061 {"register_biome", l_register_biome},
1062 {"register_biome_groups", l_register_biome_groups},
1063 {"setting_set", l_setting_set},
1064 {"setting_get", l_setting_get},
1065 {"setting_getbool", l_setting_getbool},
1066 {"setting_save",l_setting_save},
1067 {"chat_send_all", l_chat_send_all},
1068 {"chat_send_player", l_chat_send_player},
1069 {"get_player_privs", l_get_player_privs},
1070 {"get_ban_list", l_get_ban_list},
1071 {"get_ban_description", l_get_ban_description},
1072 {"ban_player", l_ban_player},
1073 {"unban_player_or_ip", l_unban_player_of_ip},
1074 {"get_inventory", l_get_inventory},
1075 {"create_detached_inventory_raw", l_create_detached_inventory_raw},
1076 {"show_formspec", l_show_formspec},
1077 {"get_dig_params", l_get_dig_params},
1078 {"get_hit_params", l_get_hit_params},
1079 {"get_current_modname", l_get_current_modname},
1080 {"get_modpath", l_get_modpath},
1081 {"get_modnames", l_get_modnames},
1082 {"get_worldpath", l_get_worldpath},
1083 {"sound_play", l_sound_play},
1084 {"sound_stop", l_sound_stop},
1085 {"is_singleplayer", l_is_singleplayer},
1086 {"get_password_hash", l_get_password_hash},
1087 {"notify_authentication_modified", l_notify_authentication_modified},
1088 {"get_craft_result", l_get_craft_result},
1089 {"get_craft_recipe", l_get_craft_recipe},
1090 {"get_all_craft_recipes", l_get_all_craft_recipes},
1091 {"rollback_get_last_node_actor", l_rollback_get_last_node_actor},
1092 {"rollback_revert_actions_by", l_rollback_revert_actions_by},
1093 {"add_particle", l_add_particle},
1094 {"add_particlespawner", l_add_particlespawner},
1095 {"delete_particlespawner", l_delete_particlespawner},
1101 Main export function
1104 void scriptapi_export(lua_State *L, Server *server)
1107 assert(lua_checkstack(L, 20));
1108 verbosestream<<"scriptapi_export()"<<std::endl;
1109 StackUnroller stack_unroller(L);
1111 // Store server as light userdata in registry
1112 lua_pushlightuserdata(L, server);
1113 lua_setfield(L, LUA_REGISTRYINDEX, "minetest_server");
1115 // Register global functions in table minetest
1117 luaL_register(L, NULL, minetest_f);
1118 lua_setglobal(L, "minetest");
1120 // Get the main minetest table
1121 lua_getglobal(L, "minetest");
1123 // Add tables to minetest
1125 lua_setfield(L, -2, "object_refs");
1127 lua_setfield(L, -2, "luaentities");
1129 // Register wrappers
1130 LuaItemStack::Register(L);
1131 InvRef::Register(L);
1132 NodeMetaRef::Register(L);
1133 NodeTimerRef::Register(L);
1134 ObjectRef::Register(L);
1135 EnvRef::Register(L);
1136 LuaPseudoRandom::Register(L);
1137 LuaPerlinNoise::Register(L);
1138 LuaPerlinNoiseMap::Register(L);