int ScriptApiSecurity::sl_os_rename(lua_State *L) { luaL_checktype(L, 1, LUA_TSTRING); const char *path1 = lua_tostring(L, 1); CHECK_SECURE_PATH(L, path1); luaL_checktype(L, 2, LUA_TSTRING); const char *path2 = lua_tostring(L, 2); CHECK_SECURE_PATH(L, path2); push_original(L, "os", "rename"); lua_pushvalue(L, 1); lua_pushvalue(L, 2); lua_call(L, 2, 2); return 2; }
// mkdir(path) int ModApiUtil::l_mkdir(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *path = luaL_checkstring(L, 1); CHECK_SECURE_PATH(L, path, true); lua_pushboolean(L, fs::CreateAllDirs(path)); return 1; }
int ScriptApiSecurity::sl_io_output(lua_State *L) { if (lua_isstring(L, 1)) { const char *path = lua_tostring(L, 1); CHECK_SECURE_PATH(L, path); } push_original(L, "io", "output"); lua_pushvalue(L, 1); lua_call(L, 1, 1); return 1; }
int ScriptApiSecurity::sl_io_open(lua_State *L) { luaL_checktype(L, 1, LUA_TSTRING); const char *path = lua_tostring(L, 1); CHECK_SECURE_PATH(L, path); push_original(L, "io", "open"); lua_pushvalue(L, 1); lua_pushvalue(L, 2); lua_call(L, 2, 2); return 2; }
int ScriptApiSecurity::sl_io_lines(lua_State *L) { if (lua_isstring(L, 1)) { const char *path = lua_tostring(L, 1); CHECK_SECURE_PATH(L, path); } push_original(L, "io", "lines"); lua_pushvalue(L, 1); int top_precall = lua_gettop(L); lua_call(L, 1, LUA_MULTRET); // Return number of arguments returned by the function, // adjusting for the function being poped. return lua_gettop(L) - (top_precall - 1); }
int ScriptApiSecurity::sl_g_loadfile(lua_State *L) { const char *path = NULL; if (lua_isstring(L, 1)) { path = lua_tostring(L, 1); CHECK_SECURE_PATH(L, path); } if (!safeLoadFile(L, path)) { lua_pushnil(L); lua_insert(L, -2); return 2; } return 1; }
// get_dir_list(path, is_dir) int ModApiUtil::l_get_dir_list(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *path = luaL_checkstring(L, 1); short is_dir = lua_isboolean(L, 2) ? lua_toboolean(L, 2) : -1; CHECK_SECURE_PATH(L, path, false); std::vector<fs::DirListNode> list = fs::GetDirListing(path); int index = 0; lua_newtable(L); for (size_t i = 0; i < list.size(); i++) { if (is_dir == -1 || is_dir == list[i].dir) { lua_pushstring(L, list[i].name.c_str()); lua_rawseti(L, -2, ++index); } } return 1; }
// get_dir_list(path, is_dir) int ModApiUtil::l_get_dir_list(lua_State *L) { NO_MAP_LOCK_REQUIRED; const char *path = luaL_checkstring(L, 1); bool list_all = !lua_isboolean(L, 2); // if its not a boolean list all bool list_dirs = lua_toboolean(L, 2); // true: list dirs, false: list files CHECK_SECURE_PATH(L, path, false); std::vector<fs::DirListNode> list = fs::GetDirListing(path); int index = 0; lua_newtable(L); for (size_t i = 0; i < list.size(); i++) { if (list_all || list_dirs == list[i].dir) { lua_pushstring(L, list[i].name.c_str()); lua_rawseti(L, -2, ++index); } } return 1; }
// create_schematic(p1, p2, probability_list, filename, y_slice_prob_list) int ModApiMapgen::l_create_schematic(lua_State *L) { MAP_LOCK_REQUIRED; INodeDefManager *ndef = getServer(L)->getNodeDefManager(); const char *filename = luaL_checkstring(L, 4); CHECK_SECURE_PATH(L, filename, true); Map *map = &(getEnv(L)->getMap()); Schematic schem; v3s16 p1 = check_v3s16(L, 1); v3s16 p2 = check_v3s16(L, 2); sortBoxVerticies(p1, p2); std::vector<std::pair<v3s16, u8> > prob_list; if (lua_istable(L, 3)) { lua_pushnil(L); while (lua_next(L, 3)) { if (lua_istable(L, -1)) { lua_getfield(L, -1, "pos"); v3s16 pos = check_v3s16(L, -1); lua_pop(L, 1); u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS); prob_list.emplace_back(pos, prob); } lua_pop(L, 1); } } std::vector<std::pair<s16, u8> > slice_prob_list; if (lua_istable(L, 5)) { lua_pushnil(L); while (lua_next(L, 5)) { if (lua_istable(L, -1)) { s16 ypos = getintfield_default(L, -1, "ypos", 0); u8 prob = getintfield_default(L, -1, "prob", MTSCHEM_PROB_ALWAYS); slice_prob_list.emplace_back(ypos, prob); } lua_pop(L, 1); } } if (!schem.getSchematicFromMap(map, p1, p2)) { errorstream << "create_schematic: failed to get schematic " "from map" << std::endl; return 0; } schem.applyProbabilities(p1, &prob_list, &slice_prob_list); schem.saveSchematicToFile(filename, ndef); actionstream << "create_schematic: saved schematic file '" << filename << "'." << std::endl; lua_pushboolean(L, true); return 1; }