diff --git a/README.md b/README.md index d7985dee..418d372c 100644 --- a/README.md +++ b/README.md @@ -62,6 +62,12 @@ If you use Wayland sudo pacman -S glfw-wayland glew glm libpng libvorbis openal luajit libcurl ``` +And you need entt. In yay you can use + +```sh +yay -S entt +``` + ### Build engine with CMake ```sh diff --git a/doc/en/main-page.md b/doc/en/main-page.md index 21d4dc95..67db8c10 100644 --- a/doc/en/main-page.md +++ b/doc/en/main-page.md @@ -1,6 +1,8 @@ # Documentation -Documentation for stable release 0.24.x. +Documentation for in-development version 0.26. + +[Documentation for stable release 0.25.x.](https://github.com/MihailRis/VoxelEngine-Cpp/blob/release-0.25/doc/en/main-page.md) ## Sections diff --git a/doc/en/scripting/builtins/libnetwork.md b/doc/en/scripting/builtins/libnetwork.md index 8f4b01eb..48caefbd 100644 --- a/doc/en/scripting/builtins/libnetwork.md +++ b/doc/en/scripting/builtins/libnetwork.md @@ -39,7 +39,7 @@ The Socket class has the following methods: ```lua -- Sends a byte array -socket:send(table|ByteArray) +socket:send(table|ByteArray|str) -- Reads the received data socket:recv( diff --git a/doc/ru/main-page.md b/doc/ru/main-page.md index 05269cee..17dbe6ba 100644 --- a/doc/ru/main-page.md +++ b/doc/ru/main-page.md @@ -1,6 +1,8 @@ # Документация -Документация стабильной версии 0.25.x. +Документация разрабатываемой версии 0.26. + +[Документация стабильной версии 0.25.x.](https://github.com/MihailRis/VoxelEngine-Cpp/blob/release-0.25/doc/ru/main-page.md) ## Разделы diff --git a/doc/ru/scripting/builtins/libnetwork.md b/doc/ru/scripting/builtins/libnetwork.md index a2304ed9..7fedf2bd 100644 --- a/doc/ru/scripting/builtins/libnetwork.md +++ b/doc/ru/scripting/builtins/libnetwork.md @@ -39,7 +39,7 @@ network.tcp_connect( ```lua -- Отправляет массив байт -socket:send(table|ByteArray) +socket:send(table|ByteArray|str) -- Читает полученные данные socket:recv( diff --git a/res/content/base/package.json b/res/content/base/package.json index 7645a6e0..2878d3a6 100644 --- a/res/content/base/package.json +++ b/res/content/base/package.json @@ -1,6 +1,6 @@ { "id": "base", "title": "Base", - "version": "0.25", + "version": "0.26", "description": "basic content package" } diff --git a/src/coders/png.cpp b/src/coders/png.cpp index 60965ed1..b29afd34 100644 --- a/src/coders/png.cpp +++ b/src/coders/png.cpp @@ -13,7 +13,7 @@ static debug::Logger logger("png-coder"); // returns 0 if all-right, 1 otherwise -int _png_write( +static int png_write( const char* filename, uint width, uint height, const ubyte* data, bool alpha ) { uint pixsize = alpha ? 4 : 3; @@ -112,7 +112,7 @@ static void read_in_memory(png_structp pngPtr, png_bytep dst, png_size_t toread) } std::unique_ptr png::load_image(const ubyte* bytes, size_t size) { - if (!png_check_sig(bytes, size)) { + if (size < 8 || !png_check_sig(bytes, 8)) { throw std::runtime_error("invalid png signature"); } png_structp pngPtr = nullptr; @@ -223,7 +223,7 @@ std::unique_ptr png::load_texture(const std::string& filename) { } void png::write_image(const std::string& filename, const ImageData* image) { - _png_write( + png_write( filename.c_str(), image->getWidth(), image->getHeight(), diff --git a/src/constants.hpp b/src/constants.hpp index 84d8b22c..c8de5a8d 100644 --- a/src/constants.hpp +++ b/src/constants.hpp @@ -6,7 +6,7 @@ #include inline constexpr int ENGINE_VERSION_MAJOR = 0; -inline constexpr int ENGINE_VERSION_MINOR = 25; +inline constexpr int ENGINE_VERSION_MINOR = 26; #ifdef NDEBUG inline constexpr bool ENGINE_DEBUG_BUILD = false; @@ -14,7 +14,7 @@ inline constexpr bool ENGINE_DEBUG_BUILD = false; inline constexpr bool ENGINE_DEBUG_BUILD = true; #endif // NDEBUG -inline const std::string ENGINE_VERSION_STRING = "0.25"; +inline const std::string ENGINE_VERSION_STRING = "0.26"; /// @brief world regions format version inline constexpr uint REGION_FORMAT_VERSION = 3; diff --git a/src/items/Inventories.cpp b/src/items/Inventories.cpp index d3af607d..4d2baa36 100644 --- a/src/items/Inventories.cpp +++ b/src/items/Inventories.cpp @@ -20,7 +20,9 @@ std::shared_ptr Inventories::create(size_t size) { std::shared_ptr Inventories::createVirtual(size_t size) { int64_t id; do { - id = -std::max(1LL, std::llabs(random.rand64())); + // lua does not support long integers because Number is floating-point + // type. Changing int_consumer to use 64 bit integer does not change anything + id = -std::max(1LL, std::llabs(random.rand64() % 1000'000'000)); } while (map.find(id) != map.end()); auto inv = std::make_shared(id, size); diff --git a/src/logic/scripting/lua/libs/libnetwork.cpp b/src/logic/scripting/lua/libs/libnetwork.cpp index cce00067..dc752710 100644 --- a/src/logic/scripting/lua/libs/libnetwork.cpp +++ b/src/logic/scripting/lua/libs/libnetwork.cpp @@ -87,6 +87,9 @@ static int l_send(lua::State* L) { connection->send( reinterpret_cast(bytes->data().data()), bytes->data().size() ); + } else if (lua::isstring(L, 2)) { + auto string = lua::tolstring(L, 2); + connection->send(string.data(), string.length()); } return 0; } @@ -98,7 +101,8 @@ static int l_recv(lua::State* L) { if (connection == nullptr) { return 0; } - util::Buffer buffer(glm::min(length, connection->available())); + length = glm::min(length, connection->available()); + util::Buffer buffer(length); int size = connection->recv(buffer.data(), length); if (size == -1) { diff --git a/src/network/Network.cpp b/src/network/Network.cpp index 5036d65a..57fcab40 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -293,37 +293,46 @@ public: } } + void startListen() { + while (state == ConnectionState::CONNECTED) { + int size = recvsocket(descriptor, buffer.data(), buffer.size()); + if (size == 0) { + logger.info() << "closed connection with " << to_string(addr); + closesocket(descriptor); + state = ConnectionState::CLOSED; + break; + } else if (size < 0) { + logger.warning() << "an error ocurred while receiving from " + << to_string(addr); + auto error = handle_socket_error("recv(...) error"); + closesocket(descriptor); + state = ConnectionState::CLOSED; + logger.error() << error.what(); + break; + } + { + std::lock_guard lock(mutex); + for (size_t i = 0; i < size; i++) { + readBatch.emplace_back(buffer[i]); + } + totalDownload += size; + } + logger.debug() << "read " << size << " bytes from " << to_string(addr); + } + } + + void startClient() { + state = ConnectionState::CONNECTED; + thread = std::make_unique([this]() { startListen();}); + } + void connect(runnable callback) override { thread = std::make_unique([this, callback]() { connectSocket(); if (state == ConnectionState::CONNECTED) { callback(); } - while (state == ConnectionState::CONNECTED) { - int size = recvsocket(descriptor, buffer.data(), buffer.size()); - if (size == 0) { - logger.info() << "closed connection with " << to_string(addr); - closesocket(descriptor); - state = ConnectionState::CLOSED; - break; - } else if (size < 0) { - logger.info() << "an error ocurred while receiving from " - << to_string(addr); - auto error = handle_socket_error("recv(...) error"); - closesocket(descriptor); - state = ConnectionState::CLOSED; - logger.error() << error.what(); - break; - } - { - std::lock_guard lock(mutex); - for (size_t i = 0; i < size; i++) { - readBatch.emplace_back(buffer[i]); - } - totalDownload += size; - } - logger.info() << "read " << size << " bytes from " << to_string(addr); - } + startListen(); }); } @@ -459,6 +468,7 @@ public: auto socket = std::make_shared( clientDescriptor, address ); + socket->startClient(); u64id_t id = network->addConnection(socket); { std::lock_guard lock(clientsMutex);