diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index a27ac949..a38081c9 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -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& 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 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 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 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); diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index 983abb39..f9da3b21 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -2,6 +2,7 @@ #include #include +#include #include #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& 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); + } + } + } } diff --git a/src/logic/scripting/lua/usertypes/lua_type_bytearray.cpp b/src/logic/scripting/lua/usertypes/lua_type_bytearray.cpp index 0493995b..c982d2d8 100644 --- a/src/logic/scripting/lua/usertypes/lua_type_bytearray.cpp +++ b/src/logic/scripting/lua/usertypes/lua_type_bytearray.cpp @@ -2,6 +2,7 @@ #include +#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(L, 1)) { - auto value = tointeger(L, 2); - buffer->data().push_back(static_cast(value)); + if (lua::isnumber(L, 2)) { + auto value = tointeger(L, 2); + buffer->data().push_back(static_cast(value)); + } else if (lua::istable(L, 2)) { + lua::read_bytes_from_table(L, 2, buffer->data()); + } else if (auto extension = lua::touserdata(L, 2)) { + util::concat(buffer->data(), extension->data()); + } else { + throw std::runtime_error("integer/table/Bytearray expected"); + } } return 0; }