Merge branch 'dev' into pathfinding

This commit is contained in:
MihailRis
2025-09-09 20:03:11 +03:00
5 changed files with 111 additions and 20 deletions
+26 -6
View File
@@ -5,6 +5,19 @@
using namespace scripting;
static std::vector<std::string> read_headers(lua::State* L, int index) {
std::vector<std::string> headers;
if (lua::istable(L, index)) {
int len = lua::objlen(L, index);
for (int i = 1; i <= len; i++) {
lua::rawgeti(L, i, index);
headers.push_back(lua::tostring(L, -1));
lua::pop(L);
}
}
return headers;
}
static int l_get(lua::State* L, network::Network& network) {
std::string url(lua::require_lstring(L, 1));
@@ -12,7 +25,7 @@ static int l_get(lua::State* L, network::Network& network) {
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 3) {
if (!lua::isnoneornil(L, 3)) {
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
@@ -20,11 +33,13 @@ static int l_get(lua::State* L, network::Network& network) {
};
}
auto headers = read_headers(L, 4);
network.get(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
}, std::move(onReject));
}, std::move(onReject), std::move(headers));
return 0;
}
@@ -35,7 +50,7 @@ static int l_get_binary(lua::State* L, network::Network& network) {
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 3) {
if (!lua::isnoneornil(L, 3)) {
lua::pushvalue(L, 3);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
@@ -43,6 +58,8 @@ static int l_get_binary(lua::State* L, network::Network& network) {
};
}
auto headers = read_headers(L, 4);
network.get(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
@@ -50,7 +67,8 @@ static int l_get_binary(lua::State* L, network::Network& network) {
engine->postRunnable([=]() {
onResponse({buffer});
});
}, std::move(onReject));
}, std::move(onReject), std::move(headers));
return 0;
}
@@ -62,7 +80,7 @@ static int l_post(lua::State* L, network::Network& network) {
auto onResponse = lua::create_lambda_nothrow(L);
network::OnReject onReject = nullptr;
if (lua::gettop(L) >= 4) {
if (!lua::isnoneornil(L, 4)) {
lua::pushvalue(L, 4);
auto callback = lua::create_lambda_nothrow(L);
onReject = [callback](int code) {
@@ -77,6 +95,8 @@ static int l_post(lua::State* L, network::Network& network) {
string = json::stringify(data, false);
}
auto headers = read_headers(L, 5);
engine->getNetwork().post(url, string, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
@@ -84,7 +104,7 @@ static int l_post(lua::State* L, network::Network& network) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
}, std::move(onReject));
}, std::move(onReject), std::move(headers));
return 0;
}