Skip to content

Commit

Permalink
Merge branch 'main' into headless-mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MihailRis committed Dec 21, 2024
2 parents 4363883 + c9b69fd commit 1149d58
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 33 deletions.
13 changes: 9 additions & 4 deletions res/modules/data_buffer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,14 @@ function data_buffer:put_byte(byte)
end

function data_buffer:put_bytes(bytes)
for i = 1, #bytes do
self:put_byte(bytes[i])
end
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)
Expand Down Expand Up @@ -308,4 +313,4 @@ end

setmetatable(data_buffer, data_buffer)

return data_buffer
return data_buffer
2 changes: 1 addition & 1 deletion res/scripts/stdmin.lua
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 3 additions & 24 deletions src/logic/scripting/lua/libs/libfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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())
);
Expand Down Expand Up @@ -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);

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

Expand Down
22 changes: 22 additions & 0 deletions src/logic/scripting/lua/lua_util.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <typeindex>
#include <typeinfo>
#include <stdexcept>
#include <unordered_map>

#include "data/dv.hpp"
Expand Down Expand Up @@ -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);
}
}
}
}
28 changes: 24 additions & 4 deletions src/logic/scripting/lua/usertypes/lua_type_bytearray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <sstream>

#include "util/listutil.hpp"
#include "../lua_util.hpp"

using namespace lua;
Expand All @@ -18,8 +19,16 @@ LuaBytearray::~LuaBytearray() {

static int l_append(lua::State* L) {
if (auto buffer = touserdata<LuaBytearray>(L, 1)) {
auto value = tointeger(L, 2);
buffer->data().push_back(static_cast<ubyte>(value));
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;
}
Expand All @@ -34,8 +43,19 @@ static int l_insert(lua::State* L) {
if (static_cast<size_t>(index) > data.size()) {
return 0;
}
auto value = tointeger(L, 3);
data.insert(data.begin() + index, static_cast<ubyte>(value));
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;
}

Expand Down

0 comments on commit 1149d58

Please sign in to comment.