mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Add minetest.parse_json, engine.parse_json
This commit is contained in:
parent
71a6ffa762
commit
1ecf51a13f
6 changed files with 141 additions and 0 deletions
|
@ -26,6 +26,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "tool.h"
|
||||
#include "settings.h"
|
||||
#include "main.h" //required for g_settings, g_settings_path
|
||||
#include "json/json.h"
|
||||
|
||||
// debug(...)
|
||||
// Writes a line to dstream
|
||||
|
@ -138,6 +139,45 @@ int ModApiUtil::l_setting_save(lua_State *L)
|
|||
return 0;
|
||||
}
|
||||
|
||||
// parse_json(str[, nullvalue])
|
||||
int ModApiUtil::l_parse_json(lua_State *L)
|
||||
{
|
||||
NO_MAP_LOCK_REQUIRED;
|
||||
|
||||
const char *jsonstr = luaL_checkstring(L, 1);
|
||||
|
||||
// Use passed nullvalue or default to nil
|
||||
int nullindex = 2;
|
||||
if (lua_isnone(L, nullindex)) {
|
||||
lua_pushnil(L);
|
||||
nullindex = lua_gettop(L);
|
||||
}
|
||||
|
||||
Json::Value root;
|
||||
|
||||
{
|
||||
Json::Reader reader;
|
||||
std::istringstream stream(jsonstr);
|
||||
|
||||
if (!reader.parse(stream, root)) {
|
||||
errorstream << "Failed to parse json data "
|
||||
<< reader.getFormattedErrorMessages();
|
||||
errorstream << "data: \"" << jsonstr << "\""
|
||||
<< std::endl;
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (!push_json_value(L, root, nullindex)) {
|
||||
errorstream << "Failed to parse json data, "
|
||||
<< "depth exceeds lua stack limit" << std::endl;
|
||||
errorstream << "data: \"" << jsonstr << "\"" << std::endl;
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
|
||||
int ModApiUtil::l_get_dig_params(lua_State *L)
|
||||
{
|
||||
|
@ -191,6 +231,8 @@ void ModApiUtil::Initialize(lua_State *L, int top)
|
|||
API_FCT(setting_getbool);
|
||||
API_FCT(setting_save);
|
||||
|
||||
API_FCT(parse_json);
|
||||
|
||||
API_FCT(get_dig_params);
|
||||
API_FCT(get_hit_params);
|
||||
|
||||
|
|
|
@ -59,6 +59,9 @@ private:
|
|||
// setting_save()
|
||||
static int l_setting_save(lua_State *L);
|
||||
|
||||
// parse_json(str[, nullvalue])
|
||||
static int l_parse_json(lua_State *L);
|
||||
|
||||
// get_dig_params(groups, tool_capabilities[, time_from_last_punch])
|
||||
static int l_get_dig_params(lua_State *L);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue