30 #define DBG_LUA LOG_STREAM(debug, log_scripting_lua)
31 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
32 #define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
33 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
43 char const *m = luaL_checkstring(L, 1);
47 lua_pushinteger(L,
s.x);
48 lua_pushinteger(L,
s.y);
59 std::string
type = luaL_checkstring(L, 1), name = luaL_checkstring(L, 2);
72 std::string
type = luaL_checkstring(L, 1), name = luaL_checkstring(L, 2);
80 std::string currentdir;
82 if(lua_getstack(L, 1, &ar)) {
83 lua_getinfo(L,
"S", &ar);
84 if(ar.source[0] ==
'@') {
85 std::string calling_file(ar.source + 1);
86 for(
int stack_pos = 2; calling_file ==
"lua/package.lua"; stack_pos++) {
87 if(!lua_getstack(L, stack_pos, &ar)) {
90 lua_getinfo(L,
"S", &ar);
91 if(ar.source[0] ==
'@') {
92 calling_file.assign(ar.source + 1);
117 std::size_t pos =
filename.find(
"/./");
118 if(pos == std::string::npos) {
125 std::size_t pos =
filename.find(
"//");
126 if(pos == std::string::npos) {
133 std::size_t pos =
filename.find(
"/..");
134 if(pos == std::string::npos) {
137 std::size_t pos2 =
filename.find_last_of(
'/', pos - 1);
138 if(pos2 == std::string::npos || pos2 >= pos) {
143 if(
filename.find(
"..") != std::string::npos) {
171 std::string m = luaL_checkstring(L, 1);
176 return luaL_argerror(L, 1,
"invalid path");
187 std::string m = luaL_checkstring(L, 1);
189 lua_pushboolean(L,
false);
193 lua_pushboolean(L,
true);
205 std::string
p = luaL_checkstring(L, 1);
208 return luaL_argerror(L, -1,
"file not found");
212 std::vector<std::string> files, dirs;
215 std::size_t ndirs = dirs.size();
216 std::copy(files.begin(), files.end(), std::back_inserter(dirs));
218 lua_pushnumber(L, ndirs);
219 lua_setfield(L, -2,
"ndirs");
223 fs->exceptions(std::ios_base::goodbit);
224 std::size_t
size = 0;
225 fs->seekg(0, std::ios::end);
227 return luaL_error(L,
"Error when reading file");
230 fs->seekg(0, std::ios::beg);
232 return luaL_error(L,
"Error when reading file");
235 luaL_buffinit(L, &
b);
237 char* out = luaL_prepbuffsize(&
b,
size);
240 luaL_addsize(&
b,
size);
265 ERR_LUA <<
"read bytes from " << startpos <<
" to " << newpos <<
" in total " *
size <<
" from steam";
266 ERR_LUA <<
"streamstate being "
268 <<
" endoffile:" << lfs->
pistream_->eof()
270 <<
" failbit:" << lfs->
pistream_->fail();
275 static int lua_loadfile(lua_State *L,
const std::string& fname,
const std::string& relativename)
279 std::string chunkname =
'@' + relativename;
280 LOG_LUA <<
"starting to read from " << fname;
295 std::string
p = luaL_checkstring(L, -1);
299 return luaL_argerror(L, -1,
"file not found");
308 catch(
const std::exception & ex)
310 luaL_argerror(L, -1, ex.what());
319 static luaL_Reg
const callbacks[] {
329 luaL_setfuncs(L, callbacks, 0);
void remove_blacklisted_files_and_dirs(std::vector< std::string > &files, std::vector< std::string > &directories) const
Generic locator abstracting the location of an image.
static const char * lua_read_data(lua_State *, void *data, std::size_t *size)
lua_filestream(const std::string &fname)
static int lua_loadfile(lua_State *L, const std::string &fname, const std::string &relativename)
char buff_[luaL_buffersize]
const std::unique_ptr< std::istream > pistream_
Declarations for File-IO.
Standard logging facilities (interface).
bool luaW_toboolean(lua_State *L, int n)
static lg::log_domain log_scripting_lua("scripting/lua")
static int intf_have_asset(lua_State *L)
Returns true if an asset with the given path can be found in the binary paths.
static int intf_resolve_asset(lua_State *L)
Given an asset path relative to binary paths, resolves to an absolute asset path starting from data/.
static int intf_get_image_size(lua_State *L)
Gets the dimension of an image.
filesystem::scoped_istream istream_file(const std::string &fname, bool treat_failure_as_error)
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs, name_mode mode, filter_mode filter, reorder_mode reorder, file_tree_checksum *checksum)
Get a list of all files and/or directories in a given directory.
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
utils::optional< std::string > get_wml_location(const std::string &path, const utils::optional< std::string > ¤t_dir)
Returns a translated path to the actual file or directory, if it exists.
utils::optional< std::string > get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type, if it exists.
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.
utils::optional< std::string > get_independent_binary_file_path(const std::string &type, const std::string &filename)
Returns an asset path to filename for binary path-independent use in saved games.
const blacklist_pattern_list default_blacklist
bool exists(const image::locator &i_locator)
Returns true if the given image actually exists, without loading it.
point get_size(const locator &i_locator, bool skip_cache)
Returns the width and height of an image.
int luaW_open(lua_State *L)
static bool canonical_path(std::string &filename, const std::string ¤tdir)
resolves filename to an absolute path
int load_file(lua_State *L)
Loads a Lua file and pushes the contents on the stack.
int intf_read_file(lua_State *L)
Reads a file into a string, or a directory into a list of files therein.
int intf_have_file(lua_State *L)
Checks if a file exists (not necessarily a Lua script).
static std::string get_calling_file(lua_State *L)
int intf_canonical_path(lua_State *L)
static bool resolve_filename(std::string &filename, const std::string ¤tdir, std::string *rel=nullptr)
resolves filename to an absolute path
std::size_t size(const std::string &str)
Length in characters of a UTF-8 string.
void lua_push(lua_State *L, const T &val)
std::string filename
Filename.
static map_location::DIRECTION s
constexpr int luaL_buffersize