From 516e62eade2c310e63febc9493a9f19679db5db2 Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Wed, 1 Oct 2025 00:56:39 +0300 Subject: [PATCH 1/8] Fix file.gzip --- src/logic/scripting/lua/libs/libfile.cpp | 28 ++++++++++-------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index f5cb2bfc..191f6371 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -189,30 +189,26 @@ static int l_list(lua::State* L) { } static int l_gzip_compress(lua::State* L) { - std::vector bytes; + auto string = lua::bytearray_as_string(L, 1); - lua::read_bytes_from_table(L, 1, bytes); - auto compressed_bytes = gzip::compress(bytes.data(), bytes.size()); - int newTable = lua::gettop(L); + auto compressedBytes = gzip::compress( + reinterpret_cast(string.data()), + string.size() + ); - for (size_t i = 0; i < compressed_bytes.size(); i++) { - lua::pushinteger(L, compressed_bytes.data()[i]); - lua::rawseti(L, i + 1, newTable); - } + lua::create_bytearray(L, std::move(compressedBytes)); return 1; } static int l_gzip_decompress(lua::State* L) { - std::vector bytes; + auto string = lua::bytearray_as_string(L, 1); - lua::read_bytes_from_table(L, 1, bytes); - auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size()); - int newTable = lua::gettop(L); + auto decompressedBytes = gzip::decompress( + reinterpret_cast(string.data()), + string.size() + ); - for (size_t i = 0; i < decompressed_bytes.size(); i++) { - lua::pushinteger(L, decompressed_bytes.data()[i]); - lua::rawseti(L, i + 1, newTable); - } + lua::create_bytearray(L, std::move(decompressedBytes)); return 1; } From fd8d2c22e5fb8ae4a392bab5bf6d721918ee7f43 Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:01:53 +0300 Subject: [PATCH 2/8] fixes --- src/logic/scripting/lua/libs/libfile.cpp | 42 ++++++++++++++++++------ 1 file changed, 32 insertions(+), 10 deletions(-) diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index 191f6371..eb81fd85 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -189,26 +189,48 @@ static int l_list(lua::State* L) { } static int l_gzip_compress(lua::State* L) { - auto string = lua::bytearray_as_string(L, 1); - + char argc = lua_gettop(L); + auto str = lua::bytearray_as_string(L, 1); auto compressedBytes = gzip::compress( - reinterpret_cast(string.data()), - string.size() + reinterpret_cast(str.data()), + str.size() ); - lua::create_bytearray(L, std::move(compressedBytes)); + if (argc < 2 || !lua::toboolean(L, 2)) { + lua::create_bytearray(L, std::move(compressedBytes)); + } else { + size_t length = compressedBytes.size(); + lua::createtable(L, length, 0); + int newTable = lua::gettop(L); + for (size_t i = 0; i < length; i++) { + lua::pushinteger(L, compressedBytes.data()[i]); + lua::rawseti(L, i + 1, newTable); + } + } + return 1; } static int l_gzip_decompress(lua::State* L) { - auto string = lua::bytearray_as_string(L, 1); - + char argc = lua_gettop(L); + auto str = lua::bytearray_as_string(L, 1); auto decompressedBytes = gzip::decompress( - reinterpret_cast(string.data()), - string.size() + reinterpret_cast(str.data()), + str.size() ); - lua::create_bytearray(L, std::move(decompressedBytes)); + if (argc < 2 || !lua::toboolean(L, 2)) { + lua::create_bytearray(L, std::move(decompressedBytes)); + } else { + size_t length = decompressedBytes.size(); + lua::createtable(L, length, 0); + int newTable = lua::gettop(L); + for (size_t i = 0; i < length; i++) { + lua::pushinteger(L, decompressedBytes.data()[i]); + lua::rawseti(L, i + 1, newTable); + } + } + return 1; } From ab60e7b835a9de7a4897b974c969d272acfbdb1d Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:05:25 +0300 Subject: [PATCH 3/8] Update libfile.md --- doc/ru/scripting/builtins/libfile.md | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/doc/ru/scripting/builtins/libfile.md b/doc/ru/scripting/builtins/libfile.md index e06e420c..1aa51f5e 100644 --- a/doc/ru/scripting/builtins/libfile.md +++ b/doc/ru/scripting/builtins/libfile.md @@ -43,6 +43,18 @@ file.write_bytes(путь: str, data: array of integers) Записывает массив байт в файл (с перезаписью) +```python +file.gzip_compress(data: array of integers, [опционально] usetable) -> array of integers +``` + +Сжимает массив байт алгоритмом gzip. При значении usetable = false возвращает Bytearray вместо table. + +```python +file.gzip_decompress(data: array of integers, [опционально] usetable) -> array of integers +``` + +Разжимает массив байт, который был сжат алгоритмом gzip. При значении usetable = false возвращает Bytearray вместо table. + ```python file.length(путь: str) -> int ``` @@ -205,4 +217,4 @@ file.open_named_pipe(имя: str, режим: str) -> io_stream `/tmp/` или `\\\\.\\pipe\\` добавлять не нужно - движок делает это автоматически. -Доступные режимы такие же, как и в `file.open`, за исключением `+` \ No newline at end of file +Доступные режимы такие же, как и в `file.open`, за исключением `+` From 8eeb9f5fdef801ef1d83fde78452ce32be2a4a13 Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Wed, 1 Oct 2025 19:14:43 +0300 Subject: [PATCH 4/8] change lua_gettop to lua::gettop --- src/logic/scripting/lua/libs/libfile.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index eb81fd85..a616718e 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -189,7 +189,7 @@ static int l_list(lua::State* L) { } static int l_gzip_compress(lua::State* L) { - char argc = lua_gettop(L); + char argc = lua::gettop(L); auto str = lua::bytearray_as_string(L, 1); auto compressedBytes = gzip::compress( reinterpret_cast(str.data()), @@ -212,7 +212,7 @@ static int l_gzip_compress(lua::State* L) { } static int l_gzip_decompress(lua::State* L) { - char argc = lua_gettop(L); + char argc = lua::gettop(L); auto str = lua::bytearray_as_string(L, 1); auto decompressedBytes = gzip::decompress( reinterpret_cast(str.data()), @@ -454,3 +454,4 @@ const luaL_Reg filelib[] = { {"__close_all_descriptors", lua::wrap}, {NULL, NULL} }; + From b95d3f0779f7464842493e363c733244d76c85a2 Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Thu, 2 Oct 2025 22:41:53 +0300 Subject: [PATCH 5/8] make compression lib --- src/logic/scripting/lua/libs/api_lua.hpp | 1 + .../scripting/lua/libs/libcompression.cpp | 84 +++++++++++++++++++ src/logic/scripting/lua/libs/libfile.cpp | 48 ----------- src/logic/scripting/lua/lua_engine.cpp | 1 + 4 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 src/logic/scripting/lua/libs/libcompression.cpp diff --git a/src/logic/scripting/lua/libs/api_lua.hpp b/src/logic/scripting/lua/libs/api_lua.hpp index 6c24d9d4..e304d6dc 100644 --- a/src/logic/scripting/lua/libs/api_lua.hpp +++ b/src/logic/scripting/lua/libs/api_lua.hpp @@ -43,6 +43,7 @@ extern const luaL_Reg playerlib[]; extern const luaL_Reg posteffectslib[]; // gfx.posteffects extern const luaL_Reg quatlib[]; extern const luaL_Reg randomlib[]; +extern const luaL_Reg compressionlib[]; extern const luaL_Reg text3dlib[]; // gfx.text3d extern const luaL_Reg timelib[]; extern const luaL_Reg tomllib[]; diff --git a/src/logic/scripting/lua/libs/libcompression.cpp b/src/logic/scripting/lua/libs/libcompression.cpp new file mode 100644 index 00000000..c212f42e --- /dev/null +++ b/src/logic/scripting/lua/libs/libcompression.cpp @@ -0,0 +1,84 @@ +#include "api_lua.hpp" +#include "coders/gzip.hpp" +#include "../lua_engine.hpp" + +static int l_encode(lua::State* L) { + char argc = lua::gettop(L); + std::vector compressedBytes; + + std::string algo = "gzip"; + if (argc >= 2) { + if (!lua::isstring(L, 2)) { + throw std::runtime_error("compression algorithm must be a string"); + } + algo = lua::require_lstring(L, 2); + } + + if (algo == "gzip") { + auto str = lua::bytearray_as_string(L, 1); + compressedBytes = gzip::compress( + reinterpret_cast(str.data()), + str.size() + ); + } else { + throw std::runtime_error("unsupported compression algorithm"); + } + + if (argc < 3 || !lua::toboolean(L, 3)) { + lua::create_bytearray(L, std::move(compressedBytes)); + } else { + size_t length = compressedBytes.size(); + lua::createtable(L, length, 0); + int newTable = lua::gettop(L); + for (size_t i = 0; i < length; i++) { + lua::pushinteger(L, compressedBytes.data()[i]); + lua::rawseti(L, i + 1, newTable); + } + } + + return 1; +} + +static int l_decode(lua::State* L) { + char argc = lua::gettop(L); + std::vector decompressedBytes; + + std::string algo = "gzip"; + if (argc >= 2) { + if (!lua::isstring(L, 2)) { + throw std::runtime_error("compression algorithm must be a string"); + } + algo = lua::require_lstring(L, 2); + } + + if (algo == "gzip") { + auto str = lua::bytearray_as_string(L, 1); + decompressedBytes = gzip::decompress( + reinterpret_cast(str.data()), + str.size() + ); + } else { + throw std::runtime_error("unsupported compression algorithm"); + } + + if (argc < 3 || !lua::toboolean(L, 3)) { + lua::create_bytearray(L, std::move(decompressedBytes)); + } else { + size_t length = decompressedBytes.size(); + lua::createtable(L, length, 0); + int newTable = lua::gettop(L); + for (size_t i = 0; i < length; i++) { + lua::pushinteger(L, decompressedBytes.data()[i]); + lua::rawseti(L, i + 1, newTable); + } + } + + return 1; +} + +const luaL_Reg compressionlib[] = { + {"encode", lua::wrap}, + {"decode", lua::wrap}, + {NULL, NULL} +}; + diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index a616718e..dae5e6d2 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -188,52 +188,6 @@ static int l_list(lua::State* L) { return 1; } -static int l_gzip_compress(lua::State* L) { - char argc = lua::gettop(L); - auto str = lua::bytearray_as_string(L, 1); - auto compressedBytes = gzip::compress( - reinterpret_cast(str.data()), - str.size() - ); - - if (argc < 2 || !lua::toboolean(L, 2)) { - lua::create_bytearray(L, std::move(compressedBytes)); - } else { - size_t length = compressedBytes.size(); - lua::createtable(L, length, 0); - int newTable = lua::gettop(L); - for (size_t i = 0; i < length; i++) { - lua::pushinteger(L, compressedBytes.data()[i]); - lua::rawseti(L, i + 1, newTable); - } - } - - return 1; -} - -static int l_gzip_decompress(lua::State* L) { - char argc = lua::gettop(L); - auto str = lua::bytearray_as_string(L, 1); - auto decompressedBytes = gzip::decompress( - reinterpret_cast(str.data()), - str.size() - ); - - if (argc < 2 || !lua::toboolean(L, 2)) { - lua::create_bytearray(L, std::move(decompressedBytes)); - } else { - size_t length = decompressedBytes.size(); - lua::createtable(L, length, 0); - int newTable = lua::gettop(L); - for (size_t i = 0; i < length; i++) { - lua::pushinteger(L, decompressedBytes.data()[i]); - lua::rawseti(L, i + 1, newTable); - } - } - - return 1; -} - static int l_read_combined_list(lua::State* L) { std::string path = lua::require_string(L, 1); if (path.find(':') != std::string::npos) { @@ -437,8 +391,6 @@ const luaL_Reg filelib[] = { {"resolve", lua::wrap}, {"write_bytes", lua::wrap}, {"write", lua::wrap}, - {"gzip_compress", lua::wrap}, - {"gzip_decompress", lua::wrap}, {"read_combined_list", lua::wrap}, {"read_combined_object", lua::wrap}, {"is_writeable", lua::wrap}, diff --git a/src/logic/scripting/lua/lua_engine.cpp b/src/logic/scripting/lua/lua_engine.cpp index 6ffa9057..665a9adf 100644 --- a/src/logic/scripting/lua/lua_engine.cpp +++ b/src/logic/scripting/lua/lua_engine.cpp @@ -52,6 +52,7 @@ static void create_libs(State* L, StateType stateType) { openlib(L, "pack", packlib); openlib(L, "quat", quatlib); openlib(L, "random", randomlib); + openlib(L, "compression", compressionlib); openlib(L, "toml", tomllib); openlib(L, "utf8", utf8lib); openlib(L, "vec2", vec2lib); From 063e390c92b0ace148c41072534203398163264c Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Thu, 2 Oct 2025 22:50:15 +0300 Subject: [PATCH 6/8] make libcompression.md --- doc/ru/scripting/builtins/libcompression.md | 25 +++++++++++++++++++++ doc/ru/scripting/builtins/libfile.md | 12 ---------- 2 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 doc/ru/scripting/builtins/libcompression.md diff --git a/doc/ru/scripting/builtins/libcompression.md b/doc/ru/scripting/builtins/libcompression.md new file mode 100644 index 00000000..52c70d27 --- /dev/null +++ b/doc/ru/scripting/builtins/libcompression.md @@ -0,0 +1,25 @@ +# Библиотека *compression* + +Библиотека функций для работы сжатия/разжатия массивов байт + +```lua +-- Сжимает массив байт. +compression.encode( + -- Массив байт + data: array of integers, + -- Алгоритм сжатия (поддерживается только gzip) + [опционально] algorithm="gzip", + -- Вернуть результат в table? + [опционально] usetable=false, +) -> array of integers + +-- Разжимает массив байт. +compression.encode( + -- Массив байт + data: array of integers, + -- Алгоритм разжатия (поддерживается только gzip) + [опционально] algorithm="gzip", + -- Вернуть результат в table? + [опционально] usetable=false, +) -> array of integers +``` \ No newline at end of file diff --git a/doc/ru/scripting/builtins/libfile.md b/doc/ru/scripting/builtins/libfile.md index 1aa51f5e..4afbe23c 100644 --- a/doc/ru/scripting/builtins/libfile.md +++ b/doc/ru/scripting/builtins/libfile.md @@ -43,18 +43,6 @@ file.write_bytes(путь: str, data: array of integers) Записывает массив байт в файл (с перезаписью) -```python -file.gzip_compress(data: array of integers, [опционально] usetable) -> array of integers -``` - -Сжимает массив байт алгоритмом gzip. При значении usetable = false возвращает Bytearray вместо table. - -```python -file.gzip_decompress(data: array of integers, [опционально] usetable) -> array of integers -``` - -Разжимает массив байт, который был сжат алгоритмом gzip. При значении usetable = false возвращает Bytearray вместо table. - ```python file.length(путь: str) -> int ``` From d1407404c169086b10e754f3affff43f73c4b57f Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Thu, 2 Oct 2025 22:52:11 +0300 Subject: [PATCH 7/8] Update libcompression.md --- doc/ru/scripting/builtins/libcompression.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/ru/scripting/builtins/libcompression.md b/doc/ru/scripting/builtins/libcompression.md index 52c70d27..c309996a 100644 --- a/doc/ru/scripting/builtins/libcompression.md +++ b/doc/ru/scripting/builtins/libcompression.md @@ -14,7 +14,7 @@ compression.encode( ) -> array of integers -- Разжимает массив байт. -compression.encode( +compression.decode( -- Массив байт data: array of integers, -- Алгоритм разжатия (поддерживается только gzip) From d7c2ea43e5d7397d19b9d7a40830f7bb86cfeb54 Mon Sep 17 00:00:00 2001 From: Xertis <118364459+Xertis@users.noreply.github.com> Date: Thu, 2 Oct 2025 22:53:45 +0300 Subject: [PATCH 8/8] Update libcompression x2 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Перфекционизм мешает --- doc/ru/scripting/builtins/libcompression.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/ru/scripting/builtins/libcompression.md b/doc/ru/scripting/builtins/libcompression.md index c309996a..276fbd29 100644 --- a/doc/ru/scripting/builtins/libcompression.md +++ b/doc/ru/scripting/builtins/libcompression.md @@ -10,7 +10,7 @@ compression.encode( -- Алгоритм сжатия (поддерживается только gzip) [опционально] algorithm="gzip", -- Вернуть результат в table? - [опционально] usetable=false, + [опционально] usetable=false ) -> array of integers -- Разжимает массив байт. @@ -20,6 +20,6 @@ compression.decode( -- Алгоритм разжатия (поддерживается только gzip) [опционально] algorithm="gzip", -- Вернуть результат в table? - [опционально] usetable=false, + [опционально] usetable=false ) -> array of integers ``` \ No newline at end of file