Add core.get_dir_list

Conflicts:
	builtin/mainmenu/tab_texturepacks.lua
This commit is contained in:
ShadowNinja 2015-05-04 14:59:13 -04:00 committed by OttoLidenbrock
parent 8a6bb4f9cd
commit 9cca4e1198
7 changed files with 109 additions and 116 deletions

View File

@ -17,7 +17,7 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
function get_mods(path,retval,modpack) function get_mods(path,retval,modpack)
local mods = core.get_dirlist(path, true) local mods = core.get_dir_list(path, true)
for i=1, #mods, 1 do for i=1, #mods, 1 do
if mods[i]:sub(1,1) ~= "." then if mods[i]:sub(1,1) ~= "." then
@ -94,7 +94,7 @@ function modmgr.getbasefolder(temppath)
} }
end end
local subdirs = core.get_dirlist(temppath,true) local subdirs = core.get_dir_list(temppath, true)
--only single mod or modpack allowed --only single mod or modpack allowed
if #subdirs ~= 1 then if #subdirs ~= 1 then

View File

@ -17,112 +17,102 @@
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function filter_texture_pack_list(list) local function filter_texture_pack_list(list)
local retval = {"None"} local retval = {"None"}
for _, item in ipairs(list) do for _, item in ipairs(list) do
if item ~= "base" then if item ~= "base" then
table.insert(retval, item) table.insert(retval, item)
end end
end end
return retval return retval
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function render_texture_pack_list(list) local function render_texture_pack_list(list)
local retval = "" local retval = ""
for i, v in ipairs(list) do for i, v in ipairs(list) do
if v:sub(1,1) ~= "." then if v:sub(1,1) ~= "." then
if retval ~= "" then if retval ~= "" then
retval = retval .."," retval = retval ..","
end end
retval = retval .. core.formspec_escape(v) retval = retval .. core.formspec_escape(v)
end end
end end
return retval return retval
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function get_formspec(tabview, name, tabdata) local function get_formspec(tabview, name, tabdata)
local retval = "size[16,11]".. local retval = "label[4,-0.25;".. fgettext("Select texture pack:") .. "]"..
"bgcolor[#00000070;true]".. "textlist[4,0.25;7.5,5.0;TPs;"
"box[-100,8.5;200,10;#999999]" ..
"box[-100,-10;200,12;#999999]" ..
"label[4,-0.25;".. fgettext("Select texture pack:") .. "]"..
"image_button[12,9.55;4,0.8;"..minetest.formspec_escape(mm_texture.basetexturedir).."menu_button.png;btn_cancel;".. fgettext("OK") .. ";true;true;"..minetest.formspec_escape(mm_texture.basetexturedir).."menu_button_b.png]"..
"textlist[0,2.0;15.8,6.25;TPs;"
local current_texture_path = core.setting_get("texture_path") local current_texture_path = core.setting_get("texture_path")
local list = filter_texture_pack_list(core.get_dirlist(core.get_texturepath(), true)) local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
local index = tonumber(core.setting_get("mainmenu_last_selected_TP")) local index = tonumber(core.setting_get("mainmenu_last_selected_TP"))
if index == nil then index = 1 end if index == nil then index = 1 end
if current_texture_path == "" then if current_texture_path == "" then
retval = retval .. retval = retval ..
render_texture_pack_list(list) .. render_texture_pack_list(list) ..
";" .. index .. ";true]" ";" .. index .. "]"
return retval return retval
end end
local infofile = current_texture_path ..DIR_DELIM.."info.txt" local infofile = current_texture_path ..DIR_DELIM.."info.txt"
local infotext = "" local infotext = ""
local f = io.open(infofile, "r") local f = io.open(infofile, "r")
if f==nil then if f==nil then
infotext = fgettext("No information available") infotext = fgettext("No information available")
else else
infotext = f:read("*all") infotext = f:read("*all")
f:close() f:close()
end end
local screenfile = current_texture_path..DIR_DELIM.."screenshot.png" local screenfile = current_texture_path..DIR_DELIM.."screenshot.png"
local no_screenshot = nil local no_screenshot = nil
if not file_exists(screenfile) then if not file_exists(screenfile) then
screenfile = nil screenfile = nil
no_screenshot = defaulttexturedir .. "no_screenshot.png" no_screenshot = defaulttexturedir .. "no_screenshot.png"
end end
return retval .. return retval ..
render_texture_pack_list(list) .. render_texture_pack_list(list) ..
";" .. index .. ";true]" .. ";" .. index .. "]" ..
"image[0.25,9.25;4.0,3.7;"..core.formspec_escape(screenfile or no_screenshot).."]".. "image[0.25,0.25;4.0,3.7;"..core.formspec_escape(screenfile or no_screenshot).."]"..
"textarea[4.5,9.75;6.7,2.5;;"..core.formspec_escape(infotext or "")..";]" "textarea[0.6,3.25;3.7,1.5;;"..core.formspec_escape(infotext or "")..";]"
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
local function main_button_handler(tabview, fields, name, tabdata) local function main_button_handler(tabview, fields, name, tabdata)
if fields["TPs"] ~= nil then if fields["TPs"] ~= nil then
local event = core.explode_textlist_event(fields["TPs"]) local event = core.explode_textlist_event(fields["TPs"])
if event.type == "CHG" or event.type == "DCL" then if event.type == "CHG" or event.type == "DCL" then
local index = core.get_textlist_index("TPs") local index = core.get_textlist_index("TPs")
core.setting_set("mainmenu_last_selected_TP", core.setting_set("mainmenu_last_selected_TP",
index) index)
local list = filter_texture_pack_list(core.get_dirlist(core.get_texturepath(), true)) local list = filter_texture_pack_list(core.get_dir_list(core.get_texturepath(), true))
local current_index = core.get_textlist_index("TPs") local current_index = core.get_textlist_index("TPs")
if current_index ~= nil and #list >= current_index then if current_index ~= nil and #list >= current_index then
local new_path = core.get_texturepath()..DIR_DELIM..list[current_index] local new_path = core.get_texturepath()..DIR_DELIM..list[current_index]
if list[current_index] == "None" then new_path = "" end if list[current_index] == "None" then new_path = "" end
core.setting_set("texture_path", new_path) core.setting_set("texture_path", new_path)
end end
end end
return true return true
end end
if fields["btn_cancel"] ~= nil then return false
tabview:hide()
tabview.parent:show()
return true
end
return false
end end
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
tab_texturepacks = { tab_texturepacks = {
name = "texturepacks", name = "texturepacks",
caption = fgettext("Texturepacks"), caption = fgettext("Texturepacks"),
cbf_formspec = get_formspec, cbf_formspec = get_formspec,
cbf_button_handler = main_button_handler, cbf_button_handler = main_button_handler,
on_change = nil on_change = nil
} }

View File

@ -1704,6 +1704,11 @@ Helper functions
* `minetest.mkdir(path)`: returns success. * `minetest.mkdir(path)`: returns success.
* Creates a directory specified by `path`, creating parent directories * Creates a directory specified by `path`, creating parent directories
if they don't exist. if they don't exist.
* `minetest.get_dir_list(path, [is_dir])`: returns list of entry names
* is_dir is one of:
* nil: return all entries,
* true: return only subdirectory names, or
* false: return only file names.
### Logging ### Logging
* `minetest.debug(line)` * `minetest.debug(line)`

View File

@ -31,8 +31,8 @@ core.start()
core.close() core.close()
Filesystem: Filesystem:
core.get_scriptdir() core.get_builtin_path()
^ returns directory of script ^ returns path to builtin root
core.get_modpath() (possible in async calls) core.get_modpath() (possible in async calls)
^ returns path to global modpath ^ returns path to global modpath
core.get_modstore_details(modid) (possible in async calls) core.get_modstore_details(modid) (possible in async calls)
@ -59,10 +59,6 @@ core.get_gamepath() (possible in async calls)
^ returns path to global gamepath ^ returns path to global gamepath
core.get_texturepath() (possible in async calls) core.get_texturepath() (possible in async calls)
^ returns path to default textures ^ returns path to default textures
core.get_dirlist(path,onlydirs) (possible in async calls)
^ path to get subdirs from
^ onlydirs should result contain only dirs?
^ returns list of folders within path
core.create_dir(absolute_path) (possible in async calls) core.create_dir(absolute_path) (possible in async calls)
^ absolute_path to directory to create (needs to be absolute) ^ absolute_path to directory to create (needs to be absolute)
^ returns true/false ^ returns true/false

View File

@ -753,30 +753,6 @@ int ModApiMainMenu::l_get_texturepath_share(lua_State *L)
return 1; return 1;
} }
/******************************************************************************/
int ModApiMainMenu::l_get_dirlist(lua_State *L)
{
const char *path = luaL_checkstring(L, 1);
bool dironly = lua_toboolean(L, 2);
std::vector<fs::DirListNode> 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++;
}
}
return 1;
}
/******************************************************************************/ /******************************************************************************/
int ModApiMainMenu::l_create_dir(lua_State *L) { int ModApiMainMenu::l_create_dir(lua_State *L) {
const char *path = luaL_checkstring(L, 1); const char *path = luaL_checkstring(L, 1);
@ -1170,7 +1146,6 @@ void ModApiMainMenu::Initialize(lua_State *L, int top)
API_FCT(get_gamepath); API_FCT(get_gamepath);
API_FCT(get_texturepath); API_FCT(get_texturepath);
API_FCT(get_texturepath_share); API_FCT(get_texturepath_share);
API_FCT(get_dirlist);
API_FCT(create_dir); API_FCT(create_dir);
API_FCT(delete_dir); API_FCT(delete_dir);
API_FCT(copy_dir); API_FCT(copy_dir);
@ -1204,7 +1179,6 @@ void ModApiMainMenu::InitializeAsync(AsyncEngine& engine)
ASYNC_API_FCT(get_gamepath); ASYNC_API_FCT(get_gamepath);
ASYNC_API_FCT(get_texturepath); ASYNC_API_FCT(get_texturepath);
ASYNC_API_FCT(get_texturepath_share); ASYNC_API_FCT(get_texturepath_share);
ASYNC_API_FCT(get_dirlist);
ASYNC_API_FCT(create_dir); ASYNC_API_FCT(create_dir);
ASYNC_API_FCT(delete_dir); ASYNC_API_FCT(delete_dir);
ASYNC_API_FCT(copy_dir); ASYNC_API_FCT(copy_dir);

View File

@ -339,6 +339,29 @@ int ModApiUtil::l_mkdir(lua_State *L)
return 1; 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_OPTIONAL(L, path);
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;
}
int ModApiUtil::l_request_insecure_environment(lua_State *L) int ModApiUtil::l_request_insecure_environment(lua_State *L)
{ {
@ -391,6 +414,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
API_FCT(decompress); API_FCT(decompress);
API_FCT(mkdir); API_FCT(mkdir);
API_FCT(get_dir_list);
API_FCT(request_insecure_environment); API_FCT(request_insecure_environment);
} }
@ -417,5 +441,6 @@ void ModApiUtil::InitializeAsync(AsyncEngine& engine)
ASYNC_API_FCT(decompress); ASYNC_API_FCT(decompress);
ASYNC_API_FCT(mkdir); ASYNC_API_FCT(mkdir);
ASYNC_API_FCT(get_dir_list);
} }

View File

@ -90,6 +90,9 @@ private:
// mkdir(path) // mkdir(path)
static int l_mkdir(lua_State *L); static int l_mkdir(lua_State *L);
// get_dir_list(path, is_dir)
static int l_get_dir_list(lua_State *L);
// request_insecure_environment() // request_insecure_environment()
static int l_request_insecure_environment(lua_State *L); static int l_request_insecure_environment(lua_State *L);