make compression lib

This commit is contained in:
Xertis 2025-10-02 22:41:53 +03:00
parent 8eeb9f5fde
commit b95d3f0779
4 changed files with 86 additions and 48 deletions

View File

@ -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[];

View 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}
};

View File

@ -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<const ubyte*>(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<const ubyte*>(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<l_resolve>},
{"write_bytes", lua::wrap<l_write_bytes>},
{"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_object", lua::wrap<l_read_combined_object>},
{"is_writeable", lua::wrap<l_is_writeable>},

View File

@ -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);