33 #define DBG_LUA LOG_STREAM(debug, log_scripting_lua)
34 #define LOG_LUA LOG_STREAM(info, log_scripting_lua)
35 #define WRN_LUA LOG_STREAM(warn, log_scripting_lua)
36 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
46 char const *m = luaL_checkstring(L, 1);
50 lua_pushinteger(L,
s.x);
51 lua_pushinteger(L,
s.y);
62 std::string
type = luaL_checkstring(L, 1), name = luaL_checkstring(L, 2);
75 std::string
type = luaL_checkstring(L, 1), name = luaL_checkstring(L, 2);
83 std::string currentdir;
85 if(lua_getstack(L, 1, &ar)) {
86 lua_getinfo(L,
"S", &ar);
87 if(ar.source[0] ==
'@') {
88 std::string calling_file(ar.source + 1);
89 for(
int stack_pos = 2; calling_file ==
"lua/package.lua"; stack_pos++) {
90 if(!lua_getstack(L, stack_pos, &ar)) {
93 lua_getinfo(L,
"S", &ar);
94 if(ar.source[0] ==
'@') {
95 calling_file.assign(ar.source + 1);
109 if(filename.size() < 2) {
112 if(filename[0] ==
'.' && filename[1] ==
'/') {
113 filename = currentdir + filename.substr(1);
115 if(std::find(filename.begin(), filename.end(),
'\\') != filename.end()) {
120 std::size_t pos = filename.find(
"/./");
121 if(pos == std::string::npos) {
124 filename = filename.replace(pos, 2,
"");
128 std::size_t pos = filename.find(
"//");
129 if(pos == std::string::npos) {
132 filename = filename.replace(pos, 1,
"");
136 std::size_t pos = filename.find(
"/..");
137 if(pos == std::string::npos) {
140 std::size_t pos2 = filename.find_last_of(
'/', pos - 1);
141 if(pos2 == std::string::npos || pos2 >= pos) {
144 filename = filename.replace(pos2, pos- pos2 + 3,
"");
146 if(filename.find(
"..") != std::string::npos) {
156 static bool resolve_filename(std::string& filename,
const std::string& currentdir, std::string* rel =
nullptr)
174 std::string m = luaL_checkstring(L, 1);
179 return luaL_argerror(L, 1,
"invalid path");
190 std::string m = luaL_checkstring(L, 1);
192 lua_pushboolean(L,
false);
196 lua_pushboolean(L,
true);
208 std::string
p = luaL_checkstring(L, 1);
211 return luaL_argerror(L, -1,
"file not found");
215 std::vector<std::string> files, dirs;
218 std::size_t ndirs = dirs.size();
219 std::copy(files.begin(), files.end(), std::back_inserter(dirs));
221 lua_pushnumber(L, ndirs);
222 lua_setfield(L, -2,
"ndirs");
226 fs->exceptions(std::ios_base::goodbit);
227 std::size_t
size = 0;
228 fs->seekg(0, std::ios::end);
230 return luaL_error(L,
"Error when reading file");
233 fs->seekg(0, std::ios::beg);
235 return luaL_error(L,
"Error when reading file");
238 luaL_buffinit(L, &
b);
240 char* out = luaL_prepbuffsize(&
b,
size);
243 luaL_addsize(&
b,
size);
268 ERR_LUA <<
"read bytes from " << startpos <<
" to " << newpos <<
" in total " *
size <<
" from steam";
269 ERR_LUA <<
"streamstate being "
271 <<
" endoffile:" << lfs->
pistream_->eof()
273 <<
" failbit:" << lfs->
pistream_->fail();
278 static int lua_loadfile(lua_State *L,
const std::string& fname,
const std::string& relativename)
282 std::string chunkname =
'@' + relativename;
283 LOG_LUA <<
"starting to read from " << fname;
298 std::string
p = luaL_checkstring(L, -1);
302 return luaL_argerror(L, -1,
"file not found");
311 catch(
const std::exception & ex)
313 luaL_argerror(L, -1, ex.what());
322 static luaL_Reg
const callbacks[] {
332 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.
bool file_exists() const
Tests whether the file the locator points at exists.
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.
std::string get_wml_location(const std::string &filename, const std::string ¤t_dir)
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
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.
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 or an empty string if the file isn't prese...
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.
const blacklist_pattern_list default_blacklist
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)
static map_location::DIRECTION s