]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_mainmenu.cpp
f7b2393fb0b98cbc087236b139dc5bc9bf141b6e
[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 "scripting_mainmenu.h"
25 #include "gui/guiEngine.h"
26 #include "gui/guiMainMenu.h"
27 #include "gui/guiKeyChangeMenu.h"
28 #include "gui/guiPathSelectMenu.h"
29 #include "version.h"
30 #include "porting.h"
31 #include "filesys.h"
32 #include "convert_json.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 #include "client/client.h"
39 #include "client/renderingengine.h"
40 #include "network/networkprotocol.h"
41
42
43 /******************************************************************************/
44 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
45 {
46         lua_getglobal(L, "gamedata");
47
48         lua_getfield(L, -1, name.c_str());
49
50         if(lua_isnil(L, -1))
51                 return "";
52
53         return luaL_checkstring(L, -1);
54 }
55
56 /******************************************************************************/
57 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
58 {
59         lua_getglobal(L, "gamedata");
60
61         lua_getfield(L, -1, name.c_str());
62
63         if(lua_isnil(L, -1)) {
64                 valid = false;
65                 return -1;
66                 }
67
68         valid = true;
69         return luaL_checkinteger(L, -1);
70 }
71
72 /******************************************************************************/
73 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
74 {
75         lua_getglobal(L, "gamedata");
76
77         lua_getfield(L, -1, name.c_str());
78
79         if(lua_isnil(L, -1)) {
80                 valid = false;
81                 return false;
82                 }
83
84         valid = true;
85         return readParam<bool>(L, -1);
86 }
87
88 /******************************************************************************/
89 int ModApiMainMenu::l_update_formspec(lua_State *L)
90 {
91         GUIEngine* engine = getGuiEngine(L);
92         sanity_check(engine != NULL);
93
94         if (engine->m_startgame)
95                 return 0;
96
97         //read formspec
98         std::string formspec(luaL_checkstring(L, 1));
99
100         if (engine->m_formspecgui != 0) {
101                 engine->m_formspecgui->setForm(formspec);
102         }
103
104         return 0;
105 }
106
107 /******************************************************************************/
108 int ModApiMainMenu::l_set_formspec_prepend(lua_State *L)
109 {
110         GUIEngine *engine = getGuiEngine(L);
111         sanity_check(engine != NULL);
112
113         if (engine->m_startgame)
114                 return 0;
115
116         std::string formspec(luaL_checkstring(L, 1));
117         engine->setFormspecPrepend(formspec);
118
119         return 0;
120 }
121
122 /******************************************************************************/
123 int ModApiMainMenu::l_start(lua_State *L)
124 {
125         GUIEngine* engine = getGuiEngine(L);
126         sanity_check(engine != NULL);
127
128         //update c++ gamedata from lua table
129
130         bool valid = false;
131
132         MainMenuData *data = engine->m_data;
133
134         data->selected_world = getIntegerData(L, "selected_world",valid) -1;
135         data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
136         data->do_reconnect = getBoolData(L, "do_reconnect", valid);
137         if (!data->do_reconnect) {
138                 data->name     = getTextData(L,"playername");
139                 data->password = getTextData(L,"password");
140                 data->address  = getTextData(L,"address");
141                 data->port     = getTextData(L,"port");
142
143                 const auto val = getTextData(L, "allow_login_or_register");
144                 if (val == "login")
145                         data->allow_login_or_register = ELoginRegister::Login;
146                 else if (val == "register")
147                         data->allow_login_or_register = ELoginRegister::Register;
148                 else
149                         data->allow_login_or_register = ELoginRegister::Any;
150         }
151         data->serverdescription = getTextData(L,"serverdescription");
152         data->servername        = getTextData(L,"servername");
153
154         //close menu next time
155         engine->m_startgame = true;
156         return 0;
157 }
158
159 /******************************************************************************/
160 int ModApiMainMenu::l_close(lua_State *L)
161 {
162         GUIEngine* engine = getGuiEngine(L);
163         sanity_check(engine != NULL);
164
165         engine->m_kill = true;
166         return 0;
167 }
168
169 /******************************************************************************/
170 int ModApiMainMenu::l_set_background(lua_State *L)
171 {
172         GUIEngine* engine = getGuiEngine(L);
173         sanity_check(engine != NULL);
174
175         std::string backgroundlevel(luaL_checkstring(L, 1));
176         std::string texturename(luaL_checkstring(L, 2));
177
178         bool tile_image = false;
179         bool retval     = false;
180         unsigned int minsize = 16;
181
182         if (!lua_isnone(L, 3)) {
183                 tile_image = readParam<bool>(L, 3);
184         }
185
186         if (!lua_isnone(L, 4)) {
187                 minsize = lua_tonumber(L, 4);
188         }
189
190         if (backgroundlevel == "background") {
191                 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
192                                 tile_image, minsize);
193         }
194
195         if (backgroundlevel == "overlay") {
196                 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
197                                 tile_image, minsize);
198         }
199
200         if (backgroundlevel == "header") {
201                 retval |= engine->setTexture(TEX_LAYER_HEADER,  texturename,
202                                 tile_image, minsize);
203         }
204
205         if (backgroundlevel == "footer") {
206                 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
207                                 tile_image, minsize);
208         }
209
210         lua_pushboolean(L,retval);
211         return 1;
212 }
213
214 /******************************************************************************/
215 int ModApiMainMenu::l_set_clouds(lua_State *L)
216 {
217         GUIEngine* engine = getGuiEngine(L);
218         sanity_check(engine != NULL);
219
220         bool value = readParam<bool>(L,1);
221
222         engine->m_clouds_enabled = value;
223
224         return 0;
225 }
226
227 /******************************************************************************/
228 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
229 {
230         // get_table_index accepts both tables and textlists
231         return l_get_table_index(L);
232 }
233
234 /******************************************************************************/
235 int ModApiMainMenu::l_get_table_index(lua_State *L)
236 {
237         GUIEngine* engine = getGuiEngine(L);
238         sanity_check(engine != NULL);
239
240         std::string tablename(luaL_checkstring(L, 1));
241         GUITable *table = engine->m_menu->getTable(tablename);
242         s32 selection = table ? table->getSelected() : 0;
243
244         if (selection >= 1)
245                 lua_pushinteger(L, selection);
246         else
247                 lua_pushnil(L);
248         return 1;
249 }
250
251 /******************************************************************************/
252 int ModApiMainMenu::l_get_worlds(lua_State *L)
253 {
254         std::vector<WorldSpec> worlds = getAvailableWorlds();
255
256         lua_newtable(L);
257         int top = lua_gettop(L);
258         unsigned int index = 1;
259
260         for (const WorldSpec &world : worlds) {
261                 lua_pushnumber(L,index);
262
263                 lua_newtable(L);
264                 int top_lvl2 = lua_gettop(L);
265
266                 lua_pushstring(L,"path");
267                 lua_pushstring(L, world.path.c_str());
268                 lua_settable(L, top_lvl2);
269
270                 lua_pushstring(L,"name");
271                 lua_pushstring(L, world.name.c_str());
272                 lua_settable(L, top_lvl2);
273
274                 lua_pushstring(L,"gameid");
275                 lua_pushstring(L, world.gameid.c_str());
276                 lua_settable(L, top_lvl2);
277
278                 lua_settable(L, top);
279                 index++;
280         }
281         return 1;
282 }
283
284 /******************************************************************************/
285 int ModApiMainMenu::l_get_games(lua_State *L)
286 {
287         std::vector<SubgameSpec> games = getAvailableGames();
288
289         lua_newtable(L);
290         int top = lua_gettop(L);
291         unsigned int index = 1;
292
293         for (const SubgameSpec &game : games) {
294                 lua_pushnumber(L, index);
295                 lua_newtable(L);
296                 int top_lvl2 = lua_gettop(L);
297
298                 lua_pushstring(L,  "id");
299                 lua_pushstring(L,  game.id.c_str());
300                 lua_settable(L,    top_lvl2);
301
302                 lua_pushstring(L,  "path");
303                 lua_pushstring(L,  game.path.c_str());
304                 lua_settable(L,    top_lvl2);
305
306                 lua_pushstring(L,  "type");
307                 lua_pushstring(L,  "game");
308                 lua_settable(L,    top_lvl2);
309
310                 lua_pushstring(L,  "gamemods_path");
311                 lua_pushstring(L,  game.gamemods_path.c_str());
312                 lua_settable(L,    top_lvl2);
313
314                 lua_pushstring(L,  "name");
315                 lua_pushstring(L,  game.title.c_str());
316                 lua_settable(L,    top_lvl2);
317
318                 lua_pushstring(L,  "title");
319                 lua_pushstring(L,  game.title.c_str());
320                 lua_settable(L,    top_lvl2);
321
322                 lua_pushstring(L,  "author");
323                 lua_pushstring(L,  game.author.c_str());
324                 lua_settable(L,    top_lvl2);
325
326                 lua_pushstring(L,  "release");
327                 lua_pushinteger(L, game.release);
328                 lua_settable(L,    top_lvl2);
329
330                 lua_pushstring(L,  "menuicon_path");
331                 lua_pushstring(L,  game.menuicon_path.c_str());
332                 lua_settable(L,    top_lvl2);
333
334                 lua_pushstring(L, "addon_mods_paths");
335                 lua_newtable(L);
336                 int table2 = lua_gettop(L);
337                 int internal_index = 1;
338                 for (const auto &addon_mods_path : game.addon_mods_paths) {
339                         lua_pushnumber(L, internal_index);
340                         lua_pushstring(L, addon_mods_path.second.c_str());
341                         lua_settable(L,   table2);
342                         internal_index++;
343                 }
344                 lua_settable(L, top_lvl2);
345                 lua_settable(L, top);
346                 index++;
347         }
348         return 1;
349 }
350
351 /******************************************************************************/
352 int ModApiMainMenu::l_get_content_info(lua_State *L)
353 {
354         std::string path = luaL_checkstring(L, 1);
355
356         ContentSpec spec;
357         spec.path = path;
358         parseContentInfo(spec);
359
360         lua_newtable(L);
361
362         lua_pushstring(L, spec.name.c_str());
363         lua_setfield(L, -2, "name");
364
365         lua_pushstring(L, spec.type.c_str());
366         lua_setfield(L, -2, "type");
367
368         lua_pushstring(L, spec.author.c_str());
369         lua_setfield(L, -2, "author");
370
371         if (!spec.title.empty()) {
372                 lua_pushstring(L, spec.title.c_str());
373                 lua_setfield(L, -2, "title");
374         }
375
376         lua_pushinteger(L, spec.release);
377         lua_setfield(L, -2, "release");
378
379         lua_pushstring(L, spec.desc.c_str());
380         lua_setfield(L, -2, "description");
381
382         lua_pushstring(L, spec.path.c_str());
383         lua_setfield(L, -2, "path");
384
385         if (spec.type == "mod") {
386                 ModSpec spec;
387                 spec.path = path;
388                 parseModContents(spec);
389
390                 // Dependencies
391                 lua_newtable(L);
392                 int i = 1;
393                 for (const auto &dep : spec.depends) {
394                         lua_pushstring(L, dep.c_str());
395                         lua_rawseti(L, -2, i++);
396                 }
397                 lua_setfield(L, -2, "depends");
398
399                 // Optional Dependencies
400                 lua_newtable(L);
401                 i = 1;
402                 for (const auto &dep : spec.optdepends) {
403                         lua_pushstring(L, dep.c_str());
404                         lua_rawseti(L, -2, i++);
405                 }
406                 lua_setfield(L, -2, "optional_depends");
407         }
408
409         return 1;
410 }
411
412 /******************************************************************************/
413 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
414 {
415         GUIEngine* engine = getGuiEngine(L);
416         sanity_check(engine != NULL);
417
418         GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(
419                         engine->m_rendering_engine->get_gui_env(),
420                         engine->m_parent,
421                         -1,
422                         engine->m_menumanager,
423                         engine->m_texture_source);
424         kmenu->drop();
425         return 0;
426 }
427
428 /******************************************************************************/
429 int ModApiMainMenu::l_create_world(lua_State *L)
430 {
431         const char *name        = luaL_checkstring(L, 1);
432         int gameidx                     = luaL_checkinteger(L,2) -1;
433
434         StringMap use_settings;
435         luaL_checktype(L, 3, LUA_TTABLE);
436         lua_pushnil(L);
437         while (lua_next(L, 3) != 0) {
438                 // key at index -2 and value at index -1
439                 use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
440                 lua_pop(L, 1);
441         }
442         lua_pop(L, 1);
443
444         std::string path = porting::path_user + DIR_DELIM
445                         "worlds" + DIR_DELIM
446                         + sanitizeDirName(name, "world_");
447
448         std::vector<SubgameSpec> games = getAvailableGames();
449         if (gameidx < 0 || gameidx >= (int) games.size()) {
450                 lua_pushstring(L, "Invalid game index");
451                 return 1;
452         }
453
454         // Set the settings for world creation
455         // this is a bad hack but the best we have right now..
456         StringMap backup;
457         for (auto it : use_settings) {
458                 if (g_settings->existsLocal(it.first))
459                         backup[it.first] = g_settings->get(it.first);
460                 g_settings->set(it.first, it.second);
461         }
462
463         // Create world if it doesn't exist
464         try {
465                 loadGameConfAndInitWorld(path, name, games[gameidx], true);
466                 lua_pushnil(L);
467         } catch (const BaseException &e) {
468                 auto err = std::string("Failed to initialize world: ") + e.what();
469                 lua_pushstring(L, err.c_str());
470         }
471
472         // Restore previous settings
473         for (auto it : use_settings) {
474                 auto it2 = backup.find(it.first);
475                 if (it2 == backup.end())
476                         g_settings->remove(it.first); // wasn't set before
477                 else
478                         g_settings->set(it.first, it2->second); // was set before
479         }
480
481         return 1;
482 }
483
484 /******************************************************************************/
485 int ModApiMainMenu::l_delete_world(lua_State *L)
486 {
487         int world_id = luaL_checkinteger(L, 1) - 1;
488         std::vector<WorldSpec> worlds = getAvailableWorlds();
489         if (world_id < 0 || world_id >= (int) worlds.size()) {
490                 lua_pushstring(L, "Invalid world index");
491                 return 1;
492         }
493         const WorldSpec &spec = worlds[world_id];
494         if (!fs::RecursiveDelete(spec.path)) {
495                 lua_pushstring(L, "Failed to delete world");
496                 return 1;
497         }
498         return 0;
499 }
500
501 /******************************************************************************/
502 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
503 {
504         GUIEngine* engine = getGuiEngine(L);
505         sanity_check(engine != NULL);
506
507         std::string text;
508
509         if (!lua_isnone(L,1) && !lua_isnil(L,1))
510                 text = luaL_checkstring(L, 1);
511
512         engine->setTopleftText(text);
513         return 0;
514 }
515
516 /******************************************************************************/
517 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
518 {
519         std::vector<const char *> names;
520         bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
521         Mapgen::getMapgenNames(&names, include_hidden);
522
523         lua_newtable(L);
524         for (size_t i = 0; i != names.size(); i++) {
525                 lua_pushstring(L, names[i]);
526                 lua_rawseti(L, -2, i + 1);
527         }
528
529         return 1;
530 }
531
532
533 /******************************************************************************/
534 int ModApiMainMenu::l_get_user_path(lua_State *L)
535 {
536         std::string path = fs::RemoveRelativePathComponents(porting::path_user);
537         lua_pushstring(L, path.c_str());
538         return 1;
539 }
540
541 /******************************************************************************/
542 int ModApiMainMenu::l_get_modpath(lua_State *L)
543 {
544         std::string modpath = fs::RemoveRelativePathComponents(
545                 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
546         lua_pushstring(L, modpath.c_str());
547         return 1;
548 }
549
550 /******************************************************************************/
551 int ModApiMainMenu::l_get_modpaths(lua_State *L)
552 {
553         lua_newtable(L);
554
555         ModApiMainMenu::l_get_modpath(L);
556         lua_setfield(L, -2, "mods");
557
558         for (const std::string &component : getEnvModPaths()) {
559                 lua_pushstring(L, component.c_str());
560                 lua_setfield(L, -2, fs::AbsolutePath(component).c_str());
561         }
562         return 1;
563 }
564
565 /******************************************************************************/
566 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
567 {
568         std::string modpath = fs::RemoveRelativePathComponents(
569                 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
570         lua_pushstring(L, modpath.c_str());
571         return 1;
572 }
573
574 /******************************************************************************/
575 int ModApiMainMenu::l_get_gamepath(lua_State *L)
576 {
577         std::string gamepath = fs::RemoveRelativePathComponents(
578                 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
579         lua_pushstring(L, gamepath.c_str());
580         return 1;
581 }
582
583 /******************************************************************************/
584 int ModApiMainMenu::l_get_texturepath(lua_State *L)
585 {
586         std::string gamepath = fs::RemoveRelativePathComponents(
587                 porting::path_user + DIR_DELIM + "textures");
588         lua_pushstring(L, gamepath.c_str());
589         return 1;
590 }
591
592 /******************************************************************************/
593 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
594 {
595         std::string gamepath = fs::RemoveRelativePathComponents(
596                 porting::path_share + DIR_DELIM + "textures");
597         lua_pushstring(L, gamepath.c_str());
598         return 1;
599 }
600
601 /******************************************************************************/
602 int ModApiMainMenu::l_get_cache_path(lua_State *L)
603 {
604         lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
605         return 1;
606 }
607
608 /******************************************************************************/
609 int ModApiMainMenu::l_get_temp_path(lua_State *L)
610 {
611         if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
612                 lua_pushstring(L, fs::TempPath().c_str());
613         else
614                 lua_pushstring(L, fs::CreateTempFile().c_str());
615         return 1;
616 }
617
618 /******************************************************************************/
619 int ModApiMainMenu::l_create_dir(lua_State *L) {
620         const char *path = luaL_checkstring(L, 1);
621
622         if (ModApiMainMenu::mayModifyPath(path)) {
623                 lua_pushboolean(L, fs::CreateAllDirs(path));
624                 return 1;
625         }
626
627         lua_pushboolean(L, false);
628         return 1;
629 }
630
631 /******************************************************************************/
632 int ModApiMainMenu::l_delete_dir(lua_State *L)
633 {
634         const char *path = luaL_checkstring(L, 1);
635
636         std::string absolute_path = fs::RemoveRelativePathComponents(path);
637
638         if (ModApiMainMenu::mayModifyPath(absolute_path)) {
639                 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
640                 return 1;
641         }
642
643         lua_pushboolean(L, false);
644         return 1;
645 }
646
647 /******************************************************************************/
648 int ModApiMainMenu::l_copy_dir(lua_State *L)
649 {
650         const char *source      = luaL_checkstring(L, 1);
651         const char *destination = luaL_checkstring(L, 2);
652
653         bool keep_source = true;
654         if (!lua_isnoneornil(L, 3))
655                 keep_source = readParam<bool>(L, 3);
656
657         std::string abs_destination = fs::RemoveRelativePathComponents(destination);
658         std::string abs_source = fs::RemoveRelativePathComponents(source);
659
660         if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
661                 (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
662                 lua_pushboolean(L, false);
663                 return 1;
664         }
665
666         bool retval;
667         if (keep_source)
668                 retval = fs::CopyDir(abs_source, abs_destination);
669         else
670                 retval = fs::MoveDir(abs_source, abs_destination);
671         lua_pushboolean(L, retval);
672         return 1;
673 }
674
675 /******************************************************************************/
676 int ModApiMainMenu::l_is_dir(lua_State *L)
677 {
678         const char *path = luaL_checkstring(L, 1);
679
680         lua_pushboolean(L, fs::IsDir(path));
681         return 1;
682 }
683
684 /******************************************************************************/
685 int ModApiMainMenu::l_extract_zip(lua_State *L)
686 {
687         const char *zipfile     = luaL_checkstring(L, 1);
688         const char *destination = luaL_checkstring(L, 2);
689
690         std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
691
692         if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
693                 auto fs = RenderingEngine::get_raw_device()->getFileSystem();
694                 bool ok = fs::extractZipFile(fs, zipfile, destination);
695                 lua_pushboolean(L, ok);
696                 return 1;
697         }
698
699         lua_pushboolean(L,false);
700         return 1;
701 }
702
703 /******************************************************************************/
704 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
705 {
706         GUIEngine* engine = getGuiEngine(L);
707         sanity_check(engine != NULL);
708
709         lua_pushstring(L,engine->getScriptDir().c_str());
710         return 1;
711 }
712
713 /******************************************************************************/
714 bool ModApiMainMenu::mayModifyPath(std::string path)
715 {
716         path = fs::RemoveRelativePathComponents(path);
717
718         if (fs::PathStartsWith(path, fs::TempPath()))
719                 return true;
720
721         std::string path_user = fs::RemoveRelativePathComponents(porting::path_user);
722
723         if (fs::PathStartsWith(path, path_user + DIR_DELIM "client"))
724                 return true;
725         if (fs::PathStartsWith(path, path_user + DIR_DELIM "games"))
726                 return true;
727         if (fs::PathStartsWith(path, path_user + DIR_DELIM "mods"))
728                 return true;
729         if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures"))
730                 return true;
731         if (fs::PathStartsWith(path, path_user + DIR_DELIM "worlds"))
732                 return true;
733
734         if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_cache)))
735                 return true;
736
737         return false;
738 }
739
740
741 /******************************************************************************/
742 int ModApiMainMenu::l_may_modify_path(lua_State *L)
743 {
744         const char *target = luaL_checkstring(L, 1);
745         std::string absolute_destination = fs::RemoveRelativePathComponents(target);
746         lua_pushboolean(L, ModApiMainMenu::mayModifyPath(absolute_destination));
747         return 1;
748 }
749
750 /******************************************************************************/
751 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
752 {
753         GUIEngine* engine = getGuiEngine(L);
754         sanity_check(engine != NULL);
755
756         const char *formname= luaL_checkstring(L, 1);
757         const char *title       = luaL_checkstring(L, 2);
758         bool is_file_select = readParam<bool>(L, 3);
759
760         GUIFileSelectMenu* fileOpenMenu =
761                 new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(),
762                                 engine->m_parent,
763                                 -1,
764                                 engine->m_menumanager,
765                                 title,
766                                 formname,
767                                 is_file_select);
768         fileOpenMenu->setTextDest(engine->m_buttonhandler);
769         fileOpenMenu->drop();
770         return 0;
771 }
772
773 /******************************************************************************/
774 int ModApiMainMenu::l_download_file(lua_State *L)
775 {
776         const char *url    = luaL_checkstring(L, 1);
777         const char *target = luaL_checkstring(L, 2);
778
779         //check path
780         std::string absolute_destination = fs::RemoveRelativePathComponents(target);
781
782         if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
783                 if (GUIEngine::downloadFile(url,absolute_destination)) {
784                         lua_pushboolean(L,true);
785                         return 1;
786                 }
787         } else {
788                 errorstream << "DOWNLOAD denied: " << absolute_destination
789                                 << " isn't a allowed path" << std::endl;
790         }
791         lua_pushboolean(L,false);
792         return 1;
793 }
794
795 /******************************************************************************/
796 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
797 {
798         std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
799
800         lua_newtable(L);
801         for (u32 i = 0; i != drivers.size(); i++) {
802                 auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]);
803
804                 lua_newtable(L);
805                 lua_pushstring(L, info.name.c_str());
806                 lua_setfield(L, -2, "name");
807                 lua_pushstring(L, info.friendly_name.c_str());
808                 lua_setfield(L, -2, "friendly_name");
809
810                 lua_rawseti(L, -2, i + 1);
811         }
812
813         return 1;
814 }
815
816 /******************************************************************************/
817 int ModApiMainMenu::l_gettext(lua_State *L)
818 {
819         const char *srctext = luaL_checkstring(L, 1);
820         const char *text = *srctext ? gettext(srctext) : "";
821         lua_pushstring(L, text);
822
823         return 1;
824 }
825
826 /******************************************************************************/
827 int ModApiMainMenu::l_get_screen_info(lua_State *L)
828 {
829         lua_newtable(L);
830         int top = lua_gettop(L);
831         lua_pushstring(L,"density");
832         lua_pushnumber(L,RenderingEngine::getDisplayDensity());
833         lua_settable(L, top);
834
835         const v2u32 &window_size = RenderingEngine::getWindowSize();
836         lua_pushstring(L,"window_width");
837         lua_pushnumber(L, window_size.X);
838         lua_settable(L, top);
839
840         lua_pushstring(L,"window_height");
841         lua_pushnumber(L, window_size.Y);
842         lua_settable(L, top);
843
844         lua_pushstring(L, "render_info");
845         lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str());
846         lua_settable(L, top);
847         return 1;
848 }
849
850 /******************************************************************************/
851 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
852 {
853         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
854         return 1;
855 }
856
857 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
858 {
859         lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
860         return 1;
861 }
862
863 /******************************************************************************/
864 int ModApiMainMenu::l_open_url(lua_State *L)
865 {
866         std::string url = luaL_checkstring(L, 1);
867         lua_pushboolean(L, porting::open_url(url));
868         return 1;
869 }
870
871 /******************************************************************************/
872 int ModApiMainMenu::l_open_dir(lua_State *L)
873 {
874         std::string path = luaL_checkstring(L, 1);
875         lua_pushboolean(L, porting::open_directory(path));
876         return 1;
877 }
878
879 /******************************************************************************/
880 int ModApiMainMenu::l_do_async_callback(lua_State *L)
881 {
882         MainMenuScripting *script = getScriptApi<MainMenuScripting>(L);
883
884         size_t func_length, param_length;
885         const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
886         const char* serialized_param_raw = luaL_checklstring(L, 2, &param_length);
887
888         sanity_check(serialized_func_raw != NULL);
889         sanity_check(serialized_param_raw != NULL);
890
891         u32 jobId = script->queueAsync(
892                 std::string(serialized_func_raw, func_length),
893                 std::string(serialized_param_raw, param_length));
894
895         lua_pushinteger(L, jobId);
896
897         return 1;
898 }
899
900 /******************************************************************************/
901 void ModApiMainMenu::Initialize(lua_State *L, int top)
902 {
903         API_FCT(update_formspec);
904         API_FCT(set_formspec_prepend);
905         API_FCT(set_clouds);
906         API_FCT(get_textlist_index);
907         API_FCT(get_table_index);
908         API_FCT(get_worlds);
909         API_FCT(get_games);
910         API_FCT(get_content_info);
911         API_FCT(start);
912         API_FCT(close);
913         API_FCT(show_keys_menu);
914         API_FCT(create_world);
915         API_FCT(delete_world);
916         API_FCT(set_background);
917         API_FCT(set_topleft_text);
918         API_FCT(get_mapgen_names);
919         API_FCT(get_user_path);
920         API_FCT(get_modpath);
921         API_FCT(get_modpaths);
922         API_FCT(get_clientmodpath);
923         API_FCT(get_gamepath);
924         API_FCT(get_texturepath);
925         API_FCT(get_texturepath_share);
926         API_FCT(get_cache_path);
927         API_FCT(get_temp_path);
928         API_FCT(create_dir);
929         API_FCT(delete_dir);
930         API_FCT(copy_dir);
931         API_FCT(is_dir);
932         API_FCT(extract_zip);
933         API_FCT(may_modify_path);
934         API_FCT(get_mainmenu_path);
935         API_FCT(show_path_select_dialog);
936         API_FCT(download_file);
937         API_FCT(gettext);
938         API_FCT(get_video_drivers);
939         API_FCT(get_screen_info);
940         API_FCT(get_min_supp_proto);
941         API_FCT(get_max_supp_proto);
942         API_FCT(open_url);
943         API_FCT(open_dir);
944         API_FCT(do_async_callback);
945 }
946
947 /******************************************************************************/
948 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
949 {
950         API_FCT(get_worlds);
951         API_FCT(get_games);
952         API_FCT(get_mapgen_names);
953         API_FCT(get_user_path);
954         API_FCT(get_modpath);
955         API_FCT(get_modpaths);
956         API_FCT(get_clientmodpath);
957         API_FCT(get_gamepath);
958         API_FCT(get_texturepath);
959         API_FCT(get_texturepath_share);
960         API_FCT(get_cache_path);
961         API_FCT(get_temp_path);
962         API_FCT(create_dir);
963         API_FCT(delete_dir);
964         API_FCT(copy_dir);
965         API_FCT(is_dir);
966         API_FCT(extract_zip);
967         API_FCT(may_modify_path);
968         API_FCT(download_file);
969         API_FCT(get_min_supp_proto);
970         API_FCT(get_max_supp_proto);
971         API_FCT(gettext);
972 }