3 Copyright (C) 2013 sapier
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "lua_api/l_mainmenu.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_content.h"
23 #include "cpp_api/s_async.h"
24 #include "scripting_mainmenu.h"
25 #include "gui/guiEngine.h"
26 #include "gui/guiMainMenu.h"
27 #include "gui/guiKeyChangeMenu.h"
28 #include "gui/guiPathSelectMenu.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"
38 #include "client/client.h"
39 #include "client/renderingengine.h"
40 #include "network/networkprotocol.h"
41 #include "content/mod_configuration.h"
42 #include "threading/mutex_auto_lock.h"
43 #include "common/c_converter.h"
45 /******************************************************************************/
46 std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
48 lua_getglobal(L, "gamedata");
50 lua_getfield(L, -1, name.c_str());
55 return luaL_checkstring(L, -1);
58 /******************************************************************************/
59 int ModApiMainMenu::getIntegerData(lua_State *L, std::string name,bool& valid)
61 lua_getglobal(L, "gamedata");
63 lua_getfield(L, -1, name.c_str());
65 if(lua_isnil(L, -1)) {
71 return luaL_checkinteger(L, -1);
74 /******************************************************************************/
75 int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid)
77 lua_getglobal(L, "gamedata");
79 lua_getfield(L, -1, name.c_str());
81 if(lua_isnil(L, -1)) {
87 return readParam<bool>(L, -1);
90 /******************************************************************************/
91 int ModApiMainMenu::l_update_formspec(lua_State *L)
93 GUIEngine* engine = getGuiEngine(L);
94 sanity_check(engine != NULL);
96 if (engine->m_startgame)
100 std::string formspec(luaL_checkstring(L, 1));
102 if (engine->m_formspecgui != 0) {
103 engine->m_formspecgui->setForm(formspec);
109 /******************************************************************************/
110 int ModApiMainMenu::l_set_formspec_prepend(lua_State *L)
112 GUIEngine *engine = getGuiEngine(L);
113 sanity_check(engine != NULL);
115 if (engine->m_startgame)
118 std::string formspec(luaL_checkstring(L, 1));
119 engine->setFormspecPrepend(formspec);
124 /******************************************************************************/
125 int ModApiMainMenu::l_start(lua_State *L)
127 GUIEngine* engine = getGuiEngine(L);
128 sanity_check(engine != NULL);
130 //update c++ gamedata from lua table
134 MainMenuData *data = engine->m_data;
136 data->selected_world = getIntegerData(L, "selected_world",valid) -1;
137 data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid);
138 data->do_reconnect = getBoolData(L, "do_reconnect", valid);
139 if (!data->do_reconnect) {
140 data->name = getTextData(L,"playername");
141 data->password = getTextData(L,"password");
142 data->address = getTextData(L,"address");
143 data->port = getTextData(L,"port");
145 const auto val = getTextData(L, "allow_login_or_register");
147 data->allow_login_or_register = ELoginRegister::Login;
148 else if (val == "register")
149 data->allow_login_or_register = ELoginRegister::Register;
151 data->allow_login_or_register = ELoginRegister::Any;
153 data->serverdescription = getTextData(L,"serverdescription");
154 data->servername = getTextData(L,"servername");
156 //close menu next time
157 engine->m_startgame = true;
161 /******************************************************************************/
162 int ModApiMainMenu::l_close(lua_State *L)
164 GUIEngine* engine = getGuiEngine(L);
165 sanity_check(engine != NULL);
167 engine->m_kill = true;
171 /******************************************************************************/
172 int ModApiMainMenu::l_set_background(lua_State *L)
174 GUIEngine* engine = getGuiEngine(L);
175 sanity_check(engine != NULL);
177 std::string backgroundlevel(luaL_checkstring(L, 1));
178 std::string texturename(luaL_checkstring(L, 2));
180 bool tile_image = false;
182 unsigned int minsize = 16;
184 if (!lua_isnone(L, 3)) {
185 tile_image = readParam<bool>(L, 3);
188 if (!lua_isnone(L, 4)) {
189 minsize = lua_tonumber(L, 4);
192 if (backgroundlevel == "background") {
193 retval |= engine->setTexture(TEX_LAYER_BACKGROUND, texturename,
194 tile_image, minsize);
197 if (backgroundlevel == "overlay") {
198 retval |= engine->setTexture(TEX_LAYER_OVERLAY, texturename,
199 tile_image, minsize);
202 if (backgroundlevel == "header") {
203 retval |= engine->setTexture(TEX_LAYER_HEADER, texturename,
204 tile_image, minsize);
207 if (backgroundlevel == "footer") {
208 retval |= engine->setTexture(TEX_LAYER_FOOTER, texturename,
209 tile_image, minsize);
212 lua_pushboolean(L,retval);
216 /******************************************************************************/
217 int ModApiMainMenu::l_set_clouds(lua_State *L)
219 GUIEngine* engine = getGuiEngine(L);
220 sanity_check(engine != NULL);
222 bool value = readParam<bool>(L,1);
224 engine->m_clouds_enabled = value;
229 /******************************************************************************/
230 int ModApiMainMenu::l_get_textlist_index(lua_State *L)
232 // get_table_index accepts both tables and textlists
233 return l_get_table_index(L);
236 /******************************************************************************/
237 int ModApiMainMenu::l_get_table_index(lua_State *L)
239 GUIEngine* engine = getGuiEngine(L);
240 sanity_check(engine != NULL);
242 std::string tablename(luaL_checkstring(L, 1));
243 GUITable *table = engine->m_menu->getTable(tablename);
244 s32 selection = table ? table->getSelected() : 0;
247 lua_pushinteger(L, selection);
253 /******************************************************************************/
254 int ModApiMainMenu::l_get_worlds(lua_State *L)
256 std::vector<WorldSpec> worlds = getAvailableWorlds();
259 int top = lua_gettop(L);
260 unsigned int index = 1;
262 for (const WorldSpec &world : worlds) {
263 lua_pushnumber(L,index);
266 int top_lvl2 = lua_gettop(L);
268 lua_pushstring(L,"path");
269 lua_pushstring(L, world.path.c_str());
270 lua_settable(L, top_lvl2);
272 lua_pushstring(L,"name");
273 lua_pushstring(L, world.name.c_str());
274 lua_settable(L, top_lvl2);
276 lua_pushstring(L,"gameid");
277 lua_pushstring(L, world.gameid.c_str());
278 lua_settable(L, top_lvl2);
280 lua_settable(L, top);
286 /******************************************************************************/
287 int ModApiMainMenu::l_get_games(lua_State *L)
289 std::vector<SubgameSpec> games = getAvailableGames();
292 int top = lua_gettop(L);
293 unsigned int index = 1;
295 for (const SubgameSpec &game : games) {
296 lua_pushnumber(L, index);
298 int top_lvl2 = lua_gettop(L);
300 lua_pushstring(L, "id");
301 lua_pushstring(L, game.id.c_str());
302 lua_settable(L, top_lvl2);
304 lua_pushstring(L, "path");
305 lua_pushstring(L, game.path.c_str());
306 lua_settable(L, top_lvl2);
308 lua_pushstring(L, "type");
309 lua_pushstring(L, "game");
310 lua_settable(L, top_lvl2);
312 lua_pushstring(L, "gamemods_path");
313 lua_pushstring(L, game.gamemods_path.c_str());
314 lua_settable(L, top_lvl2);
316 lua_pushstring(L, "name");
317 lua_pushstring(L, game.title.c_str());
318 lua_settable(L, top_lvl2);
320 lua_pushstring(L, "title");
321 lua_pushstring(L, game.title.c_str());
322 lua_settable(L, top_lvl2);
324 lua_pushstring(L, "author");
325 lua_pushstring(L, game.author.c_str());
326 lua_settable(L, top_lvl2);
328 lua_pushstring(L, "release");
329 lua_pushinteger(L, game.release);
330 lua_settable(L, top_lvl2);
332 lua_pushstring(L, "menuicon_path");
333 lua_pushstring(L, game.menuicon_path.c_str());
334 lua_settable(L, top_lvl2);
336 lua_pushstring(L, "addon_mods_paths");
338 int table2 = lua_gettop(L);
339 int internal_index = 1;
340 for (const auto &addon_mods_path : game.addon_mods_paths) {
341 lua_pushnumber(L, internal_index);
342 lua_pushstring(L, addon_mods_path.second.c_str());
343 lua_settable(L, table2);
346 lua_settable(L, top_lvl2);
347 lua_settable(L, top);
353 /******************************************************************************/
354 int ModApiMainMenu::l_get_content_info(lua_State *L)
356 std::string path = luaL_checkstring(L, 1);
360 parseContentInfo(spec);
364 lua_pushstring(L, spec.name.c_str());
365 lua_setfield(L, -2, "name");
367 lua_pushstring(L, spec.type.c_str());
368 lua_setfield(L, -2, "type");
370 lua_pushstring(L, spec.author.c_str());
371 lua_setfield(L, -2, "author");
373 if (!spec.title.empty()) {
374 lua_pushstring(L, spec.title.c_str());
375 lua_setfield(L, -2, "title");
378 lua_pushinteger(L, spec.release);
379 lua_setfield(L, -2, "release");
381 lua_pushstring(L, spec.desc.c_str());
382 lua_setfield(L, -2, "description");
384 lua_pushstring(L, spec.path.c_str());
385 lua_setfield(L, -2, "path");
387 if (spec.type == "mod") {
390 parseModContents(spec);
395 for (const auto &dep : spec.depends) {
396 lua_pushstring(L, dep.c_str());
397 lua_rawseti(L, -2, i++);
399 lua_setfield(L, -2, "depends");
401 // Optional Dependencies
404 for (const auto &dep : spec.optdepends) {
405 lua_pushstring(L, dep.c_str());
406 lua_rawseti(L, -2, i++);
408 lua_setfield(L, -2, "optional_depends");
414 /******************************************************************************/
415 int ModApiMainMenu::l_check_mod_configuration(lua_State *L)
417 std::string worldpath = luaL_checkstring(L, 1);
419 ModConfiguration modmgr;
422 SubgameSpec gamespec = findWorldSubgame(worldpath);
423 modmgr.addGameMods(gamespec);
424 modmgr.addModsInPath(worldpath + DIR_DELIM + "worldmods", "worldmods");
426 // Add user-configured mods
427 std::vector<ModSpec> modSpecs;
429 luaL_checktype(L, 2, LUA_TTABLE);
432 while (lua_next(L, 2)) {
433 // Ignore non-string keys
434 if (lua_type(L, -2) != LUA_TSTRING) {
436 "Unexpected non-string key in table passed to "
437 "core.check_mod_configuration");
440 std::string modpath = luaL_checkstring(L, -1);
442 std::string virtual_path = lua_tostring(L, -1);
444 modSpecs.emplace_back();
445 ModSpec &spec = modSpecs.back();
446 spec.name = fs::GetFilenameFromPath(modpath.c_str());
448 spec.virtual_path = virtual_path;
449 if (!parseModContents(spec)) {
450 throw LuaError("Not a mod!");
454 modmgr.addMods(modSpecs);
456 modmgr.checkConflictsAndDeps();
457 } catch (const ModError &err) {
458 errorstream << err.what() << std::endl;
462 lua_pushboolean(L, false);
463 lua_setfield(L, -2, "is_consistent");
466 lua_setfield(L, -2, "unsatisfied_mods");
469 lua_setfield(L, -2, "satisfied_mods");
471 lua_pushstring(L, err.what());
472 lua_setfield(L, -2, "error_message");
479 lua_pushboolean(L, modmgr.isConsistent());
480 lua_setfield(L, -2, "is_consistent");
483 int top = lua_gettop(L);
484 unsigned int index = 1;
485 for (const auto &spec : modmgr.getUnsatisfiedMods()) {
486 lua_pushnumber(L, index);
487 push_mod_spec(L, spec, true);
488 lua_settable(L, top);
492 lua_setfield(L, -2, "unsatisfied_mods");
497 for (const auto &spec : modmgr.getMods()) {
498 lua_pushnumber(L, index);
499 push_mod_spec(L, spec, false);
500 lua_settable(L, top);
503 lua_setfield(L, -2, "satisfied_mods");
508 /******************************************************************************/
509 int ModApiMainMenu::l_show_keys_menu(lua_State *L)
511 GUIEngine* engine = getGuiEngine(L);
512 sanity_check(engine != NULL);
514 GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(
515 engine->m_rendering_engine->get_gui_env(),
518 engine->m_menumanager,
519 engine->m_texture_source);
524 /******************************************************************************/
525 int ModApiMainMenu::l_create_world(lua_State *L)
527 const char *name = luaL_checkstring(L, 1);
528 const char *gameid = luaL_checkstring(L, 2);
530 StringMap use_settings;
531 luaL_checktype(L, 3, LUA_TTABLE);
533 while (lua_next(L, 3) != 0) {
534 // key at index -2 and value at index -1
535 use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
540 std::string path = porting::path_user + DIR_DELIM
542 + sanitizeDirName(name, "world_");
544 std::vector<SubgameSpec> games = getAvailableGames();
545 auto game_it = std::find_if(games.begin(), games.end(), [gameid] (const SubgameSpec &spec) {
546 return spec.id == gameid;
548 if (game_it == games.end()) {
549 lua_pushstring(L, "Game ID not found");
553 // Set the settings for world creation
554 // this is a bad hack but the best we have right now..
556 for (auto it : use_settings) {
557 if (g_settings->existsLocal(it.first))
558 backup[it.first] = g_settings->get(it.first);
559 g_settings->set(it.first, it.second);
562 // Create world if it doesn't exist
564 loadGameConfAndInitWorld(path, name, *game_it, true);
566 } catch (const BaseException &e) {
567 auto err = std::string("Failed to initialize world: ") + e.what();
568 lua_pushstring(L, err.c_str());
571 // Restore previous settings
572 for (auto it : use_settings) {
573 auto it2 = backup.find(it.first);
574 if (it2 == backup.end())
575 g_settings->remove(it.first); // wasn't set before
577 g_settings->set(it.first, it2->second); // was set before
583 /******************************************************************************/
584 int ModApiMainMenu::l_delete_world(lua_State *L)
586 int world_id = luaL_checkinteger(L, 1) - 1;
587 std::vector<WorldSpec> worlds = getAvailableWorlds();
588 if (world_id < 0 || world_id >= (int) worlds.size()) {
589 lua_pushstring(L, "Invalid world index");
592 const WorldSpec &spec = worlds[world_id];
593 if (!fs::RecursiveDelete(spec.path)) {
594 lua_pushstring(L, "Failed to delete world");
600 /******************************************************************************/
601 int ModApiMainMenu::l_set_topleft_text(lua_State *L)
603 GUIEngine* engine = getGuiEngine(L);
604 sanity_check(engine != NULL);
608 if (!lua_isnone(L,1) && !lua_isnil(L,1))
609 text = luaL_checkstring(L, 1);
611 engine->setTopleftText(text);
615 /******************************************************************************/
616 int ModApiMainMenu::l_get_mapgen_names(lua_State *L)
618 std::vector<const char *> names;
619 bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
620 Mapgen::getMapgenNames(&names, include_hidden);
623 for (size_t i = 0; i != names.size(); i++) {
624 lua_pushstring(L, names[i]);
625 lua_rawseti(L, -2, i + 1);
632 /******************************************************************************/
633 int ModApiMainMenu::l_get_user_path(lua_State *L)
635 std::string path = fs::RemoveRelativePathComponents(porting::path_user);
636 lua_pushstring(L, path.c_str());
640 /******************************************************************************/
641 int ModApiMainMenu::l_get_modpath(lua_State *L)
643 std::string modpath = fs::RemoveRelativePathComponents(
644 porting::path_user + DIR_DELIM + "mods" + DIR_DELIM);
645 lua_pushstring(L, modpath.c_str());
649 /******************************************************************************/
650 int ModApiMainMenu::l_get_modpaths(lua_State *L)
654 ModApiMainMenu::l_get_modpath(L);
655 lua_setfield(L, -2, "mods");
657 for (const std::string &component : getEnvModPaths()) {
658 lua_pushstring(L, component.c_str());
659 lua_setfield(L, -2, fs::AbsolutePath(component).c_str());
664 /******************************************************************************/
665 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
667 std::string modpath = fs::RemoveRelativePathComponents(
668 porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM);
669 lua_pushstring(L, modpath.c_str());
673 /******************************************************************************/
674 int ModApiMainMenu::l_get_gamepath(lua_State *L)
676 std::string gamepath = fs::RemoveRelativePathComponents(
677 porting::path_user + DIR_DELIM + "games" + DIR_DELIM);
678 lua_pushstring(L, gamepath.c_str());
682 /******************************************************************************/
683 int ModApiMainMenu::l_get_texturepath(lua_State *L)
685 std::string gamepath = fs::RemoveRelativePathComponents(
686 porting::path_user + DIR_DELIM + "textures");
687 lua_pushstring(L, gamepath.c_str());
691 /******************************************************************************/
692 int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
694 std::string gamepath = fs::RemoveRelativePathComponents(
695 porting::path_share + DIR_DELIM + "textures");
696 lua_pushstring(L, gamepath.c_str());
700 /******************************************************************************/
701 int ModApiMainMenu::l_get_cache_path(lua_State *L)
703 lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
707 /******************************************************************************/
708 int ModApiMainMenu::l_get_temp_path(lua_State *L)
710 if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
711 lua_pushstring(L, fs::TempPath().c_str());
713 lua_pushstring(L, fs::CreateTempFile().c_str());
717 /******************************************************************************/
718 int ModApiMainMenu::l_create_dir(lua_State *L) {
719 const char *path = luaL_checkstring(L, 1);
721 if (ModApiMainMenu::mayModifyPath(path)) {
722 lua_pushboolean(L, fs::CreateAllDirs(path));
726 lua_pushboolean(L, false);
730 /******************************************************************************/
731 int ModApiMainMenu::l_delete_dir(lua_State *L)
733 const char *path = luaL_checkstring(L, 1);
735 std::string absolute_path = fs::RemoveRelativePathComponents(path);
737 if (ModApiMainMenu::mayModifyPath(absolute_path)) {
738 lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
742 lua_pushboolean(L, false);
746 /******************************************************************************/
747 int ModApiMainMenu::l_copy_dir(lua_State *L)
749 const char *source = luaL_checkstring(L, 1);
750 const char *destination = luaL_checkstring(L, 2);
752 bool keep_source = true;
753 if (!lua_isnoneornil(L, 3))
754 keep_source = readParam<bool>(L, 3);
756 std::string abs_destination = fs::RemoveRelativePathComponents(destination);
757 std::string abs_source = fs::RemoveRelativePathComponents(source);
759 if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
760 (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
761 lua_pushboolean(L, false);
767 retval = fs::CopyDir(abs_source, abs_destination);
769 retval = fs::MoveDir(abs_source, abs_destination);
770 lua_pushboolean(L, retval);
774 /******************************************************************************/
775 int ModApiMainMenu::l_is_dir(lua_State *L)
777 const char *path = luaL_checkstring(L, 1);
779 lua_pushboolean(L, fs::IsDir(path));
783 /******************************************************************************/
784 int ModApiMainMenu::l_extract_zip(lua_State *L)
786 const char *zipfile = luaL_checkstring(L, 1);
787 const char *destination = luaL_checkstring(L, 2);
789 std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
791 if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
792 auto fs = RenderingEngine::get_raw_device()->getFileSystem();
793 bool ok = fs::extractZipFile(fs, zipfile, destination);
794 lua_pushboolean(L, ok);
798 lua_pushboolean(L,false);
802 /******************************************************************************/
803 int ModApiMainMenu::l_get_mainmenu_path(lua_State *L)
805 GUIEngine* engine = getGuiEngine(L);
806 sanity_check(engine != NULL);
808 lua_pushstring(L,engine->getScriptDir().c_str());
812 /******************************************************************************/
813 bool ModApiMainMenu::mayModifyPath(std::string path)
815 path = fs::RemoveRelativePathComponents(path);
817 if (fs::PathStartsWith(path, fs::TempPath()))
820 std::string path_user = fs::RemoveRelativePathComponents(porting::path_user);
822 if (fs::PathStartsWith(path, path_user + DIR_DELIM "client"))
824 if (fs::PathStartsWith(path, path_user + DIR_DELIM "games"))
826 if (fs::PathStartsWith(path, path_user + DIR_DELIM "mods"))
828 if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures"))
830 if (fs::PathStartsWith(path, path_user + DIR_DELIM "worlds"))
833 if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_cache)))
840 /******************************************************************************/
841 int ModApiMainMenu::l_may_modify_path(lua_State *L)
843 const char *target = luaL_checkstring(L, 1);
844 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
845 lua_pushboolean(L, ModApiMainMenu::mayModifyPath(absolute_destination));
849 /******************************************************************************/
850 int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
852 GUIEngine* engine = getGuiEngine(L);
853 sanity_check(engine != NULL);
855 const char *formname= luaL_checkstring(L, 1);
856 const char *title = luaL_checkstring(L, 2);
857 bool is_file_select = readParam<bool>(L, 3);
859 GUIFileSelectMenu* fileOpenMenu =
860 new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(),
863 engine->m_menumanager,
867 fileOpenMenu->setTextDest(engine->m_buttonhandler);
868 fileOpenMenu->drop();
872 /******************************************************************************/
873 int ModApiMainMenu::l_download_file(lua_State *L)
875 const char *url = luaL_checkstring(L, 1);
876 const char *target = luaL_checkstring(L, 2);
879 std::string absolute_destination = fs::RemoveRelativePathComponents(target);
881 if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
882 if (GUIEngine::downloadFile(url,absolute_destination)) {
883 lua_pushboolean(L,true);
887 errorstream << "DOWNLOAD denied: " << absolute_destination
888 << " isn't an allowed path" << std::endl;
890 lua_pushboolean(L,false);
894 /******************************************************************************/
895 int ModApiMainMenu::l_get_video_drivers(lua_State *L)
897 std::vector<irr::video::E_DRIVER_TYPE> drivers = RenderingEngine::getSupportedVideoDrivers();
900 for (u32 i = 0; i != drivers.size(); i++) {
901 auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]);
904 lua_pushstring(L, info.name.c_str());
905 lua_setfield(L, -2, "name");
906 lua_pushstring(L, info.friendly_name.c_str());
907 lua_setfield(L, -2, "friendly_name");
909 lua_rawseti(L, -2, i + 1);
915 /******************************************************************************/
916 int ModApiMainMenu::l_gettext(lua_State *L)
918 const char *srctext = luaL_checkstring(L, 1);
919 const char *text = *srctext ? gettext(srctext) : "";
920 lua_pushstring(L, text);
925 /******************************************************************************/
926 int ModApiMainMenu::l_get_window_info(lua_State *L)
929 int top = lua_gettop(L);
931 const v2u32 &window_size = RenderingEngine::getWindowSize();
932 f32 density = RenderingEngine::getDisplayDensity();
933 f32 gui_scaling = g_settings->getFloat("gui_scaling") * density;
934 f32 hud_scaling = g_settings->getFloat("hud_scaling") * density;
936 lua_pushstring(L, "size");
937 push_v2u32(L, window_size);
938 lua_settable(L, top);
940 lua_pushstring(L, "max_formspec_size");
941 push_v2f(L, ClientDynamicInfo::calculateMaxFSSize(window_size));
942 lua_settable(L, top);
944 lua_pushstring(L, "real_gui_scaling");
945 lua_pushnumber(L, gui_scaling);
946 lua_settable(L, top);
948 lua_pushstring(L, "real_hud_scaling");
949 lua_pushnumber(L, hud_scaling);
950 lua_settable(L, top);
955 /******************************************************************************/
957 int ModApiMainMenu::l_get_active_renderer(lua_State *L)
959 lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str());
963 /******************************************************************************/
964 int ModApiMainMenu::l_get_min_supp_proto(lua_State *L)
966 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN);
970 int ModApiMainMenu::l_get_max_supp_proto(lua_State *L)
972 lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX);
976 /******************************************************************************/
977 int ModApiMainMenu::l_open_url(lua_State *L)
979 std::string url = luaL_checkstring(L, 1);
980 lua_pushboolean(L, porting::open_url(url));
984 /******************************************************************************/
985 int ModApiMainMenu::l_open_dir(lua_State *L)
987 std::string path = luaL_checkstring(L, 1);
988 lua_pushboolean(L, porting::open_directory(path));
992 /******************************************************************************/
993 int ModApiMainMenu::l_share_file(lua_State *L)
996 std::string path = luaL_checkstring(L, 1);
997 porting::shareFileAndroid(path);
998 lua_pushboolean(L, true);
1000 lua_pushboolean(L, false);
1005 /******************************************************************************/
1006 int ModApiMainMenu::l_do_async_callback(lua_State *L)
1008 MainMenuScripting *script = getScriptApi<MainMenuScripting>(L);
1010 size_t func_length, param_length;
1011 const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length);
1012 const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length);
1014 sanity_check(serialized_func_raw != NULL);
1015 sanity_check(serialized_param_raw != NULL);
1017 u32 jobId = script->queueAsync(
1018 std::string(serialized_func_raw, func_length),
1019 std::string(serialized_param_raw, param_length));
1021 lua_pushinteger(L, jobId);
1026 /******************************************************************************/
1027 // this is intentionally a global and not part of MainMenuScripting or such
1029 std::unordered_map<std::string, std::string> once_values;
1030 std::mutex once_mutex;
1033 int ModApiMainMenu::l_set_once(lua_State *L)
1035 std::string key = readParam<std::string>(L, 1);
1036 if (lua_isnil(L, 2))
1038 std::string value = readParam<std::string>(L, 2);
1041 MutexAutoLock lock(once_mutex);
1042 once_values[key] = value;
1048 int ModApiMainMenu::l_get_once(lua_State *L)
1050 std::string key = readParam<std::string>(L, 1);
1053 MutexAutoLock lock(once_mutex);
1054 auto it = once_values.find(key);
1055 if (it == once_values.end())
1058 lua_pushstring(L, it->second.c_str());
1064 /******************************************************************************/
1065 void ModApiMainMenu::Initialize(lua_State *L, int top)
1067 API_FCT(update_formspec);
1068 API_FCT(set_formspec_prepend);
1069 API_FCT(set_clouds);
1070 API_FCT(get_textlist_index);
1071 API_FCT(get_table_index);
1072 API_FCT(get_worlds);
1074 API_FCT(get_content_info);
1075 API_FCT(check_mod_configuration);
1078 API_FCT(show_keys_menu);
1079 API_FCT(create_world);
1080 API_FCT(delete_world);
1081 API_FCT(set_background);
1082 API_FCT(set_topleft_text);
1083 API_FCT(get_mapgen_names);
1084 API_FCT(get_user_path);
1085 API_FCT(get_modpath);
1086 API_FCT(get_modpaths);
1087 API_FCT(get_clientmodpath);
1088 API_FCT(get_gamepath);
1089 API_FCT(get_texturepath);
1090 API_FCT(get_texturepath_share);
1091 API_FCT(get_cache_path);
1092 API_FCT(get_temp_path);
1093 API_FCT(create_dir);
1094 API_FCT(delete_dir);
1097 API_FCT(extract_zip);
1098 API_FCT(may_modify_path);
1099 API_FCT(get_mainmenu_path);
1100 API_FCT(show_path_select_dialog);
1101 API_FCT(download_file);
1103 API_FCT(get_video_drivers);
1104 API_FCT(get_window_info);
1105 API_FCT(get_active_renderer);
1106 API_FCT(get_min_supp_proto);
1107 API_FCT(get_max_supp_proto);
1110 API_FCT(share_file);
1111 API_FCT(do_async_callback);
1116 /******************************************************************************/
1117 void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
1119 API_FCT(get_worlds);
1121 API_FCT(get_mapgen_names);
1122 API_FCT(get_user_path);
1123 API_FCT(get_modpath);
1124 API_FCT(get_modpaths);
1125 API_FCT(get_clientmodpath);
1126 API_FCT(get_gamepath);
1127 API_FCT(get_texturepath);
1128 API_FCT(get_texturepath_share);
1129 API_FCT(get_cache_path);
1130 API_FCT(get_temp_path);
1131 API_FCT(create_dir);
1132 API_FCT(delete_dir);
1135 API_FCT(extract_zip);
1136 API_FCT(may_modify_path);
1137 API_FCT(download_file);
1138 API_FCT(get_min_supp_proto);
1139 API_FCT(get_max_supp_proto);