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"
37 #include "main.h" // for g_settings
39 #include "EDriverTypes.h"
41 #include <IFileArchive.h>
42 #include <IFileSystem.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 lua_toboolean(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
119 engine->m_data->selected_world = getIntegerData(L, "selected_world",valid) -1;
120 engine->m_data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
121 engine->m_data->name = getTextData(L,"playername");
122 engine->m_data->password = getTextData(L,"password");
123 engine->m_data->address = getTextData(L,"address");
124 engine->m_data->port = getTextData(L,"port");
125 engine->m_data->serverdescription = getTextData(L,"serverdescription");
126 engine->m_data->servername = getTextData(L,"servername");
128 //close menu next time
129 engine->m_startgame = true;
133 /******************************************************************************/
134 int ModApiMainMenu::l_close(lua_State *L)
136 GUIEngine* engine = getGuiEngine(L);
137 sanity_check(engine != NULL);
139 engine->m_kill = true;
143 /******************************************************************************/
144 int ModApiMainMenu::l_set_background(lua_State *L)
146 GUIEngine* engine = getGuiEngine(L);
147 sanity_check(engine != NULL);
149 std::string backgroundlevel(luaL_checkstring(L, 1));
150 std::string texturename(luaL_checkstring(L, 2));
152 bool tile_image = false;
154 unsigned int minsize = 16;
156 if (!lua_isnone(L, 3)) {
157 tile_image = lua_toboolean(L, 3);
160 if (!lua_isnone(L, 4)) {
161 minsize = lua_tonumber(L, 4);
164 if (backgroundlevel == "background") {
165 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
166 tile_image, minsize);
169 if (backgroundlevel == "overlay") {
170 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
171 tile_image, minsize);
174 if (backgroundlevel == "header") {
175 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
176 tile_image, minsize);
179 if (backgroundlevel == "footer") {
180 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
181 tile_image, minsize);
184 lua_pushboolean(L,retval);
188 /******************************************************************************/
189 int ModApiMainMenu::l_set_clouds(lua_State *L)
191 GUIEngine* engine = getGuiEngine(L);
192 sanity_check(engine != NULL);
194 bool value = lua_toboolean(L,1);
196 engine->m_clouds_enabled = value;
201 /******************************************************************************/
202 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
204 // get_table_index accepts both tables and textlists
205 return l_get_table_index(L);
208 /******************************************************************************/
209 int ModApiMainMenu::l_get_table_index(lua_State *L)
211 GUIEngine* engine = getGuiEngine(L);
212 sanity_check(engine != NULL);
214 std::wstring tablename(narrow_to_wide(luaL_checkstring(L, 1)));
215 GUITable *table = engine->m_menu->getTable(tablename);
216 s32 selection = table ? table->getSelected() : 0;
219 lua_pushinteger(L, selection);
225 /******************************************************************************/
226 int ModApiMainMenu::l_get_worlds(lua_State *L)
228 std::vector<WorldSpec> worlds = getAvailableWorlds();
231 int top = lua_gettop(L);
232 unsigned int index = 1;
234 for (unsigned int i = 0; i < worlds.size(); i++)
236 lua_pushnumber(L,index);
239 int top_lvl2 = lua_gettop(L);
241 lua_pushstring(L,"path");
242 lua_pushstring(L,worlds[i].path.c_str());
243 lua_settable(L, top_lvl2);
245 lua_pushstring(L,"name");
246 lua_pushstring(L,worlds[i].name.c_str());
247 lua_settable(L, top_lvl2);
249 lua_pushstring(L,"gameid");
250 lua_pushstring(L,worlds[i].gameid.c_str());
251 lua_settable(L, top_lvl2);
253 lua_settable(L, top);
259 /******************************************************************************/
260 int ModApiMainMenu::l_get_games(lua_State *L)
262 std::vector<SubgameSpec> games = getAvailableGames();
265 int top = lua_gettop(L);
266 unsigned int index = 1;
268 for (unsigned int i = 0; i < games.size(); i++)
270 lua_pushnumber(L,index);
272 int top_lvl2 = lua_gettop(L);
274 lua_pushstring(L,"id");
275 lua_pushstring(L,games[i].id.c_str());
276 lua_settable(L, top_lvl2);
278 lua_pushstring(L,"path");
279 lua_pushstring(L,games[i].path.c_str());
280 lua_settable(L, top_lvl2);
282 lua_pushstring(L,"gamemods_path");
283 lua_pushstring(L,games[i].gamemods_path.c_str());
284 lua_settable(L, top_lvl2);
286 lua_pushstring(L,"name");
287 lua_pushstring(L,games[i].name.c_str());
288 lua_settable(L, top_lvl2);
290 lua_pushstring(L,"menuicon_path");
291 lua_pushstring(L,games[i].menuicon_path.c_str());
292 lua_settable(L, top_lvl2);
294 lua_pushstring(L,"addon_mods_paths");
296 int table2 = lua_gettop(L);
297 int internal_index=1;
298 for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
299 iter != games[i].addon_mods_paths.end(); iter++) {
300 lua_pushnumber(L,internal_index);
301 lua_pushstring(L,(*iter).c_str());
302 lua_settable(L, table2);
305 lua_settable(L, top_lvl2);
306 lua_settable(L, top);
311 /******************************************************************************/
312 int ModApiMainMenu::l_get_modstore_details(lua_State *L)
314 const char *modid = luaL_checkstring(L, 1);
318 std::string url = "";
320 url = g_settings->get("modstore_details_url");
322 catch(SettingNotFoundException &e) {
327 size_t idpos = url.find("*");
329 url.insert(idpos,modid);
331 details = getModstoreUrl(url);
333 ModStoreModDetails current_mod = readModStoreModDetails(details);
335 if ( current_mod.valid) {
337 int top = lua_gettop(L);
339 lua_pushstring(L,"id");
340 lua_pushnumber(L,current_mod.id);
341 lua_settable(L, top);
343 lua_pushstring(L,"title");
344 lua_pushstring(L,current_mod.title.c_str());
345 lua_settable(L, top);
347 lua_pushstring(L,"basename");
348 lua_pushstring(L,current_mod.basename.c_str());
349 lua_settable(L, top);
351 lua_pushstring(L,"description");
352 lua_pushstring(L,current_mod.description.c_str());
353 lua_settable(L, top);
355 lua_pushstring(L,"author");
356 lua_pushstring(L,current_mod.author.username.c_str());
357 lua_settable(L, top);
359 lua_pushstring(L,"download_url");
360 lua_pushstring(L,current_mod.versions[0].file.c_str());
361 lua_settable(L, top);
363 lua_pushstring(L,"versions");
365 int versionstop = lua_gettop(L);
366 for (unsigned int i=0;i < current_mod.versions.size(); i++) {
367 lua_pushnumber(L,i+1);
369 int current_element = lua_gettop(L);
371 lua_pushstring(L,"date");
372 lua_pushstring(L,current_mod.versions[i].date.c_str());
373 lua_settable(L,current_element);
375 lua_pushstring(L,"download_url");
376 lua_pushstring(L,current_mod.versions[i].file.c_str());
377 lua_settable(L,current_element);
379 lua_settable(L,versionstop);
381 lua_settable(L, top);
383 lua_pushstring(L,"screenshot_url");
384 lua_pushstring(L,current_mod.titlepic.file.c_str());
385 lua_settable(L, top);
387 lua_pushstring(L,"license");
388 lua_pushstring(L,current_mod.license.shortinfo.c_str());
389 lua_settable(L, top);
391 lua_pushstring(L,"rating");
392 lua_pushnumber(L,current_mod.rating);
393 lua_settable(L, top);
404 /******************************************************************************/
405 int ModApiMainMenu::l_get_modstore_list(lua_State *L)
408 std::string url = "";
410 url = g_settings->get("modstore_listmods_url");
412 catch(SettingNotFoundException &e) {
417 mods = getModstoreUrl(url);
419 std::vector<ModStoreMod> moddata = readModStoreList(mods);
422 int top = lua_gettop(L);
423 unsigned int index = 1;
425 for (unsigned int i = 0; i < moddata.size(); i++)
427 if (moddata[i].valid) {
428 lua_pushnumber(L,index);
431 int top_lvl2 = lua_gettop(L);
433 lua_pushstring(L,"id");
434 lua_pushnumber(L,moddata[i].id);
435 lua_settable(L, top_lvl2);
437 lua_pushstring(L,"title");
438 lua_pushstring(L,moddata[i].title.c_str());
439 lua_settable(L, top_lvl2);
441 lua_pushstring(L,"basename");
442 lua_pushstring(L,moddata[i].basename.c_str());
443 lua_settable(L, top_lvl2);
445 lua_settable(L, top);
452 /******************************************************************************/
453 int ModApiMainMenu::l_get_favorites(lua_State *L)
455 std::string listtype = "local";
457 if (!lua_isnone(L,1)) {
458 listtype = luaL_checkstring(L,1);
461 std::vector<ServerListSpec> servers;
463 if(listtype == "online") {
464 servers = ServerList::getOnline();
466 servers = ServerList::getLocal();
470 int top = lua_gettop(L);
471 unsigned int index = 1;
473 for (unsigned int i = 0; i < servers.size(); i++)
476 lua_pushnumber(L,index);
479 int top_lvl2 = lua_gettop(L);
481 if (servers[i]["clients"].asString().size()) {
482 std::string clients_raw = servers[i]["clients"].asString();
484 int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
486 if ((clients_raw != "") && (*endptr == 0)) {
487 lua_pushstring(L,"clients");
488 lua_pushnumber(L,numbervalue);
489 lua_settable(L, top_lvl2);
493 if (servers[i]["clients_max"].asString().size()) {
495 std::string clients_max_raw = servers[i]["clients_max"].asString();
497 int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
499 if ((clients_max_raw != "") && (*endptr == 0)) {
500 lua_pushstring(L,"clients_max");
501 lua_pushnumber(L,numbervalue);
502 lua_settable(L, top_lvl2);
506 if (servers[i]["version"].asString().size()) {
507 lua_pushstring(L,"version");
508 std::string topush = servers[i]["version"].asString();
509 lua_pushstring(L,topush.c_str());
510 lua_settable(L, top_lvl2);
513 if (servers[i]["proto_min"].asString().size()) {
514 lua_pushstring(L,"proto_min");
515 lua_pushinteger(L,servers[i]["proto_min"].asInt());
516 lua_settable(L, top_lvl2);
519 if (servers[i]["proto_max"].asString().size()) {
520 lua_pushstring(L,"proto_max");
521 lua_pushinteger(L,servers[i]["proto_max"].asInt());
522 lua_settable(L, top_lvl2);
525 if (servers[i]["password"].asString().size()) {
526 lua_pushstring(L,"password");
527 lua_pushboolean(L,servers[i]["password"].asBool());
528 lua_settable(L, top_lvl2);
531 if (servers[i]["creative"].asString().size()) {
532 lua_pushstring(L,"creative");
533 lua_pushboolean(L,servers[i]["creative"].asBool());
534 lua_settable(L, top_lvl2);
537 if (servers[i]["damage"].asString().size()) {
538 lua_pushstring(L,"damage");
539 lua_pushboolean(L,servers[i]["damage"].asBool());
540 lua_settable(L, top_lvl2);
543 if (servers[i]["pvp"].asString().size()) {
544 lua_pushstring(L,"pvp");
545 lua_pushboolean(L,servers[i]["pvp"].asBool());
546 lua_settable(L, top_lvl2);
549 if (servers[i]["description"].asString().size()) {
550 lua_pushstring(L,"description");
551 std::string topush = servers[i]["description"].asString();
552 lua_pushstring(L,topush.c_str());
553 lua_settable(L, top_lvl2);
556 if (servers[i]["name"].asString().size()) {
557 lua_pushstring(L,"name");
558 std::string topush = servers[i]["name"].asString();
559 lua_pushstring(L,topush.c_str());
560 lua_settable(L, top_lvl2);
563 if (servers[i]["address"].asString().size()) {
564 lua_pushstring(L,"address");
565 std::string topush = servers[i]["address"].asString();
566 lua_pushstring(L,topush.c_str());
567 lua_settable(L, top_lvl2);
570 if (servers[i]["port"].asString().size()) {
571 lua_pushstring(L,"port");
572 std::string topush = servers[i]["port"].asString();
573 lua_pushstring(L,topush.c_str());
574 lua_settable(L, top_lvl2);
577 lua_settable(L, top);
583 /******************************************************************************/
584 int ModApiMainMenu::l_delete_favorite(lua_State *L)
586 std::vector<ServerListSpec> servers;
588 std::string listtype = "local";
590 if (!lua_isnone(L,2)) {
591 listtype = luaL_checkstring(L,2);
594 if ((listtype != "local") &&
595 (listtype != "online"))
599 if(listtype == "online") {
600 servers = ServerList::getOnline();
602 servers = ServerList::getLocal();
605 int fav_idx = luaL_checkinteger(L,1) -1;
607 if ((fav_idx >= 0) &&
608 (fav_idx < (int) servers.size())) {
610 ServerList::deleteEntry(servers[fav_idx]);
616 /******************************************************************************/
617 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
619 GUIEngine* engine = getGuiEngine(L);
620 sanity_check(engine != NULL);
622 GUIKeyChangeMenu *kmenu
623 = new GUIKeyChangeMenu( engine->m_device->getGUIEnvironment(),
626 engine->m_menumanager);
631 /******************************************************************************/
632 int ModApiMainMenu::l_create_world(lua_State *L)
634 const char *name = luaL_checkstring(L, 1);
635 int gameidx = luaL_checkinteger(L,2) -1;
637 std::string path = porting::path_user + DIR_DELIM
641 std::vector<SubgameSpec> games = getAvailableGames();
643 if ((gameidx >= 0) &&
644 (gameidx < (int) games.size())) {
646 // Create world if it doesn't exist
647 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
648 lua_pushstring(L, "Failed to initialize world");
653 lua_pushstring(L, "Invalid game index");
658 /******************************************************************************/
659 int ModApiMainMenu::l_delete_world(lua_State *L)
661 int worldidx = luaL_checkinteger(L,1) -1;
663 std::vector<WorldSpec> worlds = getAvailableWorlds();
665 if ((worldidx >= 0) &&
666 (worldidx < (int) worlds.size())) {
668 WorldSpec spec = worlds[worldidx];
670 std::vector<std::string> paths;
671 paths.push_back(spec.path);
672 fs::GetRecursiveSubPaths(spec.path, paths);
675 if (!fs::DeletePaths(paths)) {
676 lua_pushstring(L, "Failed to delete world");
683 lua_pushstring(L, "Invalid world index");
688 /******************************************************************************/
689 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
691 GUIEngine* engine = getGuiEngine(L);
692 sanity_check(engine != NULL);
694 std::string text = "";
696 if (!lua_isnone(L,1) && !lua_isnil(L,1))
697 text = luaL_checkstring(L, 1);
699 engine->setTopleftText(text);
703 /******************************************************************************/
704 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
708 std::list<const char *> names;
709 EmergeManager::getMapgenNames(names);
712 for (std::list<const char *>::const_iterator
713 it = names.begin(); it != names.end(); ++it) {
714 lua_pushstring(L, *it);
715 lua_rawseti(L, -2, i++);
722 /******************************************************************************/
723 int ModApiMainMenu::l_get_modpath(lua_State *L)
726 = fs::RemoveRelativePathComponents(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)
735 = fs::RemoveRelativePathComponents(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)
744 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures");
745 lua_pushstring(L, gamepath.c_str());
749 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
752 = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "textures");
753 lua_pushstring(L, gamepath.c_str());
757 /******************************************************************************/
758 int ModApiMainMenu::l_get_dirlist(lua_State *L)
760 const char *path = luaL_checkstring(L, 1);
761 bool dironly = lua_toboolean(L, 2);
763 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
765 unsigned int index = 1;
767 int table = lua_gettop(L);
769 for (unsigned int i=0;i< dirlist.size(); i++) {
770 if ((dirlist[i].dir) || (dironly == false)) {
771 lua_pushnumber(L,index);
772 lua_pushstring(L,dirlist[i].name.c_str());
773 lua_settable(L, table);
781 /******************************************************************************/
782 int ModApiMainMenu::l_create_dir(lua_State *L) {
783 const char *path = luaL_checkstring(L, 1);
785 if (ModApiMainMenu::isMinetestPath(path)) {
786 lua_pushboolean(L,fs::CreateAllDirs(path));
789 lua_pushboolean(L,false);
793 /******************************************************************************/
794 int ModApiMainMenu::l_delete_dir(lua_State *L)
796 const char *path = luaL_checkstring(L, 1);
798 std::string absolute_path = fs::RemoveRelativePathComponents(path);
800 if (ModApiMainMenu::isMinetestPath(absolute_path)) {
801 lua_pushboolean(L,fs::RecursiveDelete(absolute_path));
804 lua_pushboolean(L,false);
808 /******************************************************************************/
809 int ModApiMainMenu::l_copy_dir(lua_State *L)
811 const char *source = luaL_checkstring(L, 1);
812 const char *destination = luaL_checkstring(L, 2);
814 bool keep_source = true;
816 if ((!lua_isnone(L,3)) &&
818 keep_source = lua_toboolean(L,3);
821 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
822 std::string absolute_source = fs::RemoveRelativePathComponents(source);
824 if ((ModApiMainMenu::isMinetestPath(absolute_source)) &&
825 (ModApiMainMenu::isMinetestPath(absolute_destination))) {
826 bool retval = fs::CopyDir(absolute_source,absolute_destination);
828 if (retval && (!keep_source)) {
830 retval &= fs::RecursiveDelete(absolute_source);
832 lua_pushboolean(L,retval);
835 lua_pushboolean(L,false);
839 /******************************************************************************/
840 int ModApiMainMenu::l_extract_zip(lua_State *L)
842 GUIEngine* engine = getGuiEngine(L);
843 sanity_check(engine != NULL);(engine != 0);
845 const char *zipfile = luaL_checkstring(L, 1);
846 const char *destination = luaL_checkstring(L, 2);
848 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
850 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
851 fs::CreateAllDirs(absolute_destination);
853 io::IFileSystem* fs = engine->m_device->getFileSystem();
855 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
856 lua_pushboolean(L,false);
860 sanity_check(fs->getFileArchiveCount() > 0);
862 /**********************************************************************/
863 /* WARNING this is not threadsafe!! */
864 /**********************************************************************/
865 io::IFileArchive* opened_zip =
866 fs->getFileArchive(fs->getFileArchiveCount()-1);
868 const io::IFileList* files_in_zip = opened_zip->getFileList();
870 unsigned int number_of_files = files_in_zip->getFileCount();
872 for (unsigned int i=0; i < number_of_files; i++) {
873 std::string fullpath = destination;
874 fullpath += DIR_DELIM;
875 fullpath += files_in_zip->getFullFileName(i).c_str();
876 std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
878 if (!files_in_zip->isDirectory(i)) {
879 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
880 fs->removeFileArchive(fs->getFileArchiveCount()-1);
881 lua_pushboolean(L,false);
885 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
887 FILE *targetfile = fopen(fullpath.c_str(),"wb");
889 if (targetfile == NULL) {
890 fs->removeFileArchive(fs->getFileArchiveCount()-1);
891 lua_pushboolean(L,false);
895 char read_buffer[1024];
898 while (total_read < toread->getSize()) {
900 unsigned int bytes_read =
901 toread->read(read_buffer,sizeof(read_buffer));
902 if ((bytes_read == 0 ) ||
903 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
906 fs->removeFileArchive(fs->getFileArchiveCount()-1);
907 lua_pushboolean(L,false);
910 total_read += bytes_read;
918 fs->removeFileArchive(fs->getFileArchiveCount()-1);
919 lua_pushboolean(L,true);
923 lua_pushboolean(L,false);
927 /******************************************************************************/
928 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
930 GUIEngine* engine = getGuiEngine(L);
931 sanity_check(engine != NULL);
933 lua_pushstring(L,engine->getScriptDir().c_str());
937 /******************************************************************************/
938 bool ModApiMainMenu::isMinetestPath(std::string path)
940 if (fs::PathStartsWith(path,fs::TempPath()))
944 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
948 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
952 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
959 /******************************************************************************/
960 int ModApiMainMenu::l_show_file_open_dialog(lua_State *L)
962 GUIEngine* engine = getGuiEngine(L);
963 sanity_check(engine != NULL);
965 const char *formname= luaL_checkstring(L, 1);
966 const char *title = luaL_checkstring(L, 2);
968 GUIFileSelectMenu* fileOpenMenu =
969 new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(),
972 engine->m_menumanager,
975 fileOpenMenu->setTextDest(engine->m_buttonhandler);
976 fileOpenMenu->drop();
980 /******************************************************************************/
981 int ModApiMainMenu::l_get_version(lua_State *L)
983 lua_pushstring(L, minetest_version_simple);
987 /******************************************************************************/
988 int ModApiMainMenu::l_sound_play(lua_State *L)
990 GUIEngine* engine = getGuiEngine(L);
992 SimpleSoundSpec spec;
993 read_soundspec(L, 1, spec);
994 bool looped = lua_toboolean(L, 2);
996 u32 handle = engine->playSound(spec, looped);
998 lua_pushinteger(L, handle);
1003 /******************************************************************************/
1004 int ModApiMainMenu::l_sound_stop(lua_State *L)
1006 GUIEngine* engine = getGuiEngine(L);
1008 u32 handle = luaL_checkinteger(L, 1);
1009 engine->stopSound(handle);
1014 /******************************************************************************/
1015 int ModApiMainMenu::l_download_file(lua_State *L)
1017 const char *url = luaL_checkstring(L, 1);
1018 const char *target = luaL_checkstring(L, 2);
1021 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
1023 if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
1024 if (GUIEngine::downloadFile(url,absolute_destination)) {
1025 lua_pushboolean(L,true);
1029 errorstream << "DOWNLOAD denied: " << absolute_destination
1030 << " isn't a allowed path" << std::endl;
1032 lua_pushboolean(L,false);
1036 /******************************************************************************/
1037 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
1039 std::vector<irr::video::E_DRIVER_TYPE> drivers
1040 = porting::getSupportedVideoDrivers();
1043 for (u32 i = 0; i != drivers.size(); i++) {
1044 const char *name = porting::getVideoDriverName(drivers[i]);
1045 const char *fname = porting::getVideoDriverFriendlyName(drivers[i]);
1048 lua_pushstring(L, name);
1049 lua_setfield(L, -2, "name");
1050 lua_pushstring(L, fname);
1051 lua_setfield(L, -2, "friendly_name");
1053 lua_rawseti(L, -2, i + 1);
1059 /******************************************************************************/
1060 int ModApiMainMenu::l_gettext(lua_State *L)
1062 std::wstring wtext = wstrgettext((std::string) luaL_checkstring(L, 1));
1063 lua_pushstring(L, wide_to_narrow(wtext).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(get_dirlist);
1153 API_FCT(create_dir);
1154 API_FCT(delete_dir);
1156 API_FCT(extract_zip);
1157 API_FCT(get_mainmenu_path);
1158 API_FCT(show_file_open_dialog);
1159 API_FCT(get_version);
1160 API_FCT(download_file);
1161 API_FCT(get_modstore_details);
1162 API_FCT(get_modstore_list);
1163 API_FCT(sound_play);
1164 API_FCT(sound_stop);
1166 API_FCT(get_video_drivers);
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(get_dirlist);
1186 ASYNC_API_FCT(create_dir);
1187 ASYNC_API_FCT(delete_dir);
1188 ASYNC_API_FCT(copy_dir);
1189 //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1190 ASYNC_API_FCT(get_version);
1191 ASYNC_API_FCT(download_file);
1192 ASYNC_API_FCT(get_modstore_details);
1193 ASYNC_API_FCT(get_modstore_list);
1194 //ASYNC_API_FCT(gettext); (gettext lib isn't threadsafe)