]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_util.cpp
30fa56c42ba5209942119bfedad1548436b121ba
[dragonfireclient.git] / src / script / lua_api / l_util.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include "lua_api/l_util.h"
21 #include "lua_api/l_internal.h"
22 #include "common/c_converter.h"
23 #include "common/c_content.h"
24 #include "debug.h"
25 #include "log.h"
26 #include "tool.h"
27 #include "settings.h"
28 #include "main.h"  //required for g_settings, g_settings_path
29 #include "json/json.h"
30
31 // debug(...)
32 // Writes a line to dstream
33 int ModApiUtil::l_debug(lua_State *L)
34 {
35         NO_MAP_LOCK_REQUIRED;
36         // Handle multiple parameters to behave like standard lua print()
37         int n = lua_gettop(L);
38         lua_getglobal(L, "tostring");
39         for (int i = 1; i <= n; i++) {
40                 /*
41                         Call tostring(i-th argument).
42                         This is what print() does, and it behaves a bit
43                         differently from directly calling lua_tostring.
44                 */
45                 lua_pushvalue(L, -1);  /* function to be called */
46                 lua_pushvalue(L, i);   /* value to print */
47                 lua_call(L, 1, 1);
48                 const char *s = lua_tostring(L, -1);
49                 if (i>1)
50                         dstream << "\t";
51                 if (s)
52                         dstream << s;
53                 lua_pop(L, 1);
54         }
55         dstream << std::endl;
56         return 0;
57 }
58
59 // log([level,] text)
60 // Writes a line to the logger.
61 // The one-argument version logs to infostream.
62 // The two-argument version accept a log level: error, action, info, or verbose.
63 int ModApiUtil::l_log(lua_State *L)
64 {
65         NO_MAP_LOCK_REQUIRED;
66         std::string text;
67         LogMessageLevel level = LMT_INFO;
68         if (lua_isnone(L, 2)) {
69                 text = lua_tostring(L, 1);
70         }
71         else {
72                 std::string levelname = luaL_checkstring(L, 1);
73                 text = luaL_checkstring(L, 2);
74                 if(levelname == "error")
75                         level = LMT_ERROR;
76                 else if(levelname == "action")
77                         level = LMT_ACTION;
78                 else if(levelname == "verbose")
79                         level = LMT_VERBOSE;
80         }
81         log_printline(level, text);
82         return 0;
83 }
84
85 // setting_set(name, value)
86 int ModApiUtil::l_setting_set(lua_State *L)
87 {
88         NO_MAP_LOCK_REQUIRED;
89         const char *name = luaL_checkstring(L, 1);
90         const char *value = luaL_checkstring(L, 2);
91         g_settings->set(name, value);
92         return 0;
93 }
94
95 // setting_get(name)
96 int ModApiUtil::l_setting_get(lua_State *L)
97 {
98         NO_MAP_LOCK_REQUIRED;
99         const char *name = luaL_checkstring(L, 1);
100         try{
101                 std::string value = g_settings->get(name);
102                 lua_pushstring(L, value.c_str());
103         } catch(SettingNotFoundException &e){
104                 lua_pushnil(L);
105         }
106         return 1;
107 }
108
109 // setting_setbool(name)
110 int ModApiUtil::l_setting_setbool(lua_State *L)
111 {
112         NO_MAP_LOCK_REQUIRED;
113         const char *name = luaL_checkstring(L, 1);
114         bool value = lua_toboolean(L, 2);
115         g_settings->setBool(name, value);
116         return 0;
117 }
118
119 // setting_getbool(name)
120 int ModApiUtil::l_setting_getbool(lua_State *L)
121 {
122         NO_MAP_LOCK_REQUIRED;
123         const char *name = luaL_checkstring(L, 1);
124         try{
125                 bool value = g_settings->getBool(name);
126                 lua_pushboolean(L, value);
127         } catch(SettingNotFoundException &e){
128                 lua_pushnil(L);
129         }
130         return 1;
131 }
132
133 // setting_save()
134 int ModApiUtil::l_setting_save(lua_State *L)
135 {
136         NO_MAP_LOCK_REQUIRED;
137         if(g_settings_path != "")
138                 g_settings->updateConfigFile(g_settings_path.c_str());
139         return 0;
140 }
141
142 // parse_json(str[, nullvalue])
143 int ModApiUtil::l_parse_json(lua_State *L)
144 {
145         NO_MAP_LOCK_REQUIRED;
146
147         const char *jsonstr = luaL_checkstring(L, 1);
148
149         // Use passed nullvalue or default to nil
150         int nullindex = 2;
151         if (lua_isnone(L, nullindex)) {
152                 lua_pushnil(L);
153                 nullindex = lua_gettop(L);
154         }
155
156         Json::Value root;
157
158         {
159                 Json::Reader reader;
160                 std::istringstream stream(jsonstr);
161
162                 if (!reader.parse(stream, root)) {
163                         errorstream << "Failed to parse json data "
164                                 << reader.getFormattedErrorMessages();
165                         errorstream << "data: \"" << jsonstr << "\""
166                                 << std::endl;
167                         lua_pushnil(L);
168                         return 1;
169                 }
170         }
171
172         if (!push_json_value(L, root, nullindex)) {
173                 errorstream << "Failed to parse json data, "
174                         << "depth exceeds lua stack limit" << std::endl;
175                 errorstream << "data: \"" << jsonstr << "\"" << std::endl;
176                 lua_pushnil(L);
177         }
178         return 1;
179 }
180
181 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
182 int ModApiUtil::l_get_dig_params(lua_State *L)
183 {
184         NO_MAP_LOCK_REQUIRED;
185         std::map<std::string, int> groups;
186         read_groups(L, 1, groups);
187         ToolCapabilities tp = read_tool_capabilities(L, 2);
188         if(lua_isnoneornil(L, 3))
189                 push_dig_params(L, getDigParams(groups, &tp));
190         else
191                 push_dig_params(L, getDigParams(groups, &tp,
192                                         luaL_checknumber(L, 3)));
193         return 1;
194 }
195
196 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
197 int ModApiUtil::l_get_hit_params(lua_State *L)
198 {
199         NO_MAP_LOCK_REQUIRED;
200         std::map<std::string, int> groups;
201         read_groups(L, 1, groups);
202         ToolCapabilities tp = read_tool_capabilities(L, 2);
203         if(lua_isnoneornil(L, 3))
204                 push_hit_params(L, getHitParams(groups, &tp));
205         else
206                 push_hit_params(L, getHitParams(groups, &tp,
207                                         luaL_checknumber(L, 3)));
208         return 1;
209 }
210
211 // get_password_hash(name, raw_password)
212 int ModApiUtil::l_get_password_hash(lua_State *L)
213 {
214         NO_MAP_LOCK_REQUIRED;
215         std::string name = luaL_checkstring(L, 1);
216         std::string raw_password = luaL_checkstring(L, 2);
217         std::string hash = translatePassword(name,
218                         narrow_to_wide(raw_password));
219         lua_pushstring(L, hash.c_str());
220         return 1;
221 }
222
223 void ModApiUtil::Initialize(lua_State *L, int top)
224 {
225         API_FCT(debug);
226         API_FCT(log);
227
228         API_FCT(setting_set);
229         API_FCT(setting_get);
230         API_FCT(setting_setbool);
231         API_FCT(setting_getbool);
232         API_FCT(setting_save);
233
234         API_FCT(parse_json);
235
236         API_FCT(get_dig_params);
237         API_FCT(get_hit_params);
238
239         API_FCT(get_password_hash);
240 }
241