From 4e051c6faa46be3c48707ddb9e87453c847a4ec1 Mon Sep 17 00:00:00 2001 From: Alex Szpakowski Date: Fri, 22 Apr 2022 21:11:39 -0300 Subject: [PATCH] Re-add love.filesystem.exists. It's still useful sometimes because it's a bit cheaper than getInfo. --- src/modules/filesystem/Filesystem.h | 5 +++++ src/modules/filesystem/physfs/Filesystem.cpp | 8 ++++++++ src/modules/filesystem/physfs/Filesystem.h | 1 + src/modules/filesystem/wrap_Filesystem.cpp | 8 ++++++++ 4 files changed, 22 insertions(+) diff --git a/src/modules/filesystem/Filesystem.h b/src/modules/filesystem/Filesystem.h index daa735f40..e2c78b7dc 100644 --- a/src/modules/filesystem/Filesystem.h +++ b/src/modules/filesystem/Filesystem.h @@ -216,6 +216,11 @@ public: **/ virtual std::string getRealDirectory(const char *filename) const = 0; + /** + * Gets whether anything exists at the specified path. + **/ + virtual bool exists(const char *filepath) const = 0; + /** * Gets information about the item at the specified filepath. Returns false * if nothing exists at the path. diff --git a/src/modules/filesystem/physfs/Filesystem.cpp b/src/modules/filesystem/physfs/Filesystem.cpp index 02b7d0f78..27f58e826 100644 --- a/src/modules/filesystem/physfs/Filesystem.cpp +++ b/src/modules/filesystem/physfs/Filesystem.cpp @@ -738,6 +738,14 @@ std::string Filesystem::getRealDirectory(const char *filename) const return std::string(dir); } +bool Filesystem::exists(const char *filepath) const +{ + if (!PHYSFS_isInit()) + return false; + + return PHYSFS_exists(filepath) != 0; +} + bool Filesystem::getInfo(const char *filepath, Info &info) const { if (!PHYSFS_isInit()) diff --git a/src/modules/filesystem/physfs/Filesystem.h b/src/modules/filesystem/physfs/Filesystem.h index 3641e5ded..a1ec62657 100644 --- a/src/modules/filesystem/physfs/Filesystem.h +++ b/src/modules/filesystem/physfs/Filesystem.h @@ -82,6 +82,7 @@ public: std::string getRealDirectory(const char *filename) const override; + bool exists(const char *filepath) const override; bool getInfo(const char *filepath, Info &info) const override; bool createDirectory(const char *dir) override; diff --git a/src/modules/filesystem/wrap_Filesystem.cpp b/src/modules/filesystem/wrap_Filesystem.cpp index 7ca9a7c4f..4b53a86c7 100644 --- a/src/modules/filesystem/wrap_Filesystem.cpp +++ b/src/modules/filesystem/wrap_Filesystem.cpp @@ -502,6 +502,13 @@ int w_getExecutablePath(lua_State *L) return 1; } +int w_exists(lua_State *L) +{ + const char *path = luaL_checkstring(L, 1); + luax_pushboolean(L, instance()->exists(path)); + return 1; +} + int w_getInfo(lua_State *L) { const char *filepath = luaL_checkstring(L, 1); @@ -1000,6 +1007,7 @@ static const luaL_Reg functions[] = { "getDirectoryItems", w_getDirectoryItems }, { "lines", w_lines }, { "load", w_load }, + { "exists", w_exists }, { "getInfo", w_getInfo }, { "setSymlinksEnabled", w_setSymlinksEnabled }, { "areSymlinksEnabled", w_areSymlinksEnabled },