add network.http_get, network.http_get_binary

This commit is contained in:
MihailRis
2024-11-22 07:30:38 +03:00
parent 186078a8d5
commit b23318a06c
6 changed files with 62 additions and 2 deletions
+1
View File
@@ -32,6 +32,7 @@ extern const luaL_Reg inventorylib[];
extern const luaL_Reg itemlib[];
extern const luaL_Reg jsonlib[];
extern const luaL_Reg mat4lib[];
extern const luaL_Reg networklib[];
extern const luaL_Reg packlib[];
extern const luaL_Reg particleslib[]; // gfx.particles
extern const luaL_Reg playerlib[];
@@ -0,0 +1,43 @@
#include "api_lua.hpp"
#include "engine.hpp"
#include "network/Network.hpp"
using namespace scripting;
static int l_http_get(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda(L);
engine->getNetwork().httpGet(url, [onResponse](std::vector<char> bytes) {
engine->postRunnable([=]() {
onResponse({std::string(bytes.data(), bytes.size())});
});
});
return 0;
}
static int l_http_get_binary(lua::State* L) {
std::string url(lua::require_lstring(L, 1));
lua::pushvalue(L, 2);
auto onResponse = lua::create_lambda(L);
engine->getNetwork().httpGet(url, [onResponse](std::vector<char> bytes) {
auto buffer = std::make_shared<util::Buffer<ubyte>>(
reinterpret_cast<const ubyte*>(bytes.data()), bytes.size()
);
engine->postRunnable([=]() {
onResponse({buffer});
});
});
return 0;
}
const luaL_Reg networklib[] = {
{"http_get", lua::wrap<l_http_get>},
{"http_get_binary", lua::wrap<l_http_get_binary>},
{NULL, NULL}
};
+1
View File
@@ -65,6 +65,7 @@ static void create_libs(State* L, StateType stateType) {
openlib(L, "audio", audiolib);
openlib(L, "console", consolelib);
openlib(L, "player", playerlib);
openlib(L, "network", networklib);
openlib(L, "entities", entitylib);
openlib(L, "cameras", cameralib);