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"
36 #include "main.h" // for g_settings
38 #include <IFileArchive.h>
39 #include <IFileSystem.h>
41 /******************************************************************************/
42 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
44 lua_getglobal(L, "gamedata");
46 lua_getfield(L, -1, name.c_str());
51 return luaL_checkstring(L, -1);
54 /******************************************************************************/
55 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
57 lua_getglobal(L, "gamedata");
59 lua_getfield(L, -1, name.c_str());
61 if(lua_isnil(L, -1)) {
67 return luaL_checkinteger(L, -1);
70 /******************************************************************************/
71 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
73 lua_getglobal(L, "gamedata");
75 lua_getfield(L, -1, name.c_str());
77 if(lua_isnil(L, -1)) {
83 return lua_toboolean(L, -1);
86 /******************************************************************************/
87 int ModApiMainMenu::l_update_formspec(lua_State *L)
89 GUIEngine* engine = getGuiEngine(L);
92 if (engine->m_startgame)
96 std::string formspec(luaL_checkstring(L, 1));
98 if (engine->m_formspecgui != 0) {
99 engine->m_formspecgui->setForm(formspec);
105 /******************************************************************************/
106 int ModApiMainMenu::l_start(lua_State *L)
108 GUIEngine* engine = getGuiEngine(L);
111 //update c++ gamedata from lua table
116 engine->m_data->selected_world = getIntegerData(L, "selected_world",valid) -1;
117 engine->m_data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
118 engine->m_data->name = getTextData(L,"playername");
119 engine->m_data->password = getTextData(L,"password");
120 engine->m_data->address = getTextData(L,"address");
121 engine->m_data->port = getTextData(L,"port");
122 engine->m_data->serverdescription = getTextData(L,"serverdescription");
123 engine->m_data->servername = getTextData(L,"servername");
125 //close menu next time
126 engine->m_startgame = true;
130 /******************************************************************************/
131 int ModApiMainMenu::l_close(lua_State *L)
133 GUIEngine* engine = getGuiEngine(L);
136 engine->m_kill = true;
140 /******************************************************************************/
141 int ModApiMainMenu::l_set_background(lua_State *L)
143 GUIEngine* engine = getGuiEngine(L);
146 std::string backgroundlevel(luaL_checkstring(L, 1));
147 std::string texturename(luaL_checkstring(L, 2));
149 bool tile_image = false;
151 unsigned int minsize = 16;
153 if (!lua_isnone(L, 3)) {
154 tile_image = lua_toboolean(L, 3);
157 if (!lua_isnone(L, 4)) {
158 minsize = lua_tonumber(L, 4);
161 if (backgroundlevel == "background") {
162 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
163 tile_image, minsize);
166 if (backgroundlevel == "overlay") {
167 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
168 tile_image, minsize);
171 if (backgroundlevel == "header") {
172 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
173 tile_image, minsize);
176 if (backgroundlevel == "footer") {
177 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
178 tile_image, minsize);
181 lua_pushboolean(L,retval);
185 /******************************************************************************/
186 int ModApiMainMenu::l_set_clouds(lua_State *L)
188 GUIEngine* engine = getGuiEngine(L);
191 bool value = lua_toboolean(L,1);
193 engine->m_clouds_enabled = value;
198 /******************************************************************************/
199 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
201 // get_table_index accepts both tables and textlists
202 return l_get_table_index(L);
205 /******************************************************************************/
206 int ModApiMainMenu::l_get_table_index(lua_State *L)
208 GUIEngine* engine = getGuiEngine(L);
211 std::wstring tablename(narrow_to_wide(luaL_checkstring(L, 1)));
212 GUITable *table = engine->m_menu->getTable(tablename);
213 s32 selection = table ? table->getSelected() : 0;
216 lua_pushinteger(L, selection);
222 /******************************************************************************/
223 int ModApiMainMenu::l_get_worlds(lua_State *L)
225 std::vector<WorldSpec> worlds = getAvailableWorlds();
228 int top = lua_gettop(L);
229 unsigned int index = 1;
231 for (unsigned int i = 0; i < worlds.size(); i++)
233 lua_pushnumber(L,index);
236 int top_lvl2 = lua_gettop(L);
238 lua_pushstring(L,"path");
239 lua_pushstring(L,worlds[i].path.c_str());
240 lua_settable(L, top_lvl2);
242 lua_pushstring(L,"name");
243 lua_pushstring(L,worlds[i].name.c_str());
244 lua_settable(L, top_lvl2);
246 lua_pushstring(L,"gameid");
247 lua_pushstring(L,worlds[i].gameid.c_str());
248 lua_settable(L, top_lvl2);
250 lua_settable(L, top);
256 /******************************************************************************/
257 int ModApiMainMenu::l_get_games(lua_State *L)
259 std::vector<SubgameSpec> games = getAvailableGames();
262 int top = lua_gettop(L);
263 unsigned int index = 1;
265 for (unsigned int i = 0; i < games.size(); i++)
267 lua_pushnumber(L,index);
269 int top_lvl2 = lua_gettop(L);
271 lua_pushstring(L,"id");
272 lua_pushstring(L,games[i].id.c_str());
273 lua_settable(L, top_lvl2);
275 lua_pushstring(L,"path");
276 lua_pushstring(L,games[i].path.c_str());
277 lua_settable(L, top_lvl2);
279 lua_pushstring(L,"gamemods_path");
280 lua_pushstring(L,games[i].gamemods_path.c_str());
281 lua_settable(L, top_lvl2);
283 lua_pushstring(L,"name");
284 lua_pushstring(L,games[i].name.c_str());
285 lua_settable(L, top_lvl2);
287 lua_pushstring(L,"menuicon_path");
288 lua_pushstring(L,games[i].menuicon_path.c_str());
289 lua_settable(L, top_lvl2);
291 lua_pushstring(L,"addon_mods_paths");
293 int table2 = lua_gettop(L);
294 int internal_index=1;
295 for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
296 iter != games[i].addon_mods_paths.end(); iter++) {
297 lua_pushnumber(L,internal_index);
298 lua_pushstring(L,(*iter).c_str());
299 lua_settable(L, table2);
302 lua_settable(L, top_lvl2);
303 lua_settable(L, top);
308 /******************************************************************************/
309 int ModApiMainMenu::l_get_modstore_details(lua_State *L)
311 const char *modid = luaL_checkstring(L, 1);
315 std::string url = "";
317 url = g_settings->get("modstore_details_url");
319 catch(SettingNotFoundException &e) {
324 size_t idpos = url.find("*");
326 url.insert(idpos,modid);
328 details = getModstoreUrl(url);
330 ModStoreModDetails current_mod = readModStoreModDetails(details);
332 if ( current_mod.valid) {
334 int top = lua_gettop(L);
336 lua_pushstring(L,"id");
337 lua_pushnumber(L,current_mod.id);
338 lua_settable(L, top);
340 lua_pushstring(L,"title");
341 lua_pushstring(L,current_mod.title.c_str());
342 lua_settable(L, top);
344 lua_pushstring(L,"basename");
345 lua_pushstring(L,current_mod.basename.c_str());
346 lua_settable(L, top);
348 lua_pushstring(L,"description");
349 lua_pushstring(L,current_mod.description.c_str());
350 lua_settable(L, top);
352 lua_pushstring(L,"author");
353 lua_pushstring(L,current_mod.author.username.c_str());
354 lua_settable(L, top);
356 lua_pushstring(L,"download_url");
357 lua_pushstring(L,current_mod.versions[0].file.c_str());
358 lua_settable(L, top);
360 lua_pushstring(L,"versions");
362 int versionstop = lua_gettop(L);
363 for (unsigned int i=0;i < current_mod.versions.size(); i++) {
364 lua_pushnumber(L,i+1);
366 int current_element = lua_gettop(L);
368 lua_pushstring(L,"date");
369 lua_pushstring(L,current_mod.versions[i].date.c_str());
370 lua_settable(L,current_element);
372 lua_pushstring(L,"download_url");
373 lua_pushstring(L,current_mod.versions[i].file.c_str());
374 lua_settable(L,current_element);
376 lua_settable(L,versionstop);
378 lua_settable(L, top);
380 lua_pushstring(L,"screenshot_url");
381 lua_pushstring(L,current_mod.titlepic.file.c_str());
382 lua_settable(L, top);
384 lua_pushstring(L,"license");
385 lua_pushstring(L,current_mod.license.shortinfo.c_str());
386 lua_settable(L, top);
388 lua_pushstring(L,"rating");
389 lua_pushnumber(L,current_mod.rating);
390 lua_settable(L, top);
401 /******************************************************************************/
402 int ModApiMainMenu::l_get_modstore_list(lua_State *L)
405 std::string url = "";
407 url = g_settings->get("modstore_listmods_url");
409 catch(SettingNotFoundException &e) {
414 mods = getModstoreUrl(url);
416 std::vector<ModStoreMod> moddata = readModStoreList(mods);
419 int top = lua_gettop(L);
420 unsigned int index = 1;
422 for (unsigned int i = 0; i < moddata.size(); i++)
424 if (moddata[i].valid) {
425 lua_pushnumber(L,index);
428 int top_lvl2 = lua_gettop(L);
430 lua_pushstring(L,"id");
431 lua_pushnumber(L,moddata[i].id);
432 lua_settable(L, top_lvl2);
434 lua_pushstring(L,"title");
435 lua_pushstring(L,moddata[i].title.c_str());
436 lua_settable(L, top_lvl2);
438 lua_pushstring(L,"basename");
439 lua_pushstring(L,moddata[i].basename.c_str());
440 lua_settable(L, top_lvl2);
442 lua_settable(L, top);
449 /******************************************************************************/
450 int ModApiMainMenu::l_get_favorites(lua_State *L)
452 std::string listtype = "local";
454 if (!lua_isnone(L,1)) {
455 listtype = luaL_checkstring(L,1);
458 std::vector<ServerListSpec> servers;
460 if(listtype == "online") {
461 servers = ServerList::getOnline();
463 servers = ServerList::getLocal();
467 int top = lua_gettop(L);
468 unsigned int index = 1;
470 for (unsigned int i = 0; i < servers.size(); i++)
472 lua_pushnumber(L,index);
475 int top_lvl2 = lua_gettop(L);
477 if (servers[i]["clients"].asString().size()) {
478 std::string clients_raw = servers[i]["clients"].asString();
480 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
482 if ((clients_raw != "") && (*endptr == 0)) {
483 lua_pushstring(L,"clients");
484 lua_pushnumber(L,numbervalue);
485 lua_settable(L, top_lvl2);
489 if (servers[i]["clients_max"].asString().size()) {
491 std::string clients_max_raw = servers[i]["clients_max"].asString();
493 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
495 if ((clients_max_raw != "") && (*endptr == 0)) {
496 lua_pushstring(L,"clients_max");
497 lua_pushnumber(L,numbervalue);
498 lua_settable(L, top_lvl2);
502 if (servers[i]["version"].asString().size()) {
503 lua_pushstring(L,"version");
504 std::string topush = servers[i]["version"].asString();
505 lua_pushstring(L,topush.c_str());
506 lua_settable(L, top_lvl2);
509 if (servers[i]["password"].asString().size()) {
510 lua_pushstring(L,"password");
511 lua_pushboolean(L,true);
512 lua_settable(L, top_lvl2);
515 if (servers[i]["creative"].asString().size()) {
516 lua_pushstring(L,"creative");
517 lua_pushboolean(L,true);
518 lua_settable(L, top_lvl2);
521 if (servers[i]["damage"].asString().size()) {
522 lua_pushstring(L,"damage");
523 lua_pushboolean(L,true);
524 lua_settable(L, top_lvl2);
527 if (servers[i]["pvp"].asString().size()) {
528 lua_pushstring(L,"pvp");
529 lua_pushboolean(L,true);
530 lua_settable(L, top_lvl2);
533 if (servers[i]["description"].asString().size()) {
534 lua_pushstring(L,"description");
535 std::string topush = servers[i]["description"].asString();
536 lua_pushstring(L,topush.c_str());
537 lua_settable(L, top_lvl2);
540 if (servers[i]["name"].asString().size()) {
541 lua_pushstring(L,"name");
542 std::string topush = servers[i]["name"].asString();
543 lua_pushstring(L,topush.c_str());
544 lua_settable(L, top_lvl2);
547 if (servers[i]["address"].asString().size()) {
548 lua_pushstring(L,"address");
549 std::string topush = servers[i]["address"].asString();
550 lua_pushstring(L,topush.c_str());
551 lua_settable(L, top_lvl2);
554 if (servers[i]["port"].asString().size()) {
555 lua_pushstring(L,"port");
556 std::string topush = servers[i]["port"].asString();
557 lua_pushstring(L,topush.c_str());
558 lua_settable(L, top_lvl2);
561 lua_settable(L, top);
567 /******************************************************************************/
568 int ModApiMainMenu::l_delete_favorite(lua_State *L)
570 std::vector<ServerListSpec> servers;
572 std::string listtype = "local";
574 if (!lua_isnone(L,2)) {
575 listtype = luaL_checkstring(L,2);
578 if ((listtype != "local") &&
579 (listtype != "online"))
583 if(listtype == "online") {
584 servers = ServerList::getOnline();
586 servers = ServerList::getLocal();
589 int fav_idx = luaL_checkinteger(L,1) -1;
591 if ((fav_idx >= 0) &&
592 (fav_idx < (int) servers.size())) {
594 ServerList::deleteEntry(servers[fav_idx]);
600 /******************************************************************************/
601 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
603 GUIEngine* engine = getGuiEngine(L);
606 GUIKeyChangeMenu *kmenu
607 = new GUIKeyChangeMenu( engine->m_device->getGUIEnvironment(),
610 engine->m_menumanager);
615 /******************************************************************************/
616 int ModApiMainMenu::l_create_world(lua_State *L)
618 const char *name = luaL_checkstring(L, 1);
619 int gameidx = luaL_checkinteger(L,2) -1;
621 std::string path = porting::path_user + DIR_DELIM
625 std::vector<SubgameSpec> games = getAvailableGames();
627 if ((gameidx >= 0) &&
628 (gameidx < (int) games.size())) {
630 // Create world if it doesn't exist
631 if(!initializeWorld(path, games[gameidx].id)){
632 lua_pushstring(L, "Failed to initialize world");
640 lua_pushstring(L, "Invalid game index");
645 /******************************************************************************/
646 int ModApiMainMenu::l_delete_world(lua_State *L)
648 int worldidx = luaL_checkinteger(L,1) -1;
650 std::vector<WorldSpec> worlds = getAvailableWorlds();
652 if ((worldidx >= 0) &&
653 (worldidx < (int) worlds.size())) {
655 WorldSpec spec = worlds[worldidx];
657 std::vector<std::string> paths;
658 paths.push_back(spec.path);
659 fs::GetRecursiveSubPaths(spec.path, paths);
662 if (!fs::DeletePaths(paths)) {
663 lua_pushstring(L, "Failed to delete world");
670 lua_pushstring(L, "Invalid world index");
675 /******************************************************************************/
676 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
678 GUIEngine* engine = getGuiEngine(L);
681 std::string text = "";
683 if (!lua_isnone(L,1) && !lua_isnil(L,1))
684 text = luaL_checkstring(L, 1);
686 engine->setTopleftText(text);
690 /******************************************************************************/
691 int ModApiMainMenu::l_get_modpath(lua_State *L)
694 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
695 lua_pushstring(L, modpath.c_str());
699 /******************************************************************************/
700 int ModApiMainMenu::l_get_gamepath(lua_State *L)
703 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
704 lua_pushstring(L, gamepath.c_str());
708 /******************************************************************************/
709 int ModApiMainMenu::l_get_texturepath(lua_State *L)
712 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures");
713 lua_pushstring(L, gamepath.c_str());
717 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
720 = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "textures");
721 lua_pushstring(L, gamepath.c_str());
725 /******************************************************************************/
726 int ModApiMainMenu::l_get_dirlist(lua_State *L)
728 const char *path = luaL_checkstring(L, 1);
729 bool dironly = lua_toboolean(L, 2);
731 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
733 unsigned int index = 1;
735 int table = lua_gettop(L);
737 for (unsigned int i=0;i< dirlist.size(); i++) {
738 if ((dirlist[i].dir) || (dironly == false)) {
739 lua_pushnumber(L,index);
740 lua_pushstring(L,dirlist[i].name.c_str());
741 lua_settable(L, table);
749 /******************************************************************************/
750 int ModApiMainMenu::l_create_dir(lua_State *L) {
751 const char *path = luaL_checkstring(L, 1);
753 if (ModApiMainMenu::isMinetestPath(path)) {
754 lua_pushboolean(L,fs::CreateAllDirs(path));
757 lua_pushboolean(L,false);
761 /******************************************************************************/
762 int ModApiMainMenu::l_delete_dir(lua_State *L)
764 const char *path = luaL_checkstring(L, 1);
766 std::string absolute_path = fs::RemoveRelativePathComponents(path);
768 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
769 lua_pushboolean(L,fs::RecursiveDelete(absolute_path));
772 lua_pushboolean(L,false);
776 /******************************************************************************/
777 int ModApiMainMenu::l_copy_dir(lua_State *L)
779 const char *source = luaL_checkstring(L, 1);
780 const char *destination = luaL_checkstring(L, 2);
782 bool keep_source = true;
784 if ((!lua_isnone(L,3)) &&
786 keep_source = lua_toboolean(L,3);
789 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
790 std::string absolute_source = fs::RemoveRelativePathComponents(source);
792 if ((ModApiMainMenu::isMinetestPath(absolute_source)) &&
793 (ModApiMainMenu::isMinetestPath(absolute_destination))) {
794 bool retval = fs::CopyDir(absolute_source,absolute_destination);
796 if (retval && (!keep_source)) {
798 retval &= fs::RecursiveDelete(absolute_source);
800 lua_pushboolean(L,retval);
803 lua_pushboolean(L,false);
807 /******************************************************************************/
808 int ModApiMainMenu::l_extract_zip(lua_State *L)
810 GUIEngine* engine = getGuiEngine(L);
813 const char *zipfile = luaL_checkstring(L, 1);
814 const char *destination = luaL_checkstring(L, 2);
816 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
818 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
819 fs::CreateAllDirs(absolute_destination);
821 io::IFileSystem* fs = engine->m_device->getFileSystem();
823 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
824 lua_pushboolean(L,false);
828 assert(fs->getFileArchiveCount() > 0);
830 /**********************************************************************/
831 /* WARNING this is not threadsafe!! */
832 /**********************************************************************/
833 io::IFileArchive* opened_zip =
834 fs->getFileArchive(fs->getFileArchiveCount()-1);
836 const io::IFileList* files_in_zip = opened_zip->getFileList();
838 unsigned int number_of_files = files_in_zip->getFileCount();
840 for (unsigned int i=0; i < number_of_files; i++) {
841 std::string fullpath = destination;
842 fullpath += DIR_DELIM;
843 fullpath += files_in_zip->getFullFileName(i).c_str();
845 if (files_in_zip->isDirectory(i)) {
846 if (! fs::CreateAllDirs(fullpath) ) {
847 fs->removeFileArchive(fs->getFileArchiveCount()-1);
848 lua_pushboolean(L,false);
853 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
855 FILE *targetfile = fopen(fullpath.c_str(),"wb");
857 if (targetfile == NULL) {
858 fs->removeFileArchive(fs->getFileArchiveCount()-1);
859 lua_pushboolean(L,false);
863 char read_buffer[1024];
864 unsigned int total_read = 0;
866 while (total_read < toread->getSize()) {
868 unsigned int bytes_read =
869 toread->read(read_buffer,sizeof(read_buffer));
870 if ((bytes_read == 0 ) ||
871 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
874 fs->removeFileArchive(fs->getFileArchiveCount()-1);
875 lua_pushboolean(L,false);
878 total_read += bytes_read;
886 fs->removeFileArchive(fs->getFileArchiveCount()-1);
887 lua_pushboolean(L,true);
891 lua_pushboolean(L,false);
895 /******************************************************************************/
896 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
898 GUIEngine* engine = getGuiEngine(L);
901 lua_pushstring(L,engine->getScriptDir().c_str());
905 /******************************************************************************/
906 bool ModApiMainMenu::isMinetestPath(std::string path)
908 if (fs::PathStartsWith(path,fs::TempPath()))
912 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
916 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
920 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
927 /******************************************************************************/
928 int ModApiMainMenu::l_show_file_open_dialog(lua_State *L)
930 GUIEngine* engine = getGuiEngine(L);
933 const char *formname= luaL_checkstring(L, 1);
934 const char *title = luaL_checkstring(L, 2);
936 GUIFileSelectMenu* fileOpenMenu =
937 new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(),
940 engine->m_menumanager,
943 fileOpenMenu->setTextDest(engine->m_buttonhandler);
944 fileOpenMenu->drop();
948 /******************************************************************************/
949 int ModApiMainMenu::l_get_version(lua_State *L)
951 lua_pushstring(L, minetest_version_simple);
955 /******************************************************************************/
956 int ModApiMainMenu::l_sound_play(lua_State *L)
958 GUIEngine* engine = getGuiEngine(L);
960 SimpleSoundSpec spec;
961 read_soundspec(L, 1, spec);
962 bool looped = lua_toboolean(L, 2);
964 u32 handle = engine->playSound(spec, looped);
966 lua_pushinteger(L, handle);
971 /******************************************************************************/
972 int ModApiMainMenu::l_sound_stop(lua_State *L)
974 GUIEngine* engine = getGuiEngine(L);
976 u32 handle = luaL_checkinteger(L, 1);
977 engine->stopSound(handle);
982 /******************************************************************************/
983 int ModApiMainMenu::l_download_file(lua_State *L)
985 const char *url = luaL_checkstring(L, 1);
986 const char *target = luaL_checkstring(L, 2);
989 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
991 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
992 if (GUIEngine::downloadFile(url,absolute_destination)) {
993 lua_pushboolean(L,true);
997 errorstream << "DOWNLOAD denied: " << absolute_destination
998 << " isn't a allowed path" << std::endl;
1000 lua_pushboolean(L,false);
1004 /******************************************************************************/
1005 int ModApiMainMenu::l_gettext(lua_State *L)
1007 std::wstring wtext = wstrgettext((std::string) luaL_checkstring(L, 1));
1008 lua_pushstring(L, wide_to_narrow(wtext).c_str());
1013 /******************************************************************************/
1014 int ModApiMainMenu::l_get_screen_info(lua_State *L)
1017 int top = lua_gettop(L);
1018 lua_pushstring(L,"density");
1019 lua_pushnumber(L,porting::getDisplayDensity());
1020 lua_settable(L, top);
1022 lua_pushstring(L,"display_width");
1023 lua_pushnumber(L,porting::getDisplaySize().X);
1024 lua_settable(L, top);
1026 lua_pushstring(L,"display_height");
1027 lua_pushnumber(L,porting::getDisplaySize().Y);
1028 lua_settable(L, top);
1030 lua_pushstring(L,"window_width");
1031 lua_pushnumber(L,porting::getWindowSize().X);
1032 lua_settable(L, top);
1034 lua_pushstring(L,"window_height");
1035 lua_pushnumber(L,porting::getWindowSize().Y);
1036 lua_settable(L, top);
1040 /******************************************************************************/
1041 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1043 GUIEngine* engine = getGuiEngine(L);
1045 size_t func_length, param_length;
1046 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1048 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1050 assert(serialized_func_raw != NULL);
1051 assert(serialized_param_raw != NULL);
1053 std::string serialized_func = std::string(serialized_func_raw, func_length);
1054 std::string serialized_param = std::string(serialized_param_raw, param_length);
1056 lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1061 /******************************************************************************/
1062 void ModApiMainMenu::Initialize(lua_State *L, int top)
1064 API_FCT(update_formspec);
1065 API_FCT(set_clouds);
1066 API_FCT(get_textlist_index);
1067 API_FCT(get_table_index);
1068 API_FCT(get_worlds);
1072 API_FCT(get_favorites);
1073 API_FCT(show_keys_menu);
1074 API_FCT(create_world);
1075 API_FCT(delete_world);
1076 API_FCT(delete_favorite);
1077 API_FCT(set_background);
1078 API_FCT(set_topleft_text);
1079 API_FCT(get_modpath);
1080 API_FCT(get_gamepath);
1081 API_FCT(get_texturepath);
1082 API_FCT(get_texturepath_share);
1083 API_FCT(get_dirlist);
1084 API_FCT(create_dir);
1085 API_FCT(delete_dir);
1087 API_FCT(extract_zip);
1088 API_FCT(get_mainmenu_path);
1089 API_FCT(show_file_open_dialog);
1090 API_FCT(get_version);
1091 API_FCT(download_file);
1092 API_FCT(get_modstore_details);
1093 API_FCT(get_modstore_list);
1094 API_FCT(sound_play);
1095 API_FCT(sound_stop);
1097 API_FCT(get_screen_info);
1098 API_FCT(do_async_callback);
1101 /******************************************************************************/
1102 void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
1105 ASYNC_API_FCT(get_worlds);
1106 ASYNC_API_FCT(get_games);
1107 ASYNC_API_FCT(get_favorites);
1108 ASYNC_API_FCT(get_modpath);
1109 ASYNC_API_FCT(get_gamepath);
1110 ASYNC_API_FCT(get_texturepath);
1111 ASYNC_API_FCT(get_texturepath_share);
1112 ASYNC_API_FCT(get_dirlist);
1113 ASYNC_API_FCT(create_dir);
1114 ASYNC_API_FCT(delete_dir);
1115 ASYNC_API_FCT(copy_dir);
1116 //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1117 ASYNC_API_FCT(get_version);
1118 ASYNC_API_FCT(download_file);
1119 ASYNC_API_FCT(get_modstore_details);
1120 ASYNC_API_FCT(get_modstore_list);
1121 //ASYNC_API_FCT(gettext); (gettext lib isn't threadsafe)