fix file.write_bytes

This commit is contained in:
MihailRis 2024-11-22 07:13:49 +03:00
parent 5092cff730
commit 0fec17a8b6

View File

@ -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<ubyte>& bytes lua::State* L, int tableIndex, std::vector<ubyte>& bytes
) { ) {
if (!lua::istable(L, tableIndex)) { if (!lua::istable(L, tableIndex)) {
throw std::runtime_error("table expected"); throw std::runtime_error("table expected");
} else { } else {
lua::pushnil(L); size_t size = lua::objlen(L, tableIndex);
while (lua::next(L, tableIndex - 1) != 0) { for (size_t i = 0; i < size; i++) {
lua::rawgeti(L, i + 1, tableIndex);
const int byte = lua::tointeger(L, -1); const int byte = lua::tointeger(L, -1);
lua::pop(L);
if (byte < 0 || byte > 255) { if (byte < 0 || byte > 255) {
throw std::runtime_error( throw std::runtime_error(
"invalid byte '" + std::to_string(byte) + "'" "invalid byte '" + std::to_string(byte) + "'"
); );
} }
bytes.push_back(byte); bytes.push_back(byte);
lua::pop(L);
} }
return 1;
} }
} }
@ -181,15 +181,11 @@ static int l_write_bytes(lua::State* L) {
} }
std::vector<ubyte> bytes; std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes); read_bytes_from_table(L, 2, bytes);
if (result != 1) {
return result;
} else {
return lua::pushboolean( return lua::pushboolean(
L, files::write_bytes(path, bytes.data(), bytes.size()) L, files::write_bytes(path, bytes.data(), bytes.size())
); );
} }
}
static int l_list_all_res(lua::State* L, const std::string& path) { static int l_list_all_res(lua::State* L, const std::string& path) {
auto files = engine->getResPaths()->listdirRaw(path); auto files = engine->getResPaths()->listdirRaw(path);
@ -227,11 +223,7 @@ static int l_list(lua::State* L) {
static int l_gzip_compress(lua::State* L) { static int l_gzip_compress(lua::State* L) {
std::vector<ubyte> bytes; std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes); read_bytes_from_table(L, 1, bytes);
if (result != 1) {
return result;
} else {
auto compressed_bytes = gzip::compress(bytes.data(), bytes.size()); auto compressed_bytes = gzip::compress(bytes.data(), bytes.size());
int newTable = lua::gettop(L); int newTable = lua::gettop(L);
@ -241,16 +233,11 @@ static int l_gzip_compress(lua::State* L) {
} }
return 1; return 1;
} }
}
static int l_gzip_decompress(lua::State* L) { static int l_gzip_decompress(lua::State* L) {
std::vector<ubyte> bytes; std::vector<ubyte> bytes;
int result = read_bytes_from_table(L, -1, bytes); read_bytes_from_table(L, 1, bytes);
if (result != 1) {
return result;
} else {
auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size()); auto decompressed_bytes = gzip::decompress(bytes.data(), bytes.size());
int newTable = lua::gettop(L); int newTable = lua::gettop(L);
@ -260,7 +247,6 @@ static int l_gzip_decompress(lua::State* L) {
} }
return 1; return 1;
} }
}
static int l_read_combined_list(lua::State* L) { static int l_read_combined_list(lua::State* L) {
std::string path = lua::require_string(L, 1); std::string path = lua::require_string(L, 1);