#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"
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;
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;
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)
{
return 1;
}
-// is_yes(string)
+// is_yes(arg)
int ModApiUtil::l_is_yes(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
- std::string str = luaL_checkstring(L, 1);
+
+ lua_getglobal(L, "tostring"); // function to be called
+ lua_pushvalue(L, 1); // 1st argument
+ lua_call(L, 1, 1); // execute function
+ std::string str(lua_tostring(L, -1)); // get result
+ lua_pop(L, 1);
+
bool yes = is_yes(str);
lua_pushboolean(L, yes);
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);
API_FCT(setting_save);
API_FCT(parse_json);
+ API_FCT(write_json);
API_FCT(get_dig_params);
API_FCT(get_hit_params);
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);
}