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);
538 lua_setfield(L, -2, "depends");
540 // Optional Dependencies
543 for (const auto &dep : spec.optdepends) {
544 lua_pushstring(L, dep.c_str());
545 lua_rawseti(L, -2, i);
548 lua_setfield(L, -2, "optional_depends");
554 /******************************************************************************/
555 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
557 GUIEngine* engine = getGuiEngine(L);
558 sanity_check(engine != NULL);
560 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
563 engine->m_menumanager);
568 /******************************************************************************/
569 int ModApiMainMenu::l_create_world(lua_State *L)
571 const char *name = luaL_checkstring(L, 1);
572 int gameidx = luaL_checkinteger(L,2) -1;
574 std::string path = porting::path_user + DIR_DELIM
578 std::vector<SubgameSpec> games = getAvailableGames();
580 if ((gameidx >= 0) &&
581 (gameidx < (int) games.size())) {
583 // Create world if it doesn't exist
584 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
585 lua_pushstring(L, "Failed to initialize world");
590 lua_pushstring(L, "Invalid game index");
595 /******************************************************************************/
596 int ModApiMainMenu::l_delete_world(lua_State *L)
598 int world_id = luaL_checkinteger(L, 1) - 1;
599 std::vector<WorldSpec> worlds = getAvailableWorlds();
600 if (world_id < 0 || world_id >= (int) worlds.size()) {
601 lua_pushstring(L, "Invalid world index");
604 const WorldSpec &spec = worlds[world_id];
605 if (!fs::RecursiveDelete(spec.path)) {
606 lua_pushstring(L, "Failed to delete world");
612 /******************************************************************************/
613 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
615 GUIEngine* engine = getGuiEngine(L);
616 sanity_check(engine != NULL);
620 if (!lua_isnone(L,1) && !lua_isnil(L,1))
621 text = luaL_checkstring(L, 1);
623 engine->setTopleftText(text);
627 /******************************************************************************/
628 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
630 std::vector<const char *> names;
631 bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
632 Mapgen::getMapgenNames(&names, include_hidden);
635 for (size_t i = 0; i != names.size(); i++) {
636 lua_pushstring(L, names[i]);
637 lua_rawseti(L, -2, i + 1);
644 /******************************************************************************/
645 int ModApiMainMenu::l_get_modpath(lua_State *L)
647 std::string modpath = fs::RemoveRelativePathComponents(
648 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
649 lua_pushstring(L, modpath.c_str());
653 /******************************************************************************/
654 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
656 std::string modpath = fs::RemoveRelativePathComponents(
657 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
658 lua_pushstring(L, modpath.c_str());
662 /******************************************************************************/
663 int ModApiMainMenu::l_get_gamepath(lua_State *L)
665 std::string gamepath = fs::RemoveRelativePathComponents(
666 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
667 lua_pushstring(L, gamepath.c_str());
671 /******************************************************************************/
672 int ModApiMainMenu::l_get_texturepath(lua_State *L)
674 std::string gamepath = fs::RemoveRelativePathComponents(
675 porting::path_user + DIR_DELIM + "textures");
676 lua_pushstring(L, gamepath.c_str());
680 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
682 std::string gamepath = fs::RemoveRelativePathComponents(
683 porting::path_share + DIR_DELIM + "textures");
684 lua_pushstring(L, gamepath.c_str());
688 /******************************************************************************/
689 int ModApiMainMenu::l_create_dir(lua_State *L) {
690 const char *path = luaL_checkstring(L, 1);
692 if (ModApiMainMenu::isMinetestPath(path)) {
693 lua_pushboolean(L, fs::CreateAllDirs(path));
697 lua_pushboolean(L, false);
701 /******************************************************************************/
702 int ModApiMainMenu::l_delete_dir(lua_State *L)
704 const char *path = luaL_checkstring(L, 1);
706 std::string absolute_path = fs::RemoveRelativePathComponents(path);
708 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
709 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
713 lua_pushboolean(L, false);
717 /******************************************************************************/
718 int ModApiMainMenu::l_copy_dir(lua_State *L)
720 const char *source = luaL_checkstring(L, 1);
721 const char *destination = luaL_checkstring(L, 2);
723 bool keep_source = true;
725 if ((!lua_isnone(L,3)) &&
727 keep_source = readParam<bool>(L,3);
730 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
731 std::string absolute_source = fs::RemoveRelativePathComponents(source);
733 if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
734 bool retval = fs::CopyDir(absolute_source,absolute_destination);
736 if (retval && (!keep_source)) {
738 retval &= fs::RecursiveDelete(absolute_source);
740 lua_pushboolean(L,retval);
743 lua_pushboolean(L,false);
747 /******************************************************************************/
748 int ModApiMainMenu::l_extract_zip(lua_State *L)
750 const char *zipfile = luaL_checkstring(L, 1);
751 const char *destination = luaL_checkstring(L, 2);
753 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
755 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
756 fs::CreateAllDirs(absolute_destination);
758 io::IFileSystem *fs = RenderingEngine::get_filesystem();
760 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
761 lua_pushboolean(L,false);
765 sanity_check(fs->getFileArchiveCount() > 0);
767 /**********************************************************************/
768 /* WARNING this is not threadsafe!! */
769 /**********************************************************************/
770 io::IFileArchive* opened_zip =
771 fs->getFileArchive(fs->getFileArchiveCount()-1);
773 const io::IFileList* files_in_zip = opened_zip->getFileList();
775 unsigned int number_of_files = files_in_zip->getFileCount();
777 for (unsigned int i=0; i < number_of_files; i++) {
778 std::string fullpath = destination;
779 fullpath += DIR_DELIM;
780 fullpath += files_in_zip->getFullFileName(i).c_str();
781 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
783 if (!files_in_zip->isDirectory(i)) {
784 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
785 fs->removeFileArchive(fs->getFileArchiveCount()-1);
786 lua_pushboolean(L,false);
790 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
792 FILE *targetfile = fopen(fullpath.c_str(),"wb");
794 if (targetfile == NULL) {
795 fs->removeFileArchive(fs->getFileArchiveCount()-1);
796 lua_pushboolean(L,false);
800 char read_buffer[1024];
803 while (total_read < toread->getSize()) {
805 unsigned int bytes_read =
806 toread->read(read_buffer,sizeof(read_buffer));
807 if ((bytes_read == 0 ) ||
808 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
811 fs->removeFileArchive(fs->getFileArchiveCount()-1);
812 lua_pushboolean(L,false);
815 total_read += bytes_read;
823 fs->removeFileArchive(fs->getFileArchiveCount()-1);
824 lua_pushboolean(L,true);
828 lua_pushboolean(L,false);
832 /******************************************************************************/
833 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
835 GUIEngine* engine = getGuiEngine(L);
836 sanity_check(engine != NULL);
838 lua_pushstring(L,engine->getScriptDir().c_str());
842 /******************************************************************************/
843 bool ModApiMainMenu::isMinetestPath(std::string path)
845 if (fs::PathStartsWith(path,fs::TempPath()))
849 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
853 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
857 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures")))
861 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
868 /******************************************************************************/
869 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
871 GUIEngine* engine = getGuiEngine(L);
872 sanity_check(engine != NULL);
874 const char *formname= luaL_checkstring(L, 1);
875 const char *title = luaL_checkstring(L, 2);
876 bool is_file_select = readParam<bool>(L, 3);
878 GUIFileSelectMenu* fileOpenMenu =
879 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
882 engine->m_menumanager,
886 fileOpenMenu->setTextDest(engine->m_buttonhandler);
887 fileOpenMenu->drop();
891 /******************************************************************************/
892 int ModApiMainMenu::l_download_file(lua_State *L)
894 const char *url = luaL_checkstring(L, 1);
895 const char *target = luaL_checkstring(L, 2);
898 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
900 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
901 if (GUIEngine::downloadFile(url,absolute_destination)) {
902 lua_pushboolean(L,true);
906 errorstream << "DOWNLOAD denied: " << absolute_destination
907 << " isn't a allowed path" << std::endl;
909 lua_pushboolean(L,false);
913 /******************************************************************************/
914 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
916 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
919 for (u32 i = 0; i != drivers.size(); i++) {
920 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
921 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
924 lua_pushstring(L, name);
925 lua_setfield(L, -2, "name");
926 lua_pushstring(L, fname);
927 lua_setfield(L, -2, "friendly_name");
929 lua_rawseti(L, -2, i + 1);
935 /******************************************************************************/
936 int ModApiMainMenu::l_get_video_modes(lua_State *L)
938 std::vector<core::vector3d<u32> > videomodes
939 = RenderingEngine::getSupportedVideoModes();
942 for (u32 i = 0; i != videomodes.size(); i++) {
944 lua_pushnumber(L, videomodes[i].X);
945 lua_setfield(L, -2, "w");
946 lua_pushnumber(L, videomodes[i].Y);
947 lua_setfield(L, -2, "h");
948 lua_pushnumber(L, videomodes[i].Z);
949 lua_setfield(L, -2, "depth");
951 lua_rawseti(L, -2, i + 1);
957 /******************************************************************************/
958 int ModApiMainMenu::l_gettext(lua_State *L)
960 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
961 lua_pushstring(L, text.c_str());
966 /******************************************************************************/
967 int ModApiMainMenu::l_get_screen_info(lua_State *L)
970 int top = lua_gettop(L);
971 lua_pushstring(L,"density");
972 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
973 lua_settable(L, top);
975 lua_pushstring(L,"display_width");
976 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
977 lua_settable(L, top);
979 lua_pushstring(L,"display_height");
980 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
981 lua_settable(L, top);
983 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
984 lua_pushstring(L,"window_width");
985 lua_pushnumber(L, window_size.X);
986 lua_settable(L, top);
988 lua_pushstring(L,"window_height");
989 lua_pushnumber(L, window_size.Y);
990 lua_settable(L, top);
994 int ModApiMainMenu::l_get_package_list(lua_State *L)
996 std::string url = g_settings->get("contentdb_url");
997 std::vector<Package> packages = getPackagesFromURL(url +
998 "/api/packages/?type=mod&type=game&type=txp&protocol_version="
999 LATEST_PROTOCOL_VERSION_STRING);
1003 int top = lua_gettop(L);
1004 unsigned int index = 1;
1007 for (const auto &package : packages) {
1008 lua_pushnumber(L, index);
1011 int top_lvl2 = lua_gettop(L);
1013 lua_pushstring(L, "author");
1014 lua_pushstring(L, package.author.c_str());
1015 lua_settable (L, top_lvl2);
1017 lua_pushstring(L, "name");
1018 lua_pushstring(L, package.name.c_str());
1019 lua_settable (L, top_lvl2);
1021 lua_pushstring(L, "title");
1022 lua_pushstring(L, package.title.c_str());
1023 lua_settable (L, top_lvl2);
1025 lua_pushstring(L, "type");
1026 lua_pushstring(L, package.type.c_str());
1027 lua_settable (L, top_lvl2);
1029 lua_pushstring(L, "short_description");
1030 lua_pushstring(L, package.shortDesc.c_str());
1031 lua_settable (L, top_lvl2);
1033 lua_pushstring (L, "release");
1034 lua_pushinteger(L, package.release);
1035 lua_settable (L, top_lvl2);
1037 if (package.thumbnail != "") {
1038 lua_pushstring(L, "thumbnail");
1039 lua_pushstring(L, package.thumbnail.c_str());
1040 lua_settable (L, top_lvl2);
1043 lua_pushstring(L, "url");
1044 lua_pushstring(L, package.getDownloadURL(url).c_str());
1045 lua_settable (L, top_lvl2);
1047 lua_settable(L, top);
1054 /******************************************************************************/
1055 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
1057 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
1061 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1063 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1067 /******************************************************************************/
1068 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1070 GUIEngine* engine = getGuiEngine(L);
1072 size_t func_length, param_length;
1073 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1075 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1077 sanity_check(serialized_func_raw != NULL);
1078 sanity_check(serialized_param_raw != NULL);
1080 std::string serialized_func = std::string(serialized_func_raw, func_length);
1081 std::string serialized_param = std::string(serialized_param_raw, param_length);
1083 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1088 /******************************************************************************/
1089 void ModApiMainMenu::Initialize(lua_State *L, int top)
1091 API_FCT(update_formspec);
1092 API_FCT(set_clouds);
1093 API_FCT(get_textlist_index);
1094 API_FCT(get_table_index);
1095 API_FCT(get_worlds);
1097 API_FCT(get_content_info);
1100 API_FCT(get_favorites);
1101 API_FCT(show_keys_menu);
1102 API_FCT(create_world);
1103 API_FCT(delete_world);
1104 API_FCT(delete_favorite);
1105 API_FCT(set_background);
1106 API_FCT(set_topleft_text);
1107 API_FCT(get_mapgen_names);
1108 API_FCT(get_modpath);
1109 API_FCT(get_clientmodpath);
1110 API_FCT(get_gamepath);
1111 API_FCT(get_texturepath);
1112 API_FCT(get_texturepath_share);
1113 API_FCT(create_dir);
1114 API_FCT(delete_dir);
1116 API_FCT(extract_zip);
1117 API_FCT(get_mainmenu_path);
1118 API_FCT(show_path_select_dialog);
1119 API_FCT(download_file);
1121 API_FCT(get_video_drivers);
1122 API_FCT(get_video_modes);
1123 API_FCT(get_screen_info);
1124 API_FCT(get_package_list);
1125 API_FCT(get_min_supp_proto);
1126 API_FCT(get_max_supp_proto);
1127 API_FCT(do_async_callback);
1130 /******************************************************************************/
1131 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1133 API_FCT(get_worlds);
1135 API_FCT(get_favorites);
1136 API_FCT(get_mapgen_names);
1137 API_FCT(get_modpath);
1138 API_FCT(get_clientmodpath);
1139 API_FCT(get_gamepath);
1140 API_FCT(get_texturepath);
1141 API_FCT(get_texturepath_share);
1142 API_FCT(create_dir);
1143 API_FCT(delete_dir);
1145 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1146 API_FCT(download_file);
1147 //API_FCT(gettext); (gettext lib isn't threadsafe)
1148 API_FCT(get_package_list);