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 "guiEngine.h"
25 #include "guiMainMenu.h"
26 #include "guiKeyChangeMenu.h"
27 #include "guiFileSelectMenu.h"
32 #include "convert_json.h"
33 #include "serverlist.h"
38 #include "EDriverTypes.h"
40 #include <IFileArchive.h>
41 #include <IFileSystem.h>
43 /******************************************************************************/
44 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
46 lua_getglobal(L, "gamedata");
48 lua_getfield(L, -1, name.c_str());
53 return luaL_checkstring(L, -1);
56 /******************************************************************************/
57 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
59 lua_getglobal(L, "gamedata");
61 lua_getfield(L, -1, name.c_str());
63 if(lua_isnil(L, -1)) {
69 return luaL_checkinteger(L, -1);
72 /******************************************************************************/
73 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
75 lua_getglobal(L, "gamedata");
77 lua_getfield(L, -1, name.c_str());
79 if(lua_isnil(L, -1)) {
85 return lua_toboolean(L, -1);
88 /******************************************************************************/
89 int ModApiMainMenu::l_update_formspec(lua_State *L)
91 GUIEngine* engine = getGuiEngine(L);
92 sanity_check(engine != NULL);
94 if (engine->m_startgame)
98 std::string formspec(luaL_checkstring(L, 1));
100 if (engine->m_formspecgui != 0) {
101 engine->m_formspecgui->setForm(formspec);
107 /******************************************************************************/
108 int ModApiMainMenu::l_start(lua_State *L)
110 GUIEngine* engine = getGuiEngine(L);
111 sanity_check(engine != NULL);
113 //update c++ gamedata from lua table
117 MainMenuData *data = engine->m_data;
119 data->selected_world = getIntegerData(L, "selected_world",valid) -1;
120 data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
121 data->do_reconnect = getBoolData(L, "do_reconnect", valid);
122 if (!data->do_reconnect) {
123 data->name = getTextData(L,"playername");
124 data->password = getTextData(L,"password");
125 data->address = getTextData(L,"address");
126 data->port = getTextData(L,"port");
128 data->serverdescription = getTextData(L,"serverdescription");
129 data->servername = getTextData(L,"servername");
131 //close menu next time
132 engine->m_startgame = true;
136 /******************************************************************************/
137 int ModApiMainMenu::l_close(lua_State *L)
139 GUIEngine* engine = getGuiEngine(L);
140 sanity_check(engine != NULL);
142 engine->m_kill = true;
146 /******************************************************************************/
147 int ModApiMainMenu::l_set_background(lua_State *L)
149 GUIEngine* engine = getGuiEngine(L);
150 sanity_check(engine != NULL);
152 std::string backgroundlevel(luaL_checkstring(L, 1));
153 std::string texturename(luaL_checkstring(L, 2));
155 bool tile_image = false;
157 unsigned int minsize = 16;
159 if (!lua_isnone(L, 3)) {
160 tile_image = lua_toboolean(L, 3);
163 if (!lua_isnone(L, 4)) {
164 minsize = lua_tonumber(L, 4);
167 if (backgroundlevel == "background") {
168 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
169 tile_image, minsize);
172 if (backgroundlevel == "overlay") {
173 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
174 tile_image, minsize);
177 if (backgroundlevel == "header") {
178 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
179 tile_image, minsize);
182 if (backgroundlevel == "footer") {
183 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
184 tile_image, minsize);
187 lua_pushboolean(L,retval);
191 /******************************************************************************/
192 int ModApiMainMenu::l_set_clouds(lua_State *L)
194 GUIEngine* engine = getGuiEngine(L);
195 sanity_check(engine != NULL);
197 bool value = lua_toboolean(L,1);
199 engine->m_clouds_enabled = value;
204 /******************************************************************************/
205 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
207 // get_table_index accepts both tables and textlists
208 return l_get_table_index(L);
211 /******************************************************************************/
212 int ModApiMainMenu::l_get_table_index(lua_State *L)
214 GUIEngine* engine = getGuiEngine(L);
215 sanity_check(engine != NULL);
217 std::string tablename(luaL_checkstring(L, 1));
218 GUITable *table = engine->m_menu->getTable(tablename);
219 s32 selection = table ? table->getSelected() : 0;
222 lua_pushinteger(L, selection);
228 /******************************************************************************/
229 int ModApiMainMenu::l_get_worlds(lua_State *L)
231 std::vector<WorldSpec> worlds = getAvailableWorlds();
234 int top = lua_gettop(L);
235 unsigned int index = 1;
237 for (unsigned int i = 0; i < worlds.size(); i++)
239 lua_pushnumber(L,index);
242 int top_lvl2 = lua_gettop(L);
244 lua_pushstring(L,"path");
245 lua_pushstring(L,worlds[i].path.c_str());
246 lua_settable(L, top_lvl2);
248 lua_pushstring(L,"name");
249 lua_pushstring(L,worlds[i].name.c_str());
250 lua_settable(L, top_lvl2);
252 lua_pushstring(L,"gameid");
253 lua_pushstring(L,worlds[i].gameid.c_str());
254 lua_settable(L, top_lvl2);
256 lua_settable(L, top);
262 /******************************************************************************/
263 int ModApiMainMenu::l_get_games(lua_State *L)
265 std::vector<SubgameSpec> games = getAvailableGames();
268 int top = lua_gettop(L);
269 unsigned int index = 1;
271 for (unsigned int i = 0; i < games.size(); i++)
273 lua_pushnumber(L,index);
275 int top_lvl2 = lua_gettop(L);
277 lua_pushstring(L,"id");
278 lua_pushstring(L,games[i].id.c_str());
279 lua_settable(L, top_lvl2);
281 lua_pushstring(L,"path");
282 lua_pushstring(L,games[i].path.c_str());
283 lua_settable(L, top_lvl2);
285 lua_pushstring(L,"gamemods_path");
286 lua_pushstring(L,games[i].gamemods_path.c_str());
287 lua_settable(L, top_lvl2);
289 lua_pushstring(L,"name");
290 lua_pushstring(L,games[i].name.c_str());
291 lua_settable(L, top_lvl2);
293 lua_pushstring(L,"menuicon_path");
294 lua_pushstring(L,games[i].menuicon_path.c_str());
295 lua_settable(L, top_lvl2);
297 lua_pushstring(L,"addon_mods_paths");
299 int table2 = lua_gettop(L);
300 int internal_index=1;
301 for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
302 iter != games[i].addon_mods_paths.end(); iter++) {
303 lua_pushnumber(L,internal_index);
304 lua_pushstring(L,(*iter).c_str());
305 lua_settable(L, table2);
308 lua_settable(L, top_lvl2);
309 lua_settable(L, top);
314 /******************************************************************************/
315 int ModApiMainMenu::l_get_modstore_details(lua_State *L)
317 const char *modid = luaL_checkstring(L, 1);
321 std::string url = "";
323 url = g_settings->get("modstore_details_url");
325 catch(SettingNotFoundException &e) {
330 size_t idpos = url.find("*");
332 url.insert(idpos,modid);
334 details = getModstoreUrl(url);
336 ModStoreModDetails current_mod = readModStoreModDetails(details);
338 if ( current_mod.valid) {
340 int top = lua_gettop(L);
342 lua_pushstring(L,"id");
343 lua_pushnumber(L,current_mod.id);
344 lua_settable(L, top);
346 lua_pushstring(L,"title");
347 lua_pushstring(L,current_mod.title.c_str());
348 lua_settable(L, top);
350 lua_pushstring(L,"basename");
351 lua_pushstring(L,current_mod.basename.c_str());
352 lua_settable(L, top);
354 lua_pushstring(L,"description");
355 lua_pushstring(L,current_mod.description.c_str());
356 lua_settable(L, top);
358 lua_pushstring(L,"author");
359 lua_pushstring(L,current_mod.author.username.c_str());
360 lua_settable(L, top);
362 lua_pushstring(L,"download_url");
363 lua_pushstring(L,current_mod.versions[0].file.c_str());
364 lua_settable(L, top);
366 lua_pushstring(L,"versions");
368 int versionstop = lua_gettop(L);
369 for (unsigned int i=0;i < current_mod.versions.size(); i++) {
370 lua_pushnumber(L,i+1);
372 int current_element = lua_gettop(L);
374 lua_pushstring(L,"date");
375 lua_pushstring(L,current_mod.versions[i].date.c_str());
376 lua_settable(L,current_element);
378 lua_pushstring(L,"download_url");
379 lua_pushstring(L,current_mod.versions[i].file.c_str());
380 lua_settable(L,current_element);
382 lua_settable(L,versionstop);
384 lua_settable(L, top);
386 lua_pushstring(L,"screenshot_url");
387 lua_pushstring(L,current_mod.titlepic.file.c_str());
388 lua_settable(L, top);
390 lua_pushstring(L,"license");
391 lua_pushstring(L,current_mod.license.shortinfo.c_str());
392 lua_settable(L, top);
394 lua_pushstring(L,"rating");
395 lua_pushnumber(L,current_mod.rating);
396 lua_settable(L, top);
407 /******************************************************************************/
408 int ModApiMainMenu::l_get_modstore_list(lua_State *L)
411 std::string url = "";
413 url = g_settings->get("modstore_listmods_url");
415 catch(SettingNotFoundException &e) {
420 mods = getModstoreUrl(url);
422 std::vector<ModStoreMod> moddata = readModStoreList(mods);
425 int top = lua_gettop(L);
426 unsigned int index = 1;
428 for (unsigned int i = 0; i < moddata.size(); i++)
430 if (moddata[i].valid) {
431 lua_pushnumber(L,index);
434 int top_lvl2 = lua_gettop(L);
436 lua_pushstring(L,"id");
437 lua_pushnumber(L,moddata[i].id);
438 lua_settable(L, top_lvl2);
440 lua_pushstring(L,"title");
441 lua_pushstring(L,moddata[i].title.c_str());
442 lua_settable(L, top_lvl2);
444 lua_pushstring(L,"basename");
445 lua_pushstring(L,moddata[i].basename.c_str());
446 lua_settable(L, top_lvl2);
448 lua_settable(L, top);
455 /******************************************************************************/
456 int ModApiMainMenu::l_get_favorites(lua_State *L)
458 std::string listtype = "local";
460 if (!lua_isnone(L,1)) {
461 listtype = luaL_checkstring(L,1);
464 std::vector<ServerListSpec> servers;
466 if(listtype == "online") {
467 servers = ServerList::getOnline();
469 servers = ServerList::getLocal();
473 int top = lua_gettop(L);
474 unsigned int index = 1;
476 for (unsigned int i = 0; i < servers.size(); i++)
479 lua_pushnumber(L,index);
482 int top_lvl2 = lua_gettop(L);
484 if (servers[i]["clients"].asString().size()) {
485 std::string clients_raw = servers[i]["clients"].asString();
487 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
489 if ((clients_raw != "") && (*endptr == 0)) {
490 lua_pushstring(L,"clients");
491 lua_pushnumber(L,numbervalue);
492 lua_settable(L, top_lvl2);
496 if (servers[i]["clients_max"].asString().size()) {
498 std::string clients_max_raw = servers[i]["clients_max"].asString();
500 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
502 if ((clients_max_raw != "") && (*endptr == 0)) {
503 lua_pushstring(L,"clients_max");
504 lua_pushnumber(L,numbervalue);
505 lua_settable(L, top_lvl2);
509 if (servers[i]["version"].asString().size()) {
510 lua_pushstring(L,"version");
511 std::string topush = servers[i]["version"].asString();
512 lua_pushstring(L,topush.c_str());
513 lua_settable(L, top_lvl2);
516 if (servers[i]["proto_min"].asString().size()) {
517 lua_pushstring(L,"proto_min");
518 lua_pushinteger(L,servers[i]["proto_min"].asInt());
519 lua_settable(L, top_lvl2);
522 if (servers[i]["proto_max"].asString().size()) {
523 lua_pushstring(L,"proto_max");
524 lua_pushinteger(L,servers[i]["proto_max"].asInt());
525 lua_settable(L, top_lvl2);
528 if (servers[i]["password"].asString().size()) {
529 lua_pushstring(L,"password");
530 lua_pushboolean(L,servers[i]["password"].asBool());
531 lua_settable(L, top_lvl2);
534 if (servers[i]["creative"].asString().size()) {
535 lua_pushstring(L,"creative");
536 lua_pushboolean(L,servers[i]["creative"].asBool());
537 lua_settable(L, top_lvl2);
540 if (servers[i]["damage"].asString().size()) {
541 lua_pushstring(L,"damage");
542 lua_pushboolean(L,servers[i]["damage"].asBool());
543 lua_settable(L, top_lvl2);
546 if (servers[i]["pvp"].asString().size()) {
547 lua_pushstring(L,"pvp");
548 lua_pushboolean(L,servers[i]["pvp"].asBool());
549 lua_settable(L, top_lvl2);
552 if (servers[i]["description"].asString().size()) {
553 lua_pushstring(L,"description");
554 std::string topush = servers[i]["description"].asString();
555 lua_pushstring(L,topush.c_str());
556 lua_settable(L, top_lvl2);
559 if (servers[i]["name"].asString().size()) {
560 lua_pushstring(L,"name");
561 std::string topush = servers[i]["name"].asString();
562 lua_pushstring(L,topush.c_str());
563 lua_settable(L, top_lvl2);
566 if (servers[i]["address"].asString().size()) {
567 lua_pushstring(L,"address");
568 std::string topush = servers[i]["address"].asString();
569 lua_pushstring(L,topush.c_str());
570 lua_settable(L, top_lvl2);
573 if (servers[i]["port"].asString().size()) {
574 lua_pushstring(L,"port");
575 std::string topush = servers[i]["port"].asString();
576 lua_pushstring(L,topush.c_str());
577 lua_settable(L, top_lvl2);
580 lua_settable(L, top);
586 /******************************************************************************/
587 int ModApiMainMenu::l_delete_favorite(lua_State *L)
589 std::vector<ServerListSpec> servers;
591 std::string listtype = "local";
593 if (!lua_isnone(L,2)) {
594 listtype = luaL_checkstring(L,2);
597 if ((listtype != "local") &&
598 (listtype != "online"))
602 if(listtype == "online") {
603 servers = ServerList::getOnline();
605 servers = ServerList::getLocal();
608 int fav_idx = luaL_checkinteger(L,1) -1;
610 if ((fav_idx >= 0) &&
611 (fav_idx < (int) servers.size())) {
613 ServerList::deleteEntry(servers[fav_idx]);
619 /******************************************************************************/
620 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
622 GUIEngine* engine = getGuiEngine(L);
623 sanity_check(engine != NULL);
625 GUIKeyChangeMenu *kmenu
626 = new GUIKeyChangeMenu( engine->m_device->getGUIEnvironment(),
629 engine->m_menumanager);
634 /******************************************************************************/
635 int ModApiMainMenu::l_create_world(lua_State *L)
637 const char *name = luaL_checkstring(L, 1);
638 int gameidx = luaL_checkinteger(L,2) -1;
640 std::string path = porting::path_user + DIR_DELIM
644 std::vector<SubgameSpec> games = getAvailableGames();
646 if ((gameidx >= 0) &&
647 (gameidx < (int) games.size())) {
649 // Create world if it doesn't exist
650 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
651 lua_pushstring(L, "Failed to initialize world");
656 lua_pushstring(L, "Invalid game index");
661 /******************************************************************************/
662 int ModApiMainMenu::l_delete_world(lua_State *L)
664 int worldidx = luaL_checkinteger(L,1) -1;
666 std::vector<WorldSpec> worlds = getAvailableWorlds();
668 if ((worldidx >= 0) &&
669 (worldidx < (int) worlds.size())) {
671 WorldSpec spec = worlds[worldidx];
673 std::vector<std::string> paths;
674 paths.push_back(spec.path);
675 fs::GetRecursiveSubPaths(spec.path, paths);
678 if (!fs::DeletePaths(paths)) {
679 lua_pushstring(L, "Failed to delete world");
686 lua_pushstring(L, "Invalid world index");
691 /******************************************************************************/
692 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
694 GUIEngine* engine = getGuiEngine(L);
695 sanity_check(engine != NULL);
697 std::string text = "";
699 if (!lua_isnone(L,1) && !lua_isnil(L,1))
700 text = luaL_checkstring(L, 1);
702 engine->setTopleftText(text);
706 /******************************************************************************/
707 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
709 std::vector<const char *> names;
710 EmergeManager::getMapgenNames(&names, lua_toboolean(L, 1));
713 for (size_t i = 0; i != names.size(); i++) {
714 lua_pushstring(L, names[i]);
715 lua_rawseti(L, -2, i + 1);
722 /******************************************************************************/
723 int ModApiMainMenu::l_get_modpath(lua_State *L)
725 std::string modpath = fs::RemoveRelativePathComponents(
726 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
727 lua_pushstring(L, modpath.c_str());
731 /******************************************************************************/
732 int ModApiMainMenu::l_get_gamepath(lua_State *L)
734 std::string gamepath = fs::RemoveRelativePathComponents(
735 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
736 lua_pushstring(L, gamepath.c_str());
740 /******************************************************************************/
741 int ModApiMainMenu::l_get_texturepath(lua_State *L)
743 std::string gamepath = fs::RemoveRelativePathComponents(
744 porting::path_user + DIR_DELIM + "textures");
745 lua_pushstring(L, gamepath.c_str());
749 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
751 std::string gamepath = fs::RemoveRelativePathComponents(
752 porting::path_share + DIR_DELIM + "textures");
753 lua_pushstring(L, gamepath.c_str());
757 /******************************************************************************/
758 int ModApiMainMenu::l_create_dir(lua_State *L) {
759 const char *path = luaL_checkstring(L, 1);
761 if (ModApiMainMenu::isMinetestPath(path)) {
762 lua_pushboolean(L, fs::CreateAllDirs(path));
766 lua_pushboolean(L, false);
770 /******************************************************************************/
771 int ModApiMainMenu::l_delete_dir(lua_State *L)
773 const char *path = luaL_checkstring(L, 1);
775 std::string absolute_path = fs::RemoveRelativePathComponents(path);
777 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
778 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
782 lua_pushboolean(L, false);
786 /******************************************************************************/
787 int ModApiMainMenu::l_copy_dir(lua_State *L)
789 const char *source = luaL_checkstring(L, 1);
790 const char *destination = luaL_checkstring(L, 2);
792 bool keep_source = true;
794 if ((!lua_isnone(L,3)) &&
796 keep_source = lua_toboolean(L,3);
799 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
800 std::string absolute_source = fs::RemoveRelativePathComponents(source);
802 if ((ModApiMainMenu::isMinetestPath(absolute_source)) &&
803 (ModApiMainMenu::isMinetestPath(absolute_destination))) {
804 bool retval = fs::CopyDir(absolute_source,absolute_destination);
806 if (retval && (!keep_source)) {
808 retval &= fs::RecursiveDelete(absolute_source);
810 lua_pushboolean(L,retval);
813 lua_pushboolean(L,false);
817 /******************************************************************************/
818 int ModApiMainMenu::l_extract_zip(lua_State *L)
820 GUIEngine* engine = getGuiEngine(L);
821 sanity_check(engine);
823 const char *zipfile = luaL_checkstring(L, 1);
824 const char *destination = luaL_checkstring(L, 2);
826 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
828 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
829 fs::CreateAllDirs(absolute_destination);
831 io::IFileSystem* fs = engine->m_device->getFileSystem();
833 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
834 lua_pushboolean(L,false);
838 sanity_check(fs->getFileArchiveCount() > 0);
840 /**********************************************************************/
841 /* WARNING this is not threadsafe!! */
842 /**********************************************************************/
843 io::IFileArchive* opened_zip =
844 fs->getFileArchive(fs->getFileArchiveCount()-1);
846 const io::IFileList* files_in_zip = opened_zip->getFileList();
848 unsigned int number_of_files = files_in_zip->getFileCount();
850 for (unsigned int i=0; i < number_of_files; i++) {
851 std::string fullpath = destination;
852 fullpath += DIR_DELIM;
853 fullpath += files_in_zip->getFullFileName(i).c_str();
854 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
856 if (!files_in_zip->isDirectory(i)) {
857 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
858 fs->removeFileArchive(fs->getFileArchiveCount()-1);
859 lua_pushboolean(L,false);
863 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
865 FILE *targetfile = fopen(fullpath.c_str(),"wb");
867 if (targetfile == NULL) {
868 fs->removeFileArchive(fs->getFileArchiveCount()-1);
869 lua_pushboolean(L,false);
873 char read_buffer[1024];
876 while (total_read < toread->getSize()) {
878 unsigned int bytes_read =
879 toread->read(read_buffer,sizeof(read_buffer));
880 if ((bytes_read == 0 ) ||
881 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
884 fs->removeFileArchive(fs->getFileArchiveCount()-1);
885 lua_pushboolean(L,false);
888 total_read += bytes_read;
896 fs->removeFileArchive(fs->getFileArchiveCount()-1);
897 lua_pushboolean(L,true);
901 lua_pushboolean(L,false);
905 /******************************************************************************/
906 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
908 GUIEngine* engine = getGuiEngine(L);
909 sanity_check(engine != NULL);
911 lua_pushstring(L,engine->getScriptDir().c_str());
915 /******************************************************************************/
916 bool ModApiMainMenu::isMinetestPath(std::string path)
918 if (fs::PathStartsWith(path,fs::TempPath()))
922 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
926 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
930 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
937 /******************************************************************************/
938 int ModApiMainMenu::l_show_file_open_dialog(lua_State *L)
940 GUIEngine* engine = getGuiEngine(L);
941 sanity_check(engine != NULL);
943 const char *formname= luaL_checkstring(L, 1);
944 const char *title = luaL_checkstring(L, 2);
946 GUIFileSelectMenu* fileOpenMenu =
947 new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(),
950 engine->m_menumanager,
953 fileOpenMenu->setTextDest(engine->m_buttonhandler);
954 fileOpenMenu->drop();
958 /******************************************************************************/
959 int ModApiMainMenu::l_get_version(lua_State *L)
961 lua_pushstring(L, g_version_string);
965 /******************************************************************************/
966 int ModApiMainMenu::l_sound_play(lua_State *L)
968 GUIEngine* engine = getGuiEngine(L);
970 SimpleSoundSpec spec;
971 read_soundspec(L, 1, spec);
972 bool looped = lua_toboolean(L, 2);
974 u32 handle = engine->playSound(spec, looped);
976 lua_pushinteger(L, handle);
981 /******************************************************************************/
982 int ModApiMainMenu::l_sound_stop(lua_State *L)
984 GUIEngine* engine = getGuiEngine(L);
986 u32 handle = luaL_checkinteger(L, 1);
987 engine->stopSound(handle);
992 /******************************************************************************/
993 int ModApiMainMenu::l_download_file(lua_State *L)
995 const char *url = luaL_checkstring(L, 1);
996 const char *target = luaL_checkstring(L, 2);
999 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
1001 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
1002 if (GUIEngine::downloadFile(url,absolute_destination)) {
1003 lua_pushboolean(L,true);
1007 errorstream << "DOWNLOAD denied: " << absolute_destination
1008 << " isn't a allowed path" << std::endl;
1010 lua_pushboolean(L,false);
1014 /******************************************************************************/
1015 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
1017 std::vector<irr::video::E_DRIVER_TYPE> drivers
1018 = porting::getSupportedVideoDrivers();
1021 for (u32 i = 0; i != drivers.size(); i++) {
1022 const char *name = porting::getVideoDriverName(drivers[i]);
1023 const char *fname = porting::getVideoDriverFriendlyName(drivers[i]);
1026 lua_pushstring(L, name);
1027 lua_setfield(L, -2, "name");
1028 lua_pushstring(L, fname);
1029 lua_setfield(L, -2, "friendly_name");
1031 lua_rawseti(L, -2, i + 1);
1037 /******************************************************************************/
1038 int ModApiMainMenu::l_get_video_modes(lua_State *L)
1040 std::vector<core::vector3d<u32> > videomodes
1041 = porting::getSupportedVideoModes();
1044 for (u32 i = 0; i != videomodes.size(); i++) {
1046 lua_pushnumber(L, videomodes[i].X);
1047 lua_setfield(L, -2, "w");
1048 lua_pushnumber(L, videomodes[i].Y);
1049 lua_setfield(L, -2, "h");
1050 lua_pushnumber(L, videomodes[i].Z);
1051 lua_setfield(L, -2, "depth");
1053 lua_rawseti(L, -2, i + 1);
1059 /******************************************************************************/
1060 int ModApiMainMenu::l_gettext(lua_State *L)
1062 std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
1063 lua_pushstring(L, text.c_str());
1068 /******************************************************************************/
1069 int ModApiMainMenu::l_get_screen_info(lua_State *L)
1072 int top = lua_gettop(L);
1073 lua_pushstring(L,"density");
1074 lua_pushnumber(L,porting::getDisplayDensity());
1075 lua_settable(L, top);
1077 lua_pushstring(L,"display_width");
1078 lua_pushnumber(L,porting::getDisplaySize().X);
1079 lua_settable(L, top);
1081 lua_pushstring(L,"display_height");
1082 lua_pushnumber(L,porting::getDisplaySize().Y);
1083 lua_settable(L, top);
1085 lua_pushstring(L,"window_width");
1086 lua_pushnumber(L,porting::getWindowSize().X);
1087 lua_settable(L, top);
1089 lua_pushstring(L,"window_height");
1090 lua_pushnumber(L,porting::getWindowSize().Y);
1091 lua_settable(L, top);
1095 /******************************************************************************/
1096 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
1098 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
1102 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
1104 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
1108 /******************************************************************************/
1109 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1111 GUIEngine* engine = getGuiEngine(L);
1113 size_t func_length, param_length;
1114 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1116 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1118 sanity_check(serialized_func_raw != NULL);
1119 sanity_check(serialized_param_raw != NULL);
1121 std::string serialized_func = std::string(serialized_func_raw, func_length);
1122 std::string serialized_param = std::string(serialized_param_raw, param_length);
1124 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1129 /******************************************************************************/
1130 void ModApiMainMenu::Initialize(lua_State *L, int top)
1132 API_FCT(update_formspec);
1133 API_FCT(set_clouds);
1134 API_FCT(get_textlist_index);
1135 API_FCT(get_table_index);
1136 API_FCT(get_worlds);
1140 API_FCT(get_favorites);
1141 API_FCT(show_keys_menu);
1142 API_FCT(create_world);
1143 API_FCT(delete_world);
1144 API_FCT(delete_favorite);
1145 API_FCT(set_background);
1146 API_FCT(set_topleft_text);
1147 API_FCT(get_mapgen_names);
1148 API_FCT(get_modpath);
1149 API_FCT(get_gamepath);
1150 API_FCT(get_texturepath);
1151 API_FCT(get_texturepath_share);
1152 API_FCT(create_dir);
1153 API_FCT(delete_dir);
1155 API_FCT(extract_zip);
1156 API_FCT(get_mainmenu_path);
1157 API_FCT(show_file_open_dialog);
1158 API_FCT(get_version);
1159 API_FCT(download_file);
1160 API_FCT(get_modstore_details);
1161 API_FCT(get_modstore_list);
1162 API_FCT(sound_play);
1163 API_FCT(sound_stop);
1165 API_FCT(get_video_drivers);
1166 API_FCT(get_video_modes);
1167 API_FCT(get_screen_info);
1168 API_FCT(get_min_supp_proto);
1169 API_FCT(get_max_supp_proto);
1170 API_FCT(do_async_callback);
1173 /******************************************************************************/
1174 void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
1177 ASYNC_API_FCT(get_worlds);
1178 ASYNC_API_FCT(get_games);
1179 ASYNC_API_FCT(get_favorites);
1180 ASYNC_API_FCT(get_mapgen_names);
1181 ASYNC_API_FCT(get_modpath);
1182 ASYNC_API_FCT(get_gamepath);
1183 ASYNC_API_FCT(get_texturepath);
1184 ASYNC_API_FCT(get_texturepath_share);
1185 ASYNC_API_FCT(create_dir);
1186 ASYNC_API_FCT(delete_dir);
1187 ASYNC_API_FCT(copy_dir);
1188 //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1189 ASYNC_API_FCT(get_version);
1190 ASYNC_API_FCT(download_file);
1191 ASYNC_API_FCT(get_modstore_details);
1192 ASYNC_API_FCT(get_modstore_list);
1193 //ASYNC_API_FCT(gettext); (gettext lib isn't threadsafe)