refactor: add GUI instance reference to UI nodes

This commit is contained in:
MihailRis
2025-04-02 14:55:53 +03:00
parent 74a94f869c
commit 4c48afbb90
70 changed files with 1133 additions and 832 deletions
+67 -49
View File
@@ -6,13 +6,13 @@
using namespace scripting;
static int l_get(lua::State* L) {
static int l_get(lua::State* L, network::Network& network) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda_nothrow(L);
engine->getNetwork().get(url, [onResponse](std::vector<char> bytes) {
network.get(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
@@ -20,13 +20,13 @@ static int l_get(lua::State* L) {
return 0;
}
static int l_get_binary(lua::State* L) {
static int l_get_binary(lua::State* L, network::Network& network) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda_nothrow(L);
engine->getNetwork().get(url, [onResponse](std::vector<char> bytes) {
network.get(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
);
@@ -37,7 +37,7 @@ static int l_get_binary(lua::State* L) {
return 0;
}
static int l_post(lua::State* L) {
static int l_post(lua::State* L, network::Network& network) {
std::string url(lua::require_lstring(L, 1));
auto data = lua::tovalue(L, 2);
@@ -61,12 +61,12 @@ static int l_post(lua::State* L) {
return 0;
}
static int l_connect(lua::State* L) {
static int l_connect(lua::State* L, network::Network& network) {
std::string address = lua::require_string(L, 1);
int port = lua::tointeger(L, 2);
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
u64id_t id = engine->getNetwork().connect(address, port, [callback](u64id_t id) {
u64id_t id = network.connect(address, port, [callback](u64id_t id) {
engine->postRunnable([=]() {
callback({id});
});
@@ -75,25 +75,25 @@ static int l_connect(lua::State* L) {
}
static int l_close(lua::State* L) {
static int l_close(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = engine->getNetwork().getConnection(id)) {
if (auto connection = network.getConnection(id)) {
connection->close(true);
}
return 0;
}
static int l_closeserver(lua::State* L) {
static int l_closeserver(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto server = engine->getNetwork().getServer(id)) {
if (auto server = network.getServer(id)) {
server->close();
}
return 0;
}
static int l_send(lua::State* L) {
static int l_send(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
auto connection = engine->getNetwork().getConnection(id);
auto connection = network.getConnection(id);
if (connection == nullptr ||
connection->getState() == network::ConnectionState::CLOSED) {
return 0;
@@ -120,7 +120,7 @@ static int l_send(lua::State* L) {
return 0;
}
static int l_recv(lua::State* L) {
static int l_recv(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
int length = lua::tointeger(L, 2);
auto connection = engine->getNetwork().getConnection(id);
@@ -149,19 +149,19 @@ static int l_recv(lua::State* L) {
return 1;
}
static int l_available(lua::State* L) {
static int l_available(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = engine->getNetwork().getConnection(id)) {
if (auto connection = network.getConnection(id)) {
return lua::pushinteger(L, connection->available());
}
return 0;
}
static int l_open(lua::State* L) {
static int l_open(lua::State* L, network::Network& network) {
int port = lua::tointeger(L, 1);
lua::pushvalue(L, 2);
auto callback = lua::create_lambda_nothrow(L);
u64id_t id = engine->getNetwork().openServer(port, [callback](u64id_t id) {
u64id_t id = network.openServer(port, [callback](u64id_t id) {
engine->postRunnable([=]() {
callback({id});
});
@@ -169,9 +169,9 @@ static int l_open(lua::State* L) {
return lua::pushinteger(L, id);
}
static int l_is_alive(lua::State* L) {
static int l_is_alive(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = engine->getNetwork().getConnection(id)) {
if (auto connection = network.getConnection(id)) {
return lua::pushboolean(
L,
connection->getState() != network::ConnectionState::CLOSED ||
@@ -181,9 +181,9 @@ static int l_is_alive(lua::State* L) {
return lua::pushboolean(L, false);
}
static int l_is_connected(lua::State* L) {
static int l_is_connected(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = engine->getNetwork().getConnection(id)) {
if (auto connection = network.getConnection(id)) {
return lua::pushboolean(
L, connection->getState() == network::ConnectionState::CONNECTED
);
@@ -191,9 +191,9 @@ static int l_is_connected(lua::State* L) {
return lua::pushboolean(L, false);
}
static int l_get_address(lua::State* L) {
static int l_get_address(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto connection = engine->getNetwork().getConnection(id)) {
if (auto connection = network.getConnection(id)) {
lua::pushstring(L, connection->getAddress());
lua::pushinteger(L, connection->getPort());
return 2;
@@ -201,47 +201,65 @@ static int l_get_address(lua::State* L) {
return 0;
}
static int l_is_serveropen(lua::State* L) {
static int l_is_serveropen(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto server = engine->getNetwork().getServer(id)) {
if (auto server = network.getServer(id)) {
return lua::pushboolean(L, server->isOpen());
}
return lua::pushboolean(L, false);
}
static int l_get_serverport(lua::State* L) {
static int l_get_serverport(lua::State* L, network::Network& network) {
u64id_t id = lua::tointeger(L, 1);
if (auto server = engine->getNetwork().getServer(id)) {
if (auto server = network.getServer(id)) {
return lua::pushinteger(L, server->getPort());
}
return 0;
}
static int l_get_total_upload(lua::State* L) {
return lua::pushinteger(L, engine->getNetwork().getTotalUpload());
static int l_get_total_upload(lua::State* L, network::Network& network) {
return lua::pushinteger(L, network.getTotalUpload());
}
static int l_get_total_download(lua::State* L) {
return lua::pushinteger(L, engine->getNetwork().getTotalDownload());
static int l_get_total_download(lua::State* L, network::Network& network) {
return lua::pushinteger(L, network.getTotalDownload());
}
template <int(*func)(lua::State*, network::Network&)>
int wrap(lua_State* L) {
int result = 0;
try {
result = func(L, engine->getNetwork());
}
// transform exception with description into lua_error
catch (std::exception& e) {
luaL_error(L, e.what());
}
// Rethrow any other exception (lua error for example)
catch (...) {
throw;
}
return result;
}
const luaL_Reg networklib[] = {
{"get", lua::wrap<l_get>},
{"get_binary", lua::wrap<l_get_binary>},
{"post", lua::wrap<l_post>},
{"get_total_upload", lua::wrap<l_get_total_upload>},
{"get_total_download", lua::wrap<l_get_total_download>},
{"__open", lua::wrap<l_open>},
{"__closeserver", lua::wrap<l_closeserver>},
{"__connect", lua::wrap<l_connect>},
{"__close", lua::wrap<l_close>},
{"__send", lua::wrap<l_send>},
{"__recv", lua::wrap<l_recv>},
{"__available", lua::wrap<l_available>},
{"__is_alive", lua::wrap<l_is_alive>},
{"__is_connected", lua::wrap<l_is_connected>},
{"__get_address", lua::wrap<l_get_address>},
{"__is_serveropen", lua::wrap<l_is_serveropen>},
{"__get_serverport", lua::wrap<l_get_serverport>},
{"get", wrap<l_get>},
{"get_binary", wrap<l_get_binary>},
{"post", wrap<l_post>},
{"get_total_upload", wrap<l_get_total_upload>},
{"get_total_download", wrap<l_get_total_download>},
{"__open", wrap<l_open>},
{"__closeserver", wrap<l_closeserver>},
{"__connect", wrap<l_connect>},
{"__close", wrap<l_close>},
{"__send", wrap<l_send>},
{"__recv", wrap<l_recv>},
{"__available", wrap<l_available>},
{"__is_alive", wrap<l_is_alive>},
{"__is_connected", wrap<l_is_connected>},
{"__get_address", wrap<l_get_address>},
{"__is_serveropen", wrap<l_is_serveropen>},
{"__get_serverport", wrap<l_get_serverport>},
{NULL, NULL}
};