Merge branch 'MihailRis:main' into main

This commit is contained in:
Onran 2024-12-06 11:47:54 +09:00 committed by GitHub
commit 6a0bdde3b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 63 additions and 37 deletions

View File

@ -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

View File

@ -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

View File

@ -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(

View File

@ -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)
## Разделы

View File

@ -39,7 +39,7 @@ network.tcp_connect(
```lua
-- Отправляет массив байт
socket:send(table|ByteArray)
socket:send(table|ByteArray|str)
-- Читает полученные данные
socket:recv(

View File

@ -1,6 +1,6 @@
{
"id": "base",
"title": "Base",
"version": "0.25",
"version": "0.26",
"description": "basic content package"
}

View File

@ -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<ImageData> 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<Texture> 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(),

View File

@ -6,7 +6,7 @@
#include <string>
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;

View File

@ -20,7 +20,9 @@ std::shared_ptr<Inventory> Inventories::create(size_t size) {
std::shared_ptr<Inventory> Inventories::createVirtual(size_t size) {
int64_t id;
do {
id = -std::max<int64_t>(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<int64_t>(1LL, std::llabs(random.rand64() % 1000'000'000));
} while (map.find(id) != map.end());
auto inv = std::make_shared<Inventory>(id, size);

View File

@ -87,6 +87,9 @@ static int l_send(lua::State* L) {
connection->send(
reinterpret_cast<char*>(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<char> buffer(glm::min(length, connection->available()));
length = glm::min(length, connection->available());
util::Buffer<char> buffer(length);
int size = connection->recv(buffer.data(), length);
if (size == -1) {

View File

@ -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<std::thread>([this]() { startListen();});
}
void connect(runnable callback) override {
thread = std::make_unique<std::thread>([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<SocketConnection>(
clientDescriptor, address
);
socket->startClient();
u64id_t id = network->addConnection(socket);
{
std::lock_guard lock(clientsMutex);