diff --git a/doc/lua_api.md b/doc/lua_api.md index 2834854c0..bf635b4dd 100644 --- a/doc/lua_api.md +++ b/doc/lua_api.md @@ -5962,7 +5962,8 @@ Utilities touch_controls = false, } ``` - +* `core.path_exists(path)`: returns true if the given path exists else false + * `path` is the path that will be tested can be either a directory or a file * `core.mkdir(path)`: returns success. * Creates a directory specified by `path`, creating parent directories if they don't exist. diff --git a/src/script/lua_api/l_util.cpp b/src/script/lua_api/l_util.cpp index 49f177a9c..4b563fb09 100644 --- a/src/script/lua_api/l_util.cpp +++ b/src/script/lua_api/l_util.cpp @@ -263,6 +263,21 @@ int ModApiUtil::l_is_yes(lua_State *L) return 1; } +// path_exists(path) +int ModApiUtil::l_path_exists(lua_State *L) +{ + NO_MAP_LOCK_REQUIRED; + + std::string path = luaL_checkstring(L, 1); //path + + CHECK_SECURE_PATH(L, path.c_str(), false); + + bool exists = fs::PathExists(path); + lua_pushboolean(L, exists); + + return 1; +} + // get_builtin_path() int ModApiUtil::l_get_builtin_path(lua_State *L) { @@ -724,6 +739,8 @@ void ModApiUtil::Initialize(lua_State *L, int top) API_FCT(is_yes); + API_FCT(path_exists); + API_FCT(get_builtin_path); API_FCT(get_user_path); @@ -809,6 +826,8 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top) API_FCT(is_yes); + API_FCT(path_exists); + API_FCT(get_builtin_path); API_FCT(get_user_path); diff --git a/src/script/lua_api/l_util.h b/src/script/lua_api/l_util.h index 7185f369a..e129e1284 100644 --- a/src/script/lua_api/l_util.h +++ b/src/script/lua_api/l_util.h @@ -52,6 +52,9 @@ private: // is_yes(arg) static int l_is_yes(lua_State *L); + // path_exists(path) + static int l_path_exists(lua_State *L); + // get_builtin_path() static int l_get_builtin_path(lua_State *L);