Merge branch 'main' into dev

This commit is contained in:
MihailRis 2026-10-27 13:03:13 +03:00
commit 575bfb28e0
14 changed files with 95 additions and 31 deletions

View file

@ -30,10 +30,14 @@ local __vc__app_script_coroutine
local __vc_process_streams = { stdin = {}, stdout = {} } local __vc_process_streams = { stdin = {}, stdout = {} }
local function complete_app_lib(app) local function complete_app_lib(app)
local tick_required = true
local __app_load_content = app.load_content local __app_load_content = app.load_content
local __app_reset_content = app.reset_content local __app_reset_content = app.reset_content
local __app_reconfig_packs = app.reconfig_packs local __app_reconfig_packs = app.reconfig_packs
local __app_tick = coroutine.yield local __app_tick = function ()
coroutine.yield()
tick_required = false
end
local __app_set_setting = app.set_setting local __app_set_setting = app.set_setting
local __app_quit = app.quit local __app_quit = app.quit
local __app_get_stdout_stream = app.get_stdout_stream local __app_get_stdout_stream = app.get_stdout_stream
@ -90,6 +94,9 @@ local function complete_app_lib(app)
func(...) func(...)
return return
end end
if tick_required then
__app_tick()
end
local running = coroutine.running() local running = coroutine.running()
if not running or running ~= __vc__app_script_coroutine then if not running or running ~= __vc__app_script_coroutine then
error("content must be reload in application script coroutine") error("content must be reload in application script coroutine")

View file

@ -12,7 +12,7 @@ using namespace imageio;
using image_reader = using image_reader =
std::function<std::unique_ptr<ImageData>(const ubyte*, size_t)>; std::function<std::unique_ptr<ImageData>(const ubyte*, size_t)>;
using image_writer = std::function<void(const std::string&, const ImageData*)>; using image_writer = std::function<void(const io::path&, const ImageData*)>;
static std::unordered_map<ImageFileFormat, image_reader> readers { static std::unordered_map<ImageFileFormat, image_reader> readers {
{ImageFileFormat::PNG, png::load_image}, {ImageFileFormat::PNG, png::load_image},
@ -75,7 +75,7 @@ void imageio::write(const io::path& file, const ImageData* image) {
"file format is not supported (write): " + file.string() "file format is not supported (write): " + file.string()
); );
} }
return found->second(io::resolve(file).u8string(), image); return found->second(file, image);
} }
util::Buffer<unsigned char> imageio::encode( util::Buffer<unsigned char> imageio::encode(

View file

@ -47,8 +47,25 @@ std::unique_ptr<audio::PCM> ogg::load_pcm(
const io::path& file, bool headerOnly const io::path& file, bool headerOnly
) { ) {
OggVorbis_File vf; OggVorbis_File vf;
int code; FILE* fileHandle = nullptr;
if ((code = ov_fopen(io::resolve(file).u8string().c_str(), &vf))) { #ifdef _WIN32
if (errno_t err =
_wfopen_s(
&fileHandle, io::resolve(file).wstring().c_str(), L"rb"
) != 0) {
#else
if ((fileHandle = fopen(io::resolve(file).u8string().c_str(), "rb")) ==
nullptr) {
#endif
throw std::runtime_error(
"could not open file " + file.string() + " for reading"
);
}
if (int code = ov_open(fileHandle, &vf, nullptr, 0)) {
if (fileHandle) {
fclose(fileHandle);
}
throw std::runtime_error("vorbis: " + vorbis_error_message(code)); throw std::runtime_error("vorbis: " + vorbis_error_message(code));
} }
std::vector<char> data; std::vector<char> data;
@ -169,8 +186,25 @@ public:
std::unique_ptr<PCMStream> ogg::create_stream(const io::path& file) { std::unique_ptr<PCMStream> ogg::create_stream(const io::path& file) {
OggVorbis_File vf; OggVorbis_File vf;
int code; FILE* fileHandle = nullptr;
if ((code = ov_fopen(io::resolve(file).u8string().c_str(), &vf))) { #ifdef _WIN32
if (errno_t err =
_wfopen_s(
&fileHandle, io::resolve(file).wstring().c_str(), L"rb"
) != 0) {
#else
if ((fileHandle = fopen(io::resolve(file).u8string().c_str(), "rb")) ==
nullptr) {
#endif
throw std::runtime_error(
"could not open file " + file.string() + " for reading"
);
}
if (int code = ov_open(fileHandle, &vf, nullptr, 0)) {
if (fileHandle) {
fclose(fileHandle);
}
throw std::runtime_error("vorbis: " + vorbis_error_message(code)); throw std::runtime_error("vorbis: " + vorbis_error_message(code));
} }
return std::make_unique<OggStream>(vf); return std::make_unique<OggStream>(vf);

View file

@ -67,14 +67,20 @@ static util::Buffer<ubyte> write_to_memory(uint width, uint height, const ubyte*
// returns 0 if all-right, 1 otherwise // returns 0 if all-right, 1 otherwise
static int png_write( static int png_write(
const char* filename, uint width, uint height, const ubyte* data, bool alpha const io::path& file, uint width, uint height, const ubyte* data, bool alpha
) { ) {
uint pixsize = alpha ? 4 : 3; uint pixsize = alpha ? 4 : 3;
// Open file for writing (binary mode) // Open file for writing (binary mode)
FILE* fp = fopen(filename, "wb"); FILE* fp = nullptr;
if (fp == nullptr) { #ifdef _WIN32
logger.error() << "could not open file " << filename << " for writing"; if (errno_t err =
_wfopen_s(&fp, io::resolve(file).wstring().c_str(), L"wb") != 0) {
#else
if ((fp = fopen(io::resolve(file).u8string().c_str(), "wb")) == nullptr) {
#endif
logger.error() << "could not open file " << file.string()
<< " for writing";
return 1; return 1;
} }
@ -290,9 +296,9 @@ std::unique_ptr<Texture> png::load_texture(const std::string& filename) {
} }
} }
void png::write_image(const std::string& filename, const ImageData* image) { void png::write_image(const io::path& file, const ImageData* image) {
png_write( png_write(
filename.c_str(), file,
image->getWidth(), image->getWidth(),
image->getHeight(), image->getHeight(),
(const ubyte*)image->getData(), (const ubyte*)image->getData(),

View file

@ -8,10 +8,13 @@
class Texture; class Texture;
class ImageData; class ImageData;
namespace io {
class path;
}
namespace png { namespace png {
std::unique_ptr<ImageData> load_image(const ubyte* bytes, size_t size); std::unique_ptr<ImageData> load_image(const ubyte* bytes, size_t size);
void write_image(const std::string& filename, const ImageData* image); void write_image(const io::path& file, const ImageData* image);
util::Buffer<ubyte> encode_image(const ImageData& image); util::Buffer<ubyte> encode_image(const ImageData& image);
std::unique_ptr<Texture> load_texture(const ubyte* bytes, size_t size); std::unique_ptr<Texture> load_texture(const ubyte* bytes, size_t size);
std::unique_ptr<Texture> load_texture(const std::string& filename); std::unique_ptr<Texture> load_texture(const std::string& filename);

View file

@ -68,8 +68,8 @@ static void write(
} }
} }
void Logger::init(const std::string& filename) { void Logger::init(const std::filesystem::path& path) {
file.open(filename); file.open(path);
time_t tm = std::time(nullptr); time_t tm = std::time(nullptr);
std::stringstream ss; std::stringstream ss;

View file

@ -1,6 +1,7 @@
#pragma once #pragma once
#include <sstream> #include <sstream>
#include <filesystem>
namespace debug { namespace debug {
enum class LogLevel { print, debug, info, warning, error }; enum class LogLevel { print, debug, info, warning, error };
@ -27,7 +28,7 @@ namespace debug {
class Logger { class Logger {
std::string name; std::string name;
public: public:
static void init(const std::string& filename); static void init(const std::filesystem::path& path);
static void flush(); static void flush();
Logger(const std::string& name) : name(name) { Logger(const std::string& name) : name(name) {

View file

@ -16,6 +16,9 @@ AppScriptsControl::AppScriptsControl(const CoreParameters& params) {
if (io::exists(scriptFile)) { if (io::exists(scriptFile)) {
logger.info() << "starting script: " << params.scriptFile.u8string(); logger.info() << "starting script: " << params.scriptFile.u8string();
scriptCoroutine = scripting::start_app_script(scriptFile); scriptCoroutine = scripting::start_app_script(scriptFile);
if (scriptCoroutine) {
scriptCoroutine->update();
}
} else { } else {
logger.warning() << "script does not exists: " << params.scriptFile.u8string(); logger.warning() << "script does not exists: " << params.scriptFile.u8string();
} }

View file

@ -124,7 +124,7 @@ static int l_set(lua::State* L) {
auto chunksController = controller->getChunksController(); auto chunksController = controller->getChunksController();
if (chunksController == nullptr) { if (chunksController == nullptr) {
return 1; return 0;
} }
if (chunksController->lighting) { if (chunksController->lighting) {
Lighting& lighting = *chunksController->lighting; Lighting& lighting = *chunksController->lighting;
@ -682,7 +682,7 @@ static int l_get_field(lua::State* L) {
return 0; return 0;
} }
auto lx = x - cx * CHUNK_W; auto lx = x - cx * CHUNK_W;
auto lz = z - cz * CHUNK_W; auto lz = z - cz * CHUNK_D;
size_t voxelIndex = vox_index(lx, y, lz); size_t voxelIndex = vox_index(lx, y, lz);
const auto& vox = chunk->voxels[voxelIndex]; const auto& vox = chunk->voxels[voxelIndex];
@ -751,7 +751,7 @@ static int l_set_field(lua::State* L) {
auto cx = floordiv(x, CHUNK_W); auto cx = floordiv(x, CHUNK_W);
auto cz = floordiv(z, CHUNK_D); auto cz = floordiv(z, CHUNK_D);
auto lx = x - cx * CHUNK_W; auto lx = x - cx * CHUNK_W;
auto lz = z - cz * CHUNK_W; auto lz = z - cz * CHUNK_D;
auto chunk = blocks_agent::get_chunk(*level.chunks, cx, cz); auto chunk = blocks_agent::get_chunk(*level.chunks, cx, cz);
if (chunk == nullptr || y < 0 || y >= CHUNK_H) { if (chunk == nullptr || y < 0 || y >= CHUNK_H) {
return 0; return 0;

View file

@ -183,7 +183,7 @@ static int l_push_ray(
lua::pushvec3(L, ray ? ray->normal : normal); lua::pushvec3(L, ray ? ray->normal : normal);
lua::setfield(L, "normal"); lua::setfield(L, "normal");
lua::pushnumber(L, glm::distance(start, end)); lua::pushnumber(L, ray ? ray->distance : glm::distance(start, end));
lua::setfield(L, "length"); lua::setfield(L, "length");
lua::pushvec3(L, iend); lua::pushvec3(L, iend);
@ -306,12 +306,12 @@ static int l_world_raycast(lua::State* L) {
entityRaycast.ignoredUid = lua::tointeger(L, -1); entityRaycast.ignoredUid = lua::tointeger(L, -1);
lua::pop(L); lua::pop(L);
} }
if (lua::getfield(L, "filter_entities")) { if (lua::getfield(L, "filter_entities", 1)) {
load_entities_filter(L, -1, filteredEntities); load_entities_filter(L, -1, filteredEntities);
blocksRaycast.filter = &filteredEntities; entityRaycast.entitiesFilter = &filteredEntities;
lua::pop(L); lua::pop(L);
} }
if (lua::getfield(L, "entities_exclusion")) { if (lua::getfield(L, "entities_exclusion", 1)) {
entityRaycast.entityFilterExcludeMode = lua::toboolean(L, -1); entityRaycast.entityFilterExcludeMode = lua::toboolean(L, -1);
lua::pop(L); lua::pop(L);
} }
@ -324,7 +324,7 @@ static int l_world_raycast(lua::State* L) {
blocksRaycast.filter = &filteredBlocks; blocksRaycast.filter = &filteredBlocks;
lua::pop(L); lua::pop(L);
} }
if (lua::getfield(L, "blocks_exclusion")) { if (lua::getfield(L, "blocks_exclusion", 1)) {
blocksRaycast.blocksFilterExcludeMode = lua::toboolean(L, -1); blocksRaycast.blocksFilterExcludeMode = lua::toboolean(L, -1);
lua::pop(L); lua::pop(L);
} }
@ -357,7 +357,7 @@ static int l_raycast(lua::State* L) {
} }
} }
if (luaStackSize >= 7) { if (luaStackSize >= 7) {
blocksRaycast.includeNonSelectable = lua::toboolean(L, 6); blocksRaycast.includeNonSelectable = lua::toboolean(L, 7);
} }
return l_raycast( return l_raycast(
L, start, dir, maxDistance, blocksRaycast, entityRaycast, true L, start, dir, maxDistance, blocksRaycast, entityRaycast, true

View file

@ -47,7 +47,7 @@ int main(int argc, char** argv) {
? ".sub" + std::to_string(coreParameters.subProcessDepth) ? ".sub" + std::to_string(coreParameters.subProcessDepth)
: "") + ".log"s; : "") + ".log"s;
} }
debug::Logger::init(logFile.u8string()); debug::Logger::init(logFile);
platform::configure_encoding(); platform::configure_encoding();
auto& engine = Engine::getInstance(); auto& engine = Engine::getInstance();

View file

@ -1,15 +1,14 @@
#include "commons.hpp" #include "commons.hpp"
#pragma comment(lib, "Ws2_32.lib")
#define NOMINMAX
#include <stdexcept> #include <stdexcept>
#include <limits> #include <limits>
#include <queue> #include <queue>
#include <thread> #include <thread>
#ifdef _WIN32 #ifdef _WIN32
#define NOMINMAX
#include <curl/curl.h> #include <curl/curl.h>
#pragma comment(lib, "Ws2_32.lib")
#define SHUT_RDWR SD_BOTH #define SHUT_RDWR SD_BOTH
#else #else
#include <sys/types.h> #include <sys/types.h>

View file

@ -7,6 +7,14 @@
#if defined(_WIN32) #if defined(_WIN32)
#include <malloc.h> #include <malloc.h>
#elif defined(__ANDROID__)
static inline void* aligned_alloc(size_t alignment, size_t size) {
void* ptr = nullptr;
if (posix_memalign(&ptr, alignment, size) != 0) {
return nullptr;
}
return ptr;
}
#endif #endif
namespace util { namespace util {
@ -63,6 +71,8 @@ namespace util {
std::unique_ptr<void, AlignedDeleter> ptr( std::unique_ptr<void, AlignedDeleter> ptr(
#if defined(_WIN32) #if defined(_WIN32)
_aligned_malloc(sizeof(T), alignof(T)) _aligned_malloc(sizeof(T), alignof(T))
#elif defined(__ANDROID__)
aligned_alloc(sizeof(T), alignof(T))
#else #else
std::aligned_alloc(alignof(T), sizeof(T)) std::aligned_alloc(alignof(T), sizeof(T))
#endif #endif

View file

@ -38,6 +38,7 @@ TEST(SurroundMap, InitTest) {
#include "coders/png.hpp" #include "coders/png.hpp"
#include "graphics/core/ImageData.hpp" #include "graphics/core/ImageData.hpp"
#include "io/path.hpp"
void visualize(const SurroundMap& map, int mul, int max) { void visualize(const SurroundMap& map, int mul, int max) {
const auto& areaMap = map.getArea(); const auto& areaMap = map.getArea();
@ -59,7 +60,7 @@ void visualize(const SurroundMap& map, int mul, int max) {
bytes[(y * w + x) * 3 + 2] = val; bytes[(y * w + x) * 3 + 2] = val;
} }
} }
png::write_image("test.png", &image); png::write_image("user:test.png", &image);
} }
TEST(SurroundMap, Visualize) { TEST(SurroundMap, Visualize) {