add bjson library & update /doc/en/scripting/filesystem.md
This commit is contained in:
@@ -15,12 +15,14 @@
|
||||
|
||||
// Libraries
|
||||
extern const luaL_Reg audiolib[];
|
||||
extern const luaL_Reg bjsonlib[];
|
||||
extern const luaL_Reg blocklib[];
|
||||
extern const luaL_Reg cameralib[];
|
||||
extern const luaL_Reg consolelib[];
|
||||
extern const luaL_Reg corelib[];
|
||||
extern const luaL_Reg entitylib[];
|
||||
extern const luaL_Reg filelib[];
|
||||
extern const luaL_Reg generationlib[];
|
||||
extern const luaL_Reg guilib[];
|
||||
extern const luaL_Reg hudlib[];
|
||||
extern const luaL_Reg inputlib[];
|
||||
@@ -30,7 +32,6 @@ extern const luaL_Reg jsonlib[];
|
||||
extern const luaL_Reg mat4lib[];
|
||||
extern const luaL_Reg packlib[];
|
||||
extern const luaL_Reg playerlib[];
|
||||
extern const luaL_Reg generationlib[];
|
||||
extern const luaL_Reg quatlib[]; // quat.cpp
|
||||
extern const luaL_Reg timelib[];
|
||||
extern const luaL_Reg tomllib[];
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
#include "coders/binary_json.hpp"
|
||||
#include "api_lua.hpp"
|
||||
#include "util/Buffer.hpp"
|
||||
#include "../lua_custom_types.hpp"
|
||||
|
||||
static int l_tobytes(lua::State* L) {
|
||||
auto value = lua::tovalue(L, 1);
|
||||
bool compress = true;
|
||||
if (lua::gettop(L) >= 2) {
|
||||
compress = lua::toboolean(L, 2);
|
||||
}
|
||||
return lua::newuserdata<lua::LuaBytearray>(
|
||||
L, json::to_binary(value, compress)
|
||||
);
|
||||
}
|
||||
|
||||
static int l_frombytes(lua::State* L) {
|
||||
if (lua::istable(L, 1)) {
|
||||
size_t len = lua::objlen(L, 1);
|
||||
util::Buffer<ubyte> buffer(len);
|
||||
for (size_t i = 0; i < len; i++) {
|
||||
lua::rawgeti(L, i + 1);
|
||||
buffer[i] = lua::tointeger(L, -1);
|
||||
lua::pop(L);
|
||||
}
|
||||
return lua::pushvalue(L, json::from_binary(buffer.data(), len));
|
||||
} else if (auto bytes = lua::touserdata<lua::LuaBytearray>(L, -1)) {
|
||||
const auto& buffer = bytes->data();
|
||||
return lua::pushvalue(
|
||||
L, json::from_binary(buffer.data(), buffer.size())
|
||||
);
|
||||
} else {
|
||||
throw std::runtime_error("table or Bytearray expected");
|
||||
}
|
||||
}
|
||||
|
||||
const luaL_Reg bjsonlib[] = {
|
||||
{"tobytes", lua::wrap<l_tobytes>},
|
||||
{"frombytes", lua::wrap<l_frombytes>},
|
||||
{NULL, NULL}};
|
||||
@@ -39,6 +39,7 @@ static void remove_lib_funcs(
|
||||
}
|
||||
|
||||
static void create_libs(State* L, StateType stateType) {
|
||||
openlib(L, "bjson", bjsonlib);
|
||||
openlib(L, "block", blocklib);
|
||||
openlib(L, "core", corelib);
|
||||
openlib(L, "file", filelib);
|
||||
|
||||
Reference in New Issue
Block a user