#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"
#include "gui/guiPathSelectMenu.h"
-#include "subgame.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 "mapgen/mapgen.h"
#include "settings.h"
-
-#include <IFileArchive.h>
-#include <IFileSystem.h>
+#include "client/client.h"
#include "client/renderingengine.h"
-
+#include "network/networkprotocol.h"
+#include "content/mod_configuration.h"
+#include "threading/mutex_auto_lock.h"
+#include "common/c_converter.h"
/******************************************************************************/
std::string ModApiMainMenu::getTextData(lua_State *L, std::string name)
}
valid = true;
- return lua_toboolean(L, -1);
+ return readParam<bool>(L, -1);
}
/******************************************************************************/
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)
{
data->password = getTextData(L,"password");
data->address = getTextData(L,"address");
data->port = getTextData(L,"port");
+
+ const auto val = getTextData(L, "allow_login_or_register");
+ if (val == "login")
+ data->allow_login_or_register = ELoginRegister::Login;
+ else if (val == "register")
+ data->allow_login_or_register = ELoginRegister::Register;
+ else
+ data->allow_login_or_register = ELoginRegister::Any;
}
data->serverdescription = getTextData(L,"serverdescription");
data->servername = getTextData(L,"servername");
unsigned int minsize = 16;
if (!lua_isnone(L, 3)) {
- tile_image = lua_toboolean(L, 3);
+ tile_image = readParam<bool>(L, 3);
}
if (!lua_isnone(L, 4)) {
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
- bool value = lua_toboolean(L,1);
+ bool value = readParam<bool>(L,1);
engine->m_clouds_enabled = value;
}
/******************************************************************************/
-int ModApiMainMenu::l_get_favorites(lua_State *L)
+int ModApiMainMenu::l_get_games(lua_State *L)
{
- std::string listtype = "local";
-
- if (!lua_isnone(L,1)) {
- listtype = luaL_checkstring(L,1);
- }
-
- std::vector<ServerListSpec> servers;
-
- if(listtype == "online") {
- servers = ServerList::getOnline();
- } else {
- servers = ServerList::getLocal();
- }
+ std::vector<SubgameSpec> games = getAvailableGames();
lua_newtable(L);
int top = lua_gettop(L);
unsigned int index = 1;
- for (const Json::Value &server : servers) {
-
- lua_pushnumber(L,index);
-
+ for (const SubgameSpec &game : games) {
+ lua_pushnumber(L, index);
lua_newtable(L);
int top_lvl2 = lua_gettop(L);
- if (!server["clients"].asString().empty()) {
- std::string clients_raw = server["clients"].asString();
- char* endptr = 0;
- int numbervalue = strtol(clients_raw.c_str(),&endptr,10);
-
- if ((!clients_raw.empty()) && (*endptr == 0)) {
- lua_pushstring(L,"clients");
- lua_pushnumber(L,numbervalue);
- lua_settable(L, top_lvl2);
- }
- }
-
- if (!server["clients_max"].asString().empty()) {
-
- std::string clients_max_raw = server["clients_max"].asString();
- char* endptr = 0;
- int numbervalue = strtol(clients_max_raw.c_str(),&endptr,10);
-
- if ((!clients_max_raw.empty()) && (*endptr == 0)) {
- lua_pushstring(L,"clients_max");
- lua_pushnumber(L,numbervalue);
- lua_settable(L, top_lvl2);
- }
- }
-
- if (!server["version"].asString().empty()) {
- lua_pushstring(L,"version");
- std::string topush = server["version"].asString();
- lua_pushstring(L,topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (!server["proto_min"].asString().empty()) {
- lua_pushstring(L,"proto_min");
- lua_pushinteger(L, server["proto_min"].asInt());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "id");
+ lua_pushstring(L, game.id.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["proto_max"].asString().empty()) {
- lua_pushstring(L,"proto_max");
- lua_pushinteger(L, server["proto_max"].asInt());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "path");
+ lua_pushstring(L, game.path.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["password"].asString().empty()) {
- lua_pushstring(L,"password");
- lua_pushboolean(L, server["password"].asBool());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "type");
+ lua_pushstring(L, "game");
+ lua_settable(L, top_lvl2);
- if (!server["creative"].asString().empty()) {
- lua_pushstring(L,"creative");
- lua_pushboolean(L, server["creative"].asBool());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "gamemods_path");
+ lua_pushstring(L, game.gamemods_path.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["damage"].asString().empty()) {
- lua_pushstring(L,"damage");
- lua_pushboolean(L, server["damage"].asBool());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "name");
+ lua_pushstring(L, game.title.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["pvp"].asString().empty()) {
- lua_pushstring(L,"pvp");
- lua_pushboolean(L, server["pvp"].asBool());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "title");
+ lua_pushstring(L, game.title.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["description"].asString().empty()) {
- lua_pushstring(L,"description");
- std::string topush = server["description"].asString();
- lua_pushstring(L,topush.c_str());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "author");
+ lua_pushstring(L, game.author.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["name"].asString().empty()) {
- lua_pushstring(L,"name");
- std::string topush = server["name"].asString();
- lua_pushstring(L,topush.c_str());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "release");
+ lua_pushinteger(L, game.release);
+ lua_settable(L, top_lvl2);
- if (!server["address"].asString().empty()) {
- lua_pushstring(L,"address");
- std::string topush = server["address"].asString();
- lua_pushstring(L,topush.c_str());
- lua_settable(L, top_lvl2);
- }
+ lua_pushstring(L, "menuicon_path");
+ lua_pushstring(L, game.menuicon_path.c_str());
+ lua_settable(L, top_lvl2);
- if (!server["port"].asString().empty()) {
- lua_pushstring(L,"port");
- std::string topush = server["port"].asString();
- lua_pushstring(L,topush.c_str());
- lua_settable(L, top_lvl2);
- }
-
- if (server.isMember("ping")) {
- float ping = server["ping"].asFloat();
- lua_pushstring(L, "ping");
- lua_pushnumber(L, ping);
- lua_settable(L, top_lvl2);
+ lua_pushstring(L, "addon_mods_paths");
+ lua_newtable(L);
+ int table2 = lua_gettop(L);
+ int internal_index = 1;
+ for (const auto &addon_mods_path : game.addon_mods_paths) {
+ lua_pushnumber(L, internal_index);
+ lua_pushstring(L, addon_mods_path.second.c_str());
+ lua_settable(L, table2);
+ internal_index++;
}
-
+ lua_settable(L, top_lvl2);
lua_settable(L, top);
index++;
}
}
/******************************************************************************/
-int ModApiMainMenu::l_delete_favorite(lua_State *L)
+int ModApiMainMenu::l_get_content_info(lua_State *L)
{
- std::vector<ServerListSpec> servers;
+ std::string path = luaL_checkstring(L, 1);
- std::string listtype = "local";
+ ContentSpec spec;
+ spec.path = path;
+ parseContentInfo(spec);
- if (!lua_isnone(L,2)) {
- listtype = luaL_checkstring(L,2);
- }
+ lua_newtable(L);
- if ((listtype != "local") &&
- (listtype != "online"))
- return 0;
+ lua_pushstring(L, spec.name.c_str());
+ lua_setfield(L, -2, "name");
+
+ lua_pushstring(L, spec.type.c_str());
+ lua_setfield(L, -2, "type");
+ lua_pushstring(L, spec.author.c_str());
+ lua_setfield(L, -2, "author");
- if(listtype == "online") {
- servers = ServerList::getOnline();
- } else {
- servers = ServerList::getLocal();
+ if (!spec.title.empty()) {
+ lua_pushstring(L, spec.title.c_str());
+ lua_setfield(L, -2, "title");
}
- int fav_idx = luaL_checkinteger(L,1) -1;
+ lua_pushinteger(L, spec.release);
+ lua_setfield(L, -2, "release");
+
+ lua_pushstring(L, spec.desc.c_str());
+ lua_setfield(L, -2, "description");
+
+ lua_pushstring(L, spec.path.c_str());
+ lua_setfield(L, -2, "path");
+
+ if (spec.type == "mod") {
+ ModSpec spec;
+ spec.path = path;
+ parseModContents(spec);
- if ((fav_idx >= 0) &&
- (fav_idx < (int) servers.size())) {
+ // Dependencies
+ lua_newtable(L);
+ 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");
- ServerList::deleteEntry(servers[fav_idx]);
+ // 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_setfield(L, -2, "optional_depends");
}
- return 0;
+ return 1;
}
/******************************************************************************/
-int ModApiMainMenu::l_get_games(lua_State *L)
+int ModApiMainMenu::l_check_mod_configuration(lua_State *L)
{
- std::vector<SubgameSpec> games = getAvailableGames();
+ std::string worldpath = luaL_checkstring(L, 1);
- lua_newtable(L);
- int top = lua_gettop(L);
- unsigned int index = 1;
+ ModConfiguration modmgr;
- for (const SubgameSpec &game : games) {
- lua_pushnumber(L, index);
- lua_newtable(L);
- int top_lvl2 = lua_gettop(L);
+ // Add all game mods
+ SubgameSpec gamespec = findWorldSubgame(worldpath);
+ modmgr.addGameMods(gamespec);
+ modmgr.addModsInPath(worldpath + DIR_DELIM + "worldmods", "worldmods");
- lua_pushstring(L, "id");
- lua_pushstring(L, game.id.c_str());
- lua_settable(L, top_lvl2);
+ // Add user-configured mods
+ std::vector<ModSpec> modSpecs;
- lua_pushstring(L, "path");
- lua_pushstring(L, game.path.c_str());
- lua_settable(L, top_lvl2);
+ luaL_checktype(L, 2, LUA_TTABLE);
- lua_pushstring(L, "gamemods_path");
- lua_pushstring(L, game.gamemods_path.c_str());
- lua_settable(L, top_lvl2);
+ lua_pushnil(L);
+ while (lua_next(L, 2)) {
+ // Ignore non-string keys
+ if (lua_type(L, -2) != LUA_TSTRING) {
+ throw LuaError(
+ "Unexpected non-string key in table passed to "
+ "core.check_mod_configuration");
+ }
- lua_pushstring(L, "name");
- lua_pushstring(L, game.name.c_str());
- lua_settable(L, top_lvl2);
+ std::string modpath = luaL_checkstring(L, -1);
+ lua_pop(L, 1);
+ std::string virtual_path = lua_tostring(L, -1);
+
+ modSpecs.emplace_back();
+ ModSpec &spec = modSpecs.back();
+ spec.name = fs::GetFilenameFromPath(modpath.c_str());
+ spec.path = modpath;
+ spec.virtual_path = virtual_path;
+ if (!parseModContents(spec)) {
+ throw LuaError("Not a mod!");
+ }
+ }
- lua_pushstring(L, "menuicon_path");
- lua_pushstring(L, game.menuicon_path.c_str());
- lua_settable(L, top_lvl2);
+ modmgr.addMods(modSpecs);
+ try {
+ modmgr.checkConflictsAndDeps();
+ } catch (const ModError &err) {
+ errorstream << err.what() << std::endl;
- lua_pushstring(L, "addon_mods_paths");
lua_newtable(L);
- int table2 = lua_gettop(L);
- 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);
- lua_settable(L, top);
- index++;
- }
- return 1;
-}
-/******************************************************************************/
-int ModApiMainMenu::l_get_mod_info(lua_State *L)
-{
- std::string path = luaL_checkstring(L, 1);
+ lua_pushboolean(L, false);
+ lua_setfield(L, -2, "is_consistent");
- ModSpec spec;
- spec.path = path;
- parseModContents(spec);
+ lua_newtable(L);
+ lua_setfield(L, -2, "unsatisfied_mods");
- lua_newtable(L);
+ lua_newtable(L);
+ lua_setfield(L, -2, "satisfied_mods");
- lua_pushstring(L, spec.name.c_str());
- lua_setfield(L, -2, "name");
+ lua_pushstring(L, err.what());
+ lua_setfield(L, -2, "error_message");
+ return 1;
+ }
- lua_pushstring(L, spec.is_modpack ? "modpack" : "mod");
- lua_setfield(L, -2, "type");
- lua_pushstring(L, spec.desc.c_str());
- lua_setfield(L, -2, "description");
+ lua_newtable(L);
- lua_pushstring(L, spec.path.c_str());
- lua_setfield(L, -2, "path");
+ lua_pushboolean(L, modmgr.isConsistent());
+ lua_setfield(L, -2, "is_consistent");
- // Dependencies
lua_newtable(L);
- int i = 1;
- for (const auto &dep : spec.depends) {
- lua_pushstring(L, dep.c_str());
- lua_rawseti(L, -2, i);
- i++;
+ int top = lua_gettop(L);
+ unsigned int index = 1;
+ for (const auto &spec : modmgr.getUnsatisfiedMods()) {
+ lua_pushnumber(L, index);
+ push_mod_spec(L, spec, true);
+ lua_settable(L, top);
+ index++;
}
- lua_setfield(L, -2, "depends");
- // Optional Dependencies
+ lua_setfield(L, -2, "unsatisfied_mods");
+
lua_newtable(L);
- i = 1;
- for (const auto &dep : spec.optdepends) {
- lua_pushstring(L, dep.c_str());
- lua_rawseti(L, -2, i);
- i++;
+ top = lua_gettop(L);
+ index = 1;
+ for (const auto &spec : modmgr.getMods()) {
+ lua_pushnumber(L, index);
+ push_mod_spec(L, spec, false);
+ lua_settable(L, top);
+ index++;
}
- lua_setfield(L, -2, "optional_depends");
+ lua_setfield(L, -2, "satisfied_mods");
return 1;
}
GUIEngine* engine = getGuiEngine(L);
sanity_check(engine != NULL);
- GUIKeyChangeMenu *kmenu = new GUIKeyChangeMenu(RenderingEngine::get_gui_env(),
- engine->m_parent,
- -1,
- engine->m_menumanager);
+ 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;
}
/******************************************************************************/
int ModApiMainMenu::l_create_world(lua_State *L)
{
- const char *name = luaL_checkstring(L, 1);
- int gameidx = luaL_checkinteger(L,2) -1;
+ const char *name = luaL_checkstring(L, 1);
+ const char *gameid = luaL_checkstring(L, 2);
+
+ 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
- + name;
+ + sanitizeDirName(name, "world_");
std::vector<SubgameSpec> games = getAvailableGames();
+ auto game_it = std::find_if(games.begin(), games.end(), [gameid] (const SubgameSpec &spec) {
+ return spec.id == gameid;
+ });
+ if (game_it == games.end()) {
+ lua_pushstring(L, "Game ID not found");
+ 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
- if (!loadGameConfAndInitWorld(path, games[gameidx])) {
- lua_pushstring(L, "Failed to initialize world");
- } else {
- lua_pushnil(L);
- }
- } else {
- lua_pushstring(L, "Invalid game index");
+ // Create world if it doesn't exist
+ try {
+ loadGameConfAndInitWorld(path, name, *game_it, 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;
}
/******************************************************************************/
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<WorldSpec> worlds = getAvailableWorlds();
-
- if ((worldidx >= 0) &&
- (worldidx < (int) worlds.size())) {
-
- WorldSpec spec = worlds[worldidx];
-
- std::vector<std::string> paths;
- paths.push_back(spec.path);
- fs::GetRecursiveSubPaths(spec.path, paths, true);
-
- // 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_get_mapgen_names(lua_State *L)
{
std::vector<const char *> names;
- Mapgen::getMapgenNames(&names, lua_toboolean(L, 1));
+ bool include_hidden = lua_isboolean(L, 1) && readParam<bool>(L, 1);
+ Mapgen::getMapgenNames(&names, include_hidden);
lua_newtable(L);
for (size_t i = 0; i != names.size(); i++) {
}
+/******************************************************************************/
+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)
{
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)
{
return 1;
}
+/******************************************************************************/
int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
{
std::string gamepath = fs::RemoveRelativePathComponents(
return 1;
}
+/******************************************************************************/
+int ModApiMainMenu::l_get_cache_path(lua_State *L)
+{
+ lua_pushstring(L, fs::RemoveRelativePathComponents(porting::path_cache).c_str());
+ return 1;
+}
+
+/******************************************************************************/
+int ModApiMainMenu::l_get_temp_path(lua_State *L)
+{
+ 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;
+}
+
/******************************************************************************/
int ModApiMainMenu::l_create_dir(lua_State *L) {
const char *path = luaL_checkstring(L, 1);
- if (ModApiMainMenu::isMinetestPath(path)) {
+ if (ModApiMainMenu::mayModifyPath(path)) {
lua_pushboolean(L, fs::CreateAllDirs(path));
return 1;
}
std::string absolute_path = fs::RemoveRelativePathComponents(path);
- if (ModApiMainMenu::isMinetestPath(absolute_path)) {
+ if (ModApiMainMenu::mayModifyPath(absolute_path)) {
lua_pushboolean(L, fs::RecursiveDelete(absolute_path));
return 1;
}
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 = lua_toboolean(L,3);
- }
+ std::string abs_destination = fs::RemoveRelativePathComponents(destination);
+ std::string abs_source = fs::RemoveRelativePathComponents(source);
- std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
- std::string absolute_source = fs::RemoveRelativePathComponents(source);
+ if (!ModApiMainMenu::mayModifyPath(abs_destination) ||
+ (!keep_source && !ModApiMainMenu::mayModifyPath(abs_source))) {
+ lua_pushboolean(L, false);
+ return 1;
+ }
- if ((ModApiMainMenu::isMinetestPath(absolute_destination))) {
- bool retval = fs::CopyDir(absolute_source,absolute_destination);
+ 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;
+}
- if (retval && (!keep_source)) {
+/******************************************************************************/
+int ModApiMainMenu::l_is_dir(lua_State *L)
+{
+ const char *path = luaL_checkstring(L, 1);
- retval &= fs::RecursiveDelete(absolute_source);
- }
- lua_pushboolean(L,retval);
- return 1;
- }
- lua_pushboolean(L,false);
+ lua_pushboolean(L, fs::IsDir(path));
return 1;
}
std::string absolute_destination = fs::RemoveRelativePathComponents(destination);
- if (ModApiMainMenu::isMinetestPath(absolute_destination)) {
- fs::CreateAllDirs(absolute_destination);
-
- io::IFileSystem *fs = RenderingEngine::get_filesystem();
-
- if (!fs->addFileArchive(zipfile,true,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);
+ if (ModApiMainMenu::mayModifyPath(absolute_destination)) {
+ auto fs = RenderingEngine::get_raw_device()->getFileSystem();
+ bool ok = fs::extractZipFile(fs, zipfile, destination);
+ lua_pushboolean(L, ok);
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_may_modify_path(lua_State *L)
+{
+ 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_show_path_select_dialog(lua_State *L)
{
const char *formname= luaL_checkstring(L, 1);
const char *title = luaL_checkstring(L, 2);
- bool is_file_select = lua_toboolean(L, 3);
+ bool is_file_select = readParam<bool>(L, 3);
GUIFileSelectMenu* fileOpenMenu =
- new GUIFileSelectMenu(RenderingEngine::get_gui_env(),
- engine->m_parent,
- -1,
- engine->m_menumanager,
- title,
- formname,
- is_file_select);
+ 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;
//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;
}
} else {
errorstream << "DOWNLOAD denied: " << absolute_destination
- << " isn't a allowed path" << std::endl;
+ << " isn't an allowed path" << std::endl;
}
lua_pushboolean(L,false);
return 1;
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);
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;
}
/******************************************************************************/
-int ModApiMainMenu::l_get_screen_info(lua_State *L)
+int ModApiMainMenu::l_get_window_info(lua_State *L)
{
lua_newtable(L);
int top = lua_gettop(L);
- lua_pushstring(L,"density");
- lua_pushnumber(L,RenderingEngine::getDisplayDensity());
- lua_settable(L, top);
- lua_pushstring(L,"display_width");
- lua_pushnumber(L,RenderingEngine::getDisplaySize().X);
+ const v2u32 &window_size = RenderingEngine::getWindowSize();
+ f32 density = RenderingEngine::getDisplayDensity();
+ f32 gui_scaling = g_settings->getFloat("gui_scaling") * density;
+ f32 hud_scaling = g_settings->getFloat("hud_scaling") * density;
+
+ lua_pushstring(L, "size");
+ push_v2u32(L, window_size);
lua_settable(L, top);
- lua_pushstring(L,"display_height");
- lua_pushnumber(L,RenderingEngine::getDisplaySize().Y);
+ lua_pushstring(L, "max_formspec_size");
+ push_v2f(L, ClientDynamicInfo::calculateMaxFSSize(window_size));
lua_settable(L, top);
- const v2u32 &window_size = RenderingEngine::get_instance()->getWindowSize();
- lua_pushstring(L,"window_width");
- lua_pushnumber(L, window_size.X);
+ lua_pushstring(L, "real_gui_scaling");
+ lua_pushnumber(L, gui_scaling);
lua_settable(L, top);
- lua_pushstring(L,"window_height");
- lua_pushnumber(L, window_size.Y);
+ lua_pushstring(L, "real_hud_scaling");
+ lua_pushnumber(L, hud_scaling);
lua_settable(L, top);
+
+ return 1;
+}
+
+/******************************************************************************/
+
+int ModApiMainMenu::l_get_active_renderer(lua_State *L)
+{
+ lua_pushstring(L, wide_to_utf8(RenderingEngine::get_video_driver()->getName()).c_str());
return 1;
}
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_share_file(lua_State *L)
+{
+#ifdef __ANDROID__
+ std::string path = luaL_checkstring(L, 1);
+ porting::shareFileAndroid(path);
+ lua_pushboolean(L, true);
+#else
+ lua_pushboolean(L, false);
+#endif
+ return 1;
+}
+
/******************************************************************************/
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, ¶m_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, jobId);
+
+ return 1;
+}
+
+/******************************************************************************/
+// this is intentionally a global and not part of MainMenuScripting or such
+namespace {
+ std::unordered_map<std::string, std::string> once_values;
+ std::mutex once_mutex;
+}
+
+int ModApiMainMenu::l_set_once(lua_State *L)
+{
+ std::string key = readParam<std::string>(L, 1);
+ if (lua_isnil(L, 2))
+ return 0;
+ std::string value = readParam<std::string>(L, 2);
+
+ {
+ MutexAutoLock lock(once_mutex);
+ once_values[key] = value;
+ }
+
+ return 0;
+}
+
+int ModApiMainMenu::l_get_once(lua_State *L)
+{
+ std::string key = readParam<std::string>(L, 1);
- lua_pushinteger(L, engine->queueAsync(serialized_func, serialized_param));
+ {
+ MutexAutoLock lock(once_mutex);
+ auto it = once_values.find(key);
+ if (it == once_values.end())
+ lua_pushnil(L);
+ else
+ lua_pushstring(L, it->second.c_str());
+ }
return 1;
}
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_mod_info);
+ API_FCT(get_content_info);
+ API_FCT(check_mod_configuration);
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_modpaths);
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);
+ API_FCT(may_modify_path);
API_FCT(get_mainmenu_path);
API_FCT(show_path_select_dialog);
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_window_info);
+ API_FCT(get_active_renderer);
API_FCT(get_min_supp_proto);
API_FCT(get_max_supp_proto);
+ API_FCT(open_url);
+ API_FCT(open_dir);
+ API_FCT(share_file);
API_FCT(do_async_callback);
+ API_FCT(set_once);
+ API_FCT(get_once);
}
/******************************************************************************/
void ModApiMainMenu::InitializeAsync(lua_State *L, int top)
{
-
API_FCT(get_worlds);
API_FCT(get_games);
- API_FCT(get_favorites);
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);
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(extract_zip); //TODO remove dependency to GuiEngine
+ API_FCT(is_dir);
+ API_FCT(extract_zip);
+ API_FCT(may_modify_path);
API_FCT(download_file);
- //API_FCT(gettext); (gettext lib isn't threadsafe)
+ API_FCT(get_min_supp_proto);
+ API_FCT(get_max_supp_proto);
+ API_FCT(gettext);
}