From 51859c1501c9403d9a589bbe0959d1b896b3f8fb Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 13:13:27 +0300 Subject: [PATCH 01/11] add simple tcp test --- dev/tests/network.lua | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 dev/tests/network.lua diff --git a/dev/tests/network.lua b/dev/tests/network.lua new file mode 100644 index 00000000..12d0fe93 --- /dev/null +++ b/dev/tests/network.lua @@ -0,0 +1,19 @@ +local data = "hello, world" +local complete = false + +network.tcp_open(7645, function (client) + start_coroutine(function() + local received = client:recv(1024) + if received then + assert (data, utf8.tostring(received)) + complete = true + end + coroutine.yield() + end, "client-listener") +end) + +network.tcp_connect("localhost", 7645, function (socket) + socket:send(data) +end) + +app.sleep_until(function () return complete end) From ee663fa322de3496e92c673b63c80f495bf2695e Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 13:36:32 +0300 Subject: [PATCH 02/11] improve setsockopt error message --- src/network/Network.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/network/Network.cpp b/src/network/Network.cpp index d60dcb67..81e02408 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -594,6 +594,8 @@ public: flags |= SO_REUSEPORT; # endif if (setsockopt(descriptor, SOL_SOCKET, flags, (const char*)&opt, sizeof(opt))) { + logger.error() << "setsockopt(SO_REUSEADDR) failed with errno: " + << errno << "(" << std::strerror(errno) << ")"; closesocket(descriptor); throw std::runtime_error("setsockopt"); } From f531d2e358b7f3ce1b6caa5d5d266b03639ca422 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 13:45:07 +0300 Subject: [PATCH 03/11] fix opening tcp server on macos --- src/network/Network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/Network.cpp b/src/network/Network.cpp index 81e02408..d591fc5b 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -590,7 +590,7 @@ public: } int opt = 1; int flags = SO_REUSEADDR; -# ifndef _WIN32 +# if !defined(_WIN32) && !defined(__APPLE__) flags |= SO_REUSEPORT; # endif if (setsockopt(descriptor, SOL_SOCKET, flags, (const char*)&opt, sizeof(opt))) { From 40eda93398ac3018226705c839848e6f7f1dbd68 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 14:11:34 +0300 Subject: [PATCH 04/11] update network test and fix coroutines errors handling --- dev/tests/network.lua | 49 +++++++++++++++++++++++++++++------------- res/scripts/stdlib.lua | 21 ++++++++++-------- 2 files changed, 46 insertions(+), 24 deletions(-) diff --git a/dev/tests/network.lua b/dev/tests/network.lua index 12d0fe93..36f41b62 100644 --- a/dev/tests/network.lua +++ b/dev/tests/network.lua @@ -1,19 +1,38 @@ -local data = "hello, world" -local complete = false +for i=1,3 do + local text = "" + local complete = false -network.tcp_open(7645, function (client) - start_coroutine(function() - local received = client:recv(1024) - if received then - assert (data, utf8.tostring(received)) + for j=1,100 do + text = text .. math.random(0, 9) + end + + local server = network.tcp_open(7645, function (client) + start_coroutine(function() + local received_text = "" + while client:is_alive() do + local received = client:recv(512) + if received then + received_text = received_text .. utf8.tostring(received) + end + coroutine.yield() + end + assert (received_text == text) complete = true - end - coroutine.yield() - end, "client-listener") -end) + end, "client-listener") + end) -network.tcp_connect("localhost", 7645, function (socket) - socket:send(data) -end) + network.tcp_connect("localhost", 7645, function (socket) + start_coroutine(function() + local ptr = 1 + while ptr < #text do + local n = math.random(1, 20) + socket:send(string.sub(text, ptr, ptr + n - 1)) + ptr = ptr + n + end + socket:close() + end, "data-sender") + end) -app.sleep_until(function () return complete end) + app.sleep_until(function () return complete end) + server:close() +end diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 2a0c0173..217723a9 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -531,15 +531,18 @@ function start_coroutine(chunk, name) local co = coroutine.create(function() local status, error = xpcall(chunk, function(err) local fullmsg = "error: "..string.match(err, ": (.+)").."\n"..debug.traceback() - gui.alert(fullmsg, function() - if world.is_open() then - __vc_app.close_world() - else - __vc_app.reset_content() - menu:reset() - menu.page = "main" - end - end) + + if hud then + gui.alert(fullmsg, function() + if world.is_open() then + __vc_app.close_world() + else + __vc_app.reset_content() + menu:reset() + menu.page = "main" + end + end) + end return fullmsg end) if not status then From 99940da42697ca015186a7a2c8b33694e6cef881 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 14:37:11 +0300 Subject: [PATCH 05/11] add 'asserts' library --- res/modules/internal/asserts.lua | 10 ++++++++++ res/scripts/stdlib.lua | 1 + 2 files changed, 11 insertions(+) create mode 100644 res/modules/internal/asserts.lua diff --git a/res/modules/internal/asserts.lua b/res/modules/internal/asserts.lua new file mode 100644 index 00000000..2317bb7c --- /dev/null +++ b/res/modules/internal/asserts.lua @@ -0,0 +1,10 @@ +local this = {} + +function this.equals(expected, fact) + assert(fact == expected, string.format( + "(fact == expected) assertion failed\n Expected: %s\n Fact: %s", + expected, fact + )) +end + +return this diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 217723a9..1243085b 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -174,6 +174,7 @@ if enable_experimental then require "core:internal/maths_inline" end +asserts = require "core:internal/asserts" events = require "core:internal/events" function pack.unload(prefix) From 3d07507fa6418e65d2e708a7058ac7171c420700 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 14:37:22 +0300 Subject: [PATCH 06/11] update network test --- dev/tests/network.lua | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/dev/tests/network.lua b/dev/tests/network.lua index 36f41b62..5edbe012 100644 --- a/dev/tests/network.lua +++ b/dev/tests/network.lua @@ -1,4 +1,5 @@ for i=1,3 do + print(string.format("iteration %s", i + 1)) local text = "" local complete = false @@ -7,32 +8,38 @@ for i=1,3 do end local server = network.tcp_open(7645, function (client) + print("client connected") start_coroutine(function() + print("client-listener started") local received_text = "" - while client:is_alive() do + while client:is_alive() and #received_text < #text do local received = client:recv(512) if received then received_text = received_text .. utf8.tostring(received) + print(string.format("received %s byte(s) from client", #received)) end coroutine.yield() end - assert (received_text == text) + asserts.equals (text, received_text) complete = true end, "client-listener") end) network.tcp_connect("localhost", 7645, function (socket) + print("connected to server") start_coroutine(function() + print("data-sender started") local ptr = 1 - while ptr < #text do + while ptr <= #text do local n = math.random(1, 20) socket:send(string.sub(text, ptr, ptr + n - 1)) + print(string.format("sent %s byte(s) to server", n)) ptr = ptr + n end socket:close() end, "data-sender") end) - app.sleep_until(function () return complete end) + app.sleep_until(function () return complete end, 1000000) server:close() end From 9fe26cc18d8d3fbb70e6b57fbd19166b3e2ab19b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 15:05:39 +0300 Subject: [PATCH 07/11] enable follow-location for http get requests --- src/network/Network.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/network/Network.cpp b/src/network/Network.cpp index d591fc5b..b175a809 100644 --- a/src/network/Network.cpp +++ b/src/network/Network.cpp @@ -96,7 +96,7 @@ public: onResponse, onReject, maxSize, - false, + true, "", std::move(headers)}; processRequest(std::move(request)); From 95818f576bf8761c38c3feb0e9e90110990aa76b Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 15:11:39 +0300 Subject: [PATCH 08/11] update app.sleep_until --- res/scripts/stdlib.lua | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/res/scripts/stdlib.lua b/res/scripts/stdlib.lua index 1243085b..b07d9387 100644 --- a/res/scripts/stdlib.lua +++ b/res/scripts/stdlib.lua @@ -79,13 +79,20 @@ local function complete_app_lib(app) coroutine.yield() end - function app.sleep_until(predicate, max_ticks) + function app.sleep_until(predicate, max_ticks, max_time) max_ticks = max_ticks or 1e9 + max_time = max_time or 1e9 local ticks = 0 - while ticks < max_ticks and not predicate() do + local start_time = os.clock() + while ticks < max_ticks and + os.clock() - start_time < max_time + and not predicate() do app.tick() ticks = ticks + 1 end + if os.clock() - start_time >= max_time then + error("timeout") + end if ticks == max_ticks then error("max ticks exceed") end From a8d3f03004ddf2dc2746f4717d1965f3da543f0c Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 15:12:12 +0300 Subject: [PATCH 09/11] add simple http test --- dev/tests/network_http.lua | 8 ++++++++ dev/tests/{network.lua => network_tcp.lua} | 2 +- 2 files changed, 9 insertions(+), 1 deletion(-) create mode 100644 dev/tests/network_http.lua rename dev/tests/{network.lua => network_tcp.lua} (95%) diff --git a/dev/tests/network_http.lua b/dev/tests/network_http.lua new file mode 100644 index 00000000..9bae830d --- /dev/null +++ b/dev/tests/network_http.lua @@ -0,0 +1,8 @@ +local response_received = false + +network.get("https://api.github.com/repos/MihailRis/VoxelEngine-Cpp/releases/latest", function (s) + print(json.parse(s).name) + response_received = true +end) + +app.sleep_until(function () return response_received end, nil, 10) diff --git a/dev/tests/network.lua b/dev/tests/network_tcp.lua similarity index 95% rename from dev/tests/network.lua rename to dev/tests/network_tcp.lua index 5edbe012..eb6b1880 100644 --- a/dev/tests/network.lua +++ b/dev/tests/network_tcp.lua @@ -40,6 +40,6 @@ for i=1,3 do end, "data-sender") end) - app.sleep_until(function () return complete end, 1000000) + app.sleep_until(function () return complete end, nil, 5) server:close() end From 0957406b0b22a09016f33351c4072a0d134d9465 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 15:47:49 +0300 Subject: [PATCH 10/11] fix default project_script.lua --- res/project_client.lua | 1 + 1 file changed, 1 insertion(+) diff --git a/res/project_client.lua b/res/project_client.lua index a1952c08..03c43a4b 100644 --- a/res/project_client.lua +++ b/res/project_client.lua @@ -23,4 +23,5 @@ function on_menu_setup() menubg = gui.root.menubg controller.resize_menu_bg() menu.page = "main" + menu.visible = true end From b815d72ba762a0650d09bf4723e636790169ae5f Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sun, 14 Sep 2025 15:48:09 +0300 Subject: [PATCH 11/11] fix: base packs not scanned for app scripts --- res/layouts/pages/scripts.xml.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/res/layouts/pages/scripts.xml.lua b/res/layouts/pages/scripts.xml.lua index 3529b3d2..dad0d1ff 100644 --- a/res/layouts/pages/scripts.xml.lua +++ b/res/layouts/pages/scripts.xml.lua @@ -13,9 +13,9 @@ end function refresh() document.list:clear() - local available = pack.get_available() - local infos = pack.get_info(available) - for _, name in ipairs(available) do + local allpacks = table.merge(pack.get_available(), pack.get_installed()) + local infos = pack.get_info(allpacks) + for _, name in ipairs(allpacks) do local info = infos[name] local scripts_dir = info.path.."/scripts/app" if not file.exists(scripts_dir) then