mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-10-06 19:41:51 +00:00
add network.http_get, network.http_get_binary
This commit is contained in:
parent
186078a8d5
commit
b23318a06c
6 changed files with 62 additions and 2 deletions
|
|
@ -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[];
|
||||
|
|
|
|||
43
src/logic/scripting/lua/libs/libnetwork.cpp
Normal file
43
src/logic/scripting/lua/libs/libnetwork.cpp
Normal file
|
|
@ -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}
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue