From b6f2456a826a6616095ed7e3b49db0ddc3c62574 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Wed, 15 May 2024 18:01:35 +0300 Subject: [PATCH] added file.remove, file.remove_tree --- src/logic/scripting/lua/libfile.cpp | 36 ++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 7db2d5ab..b14a15b4 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -43,6 +43,28 @@ static int l_file_write(lua_State* L) { return 1; } +static int l_file_remove(lua_State* L) { + std::string rawpath = lua_tostring(L, 1); + fs::path path = resolve_path(L, rawpath); + auto entryPoint = rawpath.substr(0, rawpath.find(':')); + if (entryPoint != "world") { + throw std::runtime_error("access denied"); + } + lua_pushboolean(L, fs::remove(path)); + return 1; +} + +static int l_file_remove_tree(lua_State* L) { + std::string rawpath = lua_tostring(L, 1); + fs::path path = resolve_path(L, rawpath); + auto entryPoint = rawpath.substr(0, rawpath.find(':')); + if (entryPoint != "world") { + throw std::runtime_error("access denied"); + } + lua_pushinteger(L, fs::remove_all(path)); + return 1; +} + static int l_file_exists(lua_State* L) { fs::path path = resolve_path(L, lua_tostring(L, 1)); lua_pushboolean(L, fs::exists(path)); @@ -172,18 +194,20 @@ static int l_file_list(lua_State* L) { } const luaL_Reg filelib [] = { - {"resolve", lua_wrap_errors}, - {"find", lua_wrap_errors}, - {"read", lua_wrap_errors}, - {"write", lua_wrap_errors}, {"exists", lua_wrap_errors}, - {"isfile", lua_wrap_errors}, + {"find", lua_wrap_errors}, {"isdir", lua_wrap_errors}, + {"isfile", lua_wrap_errors}, {"length", lua_wrap_errors}, + {"list", lua_wrap_errors}, {"mkdir", lua_wrap_errors}, {"mkdirs", lua_wrap_errors}, {"read_bytes", lua_wrap_errors}, + {"read", lua_wrap_errors}, + {"remove", lua_wrap_errors}, + {"remove_tree", lua_wrap_errors}, + {"resolve", lua_wrap_errors}, {"write_bytes", lua_wrap_errors}, - {"list", lua_wrap_errors}, + {"write", lua_wrap_errors}, {NULL, NULL} };