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.
26 #include "guiMainMenu.h"
28 #include "guiKeyChangeMenu.h"
29 #include "guiFileSelectMenu.h"
33 #include "convert_json.h"
36 #include "IFileArchive.h"
37 #include "IFileSystem.h"
39 #include "guiLuaApi.h"
40 #include "guiEngine.h"
42 #define API_FCT(name) registerFunction(L,#name,l_##name,top)
44 void guiLuaApi::initialize(lua_State* L,GUIEngine* engine)
46 lua_pushlightuserdata(L, engine);
47 lua_setfield(L, LUA_REGISTRYINDEX, "engine");
49 lua_pushstring(L, DIR_DELIM);
50 lua_setglobal(L, "DIR_DELIM");
53 lua_setglobal(L, "gamedata");
56 lua_setglobal(L, "engine");
58 lua_getglobal(L, "engine");
59 int top = lua_gettop(L);
64 retval &= API_FCT(update_formspec);
65 retval &= API_FCT(set_clouds);
66 retval &= API_FCT(get_textlist_index);
67 retval &= API_FCT(get_worlds);
68 retval &= API_FCT(get_games);
69 retval &= API_FCT(start);
70 retval &= API_FCT(close);
71 retval &= API_FCT(get_favorites);
72 retval &= API_FCT(show_keys_menu);
73 retval &= API_FCT(setting_set);
74 retval &= API_FCT(setting_get);
75 retval &= API_FCT(setting_getbool);
76 retval &= API_FCT(setting_setbool);
77 retval &= API_FCT(create_world);
78 retval &= API_FCT(delete_world);
79 retval &= API_FCT(delete_favorite);
80 retval &= API_FCT(set_background);
81 retval &= API_FCT(set_topleft_text);
82 retval &= API_FCT(get_modpath);
83 retval &= API_FCT(get_gamepath);
84 retval &= API_FCT(get_dirlist);
85 retval &= API_FCT(create_dir);
86 retval &= API_FCT(delete_dir);
87 retval &= API_FCT(copy_dir);
88 retval &= API_FCT(extract_zip);
89 retval &= API_FCT(get_scriptdir);
90 retval &= API_FCT(show_file_open_dialog);
91 retval &= API_FCT(get_version);
92 retval &= API_FCT(download_file);
93 retval &= API_FCT(get_modstore_details);
94 retval &= API_FCT(get_modstore_list);
101 /******************************************************************************/
102 bool guiLuaApi::registerFunction( lua_State* L,
108 lua_pushstring(L,name);
109 lua_pushcfunction(L,fct);
110 lua_settable(L, top);
115 /******************************************************************************/
116 GUIEngine* guiLuaApi::get_engine(lua_State *L)
118 // Get server from registry
119 lua_getfield(L, LUA_REGISTRYINDEX, "engine");
120 GUIEngine* sapi_ptr = (GUIEngine*) lua_touserdata(L, -1);
125 /******************************************************************************/
126 std::string guiLuaApi::getTextData(lua_State *L, std::string name)
128 lua_getglobal(L, "gamedata");
130 lua_getfield(L, -1, name.c_str());
135 return luaL_checkstring(L, -1);
138 /******************************************************************************/
139 int guiLuaApi::getIntegerData(lua_State *L, std::string name,bool& valid)
141 lua_getglobal(L, "gamedata");
143 lua_getfield(L, -1, name.c_str());
145 if(lua_isnil(L, -1)) {
151 return luaL_checkinteger(L, -1);
154 /******************************************************************************/
155 int guiLuaApi::getBoolData(lua_State *L, std::string name,bool& valid)
157 lua_getglobal(L, "gamedata");
159 lua_getfield(L, -1, name.c_str());
161 if(lua_isnil(L, -1)) {
167 return lua_toboolean(L, -1);
170 /******************************************************************************/
171 int guiLuaApi::l_update_formspec(lua_State *L)
173 GUIEngine* engine = get_engine(L);
176 if (engine->m_startgame)
180 std::string formspec(luaL_checkstring(L, 1));
182 if (engine->m_formspecgui != 0) {
183 engine->m_formspecgui->setForm(formspec);
189 /******************************************************************************/
190 int guiLuaApi::l_start(lua_State *L)
192 GUIEngine* engine = get_engine(L);
195 //update c++ gamedata from lua table
200 engine->m_data->selected_world = getIntegerData(L, "selected_world",valid) -1;
201 engine->m_data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
202 engine->m_data->name = getTextData(L,"playername");
203 engine->m_data->password = getTextData(L,"password");
204 engine->m_data->address = getTextData(L,"address");
205 engine->m_data->port = getTextData(L,"port");
206 engine->m_data->serverdescription = getTextData(L,"serverdescription");
207 engine->m_data->servername = getTextData(L,"servername");
209 //close menu next time
210 engine->m_startgame = true;
214 /******************************************************************************/
215 int guiLuaApi::l_close(lua_State *L)
217 GUIEngine* engine = get_engine(L);
220 engine->m_data->kill = true;
222 //close menu next time
223 engine->m_startgame = true;
224 engine->m_menu->quitMenu();
228 /******************************************************************************/
229 int guiLuaApi::l_set_background(lua_State *L)
231 GUIEngine* engine = get_engine(L);
234 std::string backgroundlevel(luaL_checkstring(L, 1));
235 std::string texturename(luaL_checkstring(L, 2));
239 if (backgroundlevel == "background") {
240 retval |= engine->setTexture(TEX_LAYER_BACKGROUND,texturename);
243 if (backgroundlevel == "overlay") {
244 retval |= engine->setTexture(TEX_LAYER_OVERLAY,texturename);
247 if (backgroundlevel == "header") {
248 retval |= engine->setTexture(TEX_LAYER_HEADER,texturename);
251 if (backgroundlevel == "footer") {
252 retval |= engine->setTexture(TEX_LAYER_FOOTER,texturename);
255 lua_pushboolean(L,retval);
259 /******************************************************************************/
260 int guiLuaApi::l_set_clouds(lua_State *L)
262 GUIEngine* engine = get_engine(L);
265 bool value = lua_toboolean(L,1);
267 engine->m_clouds_enabled = value;
272 /******************************************************************************/
273 int guiLuaApi::l_get_textlist_index(lua_State *L)
275 GUIEngine* engine = get_engine(L);
278 std::string listboxname(luaL_checkstring(L, 1));
280 int selection = engine->m_menu->getListboxIndex(listboxname);
285 lua_pushinteger(L, selection);
289 /******************************************************************************/
290 int guiLuaApi::l_get_worlds(lua_State *L)
292 GUIEngine* engine = get_engine(L);
295 std::vector<WorldSpec> worlds = getAvailableWorlds();
298 int top = lua_gettop(L);
299 unsigned int index = 1;
301 for (unsigned int i = 0; i < worlds.size(); i++)
303 lua_pushnumber(L,index);
306 int top_lvl2 = lua_gettop(L);
308 lua_pushstring(L,"path");
309 lua_pushstring(L,worlds[i].path.c_str());
310 lua_settable(L, top_lvl2);
312 lua_pushstring(L,"name");
313 lua_pushstring(L,worlds[i].name.c_str());
314 lua_settable(L, top_lvl2);
316 lua_pushstring(L,"gameid");
317 lua_pushstring(L,worlds[i].gameid.c_str());
318 lua_settable(L, top_lvl2);
320 lua_settable(L, top);
326 /******************************************************************************/
327 int guiLuaApi::l_get_games(lua_State *L)
329 GUIEngine* engine = get_engine(L);
332 std::vector<SubgameSpec> games = getAvailableGames();
335 int top = lua_gettop(L);
336 unsigned int index = 1;
338 for (unsigned int i = 0; i < games.size(); i++)
340 lua_pushnumber(L,index);
342 int top_lvl2 = lua_gettop(L);
344 lua_pushstring(L,"id");
345 lua_pushstring(L,games[i].id.c_str());
346 lua_settable(L, top_lvl2);
348 lua_pushstring(L,"path");
349 lua_pushstring(L,games[i].path.c_str());
350 lua_settable(L, top_lvl2);
352 lua_pushstring(L,"gamemods_path");
353 lua_pushstring(L,games[i].gamemods_path.c_str());
354 lua_settable(L, top_lvl2);
356 lua_pushstring(L,"name");
357 lua_pushstring(L,games[i].name.c_str());
358 lua_settable(L, top_lvl2);
360 lua_pushstring(L,"menuicon_path");
361 lua_pushstring(L,games[i].menuicon_path.c_str());
362 lua_settable(L, top_lvl2);
364 lua_pushstring(L,"addon_mods_paths");
366 int table2 = lua_gettop(L);
367 int internal_index=1;
368 for (std::set<std::string>::iterator iter = games[i].addon_mods_paths.begin();
369 iter != games[i].addon_mods_paths.end(); iter++) {
370 lua_pushnumber(L,internal_index);
371 lua_pushstring(L,(*iter).c_str());
372 lua_settable(L, table2);
375 lua_settable(L, top_lvl2);
376 lua_settable(L, top);
381 /******************************************************************************/
382 int guiLuaApi::l_get_modstore_details(lua_State *L)
384 const char *modid = luaL_checkstring(L, 1);
388 std::string url = "";
390 url = g_settings->get("modstore_details_url");
392 catch(SettingNotFoundException &e) {
397 size_t idpos = url.find("*");
399 url.insert(idpos,modid);
401 details = getModstoreUrl(url);
403 ModStoreModDetails current_mod = readModStoreModDetails(details);
405 if ( current_mod.valid) {
407 int top = lua_gettop(L);
409 lua_pushstring(L,"id");
410 lua_pushnumber(L,current_mod.id);
411 lua_settable(L, top);
413 lua_pushstring(L,"title");
414 lua_pushstring(L,current_mod.title.c_str());
415 lua_settable(L, top);
417 lua_pushstring(L,"basename");
418 lua_pushstring(L,current_mod.basename.c_str());
419 lua_settable(L, top);
421 lua_pushstring(L,"description");
422 lua_pushstring(L,current_mod.description.c_str());
423 lua_settable(L, top);
425 lua_pushstring(L,"author");
426 lua_pushstring(L,current_mod.author.username.c_str());
427 lua_settable(L, top);
429 lua_pushstring(L,"download_url");
430 lua_pushstring(L,current_mod.versions[0].file.c_str());
431 lua_settable(L, top);
433 lua_pushstring(L,"license");
434 lua_pushstring(L,current_mod.license.shortinfo.c_str());
435 lua_settable(L, top);
437 lua_pushstring(L,"rating");
438 lua_pushnumber(L,current_mod.rating);
439 lua_settable(L, top);
450 /******************************************************************************/
451 int guiLuaApi::l_get_modstore_list(lua_State *L)
453 GUIEngine* engine = get_engine(L);
456 std::string listtype = "local";
458 if (!lua_isnone(L,1)) {
459 listtype = luaL_checkstring(L,1);
462 std::string url = "";
464 url = g_settings->get("modstore_listmods_url");
466 catch(SettingNotFoundException &e) {
471 mods = getModstoreUrl(url);
473 std::vector<ModStoreMod> moddata = readModStoreList(mods);
476 int top = lua_gettop(L);
477 unsigned int index = 1;
479 for (unsigned int i = 0; i < moddata.size(); i++)
481 if (moddata[i].valid) {
482 lua_pushnumber(L,index);
485 int top_lvl2 = lua_gettop(L);
487 lua_pushstring(L,"id");
488 lua_pushnumber(L,moddata[i].id);
489 lua_settable(L, top_lvl2);
491 lua_pushstring(L,"title");
492 lua_pushstring(L,moddata[i].title.c_str());
493 lua_settable(L, top_lvl2);
495 lua_pushstring(L,"basename");
496 lua_pushstring(L,moddata[i].basename.c_str());
497 lua_settable(L, top_lvl2);
499 lua_settable(L, top);
506 /******************************************************************************/
507 int guiLuaApi::l_get_favorites(lua_State *L)
509 GUIEngine* engine = get_engine(L);
512 std::string listtype = "local";
514 if (!lua_isnone(L,1)) {
515 listtype = luaL_checkstring(L,1);
518 std::vector<ServerListSpec> servers;
520 if(listtype == "online") {
521 servers = ServerList::getOnline();
523 servers = ServerList::getLocal();
526 servers = ServerList::getLocal();
530 int top = lua_gettop(L);
531 unsigned int index = 1;
533 for (unsigned int i = 0; i < servers.size(); i++)
535 lua_pushnumber(L,index);
538 int top_lvl2 = lua_gettop(L);
540 if (servers[i]["clients"].asString().size()) {
542 const char* clients_raw = servers[i]["clients"].asString().c_str();
544 int numbervalue = strtol(clients_raw,&endptr,10);
546 if ((*clients_raw != 0) && (*endptr == 0)) {
547 lua_pushstring(L,"clients");
548 lua_pushnumber(L,numbervalue);
549 lua_settable(L, top_lvl2);
553 if (servers[i]["clients_max"].asString().size()) {
555 const char* clients_max_raw = servers[i]["clients"].asString().c_str();
557 int numbervalue = strtol(clients_max_raw,&endptr,10);
559 if ((*clients_max_raw != 0) && (*endptr == 0)) {
560 lua_pushstring(L,"clients_max");
561 lua_pushnumber(L,numbervalue);
562 lua_settable(L, top_lvl2);
566 if (servers[i]["version"].asString().size()) {
567 lua_pushstring(L,"version");
568 lua_pushstring(L,servers[i]["version"].asString().c_str());
569 lua_settable(L, top_lvl2);
572 if (servers[i]["password"].asString().size()) {
573 lua_pushstring(L,"password");
574 lua_pushboolean(L,true);
575 lua_settable(L, top_lvl2);
578 if (servers[i]["creative"].asString().size()) {
579 lua_pushstring(L,"creative");
580 lua_pushboolean(L,true);
581 lua_settable(L, top_lvl2);
584 if (servers[i]["damage"].asString().size()) {
585 lua_pushstring(L,"damage");
586 lua_pushboolean(L,true);
587 lua_settable(L, top_lvl2);
590 if (servers[i]["pvp"].asString().size()) {
591 lua_pushstring(L,"pvp");
592 lua_pushboolean(L,true);
593 lua_settable(L, top_lvl2);
596 if (servers[i]["description"].asString().size()) {
597 lua_pushstring(L,"description");
598 lua_pushstring(L,servers[i]["description"].asString().c_str());
599 lua_settable(L, top_lvl2);
602 if (servers[i]["name"].asString().size()) {
603 lua_pushstring(L,"name");
604 lua_pushstring(L,servers[i]["name"].asString().c_str());
605 lua_settable(L, top_lvl2);
608 if (servers[i]["address"].asString().size()) {
609 lua_pushstring(L,"address");
610 lua_pushstring(L,servers[i]["address"].asString().c_str());
611 lua_settable(L, top_lvl2);
614 if (servers[i]["port"].asString().size()) {
615 lua_pushstring(L,"port");
616 lua_pushstring(L,servers[i]["port"].asString().c_str());
617 lua_settable(L, top_lvl2);
620 lua_settable(L, top);
626 /******************************************************************************/
627 int guiLuaApi::l_delete_favorite(lua_State *L)
629 GUIEngine* engine = get_engine(L);
632 std::vector<ServerListSpec> servers;
634 std::string listtype = "local";
636 if (!lua_isnone(L,2)) {
637 listtype = luaL_checkstring(L,2);
640 if ((listtype != "local") &&
641 (listtype != "online"))
645 if(listtype == "online") {
646 servers = ServerList::getOnline();
648 servers = ServerList::getLocal();
651 servers = ServerList::getLocal();
654 int fav_idx = luaL_checkinteger(L,1) -1;
656 if ((fav_idx >= 0) &&
657 (fav_idx < (int) servers.size())) {
659 ServerList::deleteEntry(servers[fav_idx]);
665 /******************************************************************************/
666 int guiLuaApi::l_show_keys_menu(lua_State *L)
668 GUIEngine* engine = get_engine(L);
671 GUIKeyChangeMenu *kmenu
672 = new GUIKeyChangeMenu( engine->m_device->getGUIEnvironment(),
675 engine->m_menumanager);
680 /******************************************************************************/
681 int guiLuaApi::l_setting_set(lua_State *L)
683 const char *name = luaL_checkstring(L, 1);
684 const char *value = luaL_checkstring(L, 2);
685 g_settings->set(name, value);
689 /******************************************************************************/
690 int guiLuaApi::l_setting_get(lua_State *L)
692 const char *name = luaL_checkstring(L, 1);
694 std::string value = g_settings->get(name);
695 lua_pushstring(L, value.c_str());
696 } catch(SettingNotFoundException &e){
702 /******************************************************************************/
703 int guiLuaApi::l_setting_getbool(lua_State *L)
705 const char *name = luaL_checkstring(L, 1);
707 bool value = g_settings->getBool(name);
708 lua_pushboolean(L, value);
709 } catch(SettingNotFoundException &e){
715 /******************************************************************************/
716 int guiLuaApi::l_setting_setbool(lua_State *L)
718 const char *name = luaL_checkstring(L, 1);
719 bool value = lua_toboolean(L,2);
721 g_settings->setBool(name,value);
726 /******************************************************************************/
727 int guiLuaApi::l_create_world(lua_State *L)
729 GUIEngine* engine = get_engine(L);
732 const char *name = luaL_checkstring(L, 1);
733 int gameidx = luaL_checkinteger(L,2) -1;
735 std::string path = porting::path_user + DIR_DELIM
739 std::vector<SubgameSpec> games = getAvailableGames();
741 if ((gameidx >= 0) &&
742 (gameidx < (int) games.size())) {
744 // Create world if it doesn't exist
745 if(!initializeWorld(path, games[gameidx].id)){
746 lua_pushstring(L, "Failed to initialize world");
754 lua_pushstring(L, "Invalid game index");
759 /******************************************************************************/
760 int guiLuaApi::l_delete_world(lua_State *L)
762 GUIEngine* engine = get_engine(L);
765 int worldidx = luaL_checkinteger(L,1) -1;
767 std::vector<WorldSpec> worlds = getAvailableWorlds();
769 if ((worldidx >= 0) &&
770 (worldidx < (int) worlds.size())) {
772 WorldSpec spec = worlds[worldidx];
774 std::vector<std::string> paths;
775 paths.push_back(spec.path);
776 fs::GetRecursiveSubPaths(spec.path, paths);
779 if (!fs::DeletePaths(paths)) {
780 lua_pushstring(L, "Failed to delete world");
787 lua_pushstring(L, "Invalid world index");
792 /******************************************************************************/
793 int guiLuaApi::l_set_topleft_text(lua_State *L)
795 GUIEngine* engine = get_engine(L);
798 std::string text = "";
800 if (!lua_isnone(L,1) && !lua_isnil(L,1))
801 text = luaL_checkstring(L, 1);
803 engine->setTopleftText(text);
807 /******************************************************************************/
808 int guiLuaApi::l_get_modpath(lua_State *L)
811 = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
812 lua_pushstring(L, modpath.c_str());
816 /******************************************************************************/
817 int guiLuaApi::l_get_gamepath(lua_State *L)
820 = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games" + DIR_DELIM);
821 lua_pushstring(L, gamepath.c_str());
825 /******************************************************************************/
826 int guiLuaApi::l_get_dirlist(lua_State *L) {
827 const char *path = luaL_checkstring(L, 1);
828 bool dironly = lua_toboolean(L, 2);
830 std::vector<fs::DirListNode> dirlist = fs::GetDirListing(path);
832 unsigned int index = 1;
834 int table = lua_gettop(L);
836 for (unsigned int i=0;i< dirlist.size(); i++) {
837 if ((dirlist[i].dir) || (dironly == false)) {
838 lua_pushnumber(L,index);
839 lua_pushstring(L,dirlist[i].name.c_str());
840 lua_settable(L, table);
848 /******************************************************************************/
849 int guiLuaApi::l_create_dir(lua_State *L) {
850 const char *path = luaL_checkstring(L, 1);
852 if (guiLuaApi::isMinetestPath(path)) {
853 lua_pushboolean(L,fs::CreateAllDirs(path));
856 lua_pushboolean(L,false);
860 /******************************************************************************/
861 int guiLuaApi::l_delete_dir(lua_State *L) {
862 const char *path = luaL_checkstring(L, 1);
864 std::string absolute_path = fs::RemoveRelativePathComponents(path);
866 if (guiLuaApi::isMinetestPath(absolute_path)) {
867 lua_pushboolean(L,fs::RecursiveDelete(absolute_path));
870 lua_pushboolean(L,false);
874 /******************************************************************************/
875 int guiLuaApi::l_copy_dir(lua_State *L) {
876 const char *source = luaL_checkstring(L, 1);
877 const char *destination = luaL_checkstring(L, 2);
879 bool keep_source = true;
881 if ((!lua_isnone(L,3)) &&
883 keep_source = lua_toboolean(L,3);
886 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
887 std::string absolute_source = fs::RemoveRelativePathComponents(source);
889 if ((guiLuaApi::isMinetestPath(absolute_source)) &&
890 (guiLuaApi::isMinetestPath(absolute_destination))) {
891 bool retval = fs::CopyDir(absolute_source,absolute_destination);
893 if (retval && (!keep_source)) {
895 retval &= fs::RecursiveDelete(absolute_source);
897 lua_pushboolean(L,retval);
900 lua_pushboolean(L,false);
904 /******************************************************************************/
905 int guiLuaApi::l_extract_zip(lua_State *L) {
907 GUIEngine* engine = get_engine(L);
910 const char *zipfile = luaL_checkstring(L, 1);
911 const char *destination = luaL_checkstring(L, 2);
913 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
915 if (guiLuaApi::isMinetestPath(absolute_destination)) {
916 fs::CreateAllDirs(absolute_destination);
918 io::IFileSystem* fs = engine->m_device->getFileSystem();
920 fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP);
922 assert(fs->getFileArchiveCount() > 0);
924 /**********************************************************************/
925 /* WARNING this is not threadsafe!! */
926 /**********************************************************************/
927 io::IFileArchive* opened_zip =
928 fs->getFileArchive(fs->getFileArchiveCount()-1);
930 const io::IFileList* files_in_zip = opened_zip->getFileList();
932 unsigned int number_of_files = files_in_zip->getFileCount();
934 for (unsigned int i=0; i < number_of_files; i++) {
935 std::string fullpath = destination;
936 fullpath += DIR_DELIM;
937 fullpath += files_in_zip->getFullFileName(i).c_str();
939 if (files_in_zip->isDirectory(i)) {
940 if (! fs::CreateAllDirs(fullpath) ) {
941 fs->removeFileArchive(fs->getFileArchiveCount()-1);
942 lua_pushboolean(L,false);
947 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
949 FILE *targetfile = fopen(fullpath.c_str(),"wb");
951 if (targetfile == NULL) {
952 fs->removeFileArchive(fs->getFileArchiveCount()-1);
953 lua_pushboolean(L,false);
957 char read_buffer[1024];
958 unsigned int total_read = 0;
960 while (total_read < toread->getSize()) {
962 unsigned int bytes_read =
963 toread->read(read_buffer,sizeof(read_buffer));
964 unsigned int bytes_written;
965 if ((bytes_read < 0 ) ||
966 (bytes_written = fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
969 fs->removeFileArchive(fs->getFileArchiveCount()-1);
970 lua_pushboolean(L,false);
973 total_read += bytes_read;
981 fs->removeFileArchive(fs->getFileArchiveCount()-1);
982 lua_pushboolean(L,true);
986 lua_pushboolean(L,false);
990 /******************************************************************************/
991 int guiLuaApi::l_get_scriptdir(lua_State *L) {
992 GUIEngine* engine = get_engine(L);
995 lua_pushstring(L,engine->getScriptDir().c_str());
999 /******************************************************************************/
1000 bool guiLuaApi::isMinetestPath(std::string path) {
1003 if (fs::PathStartsWith(path,fs::TempPath()))
1007 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
1011 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
1015 if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
1022 /******************************************************************************/
1023 int guiLuaApi::l_show_file_open_dialog(lua_State *L) {
1025 GUIEngine* engine = get_engine(L);
1026 assert(engine != 0);
1028 const char *formname= luaL_checkstring(L, 1);
1029 const char *title = luaL_checkstring(L, 2);
1031 GUIFileSelectMenu* fileOpenMenu =
1032 new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(),
1035 engine->m_menumanager,
1038 fileOpenMenu->setTextDest(engine->m_buttonhandler);
1039 fileOpenMenu->drop();
1043 /******************************************************************************/
1044 int guiLuaApi::l_get_version(lua_State *L) {
1045 lua_pushstring(L,VERSION_STRING);
1049 /******************************************************************************/
1050 int guiLuaApi::l_download_file(lua_State *L) {
1051 GUIEngine* engine = get_engine(L);
1052 assert(engine != 0);
1054 const char *url = luaL_checkstring(L, 1);
1055 const char *target = luaL_checkstring(L, 2);
1058 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
1060 if (guiLuaApi::isMinetestPath(absolute_destination)) {
1061 if (engine->downloadFile(url,absolute_destination)) {
1062 lua_pushboolean(L,true);
1066 lua_pushboolean(L,false);