]> git.lizzy.rs Git - minetest.git/blob - src/script/lua_api/l_mainmenu.cpp
Load dependencies and description from mod.conf
[minetest.git] / src / script / lua_api / l_mainmenu.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 sapier
4
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.
9
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.
14
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.
18 */
19
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 "gui/guiEngine.h"
25 #include "gui/guiMainMenu.h"
26 #include "gui/guiKeyChangeMenu.h"
27 #include "gui/guiPathSelectMenu.h"
28 #include "subgame.h"
29 #include "version.h"
30 #include "porting.h"
31 #include "filesys.h"
32 #include "convert_json.h"
33 #include "serverlist.h"
34 #include "mapgen/mapgen.h"
35 #include "settings.h"
36
37 #include <IFileArchive.h>
38 #include <IFileSystem.h>
39 #include "client/renderingengine.h"
40
41
42 /******************************************************************************/
43 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
44 {
45         lua_getglobal(L, "gamedata");
46
47         lua_getfield(L, -1, name.c_str());
48
49         if(lua_isnil(L, -1))
50                 return "";
51
52         return luaL_checkstring(L, -1);
53 }
54
55 /******************************************************************************/
56 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
57 {
58         lua_getglobal(L, "gamedata");
59
60         lua_getfield(L, -1, name.c_str());
61
62         if(lua_isnil(L, -1)) {
63                 valid = false;
64                 return -1;
65                 }
66
67         valid = true;
68         return luaL_checkinteger(L, -1);
69 }
70
71 /******************************************************************************/
72 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
73 {
74         lua_getglobal(L, "gamedata");
75
76         lua_getfield(L, -1, name.c_str());
77
78         if(lua_isnil(L, -1)) {
79                 valid = false;
80                 return false;
81                 }
82
83         valid = true;
84         return lua_toboolean(L, -1);
85 }
86
87 /******************************************************************************/
88 int ModApiMainMenu::l_update_formspec(lua_State *L)
89 {
90         GUIEngine* engine = getGuiEngine(L);
91         sanity_check(engine != NULL);
92
93         if (engine->m_startgame)
94                 return 0;
95
96         //read formspec
97         std::string formspec(luaL_checkstring(L, 1));
98
99         if (engine->m_formspecgui != 0) {
100                 engine->m_formspecgui->setForm(formspec);
101         }
102
103         return 0;
104 }
105
106 /******************************************************************************/
107 int ModApiMainMenu::l_start(lua_State *L)
108 {
109         GUIEngine* engine = getGuiEngine(L);
110         sanity_check(engine != NULL);
111
112         //update c++ gamedata from lua table
113
114         bool valid = false;
115
116         MainMenuData *data = engine->m_data;
117
118         data->selected_world = getIntegerData(L, "selected_world",valid) -1;
119         data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
120         data->do_reconnect = getBoolData(L, "do_reconnect", valid);
121         if (!data->do_reconnect) {
122                 data->name     = getTextData(L,"playername");
123                 data->password = getTextData(L,"password");
124                 data->address  = getTextData(L,"address");
125                 data->port     = getTextData(L,"port");
126         }
127         data->serverdescription = getTextData(L,"serverdescription");
128         data->servername        = getTextData(L,"servername");
129
130         //close menu next time
131         engine->m_startgame = true;
132         return 0;
133 }
134
135 /******************************************************************************/
136 int ModApiMainMenu::l_close(lua_State *L)
137 {
138         GUIEngine* engine = getGuiEngine(L);
139         sanity_check(engine != NULL);
140
141         engine->m_kill = true;
142         return 0;
143 }
144
145 /******************************************************************************/
146 int ModApiMainMenu::l_set_background(lua_State *L)
147 {
148         GUIEngine* engine = getGuiEngine(L);
149         sanity_check(engine != NULL);
150
151         std::string backgroundlevel(luaL_checkstring(L, 1));
152         std::string texturename(luaL_checkstring(L, 2));
153
154         bool tile_image = false;
155         bool retval     = false;
156         unsigned int minsize = 16;
157
158         if (!lua_isnone(L, 3)) {
159                 tile_image = lua_toboolean(L, 3);
160         }
161
162         if (!lua_isnone(L, 4)) {
163                 minsize = lua_tonumber(L, 4);
164         }
165
166         if (backgroundlevel == "background") {
167                 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
168                                 tile_image, minsize);
169         }
170
171         if (backgroundlevel == "overlay") {
172                 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
173                                 tile_image, minsize);
174         }
175
176         if (backgroundlevel == "header") {
177                 retval |= engine->setTexture(TEX_LAYER_HEADER,  texturename,
178                                 tile_image, minsize);
179         }
180
181         if (backgroundlevel == "footer") {
182                 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
183                                 tile_image, minsize);
184         }
185
186         lua_pushboolean(L,retval);
187         return 1;
188 }
189
190 /******************************************************************************/
191 int ModApiMainMenu::l_set_clouds(lua_State *L)
192 {
193         GUIEngine* engine = getGuiEngine(L);
194         sanity_check(engine != NULL);
195
196         bool value = lua_toboolean(L,1);
197
198         engine->m_clouds_enabled = value;
199
200         return 0;
201 }
202
203 /******************************************************************************/
204 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
205 {
206         // get_table_index accepts both tables and textlists
207         return l_get_table_index(L);
208 }
209
210 /******************************************************************************/
211 int ModApiMainMenu::l_get_table_index(lua_State *L)
212 {
213         GUIEngine* engine = getGuiEngine(L);
214         sanity_check(engine != NULL);
215
216         std::string tablename(luaL_checkstring(L, 1));
217         GUITable *table = engine->m_menu->getTable(tablename);
218         s32 selection = table ? table->getSelected() : 0;
219
220         if (selection >= 1)
221                 lua_pushinteger(L, selection);
222         else
223                 lua_pushnil(L);
224         return 1;
225 }
226
227 /******************************************************************************/
228 int ModApiMainMenu::l_get_worlds(lua_State *L)
229 {
230         std::vector<WorldSpec> worlds = getAvailableWorlds();
231
232         lua_newtable(L);
233         int top = lua_gettop(L);
234         unsigned int index = 1;
235
236         for (const WorldSpec &world : worlds) {
237                 lua_pushnumber(L,index);
238
239                 lua_newtable(L);
240                 int top_lvl2 = lua_gettop(L);
241
242                 lua_pushstring(L,"path");
243                 lua_pushstring(L, world.path.c_str());
244                 lua_settable(L, top_lvl2);
245
246                 lua_pushstring(L,"name");
247                 lua_pushstring(L, world.name.c_str());
248                 lua_settable(L, top_lvl2);
249
250                 lua_pushstring(L,"gameid");
251                 lua_pushstring(L, world.gameid.c_str());
252                 lua_settable(L, top_lvl2);
253
254                 lua_settable(L, top);
255                 index++;
256         }
257         return 1;
258 }
259
260 /******************************************************************************/
261 int ModApiMainMenu::l_get_favorites(lua_State *L)
262 {
263         std::string listtype = "local";
264
265         if (!lua_isnone(L,1)) {
266                 listtype = luaL_checkstring(L,1);
267         }
268
269         std::vector<ServerListSpec> servers;
270
271         if(listtype == "online") {
272                 servers = ServerList::getOnline();
273         } else {
274                 servers = ServerList::getLocal();
275         }
276
277         lua_newtable(L);
278         int top = lua_gettop(L);
279         unsigned int index = 1;
280
281         for (const Json::Value &server : servers) {
282
283                 lua_pushnumber(L,index);
284
285                 lua_newtable(L);
286                 int top_lvl2 = lua_gettop(L);
287
288                 if (!server["clients"].asString().empty()) {
289                         std::string clients_raw = server["clients"].asString();
290                         char* endptr = 0;
291                         int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
292
293                         if ((!clients_raw.empty()) && (*endptr == 0)) {
294                                 lua_pushstring(L,"clients");
295                                 lua_pushnumber(L,numbervalue);
296                                 lua_settable(L, top_lvl2);
297                         }
298                 }
299
300                 if (!server["clients_max"].asString().empty()) {
301
302                         std::string clients_max_raw = server["clients_max"].asString();
303                         char* endptr = 0;
304                         int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
305
306                         if ((!clients_max_raw.empty()) && (*endptr == 0)) {
307                                 lua_pushstring(L,"clients_max");
308                                 lua_pushnumber(L,numbervalue);
309                                 lua_settable(L, top_lvl2);
310                         }
311                 }
312
313                 if (!server["version"].asString().empty()) {
314                         lua_pushstring(L,"version");
315                         std::string topush = server["version"].asString();
316                         lua_pushstring(L,topush.c_str());
317                         lua_settable(L, top_lvl2);
318                 }
319
320                 if (!server["proto_min"].asString().empty()) {
321                         lua_pushstring(L,"proto_min");
322                         lua_pushinteger(L, server["proto_min"].asInt());
323                         lua_settable(L, top_lvl2);
324                 }
325
326                 if (!server["proto_max"].asString().empty()) {
327                         lua_pushstring(L,"proto_max");
328                         lua_pushinteger(L, server["proto_max"].asInt());
329                         lua_settable(L, top_lvl2);
330                 }
331
332                 if (!server["password"].asString().empty()) {
333                         lua_pushstring(L,"password");
334                         lua_pushboolean(L, server["password"].asBool());
335                         lua_settable(L, top_lvl2);
336                 }
337
338                 if (!server["creative"].asString().empty()) {
339                         lua_pushstring(L,"creative");
340                         lua_pushboolean(L, server["creative"].asBool());
341                         lua_settable(L, top_lvl2);
342                 }
343
344                 if (!server["damage"].asString().empty()) {
345                         lua_pushstring(L,"damage");
346                         lua_pushboolean(L, server["damage"].asBool());
347                         lua_settable(L, top_lvl2);
348                 }
349
350                 if (!server["pvp"].asString().empty()) {
351                         lua_pushstring(L,"pvp");
352                         lua_pushboolean(L, server["pvp"].asBool());
353                         lua_settable(L, top_lvl2);
354                 }
355
356                 if (!server["description"].asString().empty()) {
357                         lua_pushstring(L,"description");
358                         std::string topush = server["description"].asString();
359                         lua_pushstring(L,topush.c_str());
360                         lua_settable(L, top_lvl2);
361                 }
362
363                 if (!server["name"].asString().empty()) {
364                         lua_pushstring(L,"name");
365                         std::string topush = server["name"].asString();
366                         lua_pushstring(L,topush.c_str());
367                         lua_settable(L, top_lvl2);
368                 }
369
370                 if (!server["address"].asString().empty()) {
371                         lua_pushstring(L,"address");
372                         std::string topush = server["address"].asString();
373                         lua_pushstring(L,topush.c_str());
374                         lua_settable(L, top_lvl2);
375                 }
376
377                 if (!server["port"].asString().empty()) {
378                         lua_pushstring(L,"port");
379                         std::string topush = server["port"].asString();
380                         lua_pushstring(L,topush.c_str());
381                         lua_settable(L, top_lvl2);
382                 }
383
384                 if (server.isMember("ping")) {
385                         float ping = server["ping"].asFloat();
386                         lua_pushstring(L, "ping");
387                         lua_pushnumber(L, ping);
388                         lua_settable(L, top_lvl2);
389                 }
390
391                 lua_settable(L, top);
392                 index++;
393         }
394         return 1;
395 }
396
397 /******************************************************************************/
398 int ModApiMainMenu::l_delete_favorite(lua_State *L)
399 {
400         std::vector<ServerListSpec> servers;
401
402         std::string listtype = "local";
403
404         if (!lua_isnone(L,2)) {
405                 listtype = luaL_checkstring(L,2);
406         }
407
408         if ((listtype != "local") &&
409                 (listtype != "online"))
410                 return 0;
411
412
413         if(listtype == "online") {
414                 servers = ServerList::getOnline();
415         } else {
416                 servers = ServerList::getLocal();
417         }
418
419         int fav_idx     = luaL_checkinteger(L,1) -1;
420
421         if ((fav_idx >= 0) &&
422                         (fav_idx < (int) servers.size())) {
423
424                 ServerList::deleteEntry(servers[fav_idx]);
425         }
426
427         return 0;
428 }
429
430 /******************************************************************************/
431 int ModApiMainMenu::l_get_games(lua_State *L)
432 {
433         std::vector<SubgameSpec> games = getAvailableGames();
434
435         lua_newtable(L);
436         int top = lua_gettop(L);
437         unsigned int index = 1;
438
439         for (const SubgameSpec &game : games) {
440                 lua_pushnumber(L, index);
441                 lua_newtable(L);
442                 int top_lvl2 = lua_gettop(L);
443
444                 lua_pushstring(L, "id");
445                 lua_pushstring(L, game.id.c_str());
446                 lua_settable(L,   top_lvl2);
447
448                 lua_pushstring(L, "path");
449                 lua_pushstring(L, game.path.c_str());
450                 lua_settable(L,   top_lvl2);
451
452                 lua_pushstring(L, "gamemods_path");
453                 lua_pushstring(L, game.gamemods_path.c_str());
454                 lua_settable(L,   top_lvl2);
455
456                 lua_pushstring(L, "name");
457                 lua_pushstring(L, game.name.c_str());
458                 lua_settable(L,   top_lvl2);
459
460                 lua_pushstring(L, "menuicon_path");
461                 lua_pushstring(L, game.menuicon_path.c_str());
462                 lua_settable(L,   top_lvl2);
463
464                 lua_pushstring(L, "addon_mods_paths");
465                 lua_newtable(L);
466                 int table2 = lua_gettop(L);
467                 int internal_index = 1;
468                 for (const std::string &addon_mods_path : game.addon_mods_paths) {
469                         lua_pushnumber(L, internal_index);
470                         lua_pushstring(L, addon_mods_path.c_str());
471                         lua_settable(L,   table2);
472                         internal_index++;
473                 }
474                 lua_settable(L, top_lvl2);
475                 lua_settable(L, top);
476                 index++;
477         }
478         return 1;
479 }
480
481 /******************************************************************************/
482 int ModApiMainMenu::l_get_mod_info(lua_State *L)
483 {
484         std::string path = luaL_checkstring(L, 1);
485
486         ModSpec spec;
487         spec.path = path;
488         parseModContents(spec);
489
490         lua_newtable(L);
491
492         lua_pushstring(L, spec.name.c_str());
493         lua_setfield(L, -2, "name");
494
495         lua_pushstring(L, spec.is_modpack ? "modpack" : "mod");
496         lua_setfield(L, -2, "type");
497
498         lua_pushstring(L, spec.desc.c_str());
499         lua_setfield(L, -2, "description");
500
501         lua_pushstring(L, spec.path.c_str());
502         lua_setfield(L, -2, "path");
503
504         // Dependencies
505         lua_newtable(L);
506         int i = 1;
507         for (const auto &dep : spec.depends) {
508                 lua_pushstring(L, dep.c_str());
509                 lua_rawseti(L, -2, i);
510                 i++;
511         }
512         lua_setfield(L, -2, "depends");
513
514         // Optional Dependencies
515         lua_newtable(L);
516         i = 1;
517         for (const auto &dep : spec.optdepends) {
518                 lua_pushstring(L, dep.c_str());
519                 lua_rawseti(L, -2, i);
520                 i++;
521         }
522         lua_setfield(L, -2, "optional_depends");
523
524         return 1;
525 }
526
527 /******************************************************************************/
528 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
529 {
530         GUIEngine* engine = getGuiEngine(L);
531         sanity_check(engine != NULL);
532
533         GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
534                                                                 engine->m_parent,
535                                                                 -1,
536                                                                 engine->m_menumanager);
537         kmenu->drop();
538         return 0;
539 }
540
541 /******************************************************************************/
542 int ModApiMainMenu::l_create_world(lua_State *L)
543 {
544         const char *name        = luaL_checkstring(L, 1);
545         int gameidx                     = luaL_checkinteger(L,2) -1;
546
547         std::string path = porting::path_user + DIR_DELIM
548                         "worlds" + DIR_DELIM
549                         + name;
550
551         std::vector<SubgameSpec> games = getAvailableGames();
552
553         if ((gameidx >= 0) &&
554                         (gameidx < (int) games.size())) {
555
556                 // Create world if it doesn't exist
557                 if (!loadGameConfAndInitWorld(path, games[gameidx])) {
558                         lua_pushstring(L, "Failed to initialize world");
559                 } else {
560                         lua_pushnil(L);
561                 }
562         } else {
563                 lua_pushstring(L, "Invalid game index");
564         }
565         return 1;
566 }
567
568 /******************************************************************************/
569 int ModApiMainMenu::l_delete_world(lua_State *L)
570 {
571         int worldidx    = luaL_checkinteger(L,1) -1;
572
573         std::vector<WorldSpec> worlds = getAvailableWorlds();
574
575         if ((worldidx >= 0) &&
576                 (worldidx < (int) worlds.size())) {
577
578                 WorldSpec spec = worlds[worldidx];
579
580                 std::vector<std::string> paths;
581                 paths.push_back(spec.path);
582                 fs::GetRecursiveSubPaths(spec.path, paths, true);
583
584                 // Delete files
585                 if (!fs::DeletePaths(paths)) {
586                         lua_pushstring(L, "Failed to delete world");
587                 }
588                 else {
589                         lua_pushnil(L);
590                 }
591         }
592         else {
593                 lua_pushstring(L, "Invalid world index");
594         }
595         return 1;
596 }
597
598 /******************************************************************************/
599 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
600 {
601         GUIEngine* engine = getGuiEngine(L);
602         sanity_check(engine != NULL);
603
604         std::string text;
605
606         if (!lua_isnone(L,1) && !lua_isnil(L,1))
607                 text = luaL_checkstring(L, 1);
608
609         engine->setTopleftText(text);
610         return 0;
611 }
612
613 /******************************************************************************/
614 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
615 {
616         std::vector<const char *> names;
617         Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
618
619         lua_newtable(L);
620         for (size_t i = 0; i != names.size(); i++) {
621                 lua_pushstring(L, names[i]);
622                 lua_rawseti(L, -2, i + 1);
623         }
624
625         return 1;
626 }
627
628
629 /******************************************************************************/
630 int ModApiMainMenu::l_get_modpath(lua_State *L)
631 {
632         std::string modpath = fs::RemoveRelativePathComponents(
633                 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
634         lua_pushstring(L, modpath.c_str());
635         return 1;
636 }
637
638 /******************************************************************************/
639 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
640 {
641         std::string modpath = fs::RemoveRelativePathComponents(
642                 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
643         lua_pushstring(L, modpath.c_str());
644         return 1;
645 }
646
647 /******************************************************************************/
648 int ModApiMainMenu::l_get_gamepath(lua_State *L)
649 {
650         std::string gamepath = fs::RemoveRelativePathComponents(
651                 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
652         lua_pushstring(L, gamepath.c_str());
653         return 1;
654 }
655
656 /******************************************************************************/
657 int ModApiMainMenu::l_get_texturepath(lua_State *L)
658 {
659         std::string gamepath = fs::RemoveRelativePathComponents(
660                 porting::path_user + DIR_DELIM + "textures");
661         lua_pushstring(L, gamepath.c_str());
662         return 1;
663 }
664
665 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
666 {
667         std::string gamepath = fs::RemoveRelativePathComponents(
668                 porting::path_share + DIR_DELIM + "textures");
669         lua_pushstring(L, gamepath.c_str());
670         return 1;
671 }
672
673 /******************************************************************************/
674 int ModApiMainMenu::l_create_dir(lua_State *L) {
675         const char *path = luaL_checkstring(L, 1);
676
677         if (ModApiMainMenu::isMinetestPath(path)) {
678                 lua_pushboolean(L, fs::CreateAllDirs(path));
679                 return 1;
680         }
681
682         lua_pushboolean(L, false);
683         return 1;
684 }
685
686 /******************************************************************************/
687 int ModApiMainMenu::l_delete_dir(lua_State *L)
688 {
689         const char *path = luaL_checkstring(L, 1);
690
691         std::string absolute_path = fs::RemoveRelativePathComponents(path);
692
693         if (ModApiMainMenu::isMinetestPath(absolute_path)) {
694                 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
695                 return 1;
696         }
697
698         lua_pushboolean(L, false);
699         return 1;
700 }
701
702 /******************************************************************************/
703 int ModApiMainMenu::l_copy_dir(lua_State *L)
704 {
705         const char *source      = luaL_checkstring(L, 1);
706         const char *destination = luaL_checkstring(L, 2);
707
708         bool keep_source = true;
709
710         if ((!lua_isnone(L,3)) &&
711                         (!lua_isnil(L,3))) {
712                 keep_source = lua_toboolean(L,3);
713         }
714
715         std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
716         std::string absolute_source = fs::RemoveRelativePathComponents(source);
717
718         if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
719                 bool retval = fs::CopyDir(absolute_source,absolute_destination);
720
721                 if (retval && (!keep_source)) {
722
723                         retval &= fs::RecursiveDelete(absolute_source);
724                 }
725                 lua_pushboolean(L,retval);
726                 return 1;
727         }
728         lua_pushboolean(L,false);
729         return 1;
730 }
731
732 /******************************************************************************/
733 int ModApiMainMenu::l_extract_zip(lua_State *L)
734 {
735         const char *zipfile     = luaL_checkstring(L, 1);
736         const char *destination = luaL_checkstring(L, 2);
737
738         std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
739
740         if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
741                 fs::CreateAllDirs(absolute_destination);
742
743                 io::IFileSystem *fs = RenderingEngine::get_filesystem();
744
745                 if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) {
746                         lua_pushboolean(L,false);
747                         return 1;
748                 }
749
750                 sanity_check(fs->getFileArchiveCount() > 0);
751
752                 /**********************************************************************/
753                 /* WARNING this is not threadsafe!!                                   */
754                 /**********************************************************************/
755                 io::IFileArchive* opened_zip =
756                         fs->getFileArchive(fs->getFileArchiveCount()-1);
757
758                 const io::IFileList* files_in_zip = opened_zip->getFileList();
759
760                 unsigned int number_of_files = files_in_zip->getFileCount();
761
762                 for (unsigned int i=0; i < number_of_files; i++) {
763                         std::string fullpath = destination;
764                         fullpath += DIR_DELIM;
765                         fullpath += files_in_zip->getFullFileName(i).c_str();
766                         std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
767
768                         if (!files_in_zip->isDirectory(i)) {
769                                 if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
770                                         fs->removeFileArchive(fs->getFileArchiveCount()-1);
771                                         lua_pushboolean(L,false);
772                                         return 1;
773                                 }
774
775                                 io::IReadFile* toread = opened_zip->createAndOpenFile(i);
776
777                                 FILE *targetfile = fopen(fullpath.c_str(),"wb");
778
779                                 if (targetfile == NULL) {
780                                         fs->removeFileArchive(fs->getFileArchiveCount()-1);
781                                         lua_pushboolean(L,false);
782                                         return 1;
783                                 }
784
785                                 char read_buffer[1024];
786                                 long total_read = 0;
787
788                                 while (total_read < toread->getSize()) {
789
790                                         unsigned int bytes_read =
791                                                         toread->read(read_buffer,sizeof(read_buffer));
792                                         if ((bytes_read == 0 ) ||
793                                                 (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read))
794                                         {
795                                                 fclose(targetfile);
796                                                 fs->removeFileArchive(fs->getFileArchiveCount()-1);
797                                                 lua_pushboolean(L,false);
798                                                 return 1;
799                                         }
800                                         total_read += bytes_read;
801                                 }
802
803                                 fclose(targetfile);
804                         }
805
806                 }
807
808                 fs->removeFileArchive(fs->getFileArchiveCount()-1);
809                 lua_pushboolean(L,true);
810                 return 1;
811         }
812
813         lua_pushboolean(L,false);
814         return 1;
815 }
816
817 /******************************************************************************/
818 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
819 {
820         GUIEngine* engine = getGuiEngine(L);
821         sanity_check(engine != NULL);
822
823         lua_pushstring(L,engine->getScriptDir().c_str());
824         return 1;
825 }
826
827 /******************************************************************************/
828 bool ModApiMainMenu::isMinetestPath(std::string path)
829 {
830         if (fs::PathStartsWith(path,fs::TempPath()))
831                 return true;
832
833         /* games */
834         if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games")))
835                 return true;
836
837         /* mods */
838         if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods")))
839                 return true;
840
841         /* worlds */
842         if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds")))
843                 return true;
844
845
846         return false;
847 }
848
849 /******************************************************************************/
850 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
851 {
852         GUIEngine* engine = getGuiEngine(L);
853         sanity_check(engine != NULL);
854
855         const char *formname= luaL_checkstring(L, 1);
856         const char *title       = luaL_checkstring(L, 2);
857         bool is_file_select = lua_toboolean(L, 3);
858
859         GUIFileSelectMenu* fileOpenMenu =
860                 new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
861                                                                 engine->m_parent,
862                                                                 -1,
863                                                                 engine->m_menumanager,
864                                                                 title,
865                                                                 formname,
866                                                                 is_file_select);
867         fileOpenMenu->setTextDest(engine->m_buttonhandler);
868         fileOpenMenu->drop();
869         return 0;
870 }
871
872 /******************************************************************************/
873 int ModApiMainMenu::l_download_file(lua_State *L)
874 {
875         const char *url    = luaL_checkstring(L, 1);
876         const char *target = luaL_checkstring(L, 2);
877
878         //check path
879         std::string absolute_destination = fs::RemoveRelativePathComponents(target);
880
881         if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
882                 if (GUIEngine::downloadFile(url,absolute_destination)) {
883                         lua_pushboolean(L,true);
884                         return 1;
885                 }
886         } else {
887                 errorstream << "DOWNLOAD denied: " << absolute_destination
888                                 << " isn't a allowed path" << std::endl;
889         }
890         lua_pushboolean(L,false);
891         return 1;
892 }
893
894 /******************************************************************************/
895 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
896 {
897         std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
898
899         lua_newtable(L);
900         for (u32 i = 0; i != drivers.size(); i++) {
901                 const char *name  = RenderingEngine::getVideoDriverName(drivers[i]);
902                 const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
903
904                 lua_newtable(L);
905                 lua_pushstring(L, name);
906                 lua_setfield(L, -2, "name");
907                 lua_pushstring(L, fname);
908                 lua_setfield(L, -2, "friendly_name");
909
910                 lua_rawseti(L, -2, i + 1);
911         }
912
913         return 1;
914 }
915
916 /******************************************************************************/
917 int ModApiMainMenu::l_get_video_modes(lua_State *L)
918 {
919         std::vector<core::vector3d<u32> > videomodes
920                 = RenderingEngine::getSupportedVideoModes();
921
922         lua_newtable(L);
923         for (u32 i = 0; i != videomodes.size(); i++) {
924                 lua_newtable(L);
925                 lua_pushnumber(L, videomodes[i].X);
926                 lua_setfield(L, -2, "w");
927                 lua_pushnumber(L, videomodes[i].Y);
928                 lua_setfield(L, -2, "h");
929                 lua_pushnumber(L, videomodes[i].Z);
930                 lua_setfield(L, -2, "depth");
931
932                 lua_rawseti(L, -2, i + 1);
933         }
934
935         return 1;
936 }
937
938 /******************************************************************************/
939 int ModApiMainMenu::l_gettext(lua_State *L)
940 {
941         std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
942         lua_pushstring(L, text.c_str());
943
944         return 1;
945 }
946
947 /******************************************************************************/
948 int ModApiMainMenu::l_get_screen_info(lua_State *L)
949 {
950         lua_newtable(L);
951         int top = lua_gettop(L);
952         lua_pushstring(L,"density");
953         lua_pushnumber(L,RenderingEngine::getDisplayDensity());
954         lua_settable(L, top);
955
956         lua_pushstring(L,"display_width");
957         lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
958         lua_settable(L, top);
959
960         lua_pushstring(L,"display_height");
961         lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
962         lua_settable(L, top);
963
964         const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
965         lua_pushstring(L,"window_width");
966         lua_pushnumber(L, window_size.X);
967         lua_settable(L, top);
968
969         lua_pushstring(L,"window_height");
970         lua_pushnumber(L, window_size.Y);
971         lua_settable(L, top);
972         return 1;
973 }
974
975 /******************************************************************************/
976 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
977 {
978         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
979         return 1;
980 }
981
982 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
983 {
984         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
985         return 1;
986 }
987
988 /******************************************************************************/
989 int ModApiMainMenu::l_do_async_callback(lua_State *L)
990 {
991         GUIEngine* engine = getGuiEngine(L);
992
993         size_t func_length, param_length;
994         const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
995
996         const char* serialized_param_raw = luaL_checklstring(L, 2, &param_length);
997
998         sanity_check(serialized_func_raw != NULL);
999         sanity_check(serialized_param_raw != NULL);
1000
1001         std::string serialized_func = std::string(serialized_func_raw, func_length);
1002         std::string serialized_param = std::string(serialized_param_raw, param_length);
1003
1004         lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
1005
1006         return 1;
1007 }
1008
1009 /******************************************************************************/
1010 void ModApiMainMenu::Initialize(lua_State *L, int top)
1011 {
1012         API_FCT(update_formspec);
1013         API_FCT(set_clouds);
1014         API_FCT(get_textlist_index);
1015         API_FCT(get_table_index);
1016         API_FCT(get_worlds);
1017         API_FCT(get_games);
1018         API_FCT(get_mod_info);
1019         API_FCT(start);
1020         API_FCT(close);
1021         API_FCT(get_favorites);
1022         API_FCT(show_keys_menu);
1023         API_FCT(create_world);
1024         API_FCT(delete_world);
1025         API_FCT(delete_favorite);
1026         API_FCT(set_background);
1027         API_FCT(set_topleft_text);
1028         API_FCT(get_mapgen_names);
1029         API_FCT(get_modpath);
1030         API_FCT(get_clientmodpath);
1031         API_FCT(get_gamepath);
1032         API_FCT(get_texturepath);
1033         API_FCT(get_texturepath_share);
1034         API_FCT(create_dir);
1035         API_FCT(delete_dir);
1036         API_FCT(copy_dir);
1037         API_FCT(extract_zip);
1038         API_FCT(get_mainmenu_path);
1039         API_FCT(show_path_select_dialog);
1040         API_FCT(download_file);
1041         API_FCT(gettext);
1042         API_FCT(get_video_drivers);
1043         API_FCT(get_video_modes);
1044         API_FCT(get_screen_info);
1045         API_FCT(get_min_supp_proto);
1046         API_FCT(get_max_supp_proto);
1047         API_FCT(do_async_callback);
1048 }
1049
1050 /******************************************************************************/
1051 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1052 {
1053
1054         API_FCT(get_worlds);
1055         API_FCT(get_games);
1056         API_FCT(get_favorites);
1057         API_FCT(get_mapgen_names);
1058         API_FCT(get_modpath);
1059         API_FCT(get_clientmodpath);
1060         API_FCT(get_gamepath);
1061         API_FCT(get_texturepath);
1062         API_FCT(get_texturepath_share);
1063         API_FCT(create_dir);
1064         API_FCT(delete_dir);
1065         API_FCT(copy_dir);
1066         //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
1067         API_FCT(download_file);
1068         //API_FCT(gettext); (gettext lib isn't threadsafe)
1069 }