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 readParam<bool>(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 = readParam<bool>(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 = readParam<bool>(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, "release");
471 lua_pushinteger(L, game.release);
472 lua_settable(L, top_lvl2);
474 lua_pushstring(L, "menuicon_path");
475 lua_pushstring(L, game.menuicon_path.c_str());
476 lua_settable(L, top_lvl2);
478 lua_pushstring(L, "addon_mods_paths");
480 int table2 = lua_gettop(L);
481 int internal_index = 1;
482 for (const std::string &addon_mods_path : game.addon_mods_paths) {
483 lua_pushnumber(L, internal_index);
484 lua_pushstring(L, addon_mods_path.c_str());
485 lua_settable(L, table2);
488 lua_settable(L, top_lvl2);
489 lua_settable(L, top);
495 /******************************************************************************/
496 int ModApiMainMenu::l_get_content_info(lua_State *L)
498 std::string path = luaL_checkstring(L, 1);
502 parseContentInfo(spec);
506 lua_pushstring(L, spec.name.c_str());
507 lua_setfield(L, -2, "name");
509 lua_pushstring(L, spec.type.c_str());
510 lua_setfield(L, -2, "type");
512 lua_pushstring(L, spec.author.c_str());
513 lua_setfield(L, -2, "author");
515 lua_pushinteger(L, spec.release);
516 lua_setfield(L, -2, "release");
518 lua_pushstring(L, spec.desc.c_str());
519 lua_setfield(L, -2, "description");
521 lua_pushstring(L, spec.path.c_str());
522 lua_setfield(L, -2, "path");
524 if (spec.type == "mod") {
527 parseModContents(spec);
532 for (const auto &dep : spec.depends) {
533 lua_pushstring(L, dep.c_str());
534 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);
547 lua_setfield(L, -2, "optional_depends");
553 /******************************************************************************/
554 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
556 GUIEngine* engine = getGuiEngine(L);
557 sanity_check(engine != NULL);
559 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
562 engine->m_menumanager);
567 /******************************************************************************/
568 int ModApiMainMenu::l_create_world(lua_State *L)
570 const char *name = luaL_checkstring(L, 1);
571 int gameidx = luaL_checkinteger(L,2) -1;
573 std::string path = porting::path_user + DIR_DELIM
577 std::vector<SubgameSpec> games = getAvailableGames();
579 if ((gameidx >= 0) &&
580 (gameidx < (int) games.size())) {
582 // Create world if it doesn't exist
583 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
584 lua_pushstring(L, "Failed to initialize world");
589 lua_pushstring(L, "Invalid game index");
594 /******************************************************************************/
595 int ModApiMainMenu::l_delete_world(lua_State *L)
597 int world_id = luaL_checkinteger(L, 1) - 1;
598 std::vector<WorldSpec> worlds = getAvailableWorlds();
599 if (world_id < 0 || world_id >= (int) worlds.size()) {
600 lua_pushstring(L, "Invalid world index");
603 const WorldSpec &spec = worlds[world_id];
604 if (!fs::RecursiveDelete(spec.path)) {
605 lua_pushstring(L, "Failed to delete world");
611 /******************************************************************************/
612 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
614 GUIEngine* engine = getGuiEngine(L);
615 sanity_check(engine != NULL);
619 if (!lua_isnone(L,1) && !lua_isnil(L,1))
620 text = luaL_checkstring(L, 1);
622 engine->setTopleftText(text);
626 /******************************************************************************/
627 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
629 std::vector<const char *> names;
630 bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
631 Mapgen::getMapgenNames(&names, include_hidden);
634 for (size_t i = 0; i != names.size(); i++) {
635 lua_pushstring(L, names[i]);
636 lua_rawseti(L, -2, i + 1);
643 /******************************************************************************/
644 int ModApiMainMenu::l_get_modpath(lua_State *L)
646 std::string modpath = fs::RemoveRelativePathComponents(
647 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
648 lua_pushstring(L, modpath.c_str());
652 /******************************************************************************/
653 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
655 std::string modpath = fs::RemoveRelativePathComponents(
656 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
657 lua_pushstring(L, modpath.c_str());
661 /******************************************************************************/
662 int ModApiMainMenu::l_get_gamepath(lua_State *L)
664 std::string gamepath = fs::RemoveRelativePathComponents(
665 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
666 lua_pushstring(L, gamepath.c_str());
670 /******************************************************************************/
671 int ModApiMainMenu::l_get_texturepath(lua_State *L)
673 std::string gamepath = fs::RemoveRelativePathComponents(
674 porting::path_user + DIR_DELIM + "textures");
675 lua_pushstring(L, gamepath.c_str());
679 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
681 std::string gamepath = fs::RemoveRelativePathComponents(
682 porting::path_share + DIR_DELIM + "textures");
683 lua_pushstring(L, gamepath.c_str());
687 /******************************************************************************/
688 int ModApiMainMenu::l_create_dir(lua_State *L) {
689 const char *path = luaL_checkstring(L, 1);
691 if (ModApiMainMenu::isMinetestPath(path)) {
692 lua_pushboolean(L, fs::CreateAllDirs(path));
696 lua_pushboolean(L, false);
700 /******************************************************************************/
701 int ModApiMainMenu::l_delete_dir(lua_State *L)
703 const char *path = luaL_checkstring(L, 1);
705 std::string absolute_path = fs::RemoveRelativePathComponents(path);
707 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
708 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
712 lua_pushboolean(L, false);
716 /******************************************************************************/
717 int ModApiMainMenu::l_copy_dir(lua_State *L)
719 const char *source = luaL_checkstring(L, 1);
720 const char *destination = luaL_checkstring(L, 2);
722 bool keep_source = true;
724 if ((!lua_isnone(L,3)) &&
726 keep_source = readParam<bool>(L,3);
729 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
730 std::string absolute_source = fs::RemoveRelativePathComponents(source);
732 if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
733 bool retval = fs::CopyDir(absolute_source,absolute_destination);
735 if (retval && (!keep_source)) {
737 retval &= fs::RecursiveDelete(absolute_source);
739 lua_pushboolean(L,retval);
742 lua_pushboolean(L,false);
746 /******************************************************************************/
747 int ModApiMainMenu::l_extract_zip(lua_State *L)
749 const char *zipfile = luaL_checkstring(L, 1);
750 const char *destination = luaL_checkstring(L, 2);
752 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
754 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
755 fs::CreateAllDirs(absolute_destination);
757 io::IFileSystem *fs = RenderingEngine::get_filesystem();
759 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
760 lua_pushboolean(L,false);
764 sanity_check(fs->getFileArchiveCount() > 0);
766 /**********************************************************************/
767 /* WARNING this is not threadsafe!! */
768 /**********************************************************************/
769 io::IFileArchive* opened_zip =
770 fs->getFileArchive(fs->getFileArchiveCount()-1);
772 const io::IFileList* files_in_zip = opened_zip->getFileList();
774 unsigned int number_of_files = files_in_zip->getFileCount();
776 for (unsigned int i=0; i < number_of_files; i++) {
777 std::string fullpath = destination;
778 fullpath += DIR_DELIM;
779 fullpath += files_in_zip->getFullFileName(i).c_str();
780 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
782 if (!files_in_zip->isDirectory(i)) {
783 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
784 fs->removeFileArchive(fs->getFileArchiveCount()-1);
785 lua_pushboolean(L,false);
789 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
791 FILE *targetfile = fopen(fullpath.c_str(),"wb");
793 if (targetfile == NULL) {
794 fs->removeFileArchive(fs->getFileArchiveCount()-1);
795 lua_pushboolean(L,false);
799 char read_buffer[1024];
802 while (total_read < toread->getSize()) {
804 unsigned int bytes_read =
805 toread->read(read_buffer,sizeof(read_buffer));
806 if ((bytes_read == 0 ) ||
807 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
810 fs->removeFileArchive(fs->getFileArchiveCount()-1);
811 lua_pushboolean(L,false);
814 total_read += bytes_read;
822 fs->removeFileArchive(fs->getFileArchiveCount()-1);
823 lua_pushboolean(L,true);
827 lua_pushboolean(L,false);
831 /******************************************************************************/
832 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
834 GUIEngine* engine = getGuiEngine(L);
835 sanity_check(engine != NULL);
837 lua_pushstring(L,engine->getScriptDir().c_str());
841 /******************************************************************************/
842 bool ModApiMainMenu::isMinetestPath(std::string path)
844 if (fs::PathStartsWith(path,fs::TempPath()))
848 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
852 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
856 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures")))
860 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
867 /******************************************************************************/
868 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
870 GUIEngine* engine = getGuiEngine(L);
871 sanity_check(engine != NULL);
873 const char *formname= luaL_checkstring(L, 1);
874 const char *title = luaL_checkstring(L, 2);
875 bool is_file_select = readParam<bool>(L, 3);
877 GUIFileSelectMenu* fileOpenMenu =
878 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
881 engine->m_menumanager,
885 fileOpenMenu->setTextDest(engine->m_buttonhandler);
886 fileOpenMenu->drop();
890 /******************************************************************************/
891 int ModApiMainMenu::l_download_file(lua_State *L)
893 const char *url = luaL_checkstring(L, 1);
894 const char *target = luaL_checkstring(L, 2);
897 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
899 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
900 if (GUIEngine::downloadFile(url,absolute_destination)) {
901 lua_pushboolean(L,true);
905 errorstream << "DOWNLOAD denied: " << absolute_destination
906 << " isn't a allowed path" << std::endl;
908 lua_pushboolean(L,false);
912 /******************************************************************************/
913 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
915 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
918 for (u32 i = 0; i != drivers.size(); i++) {
919 const char *name = RenderingEngine::getVideoDriverName(drivers[i]);
920 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
923 lua_pushstring(L, name);
924 lua_setfield(L, -2, "name");
925 lua_pushstring(L, fname);
926 lua_setfield(L, -2, "friendly_name");
928 lua_rawseti(L, -2, i + 1);
934 /******************************************************************************/
935 int ModApiMainMenu::l_get_video_modes(lua_State *L)
937 std::vector<core::vector3d<u32> > videomodes
938 = RenderingEngine::getSupportedVideoModes();
941 for (u32 i = 0; i != videomodes.size(); i++) {
943 lua_pushnumber(L, videomodes[i].X);
944 lua_setfield(L, -2, "w");
945 lua_pushnumber(L, videomodes[i].Y);
946 lua_setfield(L, -2, "h");
947 lua_pushnumber(L, videomodes[i].Z);
948 lua_setfield(L, -2, "depth");
950 lua_rawseti(L, -2, i + 1);
956 /******************************************************************************/
957 int ModApiMainMenu::l_gettext(lua_State *L)
959 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
960 lua_pushstring(L, text.c_str());
965 /******************************************************************************/
966 int ModApiMainMenu::l_get_screen_info(lua_State *L)
969 int top = lua_gettop(L);
970 lua_pushstring(L,"density");
971 lua_pushnumber(L,RenderingEngine::getDisplayDensity());
972 lua_settable(L, top);
974 lua_pushstring(L,"display_width");
975 lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
976 lua_settable(L, top);
978 lua_pushstring(L,"display_height");
979 lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
980 lua_settable(L, top);
982 const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
983 lua_pushstring(L,"window_width");
984 lua_pushnumber(L, window_size.X);
985 lua_settable(L, top);
987 lua_pushstring(L,"window_height");
988 lua_pushnumber(L, window_size.Y);
989 lua_settable(L, top);
993 int ModApiMainMenu::l_get_package_list(lua_State *L)
995 std::string url = g_settings->get("contentdb_url");
996 std::vector<Package> packages = getPackagesFromURL(url + "/api/packages/");
1000 int top = lua_gettop(L);
1001 unsigned int index = 1;
1004 for (const auto &package : packages) {
1005 lua_pushnumber(L, index);
1008 int top_lvl2 = lua_gettop(L);
1010 lua_pushstring(L, "author");
1011 lua_pushstring(L, package.author.c_str());
1012 lua_settable (L, top_lvl2);
1014 lua_pushstring(L, "name");
1015 lua_pushstring(L, package.name.c_str());
1016 lua_settable (L, top_lvl2);
1018 lua_pushstring(L, "title");
1019 lua_pushstring(L, package.title.c_str());
1020 lua_settable (L, top_lvl2);
1022 lua_pushstring(L, "type");
1023 lua_pushstring(L, package.type.c_str());
1024 lua_settable (L, top_lvl2);
1026 lua_pushstring(L, "short_description");
1027 lua_pushstring(L, package.shortDesc.c_str());
1028 lua_settable (L, top_lvl2);
1030 lua_pushstring (L, "release");
1031 lua_pushinteger(L, package.release);
1032 lua_settable (L, top_lvl2);
1034 if (package.thumbnail != "") {
1035 lua_pushstring(L, "thumbnail");
1036 lua_pushstring(L, package.thumbnail.c_str());
1037 lua_settable (L, top_lvl2);
1040 lua_pushstring(L, "url");
1041 lua_pushstring(L, package.getDownloadURL(url).c_str());
1042 lua_settable (L, top_lvl2);
1044 lua_settable(L, top);
1051 /******************************************************************************/
1052 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
1054 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
1058 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1060 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1064 /******************************************************************************/
1065 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1067 GUIEngine* engine = getGuiEngine(L);
1069 size_t func_length, param_length;
1070 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1072 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1074 sanity_check(serialized_func_raw != NULL);
1075 sanity_check(serialized_param_raw != NULL);
1077 std::string serialized_func = std::string(serialized_func_raw, func_length);
1078 std::string serialized_param = std::string(serialized_param_raw, param_length);
1080 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1085 /******************************************************************************/
1086 void ModApiMainMenu::Initialize(lua_State *L, int top)
1088 API_FCT(update_formspec);
1089 API_FCT(set_clouds);
1090 API_FCT(get_textlist_index);
1091 API_FCT(get_table_index);
1092 API_FCT(get_worlds);
1094 API_FCT(get_content_info);
1097 API_FCT(get_favorites);
1098 API_FCT(show_keys_menu);
1099 API_FCT(create_world);
1100 API_FCT(delete_world);
1101 API_FCT(delete_favorite);
1102 API_FCT(set_background);
1103 API_FCT(set_topleft_text);
1104 API_FCT(get_mapgen_names);
1105 API_FCT(get_modpath);
1106 API_FCT(get_clientmodpath);
1107 API_FCT(get_gamepath);
1108 API_FCT(get_texturepath);
1109 API_FCT(get_texturepath_share);
1110 API_FCT(create_dir);
1111 API_FCT(delete_dir);
1113 API_FCT(extract_zip);
1114 API_FCT(get_mainmenu_path);
1115 API_FCT(show_path_select_dialog);
1116 API_FCT(download_file);
1118 API_FCT(get_video_drivers);
1119 API_FCT(get_video_modes);
1120 API_FCT(get_screen_info);
1121 API_FCT(get_package_list);
1122 API_FCT(get_min_supp_proto);
1123 API_FCT(get_max_supp_proto);
1124 API_FCT(do_async_callback);
1127 /******************************************************************************/
1128 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1130 API_FCT(get_worlds);
1132 API_FCT(get_favorites);
1133 API_FCT(get_mapgen_names);
1134 API_FCT(get_modpath);
1135 API_FCT(get_clientmodpath);
1136 API_FCT(get_gamepath);
1137 API_FCT(get_texturepath);
1138 API_FCT(get_texturepath_share);
1139 API_FCT(create_dir);
1140 API_FCT(delete_dir);
1142 //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1143 API_FCT(download_file);
1144 //API_FCT(gettext); (gettext lib isn't threadsafe)
1145 API_FCT(get_package_list);