From 67493d93236c231d9a16af420224769cf9daf6e9 Mon Sep 17 00:00:00 2001 From: Darshan Shaligram Date: Sat, 31 Oct 2009 23:52:54 +0530 Subject: loadmaps.lua auto-loads .des files in dat. --- crawl-ref/source/l_file.cc | 40 +++++++++++++++++++++++++++++++++------- 1 file changed, 33 insertions(+), 7 deletions(-) (limited to 'crawl-ref/source/l_file.cc') diff --git a/crawl-ref/source/l_file.cc b/crawl-ref/source/l_file.cc index e619327cbe..bde31cb558 100644 --- a/crawl-ref/source/l_file.cc +++ b/crawl-ref/source/l_file.cc @@ -1,8 +1,10 @@ #include "AppHdr.h" #include "clua.h" +#include "cluautil.h" #include "dlua.h" #include "l_libs.h" +#include "files.h" #include "tags.h" @@ -144,15 +146,39 @@ static int file_unmarshall_meta(lua_State *ls) return (0); } +// Returns a Lua table of filenames in the named directory. The file names +// returned are unqualified. The directory must be a relative path, and will +// be resolved to an absolute path if necessary using datafile_path. +LUAFN(_file_datadir_files) +{ + const std::string rawdir(luaL_checkstring(ls, 1)); + // A filename suffix to match (such as ".des"). If empty, files + // will be unfiltered. + const std::string ext_filter(lua_isnoneornil(ls, 2) ? "" : + luaL_checkstring(ls, 2)); + const std::string datadir( + datafile_path(rawdir, false, false, dir_exists)); + + if (datadir.empty()) + luaL_error(ls, "Cannot find data directory: '%s'", rawdir.c_str()); + + const std::vector files = + ext_filter.empty() ? get_dir_files(datadir) : + get_dir_files_ext(datadir, ext_filter); + return clua_stringtable(ls, files); +} + static const struct luaL_reg file_dlib[] = { -{ "marshall", file_marshall }, -{ "marshall_meta", file_marshall_meta }, -{ "unmarshall_meta", file_unmarshall_meta }, -{ "unmarshall_number", file_unmarshall_number }, -{ "unmarshall_string", file_unmarshall_string }, -{ "unmarshall_fn", file_unmarshall_fn }, -{ NULL, NULL } + { "marshall", file_marshall }, + { "marshall_meta", file_marshall_meta }, + { "unmarshall_meta", file_unmarshall_meta }, + { "unmarshall_number", file_unmarshall_number }, + { "unmarshall_string", file_unmarshall_string }, + { "unmarshall_fn", file_unmarshall_fn }, + + { "datadir_files", _file_datadir_files }, + { NULL, NULL } }; void dluaopen_file(lua_State *ls) -- cgit v1.2.3-54-g00ecf