From 3d6bfc2aa9e330f0be9c777ae4d8507a6e037562 Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Tue, 22 Mar 2022 02:55:01 +0100 Subject: [PATCH 1/4] Update os.listall, os.listfiles and os.listdirs # Changes - Update functions to both accept fixed paths starting with a drive letter, while at the same time keeping their current functionality exactly the same as before. # Examples ## Same behavior as before: - `os.listdirs("")` will still list all directories in the Into the Breach directory. - `os.listdirs("mods")` will still list all directories in the Into the Breach mods directory. ## New capabilities: - `os.listdirs("C:")` will list all directories in the C directory. - `os.listdirs("D:")` will list all directories in the D directory. - `os.listdirs(GetSavedataLocation())` will list all directories in the Into the Breach save directory. # Why not use io.popen or os.execute instead? - Listing files and directories using io.popen or os.execute causes a program to briefly open, which can cause flickering issues which negatively impact the player experience. --- lua-functions.cc | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lua-functions.cc b/lua-functions.cc index 918ddca..3a9415d 100644 --- a/lua-functions.cc +++ b/lua-functions.cc @@ -11,6 +11,7 @@ using namespace luabridge; #include "SDL_syswm.h" #include #include +#include int listDirectoryFull(lua_State *L, int mode) { const char *dirname = lua_tostring(L, 1); @@ -19,8 +20,12 @@ int listDirectoryFull(lua_State *L, int mode) { WIN32_FIND_DATAA fdFile; HANDLE handle = NULL; + std::regex startsWithDriveLetter("^.:.*$"); + std::string path = format("%s\\*.*", dirname); - std::string path = format(".\\%s\\*.*", dirname); + if (!std::regex_match(path, startsWithDriveLetter)) + path = format(".\\%s", path); + if((handle = FindFirstFileA(path.c_str(), &fdFile)) == INVALID_HANDLE_VALUE) { return 1; } From 2b40710cdbffbaebea802a82a217ddb5e45e0fa9 Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Tue, 22 Mar 2022 17:44:10 +0100 Subject: [PATCH 2/4] Fix crash when function argument is not a string --- lua-functions.cc | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lua-functions.cc b/lua-functions.cc index 3a9415d..3efceb5 100644 --- a/lua-functions.cc +++ b/lua-functions.cc @@ -14,17 +14,23 @@ using namespace luabridge; #include int listDirectoryFull(lua_State *L, int mode) { - const char *dirname = lua_tostring(L, 1); - lua_newtable(L); + if (!lua_isstring(L, 1)) + return 1; + + const char *dirname = lua_tostring(L, 1); + WIN32_FIND_DATAA fdFile; HANDLE handle = NULL; + std::regex fullPath("(.*)"); std::regex startsWithDriveLetter("^.:.*$"); - std::string path = format("%s\\*.*", dirname); + std::string path; - if (!std::regex_match(path, startsWithDriveLetter)) - path = format(".\\%s", path); + if (!std::regex_match(dirname, startsWithDriveLetter)) + path = regex_replace(dirname, fullPath, ".\\$1\\*.*"); + else + path = regex_replace(dirname, fullPath, "$1\\*.*"); if((handle = FindFirstFileA(path.c_str(), &fdFile)) == INVALID_HANDLE_VALUE) { return 1; From afd4a5ab7d11ea74c229eef242537d2907173b1f Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Sun, 28 Aug 2022 18:02:56 +0200 Subject: [PATCH 3/4] Replace regex with format --- lua-functions.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua-functions.cc b/lua-functions.cc index 3efceb5..61ff5b6 100644 --- a/lua-functions.cc +++ b/lua-functions.cc @@ -28,9 +28,9 @@ int listDirectoryFull(lua_State *L, int mode) { std::string path; if (!std::regex_match(dirname, startsWithDriveLetter)) - path = regex_replace(dirname, fullPath, ".\\$1\\*.*"); + path = format(".\\%s\\*.*", dirname); else - path = regex_replace(dirname, fullPath, "$1\\*.*"); + path = format("%s\\*.*", dirname); if((handle = FindFirstFileA(path.c_str(), &fdFile)) == INVALID_HANDLE_VALUE) { return 1; From 2f0b7a3416af8ec5744d4cfaa8c7816926016442 Mon Sep 17 00:00:00 2001 From: Lemonymous <10490534+Lemonymous@users.noreply.github.com> Date: Sun, 28 Aug 2022 18:06:46 +0200 Subject: [PATCH 4/4] Remove unused code --- lua-functions.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/lua-functions.cc b/lua-functions.cc index 61ff5b6..1f634a6 100644 --- a/lua-functions.cc +++ b/lua-functions.cc @@ -23,7 +23,6 @@ int listDirectoryFull(lua_State *L, int mode) { WIN32_FIND_DATAA fdFile; HANDLE handle = NULL; - std::regex fullPath("(.*)"); std::regex startsWithDriveLetter("^.:.*$"); std::string path;