Merge branch 'main' into headless-mode

This commit is contained in:
MihailRis 2024-12-22 02:33:12 +03:00
commit 1149d5869c
5 changed files with 59 additions and 33 deletions

View File

@ -72,9 +72,14 @@ function data_buffer:put_byte(byte)
end
function data_buffer:put_bytes(bytes)
if type(self.bytes) == 'table' then
for i = 1, #bytes do
self:put_byte(bytes[i])
end
else
self.bytes:insert(self.pos, bytes)
self.pos = self.pos + #bytes
end
end
function data_buffer:put_single(single)

View File

@ -1,6 +1,6 @@
-- Check if given table is an array
function is_array(x)
if #t > 0 then
if #x > 0 then
return true
end
for k, v in pairs(x) do

View File

@ -149,27 +149,6 @@ static int l_read_bytes(lua::State* L) {
);
}
static void read_bytes_from_table(
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) {
if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected");
} else {
size_t size = lua::objlen(L, tableIndex);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, tableIndex);
const int byte = lua::tointeger(L, -1);
lua::pop(L);
if (byte < 0 || byte > 255) {
throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'"
);
}
bytes.push_back(byte);
}
}
}
static int l_write_bytes(lua::State* L) {
fs::path path = get_writeable_path(L);
@ -181,7 +160,7 @@ static int l_write_bytes(lua::State* L) {
}
std::vector<ubyte> bytes;
read_bytes_from_table(L, 2, bytes);
lua::read_bytes_from_table(L, 2, bytes);
return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size())
);
@ -223,7 +202,7 @@ static int l_list(lua::State* L) {
static int l_gzip_compress(lua::State* L) {
std::vector<ubyte> bytes;
read_bytes_from_table(L, 1, bytes);
lua::read_bytes_from_table(L, 1, bytes);
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);
@ -237,7 +216,7 @@ static int l_gzip_compress(lua::State* L) {
static int l_gzip_decompress(lua::State* L) {
std::vector<ubyte> bytes;
read_bytes_from_table(L, 1, bytes);
lua::read_bytes_from_table(L, 1, bytes);
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
int newTable = lua::gettop(L);

View File

@ -2,6 +2,7 @@
#include <typeindex>
#include <typeinfo>
#include <stdexcept>
#include <unordered_map>
#include "data/dv.hpp"
@ -698,4 +699,25 @@ namespace lua {
}
return def;
}
inline void read_bytes_from_table(
lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) {
if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected");
} else {
size_t size = lua::objlen(L, tableIndex);
for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, tableIndex);
const int byte = lua::tointeger(L, -1);
lua::pop(L);
if (byte < 0 || byte > 255) {
throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'"
);
}
bytes.push_back(byte);
}
}
}
}

View File

@ -2,6 +2,7 @@
#include <sstream>
#include "util/listutil.hpp"
#include "../lua_util.hpp"
using namespace lua;
@ -18,8 +19,16 @@ LuaBytearray::~LuaBytearray() {
static int l_append(lua::State* L) {
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
if (lua::isnumber(L, 2)) {
auto value = tointeger(L, 2);
buffer->data().push_back(static_cast<ubyte>(value));
} else if (lua::istable(L, 2)) {
lua::read_bytes_from_table(L, 2, buffer->data());
} else if (auto extension = lua::touserdata<LuaBytearray>(L, 2)) {
util::concat(buffer->data(), extension->data());
} else {
throw std::runtime_error("integer/table/Bytearray expected");
}
}
return 0;
}
@ -34,8 +43,19 @@ static int l_insert(lua::State* L) {
if (static_cast<size_t>(index) > data.size()) {
return 0;
}
if (lua::isnumber(L, 3)) {
auto value = tointeger(L, 3);
data.insert(data.begin() + index, static_cast<ubyte>(value));
} else if (lua::istable(L, 3)) {
std::vector<ubyte> temp;
lua::read_bytes_from_table(L, 3, temp);
data.insert(data.begin() + index, temp.begin(), temp.end());
} else if (auto extension = lua::touserdata<LuaBytearray>(L, 3)) {
const std::vector<ubyte>& src = extension->data();
data.insert(data.begin() + index, src.begin(), src.end());
} else {
throw std::runtime_error("integer/table/Bytearray expected");
}
return 0;
}