]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_mainmenu.cpp
Deprecate game.conf name, use title instead (#12030)
[dragonfireclient.git] / src / script / lua_api / l_mainmenu.cpp
index 6488cd0fc3aa385142e3010885b28a082083e5c1..4d9fa5b14b4570df0dd32724b94c6df7a814b908 100644 (file)
@@ -21,6 +21,7 @@ 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 "scripting_mainmenu.h"
 #include "gui/guiEngine.h"
 #include "gui/guiMainMenu.h"
 #include "gui/guiKeyChangeMenu.h"
@@ -34,9 +35,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "serverlist.h"
 #include "mapgen/mapgen.h"
 #include "settings.h"
-
-#include <IFileArchive.h>
-#include <IFileSystem.h>
+#include "client/client.h"
 #include "client/renderingengine.h"
 #include "network/networkprotocol.h"
 
@@ -305,7 +304,11 @@ int ModApiMainMenu::l_get_games(lua_State *L)
                lua_settable(L,    top_lvl2);
 
                lua_pushstring(L,  "name");
-               lua_pushstring(L,  game.name.c_str());
+               lua_pushstring(L,  game.title.c_str());
+               lua_settable(L,    top_lvl2);
+
+               lua_pushstring(L,  "title");
+               lua_pushstring(L,  game.title.c_str());
                lua_settable(L,    top_lvl2);
 
                lua_pushstring(L,  "author");
@@ -324,9 +327,9 @@ int ModApiMainMenu::l_get_games(lua_State *L)
                lua_newtable(L);
                int table2 = lua_gettop(L);
                int internal_index = 1;
-               for (const std::string &addon_mods_path : game.addon_mods_paths) {
+               for (const auto &addon_mods_path : game.addon_mods_paths) {
                        lua_pushnumber(L, internal_index);
-                       lua_pushstring(L, addon_mods_path.c_str());
+                       lua_pushstring(L, addon_mods_path.second.c_str());
                        lua_settable(L,   table2);
                        internal_index++;
                }
@@ -357,6 +360,11 @@ int ModApiMainMenu::l_get_content_info(lua_State *L)
        lua_pushstring(L, spec.author.c_str());
        lua_setfield(L, -2, "author");
 
+       if (!spec.title.empty()) {
+               lua_pushstring(L, spec.title.c_str());
+               lua_setfield(L, -2, "title");
+       }
+
        lua_pushinteger(L, spec.release);
        lua_setfield(L, -2, "release");
 
@@ -399,7 +407,8 @@ int ModApiMainMenu::l_show_keys_menu(lua_State *L)
        GUIEngine* engine = getGuiEngine(L);
        sanity_check(engine != NULL);
 
-       GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
+       GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(
+                       engine->m_rendering_engine->get_gui_env(),
                        engine->m_parent,
                        -1,
                        engine->m_menumanager,
@@ -414,25 +423,53 @@ int ModApiMainMenu::l_create_world(lua_State *L)
        const char *name        = luaL_checkstring(L, 1);
        int gameidx                     = luaL_checkinteger(L,2) -1;
 
+       StringMap use_settings;
+       luaL_checktype(L, 3, LUA_TTABLE);
+       lua_pushnil(L);
+       while (lua_next(L, 3) != 0) {
+               // key at index -2 and value at index -1
+               use_settings[luaL_checkstring(L, -2)] = luaL_checkstring(L, -1);
+               lua_pop(L, 1);
+       }
+       lua_pop(L, 1);
+
        std::string path = porting::path_user + DIR_DELIM
                        "worlds" + DIR_DELIM
                        + sanitizeDirName(name, "world_");
 
        std::vector<SubgameSpec> games = getAvailableGames();
+       if (gameidx < 0 || gameidx >= (int) games.size()) {
+               lua_pushstring(L, "Invalid game index");
+               return 1;
+       }
 
-       if ((gameidx >= 0) &&
-                       (gameidx < (int) games.size())) {
+       // Set the settings for world creation
+       // this is a bad hack but the best we have right now..
+       StringMap backup;
+       for (auto it : use_settings) {
+               if (g_settings->existsLocal(it.first))
+                       backup[it.first] = g_settings->get(it.first);
+               g_settings->set(it.first, it.second);
+       }
 
-               // Create world if it doesn't exist
-               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 {
-               lua_pushstring(L, "Invalid game index");
+       // Create world if it doesn't exist
+       try {
+               loadGameConfAndInitWorld(path, name, games[gameidx], true);
+               lua_pushnil(L);
+       } catch (const BaseException &e) {
+               auto err = std::string("Failed to initialize world: ") + e.what();
+               lua_pushstring(L, err.c_str());
        }
+
+       // Restore previous settings
+       for (auto it : use_settings) {
+               auto it2 = backup.find(it.first);
+               if (it2 == backup.end())
+                       g_settings->remove(it.first); // wasn't set before
+               else
+                       g_settings->set(it.first, it2->second); // was set before
+       }
+
        return 1;
 }
 
@@ -502,6 +539,21 @@ int ModApiMainMenu::l_get_modpath(lua_State *L)
        return 1;
 }
 
+/******************************************************************************/
+int ModApiMainMenu::l_get_modpaths(lua_State *L)
+{
+       lua_newtable(L);
+
+       ModApiMainMenu::l_get_modpath(L);
+       lua_setfield(L, -2, "mods");
+
+       for (const std::string &component : getEnvModPaths()) {
+               lua_pushstring(L, component.c_str());
+               lua_setfield(L, -2, fs::AbsolutePath(component).c_str());
+       }
+       return 1;
+}
+
 /******************************************************************************/
 int ModApiMainMenu::l_get_clientmodpath(lua_State *L)
 {
@@ -548,7 +600,10 @@ int ModApiMainMenu::l_get_cache_path(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_get_temp_path(lua_State *L)
 {
-       lua_pushstring(L, fs::TempPath().c_str());
+       if (lua_isnoneornil(L, 1) || !lua_toboolean(L, 1))
+               lua_pushstring(L, fs::TempPath().c_str());
+       else
+               lua_pushstring(L, fs::CreateTempFile().c_str());
        return 1;
 }
 
@@ -588,26 +643,24 @@ int ModApiMainMenu::l_copy_dir(lua_State *L)
        const char *destination = luaL_checkstring(L, 2);
 
        bool keep_source = true;
+       if (!lua_isnoneornil(L, 3))
+               keep_source = readParam<bool>(L, 3);
 
-       if ((!lua_isnone(L,3)) &&
-                       (!lua_isnil(L,3))) {
-               keep_source = readParam<bool>(L,3);
-       }
-
-       std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
-       std::string absolute_source = fs::RemoveRelativePathComponents(source);
-
-       if ((ModApiMainMenu::mayModifyPath(absolute_destination))) {
-               bool retval = fs::CopyDir(absolute_source,absolute_destination);
-
-               if (retval && (!keep_source)) {
+       std::string abs_destination = fs::RemoveRelativePathComponents(destination);
+       std::string abs_source = fs::RemoveRelativePathComponents(source);
 
-                       retval &= fs::RecursiveDelete(absolute_source);
-               }
-               lua_pushboolean(L,retval);
+       if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
+               (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
+               lua_pushboolean(L, false);
                return 1;
        }
-       lua_pushboolean(L,false);
+
+       bool retval;
+       if (keep_source)
+               retval = fs::CopyDir(abs_source, abs_destination);
+       else
+               retval = fs::MoveDir(abs_source, abs_destination);
+       lua_pushboolean(L, retval);
        return 1;
 }
 
@@ -629,75 +682,9 @@ int ModApiMainMenu::l_extract_zip(lua_State *L)
        std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
 
        if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
-               fs::CreateAllDirs(absolute_destination);
-
-               io::IFileSystem *fs = RenderingEngine::get_filesystem();
-
-               if (!fs->addFileArchive(zipfile, false, false, io::EFAT_ZIP)) {
-                       lua_pushboolean(L,false);
-                       return 1;
-               }
-
-               sanity_check(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();
-                       std::string fullpath_dir = fs::RemoveLastPathComponent(fullpath);
-
-                       if (!files_in_zip->isDirectory(i)) {
-                               if (!fs::PathExists(fullpath_dir) && !fs::CreateAllDirs(fullpath_dir)) {
-                                       fs->removeFileArchive(fs->getFileArchiveCount()-1);
-                                       lua_pushboolean(L,false);
-                                       return 1;
-                               }
-
-                               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];
-                               long 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);
+               auto fs = RenderingEngine::get_raw_device()->getFileSystem();
+               bool ok = fs::extractZipFile(fs, zipfile, destination);
+               lua_pushboolean(L, ok);
                return 1;
        }
 
@@ -763,7 +750,7 @@ int ModApiMainMenu::l_show_path_select_dialog(lua_State *L)
        bool is_file_select = readParam<bool>(L, 3);
 
        GUIFileSelectMenu* fileOpenMenu =
-               new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
+               new GUIFileSelectMenu(engine->m_rendering_engine->get_gui_env(),
                                engine->m_parent,
                                -1,
                                engine->m_menumanager,
@@ -804,13 +791,12 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
 
        lua_newtable(L);
        for (u32 i = 0; i != drivers.size(); i++) {
-               const char *name  = RenderingEngine::getVideoDriverName(drivers[i]);
-               const char *fname = RenderingEngine::getVideoDriverFriendlyName(drivers[i]);
+               auto &info = RenderingEngine::getVideoDriverInfo(drivers[i]);
 
                lua_newtable(L);
-               lua_pushstring(L, name);
+               lua_pushstring(L, info.name.c_str());
                lua_setfield(L, -2, "name");
-               lua_pushstring(L, fname);
+               lua_pushstring(L, info.friendly_name.c_str());
                lua_setfield(L, -2, "friendly_name");
 
                lua_rawseti(L, -2, i + 1);
@@ -819,33 +805,12 @@ int ModApiMainMenu::l_get_video_drivers(lua_State *L)
        return 1;
 }
 
-/******************************************************************************/
-int ModApiMainMenu::l_get_video_modes(lua_State *L)
-{
-       std::vector<core::vector3d<u32> > videomodes
-               = RenderingEngine::getSupportedVideoModes();
-
-       lua_newtable(L);
-       for (u32 i = 0; i != videomodes.size(); i++) {
-               lua_newtable(L);
-               lua_pushnumber(L, videomodes[i].X);
-               lua_setfield(L, -2, "w");
-               lua_pushnumber(L, videomodes[i].Y);
-               lua_setfield(L, -2, "h");
-               lua_pushnumber(L, videomodes[i].Z);
-               lua_setfield(L, -2, "depth");
-
-               lua_rawseti(L, -2, i + 1);
-       }
-
-       return 1;
-}
-
 /******************************************************************************/
 int ModApiMainMenu::l_gettext(lua_State *L)
 {
-       std::string text = strgettext(std::string(luaL_checkstring(L, 1)));
-       lua_pushstring(L, text.c_str());
+       const char *srctext = luaL_checkstring(L, 1);
+       const char *text = *srctext ? gettext(srctext) : "";
+       lua_pushstring(L, text);
 
        return 1;
 }
@@ -859,7 +824,7 @@ int ModApiMainMenu::l_get_screen_info(lua_State *L)
        lua_pushnumber(L,RenderingEngine::getDisplayDensity());
        lua_settable(L, top);
 
-       const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
+       const v2u32 &window_size = RenderingEngine::getWindowSize();
        lua_pushstring(L,"window_width");
        lua_pushnumber(L, window_size.X);
        lua_settable(L, top);
@@ -906,20 +871,20 @@ int ModApiMainMenu::l_open_dir(lua_State *L)
 /******************************************************************************/
 int ModApiMainMenu::l_do_async_callback(lua_State *L)
 {
-       GUIEngine* engine = getGuiEngine(L);
+       MainMenuScripting *script = getScriptApi<MainMenuScripting>(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, &param_length);
 
        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;
 }
@@ -945,6 +910,7 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
        API_FCT(get_mapgen_names);
        API_FCT(get_user_path);
        API_FCT(get_modpath);
+       API_FCT(get_modpaths);
        API_FCT(get_clientmodpath);
        API_FCT(get_gamepath);
        API_FCT(get_texturepath);
@@ -962,7 +928,6 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
        API_FCT(download_file);
        API_FCT(gettext);
        API_FCT(get_video_drivers);
-       API_FCT(get_video_modes);
        API_FCT(get_screen_info);
        API_FCT(get_min_supp_proto);
        API_FCT(get_max_supp_proto);
@@ -979,6 +944,7 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
        API_FCT(get_mapgen_names);
        API_FCT(get_user_path);
        API_FCT(get_modpath);
+       API_FCT(get_modpaths);
        API_FCT(get_clientmodpath);
        API_FCT(get_gamepath);
        API_FCT(get_texturepath);
@@ -989,10 +955,10 @@ void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
        API_FCT(delete_dir);
        API_FCT(copy_dir);
        API_FCT(is_dir);
-       //API_FCT(extract_zip); //TODO remove dependency to GuiEngine
+       API_FCT(extract_zip);
        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)
+       API_FCT(gettext);
 }