Merge pull request #636 from Xertis/patch-14
Implementation of the new compression library
This commit is contained in:
commit
ecd95967c4
25
doc/ru/scripting/builtins/libcompression.md
Normal file
25
doc/ru/scripting/builtins/libcompression.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
# Библиотека *compression*
|
||||||
|
|
||||||
|
Библиотека функций для работы сжатия/разжатия массивов байт
|
||||||
|
|
||||||
|
```lua
|
||||||
|
-- Сжимает массив байт.
|
||||||
|
compression.encode(
|
||||||
|
-- Массив байт
|
||||||
|
data: array of integers,
|
||||||
|
-- Алгоритм сжатия (поддерживается только gzip)
|
||||||
|
[опционально] algorithm="gzip",
|
||||||
|
-- Вернуть результат в table?
|
||||||
|
[опционально] usetable=false
|
||||||
|
) -> array of integers
|
||||||
|
|
||||||
|
-- Разжимает массив байт.
|
||||||
|
compression.decode(
|
||||||
|
-- Массив байт
|
||||||
|
data: array of integers,
|
||||||
|
-- Алгоритм разжатия (поддерживается только gzip)
|
||||||
|
[опционально] algorithm="gzip",
|
||||||
|
-- Вернуть результат в table?
|
||||||
|
[опционально] usetable=false
|
||||||
|
) -> array of integers
|
||||||
|
```
|
||||||
@ -43,6 +43,7 @@ extern const luaL_Reg playerlib[];
|
|||||||
extern const luaL_Reg posteffectslib[]; // gfx.posteffects
|
extern const luaL_Reg posteffectslib[]; // gfx.posteffects
|
||||||
extern const luaL_Reg quatlib[];
|
extern const luaL_Reg quatlib[];
|
||||||
extern const luaL_Reg randomlib[];
|
extern const luaL_Reg randomlib[];
|
||||||
|
extern const luaL_Reg compressionlib[];
|
||||||
extern const luaL_Reg text3dlib[]; // gfx.text3d
|
extern const luaL_Reg text3dlib[]; // gfx.text3d
|
||||||
extern const luaL_Reg timelib[];
|
extern const luaL_Reg timelib[];
|
||||||
extern const luaL_Reg tomllib[];
|
extern const luaL_Reg tomllib[];
|
||||||
|
|||||||
84
src/logic/scripting/lua/libs/libcompression.cpp
Normal file
84
src/logic/scripting/lua/libs/libcompression.cpp
Normal file
@ -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<unsigned char> 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<const ubyte*>(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<unsigned char> 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<const ubyte*>(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<l_encode>},
|
||||||
|
{"decode", lua::wrap<l_decode>},
|
||||||
|
{NULL, NULL}
|
||||||
|
};
|
||||||
|
|
||||||
@ -188,34 +188,6 @@ static int l_list(lua::State* L) {
|
|||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int l_gzip_compress(lua::State* L) {
|
|
||||||
std::vector<ubyte> bytes;
|
|
||||||
|
|
||||||
lua::read_bytes_from_table(L, 1, bytes);
|
|
||||||
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
|
|
||||||
int newTable = lua::gettop(L);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < compressed_bytes.size(); i++) {
|
|
||||||
lua::pushinteger(L, compressed_bytes.data()[i]);
|
|
||||||
lua::rawseti(L, i + 1, newTable);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_gzip_decompress(lua::State* L) {
|
|
||||||
std::vector<ubyte> bytes;
|
|
||||||
|
|
||||||
lua::read_bytes_from_table(L, 1, bytes);
|
|
||||||
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
|
|
||||||
int newTable = lua::gettop(L);
|
|
||||||
|
|
||||||
for (size_t i = 0; i < decompressed_bytes.size(); i++) {
|
|
||||||
lua::pushinteger(L, decompressed_bytes.data()[i]);
|
|
||||||
lua::rawseti(L, i + 1, newTable);
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int l_read_combined_list(lua::State* L) {
|
static int l_read_combined_list(lua::State* L) {
|
||||||
std::string path = lua::require_string(L, 1);
|
std::string path = lua::require_string(L, 1);
|
||||||
if (path.find(':') != std::string::npos) {
|
if (path.find(':') != std::string::npos) {
|
||||||
@ -419,8 +391,6 @@ const luaL_Reg filelib[] = {
|
|||||||
{"resolve", lua::wrap<l_resolve>},
|
{"resolve", lua::wrap<l_resolve>},
|
||||||
{"write_bytes", lua::wrap<l_write_bytes>},
|
{"write_bytes", lua::wrap<l_write_bytes>},
|
||||||
{"write", lua::wrap<l_write>},
|
{"write", lua::wrap<l_write>},
|
||||||
{"gzip_compress", lua::wrap<l_gzip_compress>},
|
|
||||||
{"gzip_decompress", lua::wrap<l_gzip_decompress>},
|
|
||||||
{"read_combined_list", lua::wrap<l_read_combined_list>},
|
{"read_combined_list", lua::wrap<l_read_combined_list>},
|
||||||
{"read_combined_object", lua::wrap<l_read_combined_object>},
|
{"read_combined_object", lua::wrap<l_read_combined_object>},
|
||||||
{"is_writeable", lua::wrap<l_is_writeable>},
|
{"is_writeable", lua::wrap<l_is_writeable>},
|
||||||
@ -436,3 +406,4 @@ const luaL_Reg filelib[] = {
|
|||||||
{"__close_all_descriptors", lua::wrap<l_close_all_descriptors>},
|
{"__close_all_descriptors", lua::wrap<l_close_all_descriptors>},
|
||||||
{NULL, NULL}
|
{NULL, NULL}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@ -52,6 +52,7 @@ static void create_libs(State* L, StateType stateType) {
|
|||||||
openlib(L, "pack", packlib);
|
openlib(L, "pack", packlib);
|
||||||
openlib(L, "quat", quatlib);
|
openlib(L, "quat", quatlib);
|
||||||
openlib(L, "random", randomlib);
|
openlib(L, "random", randomlib);
|
||||||
|
openlib(L, "compression", compressionlib);
|
||||||
openlib(L, "toml", tomllib);
|
openlib(L, "toml", tomllib);
|
||||||
openlib(L, "utf8", utf8lib);
|
openlib(L, "utf8", utf8lib);
|
||||||
openlib(L, "vec2", vec2lib);
|
openlib(L, "vec2", vec2lib);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user