Skip to content

Commit 44e7c5b

Browse files
authored
Implement core.path_exists() (#16647)
1 parent 7e53e65 commit 44e7c5b

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/lua_api.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5962,7 +5962,8 @@ Utilities
59625962
touch_controls = false,
59635963
}
59645964
```
5965-
5965+
* `core.path_exists(path)`: returns true if the given path exists else false
5966+
* `path` is the path that will be tested can be either a directory or a file
59665967
* `core.mkdir(path)`: returns success.
59675968
* Creates a directory specified by `path`, creating parent directories
59685969
if they don't exist.

src/script/lua_api/l_util.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,21 @@ int ModApiUtil::l_is_yes(lua_State *L)
263263
return 1;
264264
}
265265

266+
// path_exists(path)
267+
int ModApiUtil::l_path_exists(lua_State *L)
268+
{
269+
NO_MAP_LOCK_REQUIRED;
270+
271+
std::string path = luaL_checkstring(L, 1); //path
272+
273+
CHECK_SECURE_PATH(L, path.c_str(), false);
274+
275+
bool exists = fs::PathExists(path);
276+
lua_pushboolean(L, exists);
277+
278+
return 1;
279+
}
280+
266281
// get_builtin_path()
267282
int ModApiUtil::l_get_builtin_path(lua_State *L)
268283
{
@@ -724,6 +739,8 @@ void ModApiUtil::Initialize(lua_State *L, int top)
724739

725740
API_FCT(is_yes);
726741

742+
API_FCT(path_exists);
743+
727744
API_FCT(get_builtin_path);
728745
API_FCT(get_user_path);
729746

@@ -809,6 +826,8 @@ void ModApiUtil::InitializeAsync(lua_State *L, int top)
809826

810827
API_FCT(is_yes);
811828

829+
API_FCT(path_exists);
830+
812831
API_FCT(get_builtin_path);
813832
API_FCT(get_user_path);
814833

src/script/lua_api/l_util.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ class ModApiUtil : public ModApiBase
5252
// is_yes(arg)
5353
static int l_is_yes(lua_State *L);
5454

55+
// path_exists(path)
56+
static int l_path_exists(lua_State *L);
57+
5558
// get_builtin_path()
5659
static int l_get_builtin_path(lua_State *L);
5760

0 commit comments

Comments
 (0)