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