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/dat/clua/loadmaps.lua | 25 ++------------------- crawl-ref/source/files.cc | 7 +++--- crawl-ref/source/files.h | 9 +++++--- crawl-ref/source/l_file.cc | 40 ++++++++++++++++++++++++++++------ 4 files changed, 45 insertions(+), 36 deletions(-) diff --git a/crawl-ref/source/dat/clua/loadmaps.lua b/crawl-ref/source/dat/clua/loadmaps.lua index 663005d333..35580cd56f 100644 --- a/crawl-ref/source/dat/clua/loadmaps.lua +++ b/crawl-ref/source/dat/clua/loadmaps.lua @@ -1,31 +1,10 @@ ------------------------------------------------------------------------------ -- loadmaps.lua: -- --- Compiles and loads .des files that Crawl needs. This only includes the --- base .des files. Optional .des files that the user requests in .crawlrc --- are handled separately. +-- Compiles and loads .des files that Crawl needs. ------------------------------------------------------------------------------ -local des_files = { - -- The dummy vaults that define global vault generation odds. - "dummy.des", - - -- Example vaults, included here so that Crawl will syntax-check them. - "didact.des", - - "arena.des", - - "layout.des", "rooms.des", - - -- Portal vaults. - "bazaar.des", "bailey.des", "icecave.des", "lab.des", "ossuary.des", - "sewer.des", "trove.des", "shrine.des", "volcano.des", "ziggurat.des", - - -- Normal vaults. - "altar.des", "entry.des", "elf.des", "float.des", "hells.des", "hive.des", - "lair.des", "large.des", "mini.des", "orc.des", "pan.des", "shoals.des", - "temple.des", "vaults.des", "crypt.des", "zot.des" -} +local des_files = file.datadir_files("dat", ".des") for _, file in ipairs(des_files) do dgn.load_des_file(file) diff --git a/crawl-ref/source/files.cc b/crawl-ref/source/files.cc index cb92d19843..30c4fa6e8f 100644 --- a/crawl-ref/source/files.cc +++ b/crawl-ref/source/files.cc @@ -503,11 +503,12 @@ std::string canonicalise_file_separator(const std::string &path) std::string datafile_path(std::string basename, bool croak_on_fail, - bool test_base_path) + bool test_base_path, + bool (*thing_exists)(const std::string&)) { basename = canonicalise_file_separator(basename); - if (test_base_path && file_exists(basename)) + if (test_base_path && thing_exists(basename)) return (basename); const std::string rawbases[] = { @@ -562,7 +563,7 @@ std::string datafile_path(std::string basename, for (unsigned p = 0; p < sizeof(prefixes) / sizeof(*prefixes); ++p) { std::string name = bases[b] + prefixes[p] + basename; - if (file_exists(name)) + if (thing_exists(name)) return (name); } diff --git a/crawl-ref/source/files.h b/crawl-ref/source/files.h index 80dcf2c3ec..5c639eeb14 100644 --- a/crawl-ref/source/files.h +++ b/crawl-ref/source/files.h @@ -41,9 +41,12 @@ std::vector get_dir_files(const std::string &dir); std::vector get_dir_files_ext(const std::string &dir, const std::string &ext); -std::string datafile_path(std::string basename, - bool croak_on_fail = true, - bool test_base_path = false); +std::string datafile_path( + std::string basename, + bool croak_on_fail = true, + bool test_base_path = false, + bool (*thing_exists)(const std::string&) = file_exists); + bool get_dos_compatible_file_name(std::string *fname); std::string get_parent_directory(const std::string &filename); 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