3 Copyright (C) 2013 sapier
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 "lua_api/l_mainmenu.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_content.h"
23 #include "cpp_api/s_async.h"
24 #include "gui/guiEngine.h"
25 #include "gui/guiMainMenu.h"
26 #include "gui/guiKeyChangeMenu.h"
27 #include "gui/guiPathSelectMenu.h"
31 #include "convert_json.h"
32 #include "content/packages.h"
33 #include "content/content.h"
34 #include "content/subgames.h"
35 #include "serverlist.h"
36 #include "mapgen/mapgen.h"
39 #include <IFileArchive.h>
40 #include <IFileSystem.h>
41 #include "client/renderingengine.h"
44 /******************************************************************************/
45 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
47 lua_getglobal(L, "gamedata");
49 lua_getfield(L, -1, name.c_str());
54 return luaL_checkstring(L, -1);
57 /******************************************************************************/
58 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
60 lua_getglobal(L, "gamedata");
62 lua_getfield(L, -1, name.c_str());
64 if(lua_isnil(L, -1)) {
70 return luaL_checkinteger(L, -1);
73 /******************************************************************************/
74 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
76 lua_getglobal(L, "gamedata");
78 lua_getfield(L, -1, name.c_str());
80 if(lua_isnil(L, -1)) {
86 return lua_toboolean(L, -1);
89 /******************************************************************************/
90 int ModApiMainMenu::l_update_formspec(lua_State *L)
92 GUIEngine* engine = getGuiEngine(L);
93 sanity_check(engine != NULL);
95 if (engine->m_startgame)
99 std::string formspec(luaL_checkstring(L, 1));
101 if (engine->m_formspecgui != 0) {
102 engine->m_formspecgui->setForm(formspec);
108 /******************************************************************************/
109 int ModApiMainMenu::l_start(lua_State *L)
111 GUIEngine* engine = getGuiEngine(L);
112 sanity_check(engine != NULL);
114 //update c++ gamedata from lua table
118 MainMenuData *data = engine->m_data;
120 data->selected_world = getIntegerData(L, "selected_world",valid) -1;
121 data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
122 data->do_reconnect = getBoolData(L, "do_reconnect", valid);
123 if (!data->do_reconnect) {
124 data->name = getTextData(L,"playername");
125 data->password = getTextData(L,"password");
126 data->address = getTextData(L,"address");
127 data->port = getTextData(L,"port");
129 data->serverdescription = getTextData(L,"serverdescription");
130 data->servername = getTextData(L,"servername");
132 //close menu next time
133 engine->m_startgame = true;
137 /******************************************************************************/
138 int ModApiMainMenu::l_close(lua_State *L)
140 GUIEngine* engine = getGuiEngine(L);
141 sanity_check(engine != NULL);
143 engine->m_kill = true;
147 /******************************************************************************/
148 int ModApiMainMenu::l_set_background(lua_State *L)
150 GUIEngine* engine = getGuiEngine(L);
151 sanity_check(engine != NULL);
153 std::string backgroundlevel(luaL_checkstring(L, 1));
154 std::string texturename(luaL_checkstring(L, 2));
156 bool tile_image = false;
158 unsigned int minsize = 16;
160 if (!lua_isnone(L, 3)) {
161 tile_image = lua_toboolean(L, 3);
164 if (!lua_isnone(L, 4)) {
165 minsize = lua_tonumber(L, 4);
168 if (backgroundlevel == "background") {
169 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
170 tile_image, minsize);
173 if (backgroundlevel == "overlay") {
174 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
175 tile_image, minsize);
178 if (backgroundlevel == "header") {
179 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
180 tile_image, minsize);
183 if (backgroundlevel == "footer") {
184 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
185 tile_image, minsize);
188 lua_pushboolean(L,retval);
192 /******************************************************************************/
193 int ModApiMainMenu::l_set_clouds(lua_State *L)
195 GUIEngine* engine = getGuiEngine(L);
196 sanity_check(engine != NULL);
198 bool value = lua_toboolean(L,1);
200 engine->m_clouds_enabled = value;
205 /******************************************************************************/
206 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
208 // get_table_index accepts both tables and textlists
209 return l_get_table_index(L);
212 /******************************************************************************/
213 int ModApiMainMenu::l_get_table_index(lua_State *L)
215 GUIEngine* engine = getGuiEngine(L);
216 sanity_check(engine != NULL);
218 std::string tablename(luaL_checkstring(L, 1));
219 GUITable *table = engine->m_menu->getTable(tablename);
220 s32 selection = table ? table->getSelected() : 0;
223 lua_pushinteger(L, selection);
229 /******************************************************************************/
230 int ModApiMainMenu::l_get_worlds(lua_State *L)
232 std::vector<WorldSpec> worlds = getAvailableWorlds();
235 int top = lua_gettop(L);
236 unsigned int index = 1;
238 for (const WorldSpec &world : worlds) {
239 lua_pushnumber(L,index);
242 int top_lvl2 = lua_gettop(L);
244 lua_pushstring(L,"path");
245 lua_pushstring(L, world.path.c_str());
246 lua_settable(L, top_lvl2);
248 lua_pushstring(L,"name");
249 lua_pushstring(L, world.name.c_str());
250 lua_settable(L, top_lvl2);
252 lua_pushstring(L,"gameid");
253 lua_pushstring(L, world.gameid.c_str());
254 lua_settable(L, top_lvl2);
256 lua_settable(L, top);
262 /******************************************************************************/
263 int ModApiMainMenu::l_get_favorites(lua_State *L)
265 std::string listtype = "local";
267 if (!lua_isnone(L,1)) {
268 listtype = luaL_checkstring(L,1);
271 std::vector<ServerListSpec> servers;
273 if(listtype == "online") {
274 servers = ServerList::getOnline();
276 servers = ServerList::getLocal();
280 int top = lua_gettop(L);
281 unsigned int index = 1;
283 for (const Json::Value &server : servers) {
285 lua_pushnumber(L,index);
288 int top_lvl2 = lua_gettop(L);
290 if (!server["clients"].asString().empty()) {
291 std::string clients_raw = server["clients"].asString();
293 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
295 if ((!clients_raw.empty()) && (*endptr == 0)) {
296 lua_pushstring(L,"clients");
297 lua_pushnumber(L,numbervalue);
298 lua_settable(L, top_lvl2);
302 if (!server["clients_max"].asString().empty()) {
304 std::string clients_max_raw = server["clients_max"].asString();
306 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
308 if ((!clients_max_raw.empty()) && (*endptr == 0)) {
309 lua_pushstring(L,"clients_max");
310 lua_pushnumber(L,numbervalue);
311 lua_settable(L, top_lvl2);
315 if (!server["version"].asString().empty()) {
316 lua_pushstring(L,"version");
317 std::string topush = server["version"].asString();
318 lua_pushstring(L,topush.c_str());
319 lua_settable(L, top_lvl2);
322 if (!server["proto_min"].asString().empty()) {
323 lua_pushstring(L,"proto_min");
324 lua_pushinteger(L, server["proto_min"].asInt());
325 lua_settable(L, top_lvl2);
328 if (!server["proto_max"].asString().empty()) {
329 lua_pushstring(L,"proto_max");
330 lua_pushinteger(L, server["proto_max"].asInt());
331 lua_settable(L, top_lvl2);
334 if (!server["password"].asString().empty()) {
335 lua_pushstring(L,"password");
336 lua_pushboolean(L, server["password"].asBool());
337 lua_settable(L, top_lvl2);
340 if (!server["creative"].asString().empty()) {
341 lua_pushstring(L,"creative");
342 lua_pushboolean(L, server["creative"].asBool());
343 lua_settable(L, top_lvl2);
346 if (!server["damage"].asString().empty()) {
347 lua_pushstring(L,"damage");
348 lua_pushboolean(L, server["damage"].asBool());
349 lua_settable(L, top_lvl2);
352 if (!server["pvp"].asString().empty()) {
353 lua_pushstring(L,"pvp");
354 lua_pushboolean(L, server["pvp"].asBool());
355 lua_settable(L, top_lvl2);
358 if (!server["description"].asString().empty()) {
359 lua_pushstring(L,"description");
360 std::string topush = server["description"].asString();
361 lua_pushstring(L,topush.c_str());
362 lua_settable(L, top_lvl2);
365 if (!server["name"].asString().empty()) {
366 lua_pushstring(L,"name");
367 std::string topush = server["name"].asString();
368 lua_pushstring(L,topush.c_str());
369 lua_settable(L, top_lvl2);
372 if (!server["address"].asString().empty()) {
373 lua_pushstring(L,"address");
374 std::string topush = server["address"].asString();
375 lua_pushstring(L,topush.c_str());
376 lua_settable(L, top_lvl2);
379 if (!server["port"].asString().empty()) {
380 lua_pushstring(L,"port");
381 std::string topush = server["port"].asString();
382 lua_pushstring(L,topush.c_str());
383 lua_settable(L, top_lvl2);
386 if (server.isMember("ping")) {
387 float ping = server["ping"].asFloat();
388 lua_pushstring(L, "ping");
389 lua_pushnumber(L, ping);
390 lua_settable(L, top_lvl2);
393 lua_settable(L, top);
399 /******************************************************************************/
400 int ModApiMainMenu::l_delete_favorite(lua_State *L)
402 std::vector<ServerListSpec> servers;
404 std::string listtype = "local";
406 if (!lua_isnone(L,2)) {
407 listtype = luaL_checkstring(L,2);
410 if ((listtype != "local") &&
411 (listtype != "online"))
415 if(listtype == "online") {
416 servers = ServerList::getOnline();
418 servers = ServerList::getLocal();
421 int fav_idx = luaL_checkinteger(L,1) -1;
423 if ((fav_idx >= 0) &&
424 (fav_idx < (int) servers.size())) {
426 ServerList::deleteEntry(servers[fav_idx]);
432 /******************************************************************************/
433 int ModApiMainMenu::l_get_games(lua_State *L)
435 std::vector<SubgameSpec> games = getAvailableGames();
438 int top = lua_gettop(L);
439 unsigned int index = 1;
441 for (const SubgameSpec &game : games) {
442 lua_pushnumber(L, index);
444 int top_lvl2 = lua_gettop(L);
446 lua_pushstring(L, "id");
447 lua_pushstring(L, game.id.c_str());
448 lua_settable(L, top_lvl2);
450 lua_pushstring(L, "path");
451 lua_pushstring(L, game.path.c_str());
452 lua_settable(L, top_lvl2);
454 lua_pushstring(L, "type");
455 lua_pushstring(L, "game");
456 lua_settable(L, top_lvl2);
458 lua_pushstring(L, "gamemods_path");
459 lua_pushstring(L, game.gamemods_path.c_str());
460 lua_settable(L, top_lvl2);
462 lua_pushstring(L, "name");
463 lua_pushstring(L, game.name.c_str());
464 lua_settable(L, top_lvl2);
466 lua_pushstring(L, "author");
467 lua_pushstring(L, game.author.c_str());
468 lua_settable(L, top_lvl2);
470 lua_pushstring(L, "menuicon_path");
471 lua_pushstring(L, game.menuicon_path.c_str());
472 lua_settable(L, top_lvl2);
474 lua_pushstring(L, "addon_mods_paths");
476 int table2 = lua_gettop(L);
477 int internal_index = 1;
478 for (const std::string &addon_mods_path : game.addon_mods_paths) {
479 lua_pushnumber(L, internal_index);
480 lua_pushstring(L, addon_mods_path.c_str());
481 lua_settable(L, table2);
484 lua_settable(L, top_lvl2);
485 lua_settable(L, top);
491 /******************************************************************************/
492 int ModApiMainMenu::l_get_content_info(lua_State *L)
494 std::string path = luaL_checkstring(L, 1);
498 parseContentInfo(spec);
502 lua_pushstring(L, spec.name.c_str());
503 lua_setfield(L, -2, "name");
505 lua_pushstring(L, spec.type.c_str());
506 lua_setfield(L, -2, "type");
508 lua_pushstring(L, spec.author.c_str());
509 lua_setfield(L, -2, "author");
511 lua_pushstring(L, spec.desc.c_str());
512 lua_setfield(L, -2, "description");
514 lua_pushstring(L, spec.path.c_str());
515 lua_setfield(L, -2, "path");
517 if (spec.type == "mod") {
520 parseModContents(spec);
525 for (const auto &dep : spec.depends) {
526 lua_pushstring(L, dep.c_str());
527 lua_rawseti(L, -2, i);
530 lua_setfield(L, -2, "depends");
532 // Optional Dependencies
535 for (const auto &dep : spec.optdepends) {
536 lua_pushstring(L, dep.c_str());
537 lua_rawseti(L, -2, i);
540 lua_setfield(L, -2, "optional_depends");
546 /******************************************************************************/
547 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
549 GUIEngine* engine = getGuiEngine(L);
550 sanity_check(engine != NULL);
552 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
555 engine->m_menumanager);
560 /******************************************************************************/
561 int ModApiMainMenu::l_create_world(lua_State *L)
563 const char *name = luaL_checkstring(L, 1);
564 int gameidx = luaL_checkinteger(L,2) -1;
566 std::string path = porting::path_user + DIR_DELIM
570 std::vector<SubgameSpec> games = getAvailableGames();
572 if ((gameidx >= 0) &&
573 (gameidx < (int) games.size())) {
575 // Create world if it doesn't exist
576 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
577 lua_pushstring(L, "Failed to initialize world");
582 lua_pushstring(L, "Invalid game index");
587 /******************************************************************************/
588 int ModApiMainMenu::l_delete_world(lua_State *L)
590 int worldidx = luaL_checkinteger(L,1) -1;
592 std::vector<WorldSpec> worlds = getAvailableWorlds();
594 if ((worldidx >= 0) &&
595 (worldidx < (int) worlds.size())) {
597 WorldSpec spec = worlds[worldidx];
599 std::vector<std::string> paths;
600 paths.push_back(spec.path);
601 fs::GetRecursiveSubPaths(spec.path, paths, true);
604 if (!fs::DeletePaths(paths)) {
605 lua_pushstring(L, "Failed to delete world");
612 lua_pushstring(L, "Invalid world index");
617 /******************************************************************************/
618 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
620 GUIEngine* engine = getGuiEngine(L);
621 sanity_check(engine != NULL);
625 if (!lua_isnone(L,1) && !lua_isnil(L,1))
626 text = luaL_checkstring(L, 1);
628 engine->setTopleftText(text);
632 /******************************************************************************/
633 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
635 std::vector<const char *> names;
636 Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
639 for (size_t i = 0; i != names.size(); i++) {
640 lua_pushstring(L, names[i]);
641 lua_rawseti(L, -2, i + 1);
648 /******************************************************************************/
649 int ModApiMainMenu::l_get_modpath(lua_State *L)
651 std::string modpath = fs::RemoveRelativePathComponents(
652 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
653 lua_pushstring(L, modpath.c_str());
657 /******************************************************************************/
658 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
660 std::string modpath = fs::RemoveRelativePathComponents(
661 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
662 lua_pushstring(L, modpath.c_str());
666 /******************************************************************************/
667 int ModApiMainMenu::l_get_gamepath(lua_State *L)
669 std::string gamepath = fs::RemoveRelativePathComponents(
670 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
671 lua_pushstring(L, gamepath.c_str());
675 /******************************************************************************/
676 int ModApiMainMenu::l_get_texturepath(lua_State *L)
678 std::string gamepath = fs::RemoveRelativePathComponents(
679 porting::path_user + DIR_DELIM + "textures");
680 lua_pushstring(L, gamepath.c_str());
684 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
686 std::string gamepath = fs::RemoveRelativePathComponents(
687 porting::path_share + DIR_DELIM + "textures");
688 lua_pushstring(L, gamepath.c_str());
692 /******************************************************************************/
693 int ModApiMainMenu::l_create_dir(lua_State *L) {
694 const char *path = luaL_checkstring(L, 1);
696 if (ModApiMainMenu::isMinetestPath(path)) {
697 lua_pushboolean(L, fs::CreateAllDirs(path));
701 lua_pushboolean(L, false);
705 /******************************************************************************/
706 int ModApiMainMenu::l_delete_dir(lua_State *L)
708 const char *path = luaL_checkstring(L, 1);
710 std::string absolute_path = fs::RemoveRelativePathComponents(path);
712 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
713 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
717 lua_pushboolean(L, false);
721 /******************************************************************************/
722 int ModApiMainMenu::l_copy_dir(lua_State *L)
724 const char *source = luaL_checkstring(L, 1);
725 const char *destination = luaL_checkstring(L, 2);
727 bool keep_source = true;
729 if ((!lua_isnone(L,3)) &&
731 keep_source = lua_toboolean(L,3);
734 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
735 std::string absolute_source = fs::RemoveRelativePathComponents(source);
737 if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
738 bool retval = fs::CopyDir(absolute_source,absolute_destination);
740 if (retval && (!keep_source)) {
742 retval &= fs::RecursiveDelete(absolute_source);
744 lua_pushboolean(L,retval);
747 lua_pushboolean(L,false);
751 /******************************************************************************/
752 int ModApiMainMenu::l_extract_zip(lua_State *L)
754 const char *zipfile = luaL_checkstring(L, 1);
755 const char *destination = luaL_checkstring(L, 2);
757 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
759 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
760 fs::CreateAllDirs(absolute_destination);
762 io::IFileSystem *fs = RenderingEngine::get_filesystem();
764 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
765 lua_pushboolean(L,false);
769 sanity_check(fs->getFileArchiveCount() > 0);
771 /**********************************************************************/
772 /* WARNING this is not threadsafe!! */
773 /**********************************************************************/
774 io::IFileArchive* opened_zip =
775 fs->getFileArchive(fs->getFileArchiveCount()-1);
777 const io::IFileList* files_in_zip = opened_zip->getFileList();
779 unsigned int number_of_files = files_in_zip->getFileCount();
781 for (unsigned int i=0; i < number_of_files; i++) {
782 std::string fullpath = destination;
783 fullpath += DIR_DELIM;
784 fullpath += files_in_zip->getFullFileName(i).c_str();
785 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
787 if (!files_in_zip->isDirectory(i)) {
788 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
789 fs->removeFileArchive(fs->getFileArchiveCount()-1);
790 lua_pushboolean(L,false);
794 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
796 FILE *targetfile = fopen(fullpath.c_str(),"wb");
798 if (targetfile == NULL) {
799 fs->removeFileArchive(fs->getFileArchiveCount()-1);
800 lua_pushboolean(L,false);
804 char read_buffer[1024];
807 while (total_read < toread->getSize()) {
809 unsigned int bytes_read =
810 toread->read(read_buffer,sizeof(read_buffer));
811 if ((bytes_read == 0 ) ||
812 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
815 fs->removeFileArchive(fs->getFileArchiveCount()-1);
816 lua_pushboolean(L,false);
819 total_read += bytes_read;
827 fs->removeFileArchive(fs->getFileArchiveCount()-1);
828 lua_pushboolean(L,true);
832 lua_pushboolean(L,false);
836 /******************************************************************************/
837 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
839 GUIEngine* engine = getGuiEngine(L);
840 sanity_check(engine != NULL);
842 lua_pushstring(L,engine->getScriptDir().c_str());
846 /******************************************************************************/
847 bool ModApiMainMenu::isMinetestPath(std::string path)
849 if (fs::PathStartsWith(path,fs::TempPath()))
853 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
857 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
861 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures")))
865 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
872 /******************************************************************************/
873 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
875 GUIEngine* engine = getGuiEngine(L);
876 sanity_check(engine != NULL);
878 const char *formname= luaL_checkstring(L, 1);
879 const char *title = luaL_checkstring(L, 2);
880 bool is_file_select = lua_toboolean(L, 3);
882 GUIFileSelectMenu* fileOpenMenu =
883 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
886 engine->m_menumanager,
890 fileOpenMenu->setTextDest(engine->m_buttonhandler);
891 fileOpenMenu->drop();
895 /******************************************************************************/
896 int ModApiMainMenu::l_download_file(lua_State *L)
898 const char *url = luaL_checkstring(L, 1);
899 const char *target = luaL_checkstring(L, 2);
902 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
904 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
905 if (GUIEngine::downloadFile(url,absolute_destination)) {
906 lua_pushboolean(L,true);
910 errorstream << "DOWNLOAD denied: " << absolute_destination
911 << " isn't a allowed path" << std::endl;
913 lua_pushboolean(L,false);
917 /******************************************************************************/
918 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
920 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
923 for (u32 i = 0; i != drivers.size(); i++) {
924 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
925 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
928 lua_pushstring(L, name);
929 lua_setfield(L, -2, "name");
930 lua_pushstring(L, fname);
931 lua_setfield(L, -2, "friendly_name");
933 lua_rawseti(L, -2, i + 1);
939 /******************************************************************************/
940 int ModApiMainMenu::l_get_video_modes(lua_State *L)
942 std::vector<core::vector3d<u32> > videomodes
943 = RenderingEngine::getSupportedVideoModes();
946 for (u32 i = 0; i != videomodes.size(); i++) {
948 lua_pushnumber(L, videomodes[i].X);
949 lua_setfield(L, -2, "w");
950 lua_pushnumber(L, videomodes[i].Y);
951 lua_setfield(L, -2, "h");
952 lua_pushnumber(L, videomodes[i].Z);
953 lua_setfield(L, -2, "depth");
955 lua_rawseti(L, -2, i + 1);
961 /******************************************************************************/
962 int ModApiMainMenu::l_gettext(lua_State *L)
964 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
965 lua_pushstring(L, text.c_str());
970 /******************************************************************************/
971 int ModApiMainMenu::l_get_screen_info(lua_State *L)
974 int top = lua_gettop(L);
975 lua_pushstring(L,"density");
976 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
977 lua_settable(L, top);
979 lua_pushstring(L,"display_width");
980 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
981 lua_settable(L, top);
983 lua_pushstring(L,"display_height");
984 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
985 lua_settable(L, top);
987 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
988 lua_pushstring(L,"window_width");
989 lua_pushnumber(L, window_size.X);
990 lua_settable(L, top);
992 lua_pushstring(L,"window_height");
993 lua_pushnumber(L, window_size.Y);
994 lua_settable(L, top);
998 int ModApiMainMenu::l_get_package_list(lua_State *L)
1000 std::string url = g_settings->get("contentdb_url");
1001 std::vector<Package> packages = getPackagesFromURL(url + "/packages/");
1005 int top = lua_gettop(L);
1006 unsigned int index = 1;
1009 for (const auto &package : packages) {
1010 lua_pushnumber(L, index);
1013 int top_lvl2 = lua_gettop(L);
1015 lua_pushstring(L, "name");
1016 lua_pushstring(L, package.name.c_str());
1017 lua_settable (L, top_lvl2);
1019 lua_pushstring(L, "title");
1020 lua_pushstring(L, package.title.c_str());
1021 lua_settable (L, top_lvl2);
1023 lua_pushstring(L, "author");
1024 lua_pushstring(L, package.author.c_str());
1025 lua_settable (L, top_lvl2);
1027 lua_pushstring(L, "type");
1028 lua_pushstring(L, package.type.c_str());
1029 lua_settable (L, top_lvl2);
1031 lua_pushstring(L, "short_description");
1032 lua_pushstring(L, package.shortDesc.c_str());
1033 lua_settable (L, top_lvl2);
1035 lua_pushstring(L, "url");
1036 lua_pushstring(L, package.url.c_str());
1037 lua_settable (L, top_lvl2);
1039 lua_settable(L, top);
1046 /******************************************************************************/
1047 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
1049 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
1053 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1055 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1059 /******************************************************************************/
1060 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1062 GUIEngine* engine = getGuiEngine(L);
1064 size_t func_length, param_length;
1065 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1067 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1069 sanity_check(serialized_func_raw != NULL);
1070 sanity_check(serialized_param_raw != NULL);
1072 std::string serialized_func = std::string(serialized_func_raw, func_length);
1073 std::string serialized_param = std::string(serialized_param_raw, param_length);
1075 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1080 /******************************************************************************/
1081 void ModApiMainMenu::Initialize(lua_State *L, int top)
1083 API_FCT(update_formspec);
1084 API_FCT(set_clouds);
1085 API_FCT(get_textlist_index);
1086 API_FCT(get_table_index);
1087 API_FCT(get_worlds);
1089 API_FCT(get_content_info);
1092 API_FCT(get_favorites);
1093 API_FCT(show_keys_menu);
1094 API_FCT(create_world);
1095 API_FCT(delete_world);
1096 API_FCT(delete_favorite);
1097 API_FCT(set_background);
1098 API_FCT(set_topleft_text);
1099 API_FCT(get_mapgen_names);
1100 API_FCT(get_modpath);
1101 API_FCT(get_clientmodpath);
1102 API_FCT(get_gamepath);
1103 API_FCT(get_texturepath);
1104 API_FCT(get_texturepath_share);
1105 API_FCT(create_dir);
1106 API_FCT(delete_dir);
1108 API_FCT(extract_zip);
1109 API_FCT(get_mainmenu_path);
1110 API_FCT(show_path_select_dialog);
1111 API_FCT(download_file);
1113 API_FCT(get_video_drivers);
1114 API_FCT(get_video_modes);
1115 API_FCT(get_screen_info);
1116 API_FCT(get_package_list);
1117 API_FCT(get_min_supp_proto);
1118 API_FCT(get_max_supp_proto);
1119 API_FCT(do_async_callback);
1122 /******************************************************************************/
1123 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1125 API_FCT(get_worlds);
1127 API_FCT(get_favorites);
1128 API_FCT(get_mapgen_names);
1129 API_FCT(get_modpath);
1130 API_FCT(get_clientmodpath);
1131 API_FCT(get_gamepath);
1132 API_FCT(get_texturepath);
1133 API_FCT(get_texturepath_share);
1134 API_FCT(create_dir);
1135 API_FCT(delete_dir);
1137 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1138 API_FCT(download_file);
1139 //API_FCT(gettext); (gettext lib isn't threadsafe)
1140 API_FCT(get_package_list);