From bfeb32841f0bae59c7b9cc6047980907b0ff259b Mon Sep 17 00:00:00 2001 From: Xertis Date: Thu, 6 Jun 2024 12:17:37 +0300 Subject: [PATCH 1/2] add file.gzip_compress_write --- src/logic/scripting/lua/libfile.cpp | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 6b97da3c..7d34393d 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -3,6 +3,7 @@ #include "LuaState.hpp" #include "../scripting.hpp" #include "../../../engine.hpp" +#include "../../../coders/gzip.hpp" #include "../../../files/files.hpp" #include "../../../files/engine_paths.hpp" #include "../../../util/stringutil.hpp" @@ -129,7 +130,7 @@ static int l_file_read_bytes(lua_State* L) { int newTable = lua_gettop(L); for(size_t i = 0; i < length; i++) { - lua_pushnumber(L, bytes[i]); + lua_pushinteger(L, bytes[i]); lua_rawseti(L, newTable, i+1); } return 1; @@ -206,6 +207,24 @@ static int l_file_list(lua_State* L) { return 1; } +static int l_file_gzip_compress(lua_State* L) { + fs::path path = resolve_path(state->requireString(1)); + if (fs::is_regular_file(path)) { + size_t length = static_cast(fs::file_size(path)); + + auto bytesPtr = files::read_bytes(path, length); + std::vector bytes (bytesPtr.get(), bytesPtr.get() + length); + + std::vector compressed_bytes; + + compressed_bytes = gzip::compress(bytes.data(), bytes.size()); + + lua_pushboolean(L, files::write_bytes(path, compressed_bytes.data(), compressed_bytes.size())); + return 1; + } + throw std::runtime_error("file does not exist " + util::quote(path.u8string())); +} + const luaL_Reg filelib [] = { {"exists", lua_wrap_errors}, {"find", lua_wrap_errors}, @@ -222,5 +241,6 @@ const luaL_Reg filelib [] = { {"resolve", lua_wrap_errors}, {"write_bytes", lua_wrap_errors}, {"write", lua_wrap_errors}, + {"gzip_compress_write", lua_wrap_errors}, {NULL, NULL} }; From 224604ec69ed13b5a656923bd7e79f41a7985db5 Mon Sep 17 00:00:00 2001 From: Xertis Date: Thu, 6 Jun 2024 13:00:23 +0300 Subject: [PATCH 2/2] Adding compression functions to lua scripting MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1) добавил две функции file.gzip_compress(path) file.gzip_decompress(path) они сжимают и расжимают файлы по выбранному пути --- src/logic/scripting/lua/libfile.cpp | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 7d34393d..ecea4c23 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -212,12 +212,7 @@ static int l_file_gzip_compress(lua_State* L) { if (fs::is_regular_file(path)) { size_t length = static_cast(fs::file_size(path)); - auto bytesPtr = files::read_bytes(path, length); - std::vector bytes (bytesPtr.get(), bytesPtr.get() + length); - - std::vector compressed_bytes; - - compressed_bytes = gzip::compress(bytes.data(), bytes.size()); + auto compressed_bytes = gzip::compress(files::read_bytes(path, length).get(), length); lua_pushboolean(L, files::write_bytes(path, compressed_bytes.data(), compressed_bytes.size())); return 1; @@ -225,6 +220,19 @@ static int l_file_gzip_compress(lua_State* L) { throw std::runtime_error("file does not exist " + util::quote(path.u8string())); } +static int l_file_gzip_decompress(lua_State* L) { + fs::path path = resolve_path(state->requireString(1)); + if (fs::is_regular_file(path)) { + size_t length = static_cast(fs::file_size(path)); + + auto decompressed_bytes = gzip::decompress(files::read_bytes(path, length).get(), length); + + lua_pushboolean(L, files::write_bytes(path, decompressed_bytes.data(), decompressed_bytes.size())); + return 1; + } + throw std::runtime_error("file does not exist " + util::quote(path.u8string())); +} + const luaL_Reg filelib [] = { {"exists", lua_wrap_errors}, {"find", lua_wrap_errors}, @@ -241,6 +249,7 @@ const luaL_Reg filelib [] = { {"resolve", lua_wrap_errors}, {"write_bytes", lua_wrap_errors}, {"write", lua_wrap_errors}, - {"gzip_compress_write", lua_wrap_errors}, + {"gzip_compress", lua_wrap_errors}, + {"gzip_decompress", lua_wrap_errors}, {NULL, NULL} };