X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=src%2Fscript%2Flua_api%2Fl_mainmenu.cpp;h=6e9a5c34f8b7d2d314d5509dd0bef3960595e121;hb=6a1424f2b18520f40ba8cfd12f7988f6b33db9a6;hp=f2628d0a10bbe946b52d4117bd075ac538e01df7;hpb=7b3602bab3a697a2729b057c5e8a354337482389;p=dragonfireclient.git diff --git a/src/script/lua_api/l_mainmenu.cpp b/src/script/lua_api/l_mainmenu.cpp index f2628d0a1..6e9a5c34f 100644 --- a/src/script/lua_api/l_mainmenu.cpp +++ b/src/script/lua_api/l_mainmenu.cpp @@ -21,22 +21,24 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "lua_api/l_internal.h" #include "common/c_content.h" #include "cpp_api/s_async.h" -#include "guiEngine.h" -#include "guiMainMenu.h" -#include "guiKeyChangeMenu.h" -#include "guiFileSelectMenu.h" -#include "subgame.h" +#include "scripting_mainmenu.h" +#include "gui/guiEngine.h" +#include "gui/guiMainMenu.h" +#include "gui/guiKeyChangeMenu.h" +#include "gui/guiPathSelectMenu.h" #include "version.h" #include "porting.h" #include "filesys.h" #include "convert_json.h" +#include "content/content.h" +#include "content/subgames.h" #include "serverlist.h" -#include "sound.h" +#include "mapgen/mapgen.h" #include "settings.h" -#include "main.h" // for g_settings +#include "client/client.h" +#include "client/renderingengine.h" +#include "network/networkprotocol.h" -#include -#include /******************************************************************************/ std::string ModApiMainMenu::getTextData(lua_State *L, std::string name) @@ -80,14 +82,14 @@ int ModApiMainMenu::getBoolData(lua_State *L, std::string name,bool& valid) } valid = true; - return lua_toboolean(L, -1); + return readParam(L, -1); } /******************************************************************************/ int ModApiMainMenu::l_update_formspec(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); if (engine->m_startgame) return 0; @@ -102,25 +104,44 @@ int ModApiMainMenu::l_update_formspec(lua_State *L) return 0; } +/******************************************************************************/ +int ModApiMainMenu::l_set_formspec_prepend(lua_State *L) +{ + GUIEngine *engine = getGuiEngine(L); + sanity_check(engine != NULL); + + if (engine->m_startgame) + return 0; + + std::string formspec(luaL_checkstring(L, 1)); + engine->setFormspecPrepend(formspec); + + return 0; +} + /******************************************************************************/ int ModApiMainMenu::l_start(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); //update c++ gamedata from lua table bool valid = false; + MainMenuData *data = engine->m_data; - engine->m_data->selected_world = getIntegerData(L, "selected_world",valid) -1; - engine->m_data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid); - engine->m_data->name = getTextData(L,"playername"); - engine->m_data->password = getTextData(L,"password"); - engine->m_data->address = getTextData(L,"address"); - engine->m_data->port = getTextData(L,"port"); - engine->m_data->serverdescription = getTextData(L,"serverdescription"); - engine->m_data->servername = getTextData(L,"servername"); + data->selected_world = getIntegerData(L, "selected_world",valid) -1; + data->simple_singleplayer_mode = getBoolData(L,"singleplayer",valid); + data->do_reconnect = getBoolData(L, "do_reconnect", valid); + if (!data->do_reconnect) { + data->name = getTextData(L,"playername"); + data->password = getTextData(L,"password"); + data->address = getTextData(L,"address"); + data->port = getTextData(L,"port"); + } + data->serverdescription = getTextData(L,"serverdescription"); + data->servername = getTextData(L,"servername"); //close menu next time engine->m_startgame = true; @@ -131,7 +152,7 @@ int ModApiMainMenu::l_start(lua_State *L) int ModApiMainMenu::l_close(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); engine->m_kill = true; return 0; @@ -141,7 +162,7 @@ int ModApiMainMenu::l_close(lua_State *L) int ModApiMainMenu::l_set_background(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); std::string backgroundlevel(luaL_checkstring(L, 1)); std::string texturename(luaL_checkstring(L, 2)); @@ -151,7 +172,7 @@ int ModApiMainMenu::l_set_background(lua_State *L) unsigned int minsize = 16; if (!lua_isnone(L, 3)) { - tile_image = lua_toboolean(L, 3); + tile_image = readParam(L, 3); } if (!lua_isnone(L, 4)) { @@ -186,9 +207,9 @@ int ModApiMainMenu::l_set_background(lua_State *L) int ModApiMainMenu::l_set_clouds(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); - bool value = lua_toboolean(L,1); + bool value = readParam(L,1); engine->m_clouds_enabled = value; @@ -206,9 +227,9 @@ int ModApiMainMenu::l_get_textlist_index(lua_State *L) int ModApiMainMenu::l_get_table_index(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); - std::wstring tablename(narrow_to_wide(luaL_checkstring(L, 1))); + std::string tablename(luaL_checkstring(L, 1)); GUITable *table = engine->m_menu->getTable(tablename); s32 selection = table ? table->getSelected() : 0; @@ -228,23 +249,22 @@ int ModApiMainMenu::l_get_worlds(lua_State *L) int top = lua_gettop(L); unsigned int index = 1; - for (unsigned int i = 0; i < worlds.size(); i++) - { + for (const WorldSpec &world : worlds) { lua_pushnumber(L,index); lua_newtable(L); int top_lvl2 = lua_gettop(L); lua_pushstring(L,"path"); - lua_pushstring(L,worlds[i].path.c_str()); + lua_pushstring(L, world.path.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"name"); - lua_pushstring(L,worlds[i].name.c_str()); + lua_pushstring(L, world.name.c_str()); lua_settable(L, top_lvl2); lua_pushstring(L,"gameid"); - lua_pushstring(L,worlds[i].gameid.c_str()); + lua_pushstring(L, world.gameid.c_str()); lua_settable(L, top_lvl2); lua_settable(L, top); @@ -262,41 +282,51 @@ int ModApiMainMenu::l_get_games(lua_State *L) int top = lua_gettop(L); unsigned int index = 1; - for (unsigned int i = 0; i < games.size(); i++) - { - lua_pushnumber(L,index); + for (const SubgameSpec &game : games) { + lua_pushnumber(L, index); lua_newtable(L); int top_lvl2 = lua_gettop(L); - lua_pushstring(L,"id"); - lua_pushstring(L,games[i].id.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, "id"); + lua_pushstring(L, game.id.c_str()); + lua_settable(L, top_lvl2); - lua_pushstring(L,"path"); - lua_pushstring(L,games[i].path.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, "path"); + lua_pushstring(L, game.path.c_str()); + lua_settable(L, top_lvl2); - lua_pushstring(L,"gamemods_path"); - lua_pushstring(L,games[i].gamemods_path.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, "type"); + lua_pushstring(L, "game"); + lua_settable(L, top_lvl2); - lua_pushstring(L,"name"); - lua_pushstring(L,games[i].name.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, "gamemods_path"); + lua_pushstring(L, game.gamemods_path.c_str()); + lua_settable(L, top_lvl2); - lua_pushstring(L,"menuicon_path"); - lua_pushstring(L,games[i].menuicon_path.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, "name"); + lua_pushstring(L, game.name.c_str()); + lua_settable(L, top_lvl2); + + lua_pushstring(L, "author"); + lua_pushstring(L, game.author.c_str()); + lua_settable(L, top_lvl2); + + lua_pushstring(L, "release"); + lua_pushinteger(L, game.release); + lua_settable(L, top_lvl2); + + lua_pushstring(L, "menuicon_path"); + lua_pushstring(L, game.menuicon_path.c_str()); + lua_settable(L, top_lvl2); - lua_pushstring(L,"addon_mods_paths"); + lua_pushstring(L, "addon_mods_paths"); lua_newtable(L); int table2 = lua_gettop(L); - int internal_index=1; - for (std::set::iterator iter = games[i].addon_mods_paths.begin(); - iter != games[i].addon_mods_paths.end(); iter++) { - lua_pushnumber(L,internal_index); - lua_pushstring(L,(*iter).c_str()); - lua_settable(L, table2); + int internal_index = 1; + for (const std::string &addon_mods_path : game.addon_mods_paths) { + lua_pushnumber(L, internal_index); + lua_pushstring(L, addon_mods_path.c_str()); + lua_settable(L, table2); internal_index++; } lua_settable(L, top_lvl2); @@ -305,315 +335,75 @@ int ModApiMainMenu::l_get_games(lua_State *L) } return 1; } -/******************************************************************************/ -int ModApiMainMenu::l_get_modstore_details(lua_State *L) -{ - const char *modid = luaL_checkstring(L, 1); - - if (modid != 0) { - Json::Value details; - std::string url = ""; - try{ - url = g_settings->get("modstore_details_url"); - } - catch(SettingNotFoundException &e) { - lua_pushnil(L); - return 1; - } - - size_t idpos = url.find("*"); - url.erase(idpos,1); - url.insert(idpos,modid); - - details = getModstoreUrl(url); - - ModStoreModDetails current_mod = readModStoreModDetails(details); - - if ( current_mod.valid) { - lua_newtable(L); - int top = lua_gettop(L); - - lua_pushstring(L,"id"); - lua_pushnumber(L,current_mod.id); - lua_settable(L, top); - - lua_pushstring(L,"title"); - lua_pushstring(L,current_mod.title.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"basename"); - lua_pushstring(L,current_mod.basename.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"description"); - lua_pushstring(L,current_mod.description.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"author"); - lua_pushstring(L,current_mod.author.username.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"download_url"); - lua_pushstring(L,current_mod.versions[0].file.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"versions"); - lua_newtable(L); - int versionstop = lua_gettop(L); - for (unsigned int i=0;i < current_mod.versions.size(); i++) { - lua_pushnumber(L,i+1); - lua_newtable(L); - int current_element = lua_gettop(L); - - lua_pushstring(L,"date"); - lua_pushstring(L,current_mod.versions[i].date.c_str()); - lua_settable(L,current_element); - - lua_pushstring(L,"download_url"); - lua_pushstring(L,current_mod.versions[i].file.c_str()); - lua_settable(L,current_element); - - lua_settable(L,versionstop); - } - lua_settable(L, top); - - lua_pushstring(L,"screenshot_url"); - lua_pushstring(L,current_mod.titlepic.file.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"license"); - lua_pushstring(L,current_mod.license.shortinfo.c_str()); - lua_settable(L, top); - - lua_pushstring(L,"rating"); - lua_pushnumber(L,current_mod.rating); - lua_settable(L, top); - - //TODO depends - - //TODO softdepends - return 1; - } - } - return 0; -} /******************************************************************************/ -int ModApiMainMenu::l_get_modstore_list(lua_State *L) +int ModApiMainMenu::l_get_content_info(lua_State *L) { - Json::Value mods; - std::string url = ""; - try{ - url = g_settings->get("modstore_listmods_url"); - } - catch(SettingNotFoundException &e) { - lua_pushnil(L); - return 1; - } - - mods = getModstoreUrl(url); + std::string path = luaL_checkstring(L, 1); - std::vector moddata = readModStoreList(mods); + ContentSpec spec; + spec.path = path; + parseContentInfo(spec); lua_newtable(L); - int top = lua_gettop(L); - unsigned int index = 1; - - for (unsigned int i = 0; i < moddata.size(); i++) - { - if (moddata[i].valid) { - lua_pushnumber(L,index); - lua_newtable(L); - - int top_lvl2 = lua_gettop(L); - lua_pushstring(L,"id"); - lua_pushnumber(L,moddata[i].id); - lua_settable(L, top_lvl2); + lua_pushstring(L, spec.name.c_str()); + lua_setfield(L, -2, "name"); - lua_pushstring(L,"title"); - lua_pushstring(L,moddata[i].title.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, spec.type.c_str()); + lua_setfield(L, -2, "type"); - lua_pushstring(L,"basename"); - lua_pushstring(L,moddata[i].basename.c_str()); - lua_settable(L, top_lvl2); + lua_pushstring(L, spec.author.c_str()); + lua_setfield(L, -2, "author"); - lua_settable(L, top); - index++; - } - } - return 1; -} + lua_pushinteger(L, spec.release); + lua_setfield(L, -2, "release"); -/******************************************************************************/ -int ModApiMainMenu::l_get_favorites(lua_State *L) -{ - std::string listtype = "local"; + lua_pushstring(L, spec.desc.c_str()); + lua_setfield(L, -2, "description"); - if (!lua_isnone(L,1)) { - listtype = luaL_checkstring(L,1); - } + lua_pushstring(L, spec.path.c_str()); + lua_setfield(L, -2, "path"); - std::vector servers; -#if USE_CURL - if(listtype == "online") { - servers = ServerList::getOnline(); - } else { - servers = ServerList::getLocal(); - } -#else - servers = ServerList::getLocal(); -#endif - - lua_newtable(L); - int top = lua_gettop(L); - unsigned int index = 1; - - for (unsigned int i = 0; i < servers.size(); i++) - { - lua_pushnumber(L,index); + if (spec.type == "mod") { + ModSpec spec; + spec.path = path; + parseModContents(spec); + // Dependencies lua_newtable(L); - int top_lvl2 = lua_gettop(L); - - if (servers[i]["clients"].asString().size()) { - std::string clients_raw = servers[i]["clients"].asString(); - char* endptr = 0; - int numbervalue = strtol(clients_raw.c_str(),&endptr,10); - - if ((clients_raw != "") && (*endptr == 0)) { - lua_pushstring(L,"clients"); - lua_pushnumber(L,numbervalue); - lua_settable(L, top_lvl2); - } - } - - if (servers[i]["clients_max"].asString().size()) { - - std::string clients_max_raw = servers[i]["clients_max"].asString(); - char* endptr = 0; - int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10); - - if ((clients_max_raw != "") && (*endptr == 0)) { - lua_pushstring(L,"clients_max"); - lua_pushnumber(L,numbervalue); - lua_settable(L, top_lvl2); - } - } - - if (servers[i]["version"].asString().size()) { - lua_pushstring(L,"version"); - std::string topush = servers[i]["version"].asString(); - lua_pushstring(L,topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (servers[i]["password"].asString().size()) { - lua_pushstring(L,"password"); - lua_pushboolean(L,true); - lua_settable(L, top_lvl2); + int i = 1; + for (const auto &dep : spec.depends) { + lua_pushstring(L, dep.c_str()); + lua_rawseti(L, -2, i++); } + lua_setfield(L, -2, "depends"); - if (servers[i]["creative"].asString().size()) { - lua_pushstring(L,"creative"); - lua_pushboolean(L,true); - lua_settable(L, top_lvl2); - } - - if (servers[i]["damage"].asString().size()) { - lua_pushstring(L,"damage"); - lua_pushboolean(L,true); - lua_settable(L, top_lvl2); - } - - if (servers[i]["pvp"].asString().size()) { - lua_pushstring(L,"pvp"); - lua_pushboolean(L,true); - lua_settable(L, top_lvl2); - } - - if (servers[i]["description"].asString().size()) { - lua_pushstring(L,"description"); - std::string topush = servers[i]["description"].asString(); - lua_pushstring(L,topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (servers[i]["name"].asString().size()) { - lua_pushstring(L,"name"); - std::string topush = servers[i]["name"].asString(); - lua_pushstring(L,topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (servers[i]["address"].asString().size()) { - lua_pushstring(L,"address"); - std::string topush = servers[i]["address"].asString(); - lua_pushstring(L,topush.c_str()); - lua_settable(L, top_lvl2); - } - - if (servers[i]["port"].asString().size()) { - lua_pushstring(L,"port"); - std::string topush = servers[i]["port"].asString(); - lua_pushstring(L,topush.c_str()); - lua_settable(L, top_lvl2); + // Optional Dependencies + lua_newtable(L); + i = 1; + for (const auto &dep : spec.optdepends) { + lua_pushstring(L, dep.c_str()); + lua_rawseti(L, -2, i++); } - - lua_settable(L, top); - index++; - } - return 1; -} - -/******************************************************************************/ -int ModApiMainMenu::l_delete_favorite(lua_State *L) -{ - std::vector servers; - - std::string listtype = "local"; - - if (!lua_isnone(L,2)) { - listtype = luaL_checkstring(L,2); - } - - if ((listtype != "local") && - (listtype != "online")) - return 0; - -#if USE_CURL - if(listtype == "online") { - servers = ServerList::getOnline(); - } else { - servers = ServerList::getLocal(); - } -#else - servers = ServerList::getLocal(); -#endif - - int fav_idx = luaL_checkinteger(L,1) -1; - - if ((fav_idx >= 0) && - (fav_idx < (int) servers.size())) { - - ServerList::deleteEntry(servers[fav_idx]); + lua_setfield(L, -2, "optional_depends"); } - return 0; + return 1; } /******************************************************************************/ int ModApiMainMenu::l_show_keys_menu(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); - - GUIKeyChangeMenu *kmenu - = new GUIKeyChangeMenu( engine->m_device->getGUIEnvironment(), - engine->m_parent, - -1, - engine->m_menumanager); + sanity_check(engine != NULL); + + GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu( + engine->m_rendering_engine->get_gui_env(), + engine->m_parent, + -1, + engine->m_menumanager, + engine->m_texture_source); kmenu->drop(); return 0; } @@ -626,7 +416,7 @@ int ModApiMainMenu::l_create_world(lua_State *L) std::string path = porting::path_user + DIR_DELIM "worlds" + DIR_DELIM - + name; + + sanitizeDirName(name, "world_"); std::vector games = getAvailableGames(); @@ -634,15 +424,13 @@ int ModApiMainMenu::l_create_world(lua_State *L) (gameidx < (int) games.size())) { // Create world if it doesn't exist - if(!initializeWorld(path, games[gameidx].id)){ - lua_pushstring(L, "Failed to initialize world"); - - } - else { - lua_pushnil(L); + try { + loadGameConfAndInitWorld(path, name, games[gameidx], true); + lua_pushnil(L); + } catch (const BaseException &e) { + lua_pushstring(L, (std::string("Failed to initialize world: ") + e.what()).c_str()); } - } - else { + } else { lua_pushstring(L, "Invalid game index"); } return 1; @@ -651,40 +439,27 @@ int ModApiMainMenu::l_create_world(lua_State *L) /******************************************************************************/ int ModApiMainMenu::l_delete_world(lua_State *L) { - int worldidx = luaL_checkinteger(L,1) -1; - + int world_id = luaL_checkinteger(L, 1) - 1; std::vector worlds = getAvailableWorlds(); - - if ((worldidx >= 0) && - (worldidx < (int) worlds.size())) { - - WorldSpec spec = worlds[worldidx]; - - std::vector paths; - paths.push_back(spec.path); - fs::GetRecursiveSubPaths(spec.path, paths); - - // Delete files - if (!fs::DeletePaths(paths)) { - lua_pushstring(L, "Failed to delete world"); - } - else { - lua_pushnil(L); - } - } - else { + if (world_id < 0 || world_id >= (int) worlds.size()) { lua_pushstring(L, "Invalid world index"); + return 1; } - return 1; + const WorldSpec &spec = worlds[world_id]; + if (!fs::RecursiveDelete(spec.path)) { + lua_pushstring(L, "Failed to delete world"); + return 1; + } + return 0; } /******************************************************************************/ int ModApiMainMenu::l_set_topleft_text(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); - std::string text = ""; + std::string text; if (!lua_isnone(L,1) && !lua_isnil(L,1)) text = luaL_checkstring(L, 1); @@ -693,11 +468,45 @@ int ModApiMainMenu::l_set_topleft_text(lua_State *L) return 0; } +/******************************************************************************/ +int ModApiMainMenu::l_get_mapgen_names(lua_State *L) +{ + std::vector names; + bool include_hidden = lua_isboolean(L, 1) && readParam(L, 1); + Mapgen::getMapgenNames(&names, include_hidden); + + lua_newtable(L); + for (size_t i = 0; i != names.size(); i++) { + lua_pushstring(L, names[i]); + lua_rawseti(L, -2, i + 1); + } + + return 1; +} + + +/******************************************************************************/ +int ModApiMainMenu::l_get_user_path(lua_State *L) +{ + std::string path = fs::RemoveRelativePathComponents(porting::path_user); + lua_pushstring(L, path.c_str()); + return 1; +} + /******************************************************************************/ int ModApiMainMenu::l_get_modpath(lua_State *L) { - std::string modpath - = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods" + DIR_DELIM); + std::string modpath = fs::RemoveRelativePathComponents( + porting::path_user + DIR_DELIM + "mods" + DIR_DELIM); + lua_pushstring(L, modpath.c_str()); + return 1; +} + +/******************************************************************************/ +int ModApiMainMenu::l_get_clientmodpath(lua_State *L) +{ + std::string modpath = fs::RemoveRelativePathComponents( + porting::path_user + DIR_DELIM + "clientmods" + DIR_DELIM); lua_pushstring(L, modpath.c_str()); return 1; } @@ -705,8 +514,8 @@ int ModApiMainMenu::l_get_modpath(lua_State *L) /******************************************************************************/ int ModApiMainMenu::l_get_gamepath(lua_State *L) { - std::string gamepath - = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "games" + DIR_DELIM); + std::string gamepath = fs::RemoveRelativePathComponents( + porting::path_user + DIR_DELIM + "games" + DIR_DELIM); lua_pushstring(L, gamepath.c_str()); return 1; } @@ -714,68 +523,61 @@ int ModApiMainMenu::l_get_gamepath(lua_State *L) /******************************************************************************/ int ModApiMainMenu::l_get_texturepath(lua_State *L) { - std::string gamepath - = fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "textures"); + std::string gamepath = fs::RemoveRelativePathComponents( + porting::path_user + DIR_DELIM + "textures"); lua_pushstring(L, gamepath.c_str()); return 1; } +/******************************************************************************/ int ModApiMainMenu::l_get_texturepath_share(lua_State *L) { - std::string gamepath - = fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "textures"); + std::string gamepath = fs::RemoveRelativePathComponents( + porting::path_share + DIR_DELIM + "textures"); lua_pushstring(L, gamepath.c_str()); return 1; } /******************************************************************************/ -int ModApiMainMenu::l_get_dirlist(lua_State *L) +int ModApiMainMenu::l_get_cache_path(lua_State *L) { - const char *path = luaL_checkstring(L, 1); - bool dironly = lua_toboolean(L, 2); - - std::vector dirlist = fs::GetDirListing(path); - - unsigned int index = 1; - lua_newtable(L); - int table = lua_gettop(L); - - for (unsigned int i=0;i< dirlist.size(); i++) { - if ((dirlist[i].dir) || (dironly == false)) { - lua_pushnumber(L,index); - lua_pushstring(L,dirlist[i].name.c_str()); - lua_settable(L, table); - index++; - } - } + lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str()); + return 1; +} +/******************************************************************************/ +int ModApiMainMenu::l_get_temp_path(lua_State *L) +{ + lua_pushstring(L, fs::TempPath().c_str()); return 1; } /******************************************************************************/ int ModApiMainMenu::l_create_dir(lua_State *L) { - const char *path = luaL_checkstring(L, 1); + const char *path = luaL_checkstring(L, 1); - if (ModApiMainMenu::isMinetestPath(path)) { - lua_pushboolean(L,fs::CreateAllDirs(path)); + if (ModApiMainMenu::mayModifyPath(path)) { + lua_pushboolean(L, fs::CreateAllDirs(path)); return 1; } - lua_pushboolean(L,false); + + lua_pushboolean(L, false); return 1; } /******************************************************************************/ int ModApiMainMenu::l_delete_dir(lua_State *L) { - const char *path = luaL_checkstring(L, 1); + const char *path = luaL_checkstring(L, 1); std::string absolute_path = fs::RemoveRelativePathComponents(path); - if (ModApiMainMenu::isMinetestPath(absolute_path)) { - lua_pushboolean(L,fs::RecursiveDelete(absolute_path)); + if (ModApiMainMenu::mayModifyPath(absolute_path)) { + lua_pushboolean(L, fs::RecursiveDelete(absolute_path)); return 1; } - lua_pushboolean(L,false); + + lua_pushboolean(L, false); return 1; } @@ -789,14 +591,13 @@ int ModApiMainMenu::l_copy_dir(lua_State *L) if ((!lua_isnone(L,3)) && (!lua_isnil(L,3))) { - keep_source = lua_toboolean(L,3); + keep_source = readParam(L,3); } std::string absolute_destination = fs::RemoveRelativePathComponents(destination); std::string absolute_source = fs::RemoveRelativePathComponents(source); - if ((ModApiMainMenu::isMinetestPath(absolute_source)) && - (ModApiMainMenu::isMinetestPath(absolute_destination))) { + if ((ModApiMainMenu::mayModifyPath(absolute_destination))) { bool retval = fs::CopyDir(absolute_source,absolute_destination); if (retval && (!keep_source)) { @@ -811,86 +612,26 @@ int ModApiMainMenu::l_copy_dir(lua_State *L) } /******************************************************************************/ -int ModApiMainMenu::l_extract_zip(lua_State *L) +int ModApiMainMenu::l_is_dir(lua_State *L) { - GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + const char *path = luaL_checkstring(L, 1); + + lua_pushboolean(L, fs::IsDir(path)); + return 1; +} +/******************************************************************************/ +int ModApiMainMenu::l_extract_zip(lua_State *L) +{ const char *zipfile = luaL_checkstring(L, 1); const char *destination = luaL_checkstring(L, 2); std::string absolute_destination = fs::RemoveRelativePathComponents(destination); - if (ModApiMainMenu::isMinetestPath(absolute_destination)) { + if (ModApiMainMenu::mayModifyPath(absolute_destination)) { + auto rendering_engine = getGuiEngine(L)->m_rendering_engine; fs::CreateAllDirs(absolute_destination); - - io::IFileSystem* fs = engine->m_device->getFileSystem(); - - if (!fs->addFileArchive(zipfile,true,false,io::EFAT_ZIP)) { - lua_pushboolean(L,false); - return 1; - } - - assert(fs->getFileArchiveCount() > 0); - - /**********************************************************************/ - /* WARNING this is not threadsafe!! */ - /**********************************************************************/ - io::IFileArchive* opened_zip = - fs->getFileArchive(fs->getFileArchiveCount()-1); - - const io::IFileList* files_in_zip = opened_zip->getFileList(); - - unsigned int number_of_files = files_in_zip->getFileCount(); - - for (unsigned int i=0; i < number_of_files; i++) { - std::string fullpath = destination; - fullpath += DIR_DELIM; - fullpath += files_in_zip->getFullFileName(i).c_str(); - - if (files_in_zip->isDirectory(i)) { - if (! fs::CreateAllDirs(fullpath) ) { - fs->removeFileArchive(fs->getFileArchiveCount()-1); - lua_pushboolean(L,false); - return 1; - } - } - else { - io::IReadFile* toread = opened_zip->createAndOpenFile(i); - - FILE *targetfile = fopen(fullpath.c_str(),"wb"); - - if (targetfile == NULL) { - fs->removeFileArchive(fs->getFileArchiveCount()-1); - lua_pushboolean(L,false); - return 1; - } - - char read_buffer[1024]; - unsigned int total_read = 0; - - while (total_read < toread->getSize()) { - - unsigned int bytes_read = - toread->read(read_buffer,sizeof(read_buffer)); - if ((bytes_read == 0 ) || - (fwrite(read_buffer, 1, bytes_read, targetfile) != bytes_read)) - { - fclose(targetfile); - fs->removeFileArchive(fs->getFileArchiveCount()-1); - lua_pushboolean(L,false); - return 1; - } - total_read += bytes_read; - } - - fclose(targetfile); - } - - } - - fs->removeFileArchive(fs->getFileArchiveCount()-1); - lua_pushboolean(L,true); + lua_pushboolean(L, fs::extractZipFile(rendering_engine->get_filesystem(), zipfile, destination)); return 1; } @@ -902,87 +643,70 @@ int ModApiMainMenu::l_extract_zip(lua_State *L) int ModApiMainMenu::l_get_mainmenu_path(lua_State *L) { GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); + sanity_check(engine != NULL); lua_pushstring(L,engine->getScriptDir().c_str()); return 1; } /******************************************************************************/ -bool ModApiMainMenu::isMinetestPath(std::string path) +bool ModApiMainMenu::mayModifyPath(std::string path) { - if (fs::PathStartsWith(path,fs::TempPath())) - return true; + path = fs::RemoveRelativePathComponents(path); - /* games */ - if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_share + DIR_DELIM + "games"))) + if (fs::PathStartsWith(path, fs::TempPath())) return true; - /* mods */ - if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "mods"))) - return true; + std::string path_user = fs::RemoveRelativePathComponents(porting::path_user); - /* worlds */ - if (fs::PathStartsWith(path,fs::RemoveRelativePathComponents(porting::path_user + DIR_DELIM + "worlds"))) + if (fs::PathStartsWith(path, path_user + DIR_DELIM "client")) + return true; + if (fs::PathStartsWith(path, path_user + DIR_DELIM "games")) + return true; + if (fs::PathStartsWith(path, path_user + DIR_DELIM "mods")) + return true; + if (fs::PathStartsWith(path, path_user + DIR_DELIM "textures")) + return true; + if (fs::PathStartsWith(path, path_user + DIR_DELIM "worlds")) return true; + if (fs::PathStartsWith(path, fs::RemoveRelativePathComponents(porting::path_cache))) + return true; return false; } -/******************************************************************************/ -int ModApiMainMenu::l_show_file_open_dialog(lua_State *L) -{ - GUIEngine* engine = getGuiEngine(L); - assert(engine != 0); - - const char *formname= luaL_checkstring(L, 1); - const char *title = luaL_checkstring(L, 2); - - GUIFileSelectMenu* fileOpenMenu = - new GUIFileSelectMenu(engine->m_device->getGUIEnvironment(), - engine->m_parent, - -1, - engine->m_menumanager, - title, - formname); - fileOpenMenu->setTextDest(engine->m_buttonhandler); - fileOpenMenu->drop(); - return 0; -} - -/******************************************************************************/ -int ModApiMainMenu::l_get_version(lua_State *L) -{ - lua_pushstring(L, minetest_version_simple); - return 1; -} /******************************************************************************/ -int ModApiMainMenu::l_sound_play(lua_State *L) +int ModApiMainMenu::l_may_modify_path(lua_State *L) { - GUIEngine* engine = getGuiEngine(L); - - SimpleSoundSpec spec; - read_soundspec(L, 1, spec); - bool looped = lua_toboolean(L, 2); - - u32 handle = engine->playSound(spec, looped); - - lua_pushinteger(L, handle); - + const char *target = luaL_checkstring(L, 1); + std::string absolute_destination = fs::RemoveRelativePathComponents(target); + lua_pushboolean(L, ModApiMainMenu::mayModifyPath(absolute_destination)); return 1; } /******************************************************************************/ -int ModApiMainMenu::l_sound_stop(lua_State *L) +int ModApiMainMenu::l_show_path_select_dialog(lua_State *L) { GUIEngine* engine = getGuiEngine(L); + sanity_check(engine != NULL); - u32 handle = luaL_checkinteger(L, 1); - engine->stopSound(handle); + const char *formname= luaL_checkstring(L, 1); + const char *title = luaL_checkstring(L, 2); + bool is_file_select = readParam(L, 3); - return 1; + GUIFileSelectMenu* fileOpenMenu = + new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(), + engine->m_parent, + -1, + engine->m_menumanager, + title, + formname, + is_file_select); + fileOpenMenu->setTextDest(engine->m_buttonhandler); + fileOpenMenu->drop(); + return 0; } /******************************************************************************/ @@ -994,7 +718,7 @@ int ModApiMainMenu::l_download_file(lua_State *L) //check path std::string absolute_destination = fs::RemoveRelativePathComponents(target); - if (ModApiMainMenu::isMinetestPath(absolute_destination)) { + if (ModApiMainMenu::mayModifyPath(absolute_destination)) { if (GUIEngine::downloadFile(url,absolute_destination)) { lua_pushboolean(L,true); return 1; @@ -1007,11 +731,32 @@ int ModApiMainMenu::l_download_file(lua_State *L) return 1; } +/******************************************************************************/ +int ModApiMainMenu::l_get_video_drivers(lua_State *L) +{ + std::vector drivers = RenderingEngine::getSupportedVideoDrivers(); + + lua_newtable(L); + for (u32 i = 0; i != drivers.size(); i++) { + auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]); + + lua_newtable(L); + lua_pushstring(L, info.name.c_str()); + lua_setfield(L, -2, "name"); + lua_pushstring(L, info.friendly_name.c_str()); + lua_setfield(L, -2, "friendly_name"); + + lua_rawseti(L, -2, i + 1); + } + + return 1; +} + /******************************************************************************/ int ModApiMainMenu::l_gettext(lua_State *L) { - std::wstring wtext = wstrgettext((std::string) luaL_checkstring(L, 1)); - lua_pushstring(L, wide_to_narrow(wtext).c_str()); + std::string text = strgettext(std::string(luaL_checkstring(L, 1))); + lua_pushstring(L, text.c_str()); return 1; } @@ -1022,44 +767,70 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L) lua_newtable(L); int top = lua_gettop(L); lua_pushstring(L,"density"); - lua_pushnumber(L,porting::getDisplayDensity()); + lua_pushnumber(L,RenderingEngine::getDisplayDensity()); lua_settable(L, top); - lua_pushstring(L,"display_width"); - lua_pushnumber(L,porting::getDisplaySize().X); + const v2u32 &window_size = RenderingEngine::getWindowSize(); + lua_pushstring(L,"window_width"); + lua_pushnumber(L, window_size.X); lua_settable(L, top); - lua_pushstring(L,"display_height"); - lua_pushnumber(L,porting::getDisplaySize().Y); + lua_pushstring(L,"window_height"); + lua_pushnumber(L, window_size.Y); lua_settable(L, top); - lua_pushstring(L,"window_width"); - lua_pushnumber(L,porting::getWindowSize().X); + lua_pushstring(L, "render_info"); + lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str()); lua_settable(L, top); + return 1; +} - lua_pushstring(L,"window_height"); - lua_pushnumber(L,porting::getWindowSize().Y); - lua_settable(L, top); +/******************************************************************************/ +int ModApiMainMenu::l_get_min_supp_proto(lua_State *L) +{ + lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MIN); + return 1; +} + +int ModApiMainMenu::l_get_max_supp_proto(lua_State *L) +{ + lua_pushinteger(L, CLIENT_PROTOCOL_VERSION_MAX); + return 1; +} + +/******************************************************************************/ +int ModApiMainMenu::l_open_url(lua_State *L) +{ + std::string url = luaL_checkstring(L, 1); + lua_pushboolean(L, porting::open_url(url)); + return 1; +} + +/******************************************************************************/ +int ModApiMainMenu::l_open_dir(lua_State *L) +{ + std::string path = luaL_checkstring(L, 1); + lua_pushboolean(L, porting::open_directory(path)); return 1; } /******************************************************************************/ int ModApiMainMenu::l_do_async_callback(lua_State *L) { - GUIEngine* engine = getGuiEngine(L); + MainMenuScripting *script = getScriptApi(L); size_t func_length, param_length; const char* serialized_func_raw = luaL_checklstring(L, 1, &func_length); - const char* serialized_param_raw = luaL_checklstring(L, 2, ¶m_length); - assert(serialized_func_raw != NULL); - assert(serialized_param_raw != NULL); + sanity_check(serialized_func_raw != NULL); + sanity_check(serialized_param_raw != NULL); - std::string serialized_func = std::string(serialized_func_raw, func_length); - std::string serialized_param = std::string(serialized_param_raw, param_length); + u32 jobId = script->queueAsync( + std::string(serialized_func_raw, func_length), + std::string(serialized_param_raw, param_length)); - lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param)); + lua_pushinteger(L, jobId); return 1; } @@ -1068,61 +839,70 @@ int ModApiMainMenu::l_do_async_callback(lua_State *L) void ModApiMainMenu::Initialize(lua_State *L, int top) { API_FCT(update_formspec); + API_FCT(set_formspec_prepend); API_FCT(set_clouds); API_FCT(get_textlist_index); API_FCT(get_table_index); API_FCT(get_worlds); API_FCT(get_games); + API_FCT(get_content_info); API_FCT(start); API_FCT(close); - API_FCT(get_favorites); API_FCT(show_keys_menu); API_FCT(create_world); API_FCT(delete_world); - API_FCT(delete_favorite); API_FCT(set_background); API_FCT(set_topleft_text); + API_FCT(get_mapgen_names); + API_FCT(get_user_path); API_FCT(get_modpath); + API_FCT(get_clientmodpath); API_FCT(get_gamepath); API_FCT(get_texturepath); API_FCT(get_texturepath_share); - API_FCT(get_dirlist); + API_FCT(get_cache_path); + API_FCT(get_temp_path); API_FCT(create_dir); API_FCT(delete_dir); API_FCT(copy_dir); + API_FCT(is_dir); API_FCT(extract_zip); + API_FCT(may_modify_path); API_FCT(get_mainmenu_path); - API_FCT(show_file_open_dialog); - API_FCT(get_version); + API_FCT(show_path_select_dialog); API_FCT(download_file); - API_FCT(get_modstore_details); - API_FCT(get_modstore_list); - API_FCT(sound_play); - API_FCT(sound_stop); API_FCT(gettext); + API_FCT(get_video_drivers); API_FCT(get_screen_info); + API_FCT(get_min_supp_proto); + API_FCT(get_max_supp_proto); + API_FCT(open_url); + API_FCT(open_dir); API_FCT(do_async_callback); } /******************************************************************************/ -void ModApiMainMenu::InitializeAsync(AsyncEngine& engine) +void ModApiMainMenu::InitializeAsync(lua_State *L, int top) { - - ASYNC_API_FCT(get_worlds); - ASYNC_API_FCT(get_games); - ASYNC_API_FCT(get_favorites); - ASYNC_API_FCT(get_modpath); - ASYNC_API_FCT(get_gamepath); - ASYNC_API_FCT(get_texturepath); - ASYNC_API_FCT(get_texturepath_share); - ASYNC_API_FCT(get_dirlist); - ASYNC_API_FCT(create_dir); - ASYNC_API_FCT(delete_dir); - ASYNC_API_FCT(copy_dir); - //ASYNC_API_FCT(extract_zip); //TODO remove dependency to GuiEngine - ASYNC_API_FCT(get_version); - ASYNC_API_FCT(download_file); - ASYNC_API_FCT(get_modstore_details); - ASYNC_API_FCT(get_modstore_list); - //ASYNC_API_FCT(gettext); (gettext lib isn't threadsafe) + API_FCT(get_worlds); + API_FCT(get_games); + API_FCT(get_mapgen_names); + API_FCT(get_user_path); + API_FCT(get_modpath); + API_FCT(get_clientmodpath); + API_FCT(get_gamepath); + API_FCT(get_texturepath); + API_FCT(get_texturepath_share); + API_FCT(get_cache_path); + API_FCT(get_temp_path); + API_FCT(create_dir); + API_FCT(delete_dir); + API_FCT(copy_dir); + API_FCT(is_dir); + //API_FCT(extract_zip); //TODO remove dependency to GuiEngine + API_FCT(may_modify_path); + API_FCT(download_file); + API_FCT(get_min_supp_proto); + API_FCT(get_max_supp_proto); + //API_FCT(gettext); (gettext lib isn't threadsafe) }