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 "lua_api/l_async_events.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));
151 if (backgroundlevel == "background") {
152 retval |= engine->setTexture(TEX_LAYER_BACKGROUND,texturename);
155 if (backgroundlevel == "overlay") {
156 retval |= engine->setTexture(TEX_LAYER_OVERLAY,texturename);
159 if (backgroundlevel == "header") {
160 retval |= engine->setTexture(TEX_LAYER_HEADER,texturename);
163 if (backgroundlevel == "footer") {
164 retval |= engine->setTexture(TEX_LAYER_FOOTER,texturename);
167 lua_pushboolean(L,retval);
171 /******************************************************************************/
172 int ModApiMainMenu::l_set_clouds(lua_State *L)
174 GUIEngine* engine = getGuiEngine(L);
177 bool value = lua_toboolean(L,1);
179 engine->m_clouds_enabled = value;
184 /******************************************************************************/
185 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
187 // get_table_index accepts both tables and textlists
188 return l_get_table_index(L);
191 /******************************************************************************/
192 int ModApiMainMenu::l_get_table_index(lua_State *L)
194 GUIEngine* engine = getGuiEngine(L);
197 std::wstring tablename(narrow_to_wide(luaL_checkstring(L, 1)));
198 GUITable *table = engine->m_menu->getTable(tablename);
199 s32 selection = table ? table->getSelected() : 0;
202 lua_pushinteger(L, selection);
208 /******************************************************************************/
209 int ModApiMainMenu::l_get_worlds(lua_State *L)
211 std::vector<WorldSpec> worlds = getAvailableWorlds();
214 int top = lua_gettop(L);
215 unsigned int index = 1;
217 for (unsigned int i = 0; i < worlds.size(); i++)
219 lua_pushnumber(L,index);
222 int top_lvl2 = lua_gettop(L);
224 lua_pushstring(L,"path");
225 lua_pushstring(L,worlds[i].path.c_str());
226 lua_settable(L, top_lvl2);
228 lua_pushstring(L,"name");
229 lua_pushstring(L,worlds[i].name.c_str());
230 lua_settable(L, top_lvl2);
232 lua_pushstring(L,"gameid");
233 lua_pushstring(L,worlds[i].gameid.c_str());
234 lua_settable(L, top_lvl2);
236 lua_settable(L, top);
242 /******************************************************************************/
243 int ModApiMainMenu::l_get_games(lua_State *L)
245 std::vector<SubgameSpec> games = getAvailableGames();
248 int top = lua_gettop(L);
249 unsigned int index = 1;
251 for (unsigned int i = 0; i < games.size(); i++)
253 lua_pushnumber(L,index);
255 int top_lvl2 = lua_gettop(L);
257 lua_pushstring(L,"id");
258 lua_pushstring(L,games[i].id.c_str());
259 lua_settable(L, top_lvl2);
261 lua_pushstring(L,"path");
262 lua_pushstring(L,games[i].path.c_str());
263 lua_settable(L, top_lvl2);
265 lua_pushstring(L,"gamemods_path");
266 lua_pushstring(L,games[i].gamemods_path.c_str());
267 lua_settable(L, top_lvl2);
269 lua_pushstring(L,"name");
270 lua_pushstring(L,games[i].name.c_str());
271 lua_settable(L, top_lvl2);
273 lua_pushstring(L,"menuicon_path");
274 lua_pushstring(L,games[i].menuicon_path.c_str());
275 lua_settable(L, top_lvl2);
277 lua_pushstring(L,"addon_mods_paths");
279 int table2 = lua_gettop(L);
280 int internal_index=1;
281 for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
282 iter != games[i].addon_mods_paths.end(); iter++) {
283 lua_pushnumber(L,internal_index);
284 lua_pushstring(L,(*iter).c_str());
285 lua_settable(L, table2);
288 lua_settable(L, top_lvl2);
289 lua_settable(L, top);
294 /******************************************************************************/
295 int ModApiMainMenu::l_get_modstore_details(lua_State *L)
297 const char *modid = luaL_checkstring(L, 1);
301 std::string url = "";
303 url = g_settings->get("modstore_details_url");
305 catch(SettingNotFoundException &e) {
310 size_t idpos = url.find("*");
312 url.insert(idpos,modid);
314 details = getModstoreUrl(url);
316 ModStoreModDetails current_mod = readModStoreModDetails(details);
318 if ( current_mod.valid) {
320 int top = lua_gettop(L);
322 lua_pushstring(L,"id");
323 lua_pushnumber(L,current_mod.id);
324 lua_settable(L, top);
326 lua_pushstring(L,"title");
327 lua_pushstring(L,current_mod.title.c_str());
328 lua_settable(L, top);
330 lua_pushstring(L,"basename");
331 lua_pushstring(L,current_mod.basename.c_str());
332 lua_settable(L, top);
334 lua_pushstring(L,"description");
335 lua_pushstring(L,current_mod.description.c_str());
336 lua_settable(L, top);
338 lua_pushstring(L,"author");
339 lua_pushstring(L,current_mod.author.username.c_str());
340 lua_settable(L, top);
342 lua_pushstring(L,"download_url");
343 lua_pushstring(L,current_mod.versions[0].file.c_str());
344 lua_settable(L, top);
346 lua_pushstring(L,"versions");
348 int versionstop = lua_gettop(L);
349 for (unsigned int i=0;i < current_mod.versions.size(); i++) {
350 lua_pushnumber(L,i+1);
352 int current_element = lua_gettop(L);
354 lua_pushstring(L,"date");
355 lua_pushstring(L,current_mod.versions[i].date.c_str());
356 lua_settable(L,current_element);
358 lua_pushstring(L,"download_url");
359 lua_pushstring(L,current_mod.versions[i].file.c_str());
360 lua_settable(L,current_element);
362 lua_settable(L,versionstop);
364 lua_settable(L, top);
366 lua_pushstring(L,"screenshot_url");
367 lua_pushstring(L,current_mod.titlepic.file.c_str());
368 lua_settable(L, top);
370 lua_pushstring(L,"license");
371 lua_pushstring(L,current_mod.license.shortinfo.c_str());
372 lua_settable(L, top);
374 lua_pushstring(L,"rating");
375 lua_pushnumber(L,current_mod.rating);
376 lua_settable(L, top);
387 /******************************************************************************/
388 int ModApiMainMenu::l_get_modstore_list(lua_State *L)
390 std::string listtype = "local";
392 if (!lua_isnone(L,1)) {
393 listtype = luaL_checkstring(L,1);
396 std::string url = "";
398 url = g_settings->get("modstore_listmods_url");
400 catch(SettingNotFoundException &e) {
405 mods = getModstoreUrl(url);
407 std::vector<ModStoreMod> moddata = readModStoreList(mods);
410 int top = lua_gettop(L);
411 unsigned int index = 1;
413 for (unsigned int i = 0; i < moddata.size(); i++)
415 if (moddata[i].valid) {
416 lua_pushnumber(L,index);
419 int top_lvl2 = lua_gettop(L);
421 lua_pushstring(L,"id");
422 lua_pushnumber(L,moddata[i].id);
423 lua_settable(L, top_lvl2);
425 lua_pushstring(L,"title");
426 lua_pushstring(L,moddata[i].title.c_str());
427 lua_settable(L, top_lvl2);
429 lua_pushstring(L,"basename");
430 lua_pushstring(L,moddata[i].basename.c_str());
431 lua_settable(L, top_lvl2);
433 lua_settable(L, top);
440 /******************************************************************************/
441 int ModApiMainMenu::l_get_favorites(lua_State *L)
443 std::string listtype = "local";
445 if (!lua_isnone(L,1)) {
446 listtype = luaL_checkstring(L,1);
449 std::vector<ServerListSpec> servers;
451 if(listtype == "online") {
452 servers = ServerList::getOnline();
454 servers = ServerList::getLocal();
457 servers = ServerList::getLocal();
461 int top = lua_gettop(L);
462 unsigned int index = 1;
464 for (unsigned int i = 0; i < servers.size(); i++)
466 lua_pushnumber(L,index);
469 int top_lvl2 = lua_gettop(L);
471 if (servers[i]["clients"].asString().size()) {
472 std::string clients_raw = servers[i]["clients"].asString();
474 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
476 if ((clients_raw != "") && (*endptr == 0)) {
477 lua_pushstring(L,"clients");
478 lua_pushnumber(L,numbervalue);
479 lua_settable(L, top_lvl2);
483 if (servers[i]["clients_max"].asString().size()) {
485 std::string clients_max_raw = servers[i]["clients_max"].asString();
487 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
489 if ((clients_max_raw != "") && (*endptr == 0)) {
490 lua_pushstring(L,"clients_max");
491 lua_pushnumber(L,numbervalue);
492 lua_settable(L, top_lvl2);
496 if (servers[i]["version"].asString().size()) {
497 lua_pushstring(L,"version");
498 std::string topush = servers[i]["version"].asString();
499 lua_pushstring(L,topush.c_str());
500 lua_settable(L, top_lvl2);
503 if (servers[i]["password"].asString().size()) {
504 lua_pushstring(L,"password");
505 lua_pushboolean(L,true);
506 lua_settable(L, top_lvl2);
509 if (servers[i]["creative"].asString().size()) {
510 lua_pushstring(L,"creative");
511 lua_pushboolean(L,true);
512 lua_settable(L, top_lvl2);
515 if (servers[i]["damage"].asString().size()) {
516 lua_pushstring(L,"damage");
517 lua_pushboolean(L,true);
518 lua_settable(L, top_lvl2);
521 if (servers[i]["pvp"].asString().size()) {
522 lua_pushstring(L,"pvp");
523 lua_pushboolean(L,true);
524 lua_settable(L, top_lvl2);
527 if (servers[i]["description"].asString().size()) {
528 lua_pushstring(L,"description");
529 std::string topush = servers[i]["description"].asString();
530 lua_pushstring(L,topush.c_str());
531 lua_settable(L, top_lvl2);
534 if (servers[i]["name"].asString().size()) {
535 lua_pushstring(L,"name");
536 std::string topush = servers[i]["name"].asString();
537 lua_pushstring(L,topush.c_str());
538 lua_settable(L, top_lvl2);
541 if (servers[i]["address"].asString().size()) {
542 lua_pushstring(L,"address");
543 std::string topush = servers[i]["address"].asString();
544 lua_pushstring(L,topush.c_str());
545 lua_settable(L, top_lvl2);
548 if (servers[i]["port"].asString().size()) {
549 lua_pushstring(L,"port");
550 std::string topush = servers[i]["port"].asString();
551 lua_pushstring(L,topush.c_str());
552 lua_settable(L, top_lvl2);
555 lua_settable(L, top);
561 /******************************************************************************/
562 int ModApiMainMenu::l_delete_favorite(lua_State *L)
564 std::vector<ServerListSpec> servers;
566 std::string listtype = "local";
568 if (!lua_isnone(L,2)) {
569 listtype = luaL_checkstring(L,2);
572 if ((listtype != "local") &&
573 (listtype != "online"))
577 if(listtype == "online") {
578 servers = ServerList::getOnline();
580 servers = ServerList::getLocal();
583 servers = ServerList::getLocal();
586 int fav_idx = luaL_checkinteger(L,1) -1;
588 if ((fav_idx >= 0) &&
589 (fav_idx < (int) servers.size())) {
591 ServerList::deleteEntry(servers[fav_idx]);
597 /******************************************************************************/
598 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
600 GUIEngine* engine = getGuiEngine(L);
603 GUIKeyChangeMenu *kmenu
604 = new GUIKeyChangeMenu( engine->m_device->getGUIEnvironment(),
607 engine->m_menumanager);
612 /******************************************************************************/
613 int ModApiMainMenu::l_create_world(lua_State *L)
615 const char *name = luaL_checkstring(L, 1);
616 int gameidx = luaL_checkinteger(L,2) -1;
618 std::string path = porting::path_user + DIR_DELIM
622 std::vector<SubgameSpec> games = getAvailableGames();
624 if ((gameidx >= 0) &&
625 (gameidx < (int) games.size())) {
627 // Create world if it doesn't exist
628 if(!initializeWorld(path, games[gameidx].id)){
629 lua_pushstring(L, "Failed to initialize world");
637 lua_pushstring(L, "Invalid game index");
642 /******************************************************************************/
643 int ModApiMainMenu::l_delete_world(lua_State *L)
645 int worldidx = luaL_checkinteger(L,1) -1;
647 std::vector<WorldSpec> worlds = getAvailableWorlds();
649 if ((worldidx >= 0) &&
650 (worldidx < (int) worlds.size())) {
652 WorldSpec spec = worlds[worldidx];
654 std::vector<std::string> paths;
655 paths.push_back(spec.path);
656 fs::GetRecursiveSubPaths(spec.path, paths);
659 if (!fs::DeletePaths(paths)) {
660 lua_pushstring(L, "Failed to delete world");
667 lua_pushstring(L, "Invalid world index");
672 /******************************************************************************/
673 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
675 GUIEngine* engine = getGuiEngine(L);
678 std::string text = "";
680 if (!lua_isnone(L,1) && !lua_isnil(L,1))
681 text = luaL_checkstring(L, 1);
683 engine->setTopleftText(text);
687 /******************************************************************************/
688 int ModApiMainMenu::l_get_modpath(lua_State *L)
691 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
692 lua_pushstring(L, modpath.c_str());
696 /******************************************************************************/
697 int ModApiMainMenu::l_get_gamepath(lua_State *L)
700 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
701 lua_pushstring(L, gamepath.c_str());
705 /******************************************************************************/
706 int ModApiMainMenu::l_get_texturepath(lua_State *L)
709 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures");
710 lua_pushstring(L, gamepath.c_str());
714 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
717 = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "textures");
718 lua_pushstring(L, gamepath.c_str());
722 /******************************************************************************/
723 int ModApiMainMenu::l_get_dirlist(lua_State *L)
725 const char *path = luaL_checkstring(L, 1);
726 bool dironly = lua_toboolean(L, 2);
728 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
730 unsigned int index = 1;
732 int table = lua_gettop(L);
734 for (unsigned int i=0;i< dirlist.size(); i++) {
735 if ((dirlist[i].dir) || (dironly == false)) {
736 lua_pushnumber(L,index);
737 lua_pushstring(L,dirlist[i].name.c_str());
738 lua_settable(L, table);
746 /******************************************************************************/
747 int ModApiMainMenu::l_create_dir(lua_State *L) {
748 const char *path = luaL_checkstring(L, 1);
750 if (ModApiMainMenu::isMinetestPath(path)) {
751 lua_pushboolean(L,fs::CreateAllDirs(path));
754 lua_pushboolean(L,false);
758 /******************************************************************************/
759 int ModApiMainMenu::l_delete_dir(lua_State *L)
761 const char *path = luaL_checkstring(L, 1);
763 std::string absolute_path = fs::RemoveRelativePathComponents(path);
765 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
766 lua_pushboolean(L,fs::RecursiveDelete(absolute_path));
769 lua_pushboolean(L,false);
773 /******************************************************************************/
774 int ModApiMainMenu::l_copy_dir(lua_State *L)
776 const char *source = luaL_checkstring(L, 1);
777 const char *destination = luaL_checkstring(L, 2);
779 bool keep_source = true;
781 if ((!lua_isnone(L,3)) &&
783 keep_source = lua_toboolean(L,3);
786 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
787 std::string absolute_source = fs::RemoveRelativePathComponents(source);
789 if ((ModApiMainMenu::isMinetestPath(absolute_source)) &&
790 (ModApiMainMenu::isMinetestPath(absolute_destination))) {
791 bool retval = fs::CopyDir(absolute_source,absolute_destination);
793 if (retval && (!keep_source)) {
795 retval &= fs::RecursiveDelete(absolute_source);
797 lua_pushboolean(L,retval);
800 lua_pushboolean(L,false);
804 /******************************************************************************/
805 int ModApiMainMenu::l_extract_zip(lua_State *L)
807 GUIEngine* engine = getGuiEngine(L);
810 const char *zipfile = luaL_checkstring(L, 1);
811 const char *destination = luaL_checkstring(L, 2);
813 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
815 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
816 fs::CreateAllDirs(absolute_destination);
818 io::IFileSystem* fs = engine->m_device->getFileSystem();
820 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
821 lua_pushboolean(L,false);
825 assert(fs->getFileArchiveCount() > 0);
827 /**********************************************************************/
828 /* WARNING this is not threadsafe!! */
829 /**********************************************************************/
830 io::IFileArchive* opened_zip =
831 fs->getFileArchive(fs->getFileArchiveCount()-1);
833 const io::IFileList* files_in_zip = opened_zip->getFileList();
835 unsigned int number_of_files = files_in_zip->getFileCount();
837 for (unsigned int i=0; i < number_of_files; i++) {
838 std::string fullpath = destination;
839 fullpath += DIR_DELIM;
840 fullpath += files_in_zip->getFullFileName(i).c_str();
842 if (files_in_zip->isDirectory(i)) {
843 if (! fs::CreateAllDirs(fullpath) ) {
844 fs->removeFileArchive(fs->getFileArchiveCount()-1);
845 lua_pushboolean(L,false);
850 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
852 FILE *targetfile = fopen(fullpath.c_str(),"wb");
854 if (targetfile == NULL) {
855 fs->removeFileArchive(fs->getFileArchiveCount()-1);
856 lua_pushboolean(L,false);
860 char read_buffer[1024];
861 unsigned int total_read = 0;
863 while (total_read < toread->getSize()) {
865 unsigned int bytes_read =
866 toread->read(read_buffer,sizeof(read_buffer));
867 if ((bytes_read == 0 ) ||
868 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
871 fs->removeFileArchive(fs->getFileArchiveCount()-1);
872 lua_pushboolean(L,false);
875 total_read += bytes_read;
883 fs->removeFileArchive(fs->getFileArchiveCount()-1);
884 lua_pushboolean(L,true);
888 lua_pushboolean(L,false);
892 /******************************************************************************/
893 int ModApiMainMenu::l_get_scriptdir(lua_State *L)
895 GUIEngine* engine = getGuiEngine(L);
898 lua_pushstring(L,engine->getScriptDir().c_str());
902 /******************************************************************************/
903 bool ModApiMainMenu::isMinetestPath(std::string path)
905 if (fs::PathStartsWith(path,fs::TempPath()))
909 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
913 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
917 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
924 /******************************************************************************/
925 int ModApiMainMenu::l_show_file_open_dialog(lua_State *L)
927 GUIEngine* engine = getGuiEngine(L);
930 const char *formname= luaL_checkstring(L, 1);
931 const char *title = luaL_checkstring(L, 2);
933 GUIFileSelectMenu* fileOpenMenu =
934 new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(),
937 engine->m_menumanager,
940 fileOpenMenu->setTextDest(engine->m_buttonhandler);
941 fileOpenMenu->drop();
945 /******************************************************************************/
946 int ModApiMainMenu::l_get_version(lua_State *L)
948 lua_pushstring(L, minetest_version_simple);
952 /******************************************************************************/
953 int ModApiMainMenu::l_sound_play(lua_State *L)
955 GUIEngine* engine = getGuiEngine(L);
957 SimpleSoundSpec spec;
958 read_soundspec(L, 1, spec);
959 bool looped = lua_toboolean(L, 2);
961 u32 handle = engine->playSound(spec, looped);
963 lua_pushinteger(L, handle);
968 /******************************************************************************/
969 int ModApiMainMenu::l_sound_stop(lua_State *L)
971 GUIEngine* engine = getGuiEngine(L);
973 u32 handle = luaL_checkinteger(L, 1);
974 engine->stopSound(handle);
979 /******************************************************************************/
980 int ModApiMainMenu::l_download_file(lua_State *L)
982 const char *url = luaL_checkstring(L, 1);
983 const char *target = luaL_checkstring(L, 2);
986 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
988 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
989 if (GUIEngine::downloadFile(url,absolute_destination)) {
990 lua_pushboolean(L,true);
994 lua_pushboolean(L,false);
998 /******************************************************************************/
999 int ModApiMainMenu::l_gettext(lua_State *L)
1001 std::wstring wtext = wstrgettext((std::string) luaL_checkstring(L, 1));
1002 lua_pushstring(L, wide_to_narrow(wtext).c_str());
1007 /******************************************************************************/
1008 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1010 GUIEngine* engine = getGuiEngine(L);
1012 const char* serialized_fct_raw = luaL_checkstring(L, 1);
1013 unsigned int lenght_fct = luaL_checkint(L, 2);
1015 const char* serialized_params_raw = luaL_checkstring(L, 3);
1016 unsigned int lenght_params = luaL_checkint(L, 4);
1018 assert(serialized_fct_raw != 0);
1019 assert(serialized_params_raw != 0);
1021 std::string serialized_fct = std::string(serialized_fct_raw,lenght_fct);
1022 std::string serialized_params = std::string(serialized_params_raw,lenght_params);
1024 lua_pushinteger(L,engine->DoAsync(serialized_fct,serialized_params));
1029 /******************************************************************************/
1030 void ModApiMainMenu::Initialize(lua_State *L, int top)
1032 API_FCT(update_formspec);
1033 API_FCT(set_clouds);
1034 API_FCT(get_textlist_index);
1035 API_FCT(get_table_index);
1036 API_FCT(get_worlds);
1040 API_FCT(get_favorites);
1041 API_FCT(show_keys_menu);
1042 API_FCT(create_world);
1043 API_FCT(delete_world);
1044 API_FCT(delete_favorite);
1045 API_FCT(set_background);
1046 API_FCT(set_topleft_text);
1047 API_FCT(get_modpath);
1048 API_FCT(get_gamepath);
1049 API_FCT(get_texturepath);
1050 API_FCT(get_texturepath_share);
1051 API_FCT(get_dirlist);
1052 API_FCT(create_dir);
1053 API_FCT(delete_dir);
1055 API_FCT(extract_zip);
1056 API_FCT(get_scriptdir);
1057 API_FCT(show_file_open_dialog);
1058 API_FCT(get_version);
1059 API_FCT(download_file);
1060 API_FCT(get_modstore_details);
1061 API_FCT(get_modstore_list);
1062 API_FCT(sound_play);
1063 API_FCT(sound_stop);
1065 API_FCT(do_async_callback);
1068 /******************************************************************************/
1069 void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
1072 ASYNC_API_FCT(get_worlds);
1073 ASYNC_API_FCT(get_games);
1074 ASYNC_API_FCT(get_favorites);
1075 ASYNC_API_FCT(get_modpath);
1076 ASYNC_API_FCT(get_gamepath);
1077 ASYNC_API_FCT(get_texturepath);
1078 ASYNC_API_FCT(get_texturepath_share);
1079 ASYNC_API_FCT(get_dirlist);
1080 ASYNC_API_FCT(create_dir);
1081 ASYNC_API_FCT(delete_dir);
1082 ASYNC_API_FCT(copy_dir);
1083 //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1084 ASYNC_API_FCT(get_version);
1085 ASYNC_API_FCT(download_file);
1086 ASYNC_API_FCT(get_modstore_details);
1087 ASYNC_API_FCT(get_modstore_list);
1088 //ASYNC_API_FCT(gettext); (gettext lib isn't threadsafe)