]> git.lizzy.rs Git - minetest.git/blobdiff - src/script/lua_api/l_util.cpp
Make print() NUL-safe
[minetest.git] / src / script / lua_api / l_util.cpp
index af9c19210360bb321b7b693e097462ab4da24960..57db632c9c9fe6526dae0e2e728b21b6985009cf 100644 (file)
@@ -21,7 +21,9 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "lua_api/l_internal.h"
 #include "common/c_converter.h"
 #include "common/c_content.h"
+#include "cpp_api/s_async.h"
 #include "debug.h"
+#include "porting.h"
 #include "log.h"
 #include "tool.h"
 #include "settings.h"
@@ -45,11 +47,12 @@ int ModApiUtil::l_debug(lua_State *L)
                lua_pushvalue(L, -1);  /* function to be called */
                lua_pushvalue(L, i);   /* value to print */
                lua_call(L, 1, 1);
-               const char *s = lua_tostring(L, -1);
-               if (i>1)
+               size_t len;
+               const char *s = lua_tolstring(L, -1, &len);
+               if (i > 1)
                        dstream << "\t";
                if (s)
-                       dstream << s;
+                       dstream << std::string(s, len);
                lua_pop(L, 1);
        }
        dstream << std::endl;
@@ -77,6 +80,11 @@ int ModApiUtil::l_log(lua_State *L)
                        level = LMT_ACTION;
                else if(levelname == "verbose")
                        level = LMT_VERBOSE;
+               else if (levelname == "deprecated") {
+                       log_deprecated(L,text);
+                       return 0;
+               }
+
        }
        log_printline(level, text);
        return 0;
@@ -178,6 +186,38 @@ int ModApiUtil::l_parse_json(lua_State *L)
        return 1;
 }
 
+// write_json(data[, styled]) -> string or nil and error message
+int ModApiUtil::l_write_json(lua_State *L)
+{
+       NO_MAP_LOCK_REQUIRED;
+
+       bool styled = false;
+       if (!lua_isnone(L, 2)) {
+               styled = lua_toboolean(L, 2);
+               lua_pop(L, 1);
+       }
+
+       Json::Value root;
+       try {
+               read_json_value(L, root, 1);
+       } catch (SerializationError &e) {
+               lua_pushnil(L);
+               lua_pushstring(L, e.what());
+               return 2;
+       }
+
+       std::string out;
+       if (styled) {
+               Json::StyledWriter writer;
+               out = writer.write(root);
+       } else {
+               Json::FastWriter writer;
+               out = writer.write(root);
+       }
+       lua_pushlstring(L, out.c_str(), out.size());
+       return 1;
+}
+
 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
 int ModApiUtil::l_get_dig_params(lua_State *L)
 {
@@ -236,6 +276,14 @@ int ModApiUtil::l_is_yes(lua_State *L)
        return 1;
 }
 
+int ModApiUtil::l_get_builtin_path(lua_State *L)
+{
+       std::string path = porting::path_share + DIR_DELIM + "builtin";
+       lua_pushstring(L, path.c_str());
+       return 1;
+}
+
+
 void ModApiUtil::Initialize(lua_State *L, int top)
 {
        API_FCT(debug);
@@ -248,6 +296,7 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(setting_save);
 
        API_FCT(parse_json);
+       API_FCT(write_json);
 
        API_FCT(get_dig_params);
        API_FCT(get_hit_params);
@@ -255,5 +304,26 @@ void ModApiUtil::Initialize(lua_State *L, int top)
        API_FCT(get_password_hash);
 
        API_FCT(is_yes);
+
+       API_FCT(get_builtin_path);
+}
+
+void ModApiUtil::InitializeAsync(AsyncEngine& engine)
+{
+       ASYNC_API_FCT(debug);
+       ASYNC_API_FCT(log);
+
+       //ASYNC_API_FCT(setting_set);
+       ASYNC_API_FCT(setting_get);
+       //ASYNC_API_FCT(setting_setbool);
+       ASYNC_API_FCT(setting_getbool);
+       //ASYNC_API_FCT(setting_save);
+
+       ASYNC_API_FCT(parse_json);
+       ASYNC_API_FCT(write_json);
+
+       ASYNC_API_FCT(is_yes);
+
+       ASYNC_API_FCT(get_builtin_path);
 }