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"
42 #include "network/networkprotocol.h"
45 /******************************************************************************/
46 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
48 lua_getglobal(L, "gamedata");
50 lua_getfield(L, -1, name.c_str());
55 return luaL_checkstring(L, -1);
58 /******************************************************************************/
59 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
61 lua_getglobal(L, "gamedata");
63 lua_getfield(L, -1, name.c_str());
65 if(lua_isnil(L, -1)) {
71 return luaL_checkinteger(L, -1);
74 /******************************************************************************/
75 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
77 lua_getglobal(L, "gamedata");
79 lua_getfield(L, -1, name.c_str());
81 if(lua_isnil(L, -1)) {
87 return readParam<bool>(L, -1);
90 /******************************************************************************/
91 int ModApiMainMenu::l_update_formspec(lua_State *L)
93 GUIEngine* engine = getGuiEngine(L);
94 sanity_check(engine != NULL);
96 if (engine->m_startgame)
100 std::string formspec(luaL_checkstring(L, 1));
102 if (engine->m_formspecgui != 0) {
103 engine->m_formspecgui->setForm(formspec);
109 /******************************************************************************/
110 int ModApiMainMenu::l_start(lua_State *L)
112 GUIEngine* engine = getGuiEngine(L);
113 sanity_check(engine != NULL);
115 //update c++ gamedata from lua table
119 MainMenuData *data = engine->m_data;
121 data->selected_world = getIntegerData(L, "selected_world",valid) -1;
122 data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
123 data->do_reconnect = getBoolData(L, "do_reconnect", valid);
124 if (!data->do_reconnect) {
125 data->name = getTextData(L,"playername");
126 data->password = getTextData(L,"password");
127 data->address = getTextData(L,"address");
128 data->port = getTextData(L,"port");
130 data->serverdescription = getTextData(L,"serverdescription");
131 data->servername = getTextData(L,"servername");
133 //close menu next time
134 engine->m_startgame = true;
138 /******************************************************************************/
139 int ModApiMainMenu::l_close(lua_State *L)
141 GUIEngine* engine = getGuiEngine(L);
142 sanity_check(engine != NULL);
144 engine->m_kill = true;
148 /******************************************************************************/
149 int ModApiMainMenu::l_set_background(lua_State *L)
151 GUIEngine* engine = getGuiEngine(L);
152 sanity_check(engine != NULL);
154 std::string backgroundlevel(luaL_checkstring(L, 1));
155 std::string texturename(luaL_checkstring(L, 2));
157 bool tile_image = false;
159 unsigned int minsize = 16;
161 if (!lua_isnone(L, 3)) {
162 tile_image = readParam<bool>(L, 3);
165 if (!lua_isnone(L, 4)) {
166 minsize = lua_tonumber(L, 4);
169 if (backgroundlevel == "background") {
170 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
171 tile_image, minsize);
174 if (backgroundlevel == "overlay") {
175 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
176 tile_image, minsize);
179 if (backgroundlevel == "header") {
180 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
181 tile_image, minsize);
184 if (backgroundlevel == "footer") {
185 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
186 tile_image, minsize);
189 lua_pushboolean(L,retval);
193 /******************************************************************************/
194 int ModApiMainMenu::l_set_clouds(lua_State *L)
196 GUIEngine* engine = getGuiEngine(L);
197 sanity_check(engine != NULL);
199 bool value = readParam<bool>(L,1);
201 engine->m_clouds_enabled = value;
206 /******************************************************************************/
207 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
209 // get_table_index accepts both tables and textlists
210 return l_get_table_index(L);
213 /******************************************************************************/
214 int ModApiMainMenu::l_get_table_index(lua_State *L)
216 GUIEngine* engine = getGuiEngine(L);
217 sanity_check(engine != NULL);
219 std::string tablename(luaL_checkstring(L, 1));
220 GUITable *table = engine->m_menu->getTable(tablename);
221 s32 selection = table ? table->getSelected() : 0;
224 lua_pushinteger(L, selection);
230 /******************************************************************************/
231 int ModApiMainMenu::l_get_worlds(lua_State *L)
233 std::vector<WorldSpec> worlds = getAvailableWorlds();
236 int top = lua_gettop(L);
237 unsigned int index = 1;
239 for (const WorldSpec &world : worlds) {
240 lua_pushnumber(L,index);
243 int top_lvl2 = lua_gettop(L);
245 lua_pushstring(L,"path");
246 lua_pushstring(L, world.path.c_str());
247 lua_settable(L, top_lvl2);
249 lua_pushstring(L,"name");
250 lua_pushstring(L, world.name.c_str());
251 lua_settable(L, top_lvl2);
253 lua_pushstring(L,"gameid");
254 lua_pushstring(L, world.gameid.c_str());
255 lua_settable(L, top_lvl2);
257 lua_settable(L, top);
263 /******************************************************************************/
264 int ModApiMainMenu::l_get_favorites(lua_State *L)
266 std::string listtype = "local";
268 if (!lua_isnone(L,1)) {
269 listtype = luaL_checkstring(L,1);
272 std::vector<ServerListSpec> servers;
274 if(listtype == "online") {
275 servers = ServerList::getOnline();
277 servers = ServerList::getLocal();
281 int top = lua_gettop(L);
282 unsigned int index = 1;
284 for (const Json::Value &server : servers) {
286 lua_pushnumber(L,index);
289 int top_lvl2 = lua_gettop(L);
291 if (!server["clients"].asString().empty()) {
292 std::string clients_raw = server["clients"].asString();
294 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
296 if ((!clients_raw.empty()) && (*endptr == 0)) {
297 lua_pushstring(L,"clients");
298 lua_pushnumber(L,numbervalue);
299 lua_settable(L, top_lvl2);
303 if (!server["clients_max"].asString().empty()) {
305 std::string clients_max_raw = server["clients_max"].asString();
307 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
309 if ((!clients_max_raw.empty()) && (*endptr == 0)) {
310 lua_pushstring(L,"clients_max");
311 lua_pushnumber(L,numbervalue);
312 lua_settable(L, top_lvl2);
316 if (!server["version"].asString().empty()) {
317 lua_pushstring(L,"version");
318 std::string topush = server["version"].asString();
319 lua_pushstring(L,topush.c_str());
320 lua_settable(L, top_lvl2);
323 if (!server["proto_min"].asString().empty()) {
324 lua_pushstring(L,"proto_min");
325 lua_pushinteger(L, server["proto_min"].asInt());
326 lua_settable(L, top_lvl2);
329 if (!server["proto_max"].asString().empty()) {
330 lua_pushstring(L,"proto_max");
331 lua_pushinteger(L, server["proto_max"].asInt());
332 lua_settable(L, top_lvl2);
335 if (!server["password"].asString().empty()) {
336 lua_pushstring(L,"password");
337 lua_pushboolean(L, server["password"].asBool());
338 lua_settable(L, top_lvl2);
341 if (!server["creative"].asString().empty()) {
342 lua_pushstring(L,"creative");
343 lua_pushboolean(L, server["creative"].asBool());
344 lua_settable(L, top_lvl2);
347 if (!server["damage"].asString().empty()) {
348 lua_pushstring(L,"damage");
349 lua_pushboolean(L, server["damage"].asBool());
350 lua_settable(L, top_lvl2);
353 if (!server["pvp"].asString().empty()) {
354 lua_pushstring(L,"pvp");
355 lua_pushboolean(L, server["pvp"].asBool());
356 lua_settable(L, top_lvl2);
359 if (!server["description"].asString().empty()) {
360 lua_pushstring(L,"description");
361 std::string topush = server["description"].asString();
362 lua_pushstring(L,topush.c_str());
363 lua_settable(L, top_lvl2);
366 if (!server["name"].asString().empty()) {
367 lua_pushstring(L,"name");
368 std::string topush = server["name"].asString();
369 lua_pushstring(L,topush.c_str());
370 lua_settable(L, top_lvl2);
373 if (!server["address"].asString().empty()) {
374 lua_pushstring(L,"address");
375 std::string topush = server["address"].asString();
376 lua_pushstring(L,topush.c_str());
377 lua_settable(L, top_lvl2);
380 if (!server["port"].asString().empty()) {
381 lua_pushstring(L,"port");
382 std::string topush = server["port"].asString();
383 lua_pushstring(L,topush.c_str());
384 lua_settable(L, top_lvl2);
387 if (server.isMember("ping")) {
388 float ping = server["ping"].asFloat();
389 lua_pushstring(L, "ping");
390 lua_pushnumber(L, ping);
391 lua_settable(L, top_lvl2);
394 lua_settable(L, top);
400 /******************************************************************************/
401 int ModApiMainMenu::l_delete_favorite(lua_State *L)
403 std::vector<ServerListSpec> servers;
405 std::string listtype = "local";
407 if (!lua_isnone(L,2)) {
408 listtype = luaL_checkstring(L,2);
411 if ((listtype != "local") &&
412 (listtype != "online"))
416 if(listtype == "online") {
417 servers = ServerList::getOnline();
419 servers = ServerList::getLocal();
422 int fav_idx = luaL_checkinteger(L,1) -1;
424 if ((fav_idx >= 0) &&
425 (fav_idx < (int) servers.size())) {
427 ServerList::deleteEntry(servers[fav_idx]);
433 /******************************************************************************/
434 int ModApiMainMenu::l_get_games(lua_State *L)
436 std::vector<SubgameSpec> games = getAvailableGames();
439 int top = lua_gettop(L);
440 unsigned int index = 1;
442 for (const SubgameSpec &game : games) {
443 lua_pushnumber(L, index);
445 int top_lvl2 = lua_gettop(L);
447 lua_pushstring(L, "id");
448 lua_pushstring(L, game.id.c_str());
449 lua_settable(L, top_lvl2);
451 lua_pushstring(L, "path");
452 lua_pushstring(L, game.path.c_str());
453 lua_settable(L, top_lvl2);
455 lua_pushstring(L, "type");
456 lua_pushstring(L, "game");
457 lua_settable(L, top_lvl2);
459 lua_pushstring(L, "gamemods_path");
460 lua_pushstring(L, game.gamemods_path.c_str());
461 lua_settable(L, top_lvl2);
463 lua_pushstring(L, "name");
464 lua_pushstring(L, game.name.c_str());
465 lua_settable(L, top_lvl2);
467 lua_pushstring(L, "author");
468 lua_pushstring(L, game.author.c_str());
469 lua_settable(L, top_lvl2);
471 lua_pushstring(L, "release");
472 lua_pushinteger(L, game.release);
473 lua_settable(L, top_lvl2);
475 lua_pushstring(L, "menuicon_path");
476 lua_pushstring(L, game.menuicon_path.c_str());
477 lua_settable(L, top_lvl2);
479 lua_pushstring(L, "addon_mods_paths");
481 int table2 = lua_gettop(L);
482 int internal_index = 1;
483 for (const std::string &addon_mods_path : game.addon_mods_paths) {
484 lua_pushnumber(L, internal_index);
485 lua_pushstring(L, addon_mods_path.c_str());
486 lua_settable(L, table2);
489 lua_settable(L, top_lvl2);
490 lua_settable(L, top);
496 /******************************************************************************/
497 int ModApiMainMenu::l_get_content_info(lua_State *L)
499 std::string path = luaL_checkstring(L, 1);
503 parseContentInfo(spec);
507 lua_pushstring(L, spec.name.c_str());
508 lua_setfield(L, -2, "name");
510 lua_pushstring(L, spec.type.c_str());
511 lua_setfield(L, -2, "type");
513 lua_pushstring(L, spec.author.c_str());
514 lua_setfield(L, -2, "author");
516 lua_pushinteger(L, spec.release);
517 lua_setfield(L, -2, "release");
519 lua_pushstring(L, spec.desc.c_str());
520 lua_setfield(L, -2, "description");
522 lua_pushstring(L, spec.path.c_str());
523 lua_setfield(L, -2, "path");
525 if (spec.type == "mod") {
528 parseModContents(spec);
533 for (const auto &dep : spec.depends) {
534 lua_pushstring(L, dep.c_str());
535 lua_rawseti(L, -2, i++);
537 lua_setfield(L, -2, "depends");
539 // Optional Dependencies
542 for (const auto &dep : spec.optdepends) {
543 lua_pushstring(L, dep.c_str());
544 lua_rawseti(L, -2, i++);
546 lua_setfield(L, -2, "optional_depends");
552 /******************************************************************************/
553 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
555 GUIEngine* engine = getGuiEngine(L);
556 sanity_check(engine != NULL);
558 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
561 engine->m_menumanager);
566 /******************************************************************************/
567 int ModApiMainMenu::l_create_world(lua_State *L)
569 const char *name = luaL_checkstring(L, 1);
570 int gameidx = luaL_checkinteger(L,2) -1;
572 std::string path = porting::path_user + DIR_DELIM
576 std::vector<SubgameSpec> games = getAvailableGames();
578 if ((gameidx >= 0) &&
579 (gameidx < (int) games.size())) {
581 // Create world if it doesn't exist
582 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
583 lua_pushstring(L, "Failed to initialize world");
588 lua_pushstring(L, "Invalid game index");
593 /******************************************************************************/
594 int ModApiMainMenu::l_delete_world(lua_State *L)
596 int world_id = luaL_checkinteger(L, 1) - 1;
597 std::vector<WorldSpec> worlds = getAvailableWorlds();
598 if (world_id < 0 || world_id >= (int) worlds.size()) {
599 lua_pushstring(L, "Invalid world index");
602 const WorldSpec &spec = worlds[world_id];
603 if (!fs::RecursiveDelete(spec.path)) {
604 lua_pushstring(L, "Failed to delete world");
610 /******************************************************************************/
611 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
613 GUIEngine* engine = getGuiEngine(L);
614 sanity_check(engine != NULL);
618 if (!lua_isnone(L,1) && !lua_isnil(L,1))
619 text = luaL_checkstring(L, 1);
621 engine->setTopleftText(text);
625 /******************************************************************************/
626 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
628 std::vector<const char *> names;
629 bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
630 Mapgen::getMapgenNames(&names, include_hidden);
633 for (size_t i = 0; i != names.size(); i++) {
634 lua_pushstring(L, names[i]);
635 lua_rawseti(L, -2, i + 1);
642 /******************************************************************************/
643 int ModApiMainMenu::l_get_modpath(lua_State *L)
645 std::string modpath = fs::RemoveRelativePathComponents(
646 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
647 lua_pushstring(L, modpath.c_str());
651 /******************************************************************************/
652 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
654 std::string modpath = fs::RemoveRelativePathComponents(
655 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
656 lua_pushstring(L, modpath.c_str());
660 /******************************************************************************/
661 int ModApiMainMenu::l_get_gamepath(lua_State *L)
663 std::string gamepath = fs::RemoveRelativePathComponents(
664 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
665 lua_pushstring(L, gamepath.c_str());
669 /******************************************************************************/
670 int ModApiMainMenu::l_get_texturepath(lua_State *L)
672 std::string gamepath = fs::RemoveRelativePathComponents(
673 porting::path_user + DIR_DELIM + "textures");
674 lua_pushstring(L, gamepath.c_str());
678 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
680 std::string gamepath = fs::RemoveRelativePathComponents(
681 porting::path_share + DIR_DELIM + "textures");
682 lua_pushstring(L, gamepath.c_str());
686 /******************************************************************************/
687 int ModApiMainMenu::l_create_dir(lua_State *L) {
688 const char *path = luaL_checkstring(L, 1);
690 if (ModApiMainMenu::isMinetestPath(path)) {
691 lua_pushboolean(L, fs::CreateAllDirs(path));
695 lua_pushboolean(L, false);
699 /******************************************************************************/
700 int ModApiMainMenu::l_delete_dir(lua_State *L)
702 const char *path = luaL_checkstring(L, 1);
704 std::string absolute_path = fs::RemoveRelativePathComponents(path);
706 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
707 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
711 lua_pushboolean(L, false);
715 /******************************************************************************/
716 int ModApiMainMenu::l_copy_dir(lua_State *L)
718 const char *source = luaL_checkstring(L, 1);
719 const char *destination = luaL_checkstring(L, 2);
721 bool keep_source = true;
723 if ((!lua_isnone(L,3)) &&
725 keep_source = readParam<bool>(L,3);
728 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
729 std::string absolute_source = fs::RemoveRelativePathComponents(source);
731 if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
732 bool retval = fs::CopyDir(absolute_source,absolute_destination);
734 if (retval && (!keep_source)) {
736 retval &= fs::RecursiveDelete(absolute_source);
738 lua_pushboolean(L,retval);
741 lua_pushboolean(L,false);
745 /******************************************************************************/
746 int ModApiMainMenu::l_extract_zip(lua_State *L)
748 const char *zipfile = luaL_checkstring(L, 1);
749 const char *destination = luaL_checkstring(L, 2);
751 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
753 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
754 fs::CreateAllDirs(absolute_destination);
756 io::IFileSystem *fs = RenderingEngine::get_filesystem();
758 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
759 lua_pushboolean(L,false);
763 sanity_check(fs->getFileArchiveCount() > 0);
765 /**********************************************************************/
766 /* WARNING this is not threadsafe!! */
767 /**********************************************************************/
768 io::IFileArchive* opened_zip =
769 fs->getFileArchive(fs->getFileArchiveCount()-1);
771 const io::IFileList* files_in_zip = opened_zip->getFileList();
773 unsigned int number_of_files = files_in_zip->getFileCount();
775 for (unsigned int i=0; i < number_of_files; i++) {
776 std::string fullpath = destination;
777 fullpath += DIR_DELIM;
778 fullpath += files_in_zip->getFullFileName(i).c_str();
779 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
781 if (!files_in_zip->isDirectory(i)) {
782 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
783 fs->removeFileArchive(fs->getFileArchiveCount()-1);
784 lua_pushboolean(L,false);
788 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
790 FILE *targetfile = fopen(fullpath.c_str(),"wb");
792 if (targetfile == NULL) {
793 fs->removeFileArchive(fs->getFileArchiveCount()-1);
794 lua_pushboolean(L,false);
798 char read_buffer[1024];
801 while (total_read < toread->getSize()) {
803 unsigned int bytes_read =
804 toread->read(read_buffer,sizeof(read_buffer));
805 if ((bytes_read == 0 ) ||
806 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
809 fs->removeFileArchive(fs->getFileArchiveCount()-1);
810 lua_pushboolean(L,false);
813 total_read += bytes_read;
821 fs->removeFileArchive(fs->getFileArchiveCount()-1);
822 lua_pushboolean(L,true);
826 lua_pushboolean(L,false);
830 /******************************************************************************/
831 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
833 GUIEngine* engine = getGuiEngine(L);
834 sanity_check(engine != NULL);
836 lua_pushstring(L,engine->getScriptDir().c_str());
840 /******************************************************************************/
841 bool ModApiMainMenu::isMinetestPath(std::string path)
843 if (fs::PathStartsWith(path,fs::TempPath()))
847 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
851 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
855 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures")))
859 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
866 /******************************************************************************/
867 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
869 GUIEngine* engine = getGuiEngine(L);
870 sanity_check(engine != NULL);
872 const char *formname= luaL_checkstring(L, 1);
873 const char *title = luaL_checkstring(L, 2);
874 bool is_file_select = readParam<bool>(L, 3);
876 GUIFileSelectMenu* fileOpenMenu =
877 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
880 engine->m_menumanager,
884 fileOpenMenu->setTextDest(engine->m_buttonhandler);
885 fileOpenMenu->drop();
889 /******************************************************************************/
890 int ModApiMainMenu::l_download_file(lua_State *L)
892 const char *url = luaL_checkstring(L, 1);
893 const char *target = luaL_checkstring(L, 2);
896 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
898 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
899 if (GUIEngine::downloadFile(url,absolute_destination)) {
900 lua_pushboolean(L,true);
904 errorstream << "DOWNLOAD denied: " << absolute_destination
905 << " isn't a allowed path" << std::endl;
907 lua_pushboolean(L,false);
911 /******************************************************************************/
912 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
914 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
917 for (u32 i = 0; i != drivers.size(); i++) {
918 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
919 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
922 lua_pushstring(L, name);
923 lua_setfield(L, -2, "name");
924 lua_pushstring(L, fname);
925 lua_setfield(L, -2, "friendly_name");
927 lua_rawseti(L, -2, i + 1);
933 /******************************************************************************/
934 int ModApiMainMenu::l_get_video_modes(lua_State *L)
936 std::vector<core::vector3d<u32> > videomodes
937 = RenderingEngine::getSupportedVideoModes();
940 for (u32 i = 0; i != videomodes.size(); i++) {
942 lua_pushnumber(L, videomodes[i].X);
943 lua_setfield(L, -2, "w");
944 lua_pushnumber(L, videomodes[i].Y);
945 lua_setfield(L, -2, "h");
946 lua_pushnumber(L, videomodes[i].Z);
947 lua_setfield(L, -2, "depth");
949 lua_rawseti(L, -2, i + 1);
955 /******************************************************************************/
956 int ModApiMainMenu::l_gettext(lua_State *L)
958 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
959 lua_pushstring(L, text.c_str());
964 /******************************************************************************/
965 int ModApiMainMenu::l_get_screen_info(lua_State *L)
968 int top = lua_gettop(L);
969 lua_pushstring(L,"density");
970 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
971 lua_settable(L, top);
973 lua_pushstring(L,"display_width");
974 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
975 lua_settable(L, top);
977 lua_pushstring(L,"display_height");
978 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
979 lua_settable(L, top);
981 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
982 lua_pushstring(L,"window_width");
983 lua_pushnumber(L, window_size.X);
984 lua_settable(L, top);
986 lua_pushstring(L,"window_height");
987 lua_pushnumber(L, window_size.Y);
988 lua_settable(L, top);
992 /******************************************************************************/
993 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
995 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
999 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1001 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1005 /******************************************************************************/
1006 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1008 GUIEngine* engine = getGuiEngine(L);
1010 size_t func_length, param_length;
1011 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1013 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1015 sanity_check(serialized_func_raw != NULL);
1016 sanity_check(serialized_param_raw != NULL);
1018 std::string serialized_func = std::string(serialized_func_raw, func_length);
1019 std::string serialized_param = std::string(serialized_param_raw, param_length);
1021 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1026 /******************************************************************************/
1027 void ModApiMainMenu::Initialize(lua_State *L, int top)
1029 API_FCT(update_formspec);
1030 API_FCT(set_clouds);
1031 API_FCT(get_textlist_index);
1032 API_FCT(get_table_index);
1033 API_FCT(get_worlds);
1035 API_FCT(get_content_info);
1038 API_FCT(get_favorites);
1039 API_FCT(show_keys_menu);
1040 API_FCT(create_world);
1041 API_FCT(delete_world);
1042 API_FCT(delete_favorite);
1043 API_FCT(set_background);
1044 API_FCT(set_topleft_text);
1045 API_FCT(get_mapgen_names);
1046 API_FCT(get_modpath);
1047 API_FCT(get_clientmodpath);
1048 API_FCT(get_gamepath);
1049 API_FCT(get_texturepath);
1050 API_FCT(get_texturepath_share);
1051 API_FCT(create_dir);
1052 API_FCT(delete_dir);
1054 API_FCT(extract_zip);
1055 API_FCT(get_mainmenu_path);
1056 API_FCT(show_path_select_dialog);
1057 API_FCT(download_file);
1059 API_FCT(get_video_drivers);
1060 API_FCT(get_video_modes);
1061 API_FCT(get_screen_info);
1062 API_FCT(get_min_supp_proto);
1063 API_FCT(get_max_supp_proto);
1064 API_FCT(do_async_callback);
1067 /******************************************************************************/
1068 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1070 API_FCT(get_worlds);
1072 API_FCT(get_favorites);
1073 API_FCT(get_mapgen_names);
1074 API_FCT(get_modpath);
1075 API_FCT(get_clientmodpath);
1076 API_FCT(get_gamepath);
1077 API_FCT(get_texturepath);
1078 API_FCT(get_texturepath_share);
1079 API_FCT(create_dir);
1080 API_FCT(delete_dir);
1082 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1083 API_FCT(download_file);
1084 //API_FCT(gettext); (gettext lib isn't threadsafe)