diff --git a/src/logic/scripting/lua/libs/libfile.cpp b/src/logic/scripting/lua/libs/libfile.cpp index d436bdfd..1e6beb25 100644 --- a/src/logic/scripting/lua/libs/libfile.cpp +++ b/src/logic/scripting/lua/libs/libfile.cpp @@ -149,24 +149,24 @@ static int l_read_bytes(lua::State* L) { ); } -static int read_bytes_from_table( +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 { - lua::pushnil(L); - while (lua::next(L, tableIndex - 1) != 0) { + 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); - lua::pop(L); } - return 1; } } @@ -181,14 +181,10 @@ static int l_write_bytes(lua::State* L) { } std::vector bytes; - int result = read_bytes_from_table(L, -1, bytes); - if (result != 1) { - return result; - } else { - return lua::pushboolean( - L, files::write_bytes(path, bytes.data(), bytes.size()) - ); - } + read_bytes_from_table(L, 2, bytes); + return lua::pushboolean( + L, files::write_bytes(path, bytes.data(), bytes.size()) + ); } static int l_list_all_res(lua::State* L, const std::string& path) { @@ -227,39 +223,29 @@ static int l_list(lua::State* L) { static int l_gzip_compress(lua::State* L) { std::vector bytes; - int result = read_bytes_from_table(L, -1, bytes); + read_bytes_from_table(L, 1, bytes); + auto compressed_bytes = gzip::compress(bytes.data(), bytes.size()); + int newTable = lua::gettop(L); - if (result != 1) { - return result; - } else { - 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; + 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 bytes; - int result = read_bytes_from_table(L, -1, bytes); + read_bytes_from_table(L, 1, bytes); + auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size()); + int newTable = lua::gettop(L); - if (result != 1) { - return result; - } else { - 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; + 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) {