From 20ef11e5a2dce1fe78739c39675c730030ee040b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 16 Jun 2024 21:00:50 +0300 Subject: [PATCH] add bytearray:append(b) --- src/files/files.cpp | 15 +++++++++++++++ src/files/files.hpp | 1 + src/logic/scripting/lua/libfile.cpp | 14 ++------------ src/logic/scripting/lua/lua_custom_types.cpp | 16 +++++++++++++++- src/logic/scripting/lua/lua_custom_types.hpp | 5 +++++ src/logic/scripting/lua/lua_util.hpp | 4 ++++ 6 files changed, 42 insertions(+), 13 deletions(-) diff --git a/src/files/files.cpp b/src/files/files.cpp index 644e8716..8b66fa55 100644 --- a/src/files/files.cpp +++ b/src/files/files.cpp @@ -78,6 +78,21 @@ std::unique_ptr files::read_bytes(const fs::path& filename, size_t& len return data; } +std::vector files::read_bytes(const fs::path& filename) { + std::ifstream input(filename, std::ios::binary); + if (!input.is_open()) + return {}; + input.seekg(0, std::ios_base::end); + size_t length = input.tellg(); + input.seekg(0, std::ios_base::beg); + + std::vector data(length); + data.resize(length); + input.read((char*)data.data(), length); + input.close(); + return data; +} + std::string files::read_string(const fs::path& filename) { size_t size; std::unique_ptr bytes (read_bytes(filename, size)); diff --git a/src/files/files.hpp b/src/files/files.hpp index 4e6ad960..1a124b25 100644 --- a/src/files/files.hpp +++ b/src/files/files.hpp @@ -57,6 +57,7 @@ namespace files { bool read(const fs::path&, char* data, size_t size); std::unique_ptr read_bytes(const fs::path&, size_t& length); + std::vector read_bytes(const fs::path&); std::string read_string(const fs::path& filename); /// @brief Read JSON or BJSON file diff --git a/src/logic/scripting/lua/libfile.cpp b/src/logic/scripting/lua/libfile.cpp index 87e9af7a..e3e88d17 100644 --- a/src/logic/scripting/lua/libfile.cpp +++ b/src/logic/scripting/lua/libfile.cpp @@ -109,18 +109,8 @@ static int l_file_mkdirs(lua::State* L) { static int l_file_read_bytes(lua::State* L) { fs::path path = resolve_path(lua::require_string(L, 1)); if (fs::is_regular_file(path)) { - size_t length = static_cast(fs::file_size(path)); - - auto bytes = files::read_bytes(path, length); - - lua::createtable(L, length, 0); - int newTable = lua::gettop(L); - - for(size_t i = 0; i < length; i++) { - lua::pushinteger(L, bytes[i]); - lua::rawseti(L, i+1, newTable); - } - return 1; + auto bytes = files::read_bytes(path); + return lua::newuserdata(L, std::move(bytes)); } throw std::runtime_error("file does not exists "+util::quote(path.u8string())); } diff --git a/src/logic/scripting/lua/lua_custom_types.cpp b/src/logic/scripting/lua/lua_custom_types.cpp index 91d44745..e3cdc643 100644 --- a/src/logic/scripting/lua/lua_custom_types.cpp +++ b/src/logic/scripting/lua/lua_custom_types.cpp @@ -18,6 +18,14 @@ Bytearray::Bytearray(std::vector buffer) : buffer(std::move(buffer)) { Bytearray::~Bytearray() { } +static int l_bytearray_append(lua::State* L) { + if (auto buffer = touserdata(L, 1)) { + auto value = tointeger(L, 2); + buffer->append(static_cast(value)); + } + return 0; +} + static int l_bytearray_meta_meta_call(lua::State* L) { auto size = tointeger(L, 2); if (size < 0) { @@ -28,6 +36,12 @@ static int l_bytearray_meta_meta_call(lua::State* L) { static int l_bytearray_meta_index(lua::State* L) { auto buffer = touserdata(L, 1); + if (isstring(L, 2)) { + std::string member = tostring(L, 2); + if (member == "append") { + return pushcfunction(L, l_bytearray_append); + } + } auto index = tointeger(L, 2)-1; if (buffer == nullptr || static_cast(index) > buffer->size()) { return 0; @@ -55,7 +69,7 @@ static int l_bytearray_meta_len(lua::State* L) { static int l_bytearray_meta_tostring(lua::State* L) { auto& buffer = *touserdata(L, 1); - if (buffer.size() > 128) { + if (buffer.size() > 512) { return pushstring(L, "bytearray["+std::to_string(buffer.size())+"]{...}"); } else { std::stringstream ss; diff --git a/src/logic/scripting/lua/lua_custom_types.hpp b/src/logic/scripting/lua/lua_custom_types.hpp index 4ea4960d..8f6d61e6 100644 --- a/src/logic/scripting/lua/lua_custom_types.hpp +++ b/src/logic/scripting/lua/lua_custom_types.hpp @@ -31,6 +31,11 @@ namespace lua { inline size_t size() const { return buffer.size(); } + + inline void append(ubyte b) { + buffer.push_back(b); + } + static int createMetatable(lua::State*); inline static std::string TYPENAME = "bytearray"; }; diff --git a/src/logic/scripting/lua/lua_util.hpp b/src/logic/scripting/lua/lua_util.hpp index a51d784c..af58ea11 100644 --- a/src/logic/scripting/lua/lua_util.hpp +++ b/src/logic/scripting/lua/lua_util.hpp @@ -49,6 +49,10 @@ namespace lua { inline const char* type_name(lua::State* L, int idx) { return lua_typename(L, idx); } + inline int rawget(lua::State* L, int idx=-2) { + lua_rawget(L, idx); + return 1; + } inline int rawgeti(lua::State* L, int n, int idx=-1) { lua_rawgeti(L, idx, n); return 1;