]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_util.cpp
Omnicleanup: header cleanup, add ModApiUtil shared between game and mainmenu
[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
30 // debug(...)
31 // Writes a line to dstream
32 int ModApiUtil::l_debug(lua_State *L)
33 {
34         NO_MAP_LOCK_REQUIRED;
35         // Handle multiple parameters to behave like standard lua print()
36         int n = lua_gettop(L);
37         lua_getglobal(L, "tostring");
38         for (int i = 1; i <= n; i++) {
39                 /*
40                         Call tostring(i-th argument).
41                         This is what print() does, and it behaves a bit
42                         differently from directly calling lua_tostring.
43                 */
44                 lua_pushvalue(L, -1);  /* function to be called */
45                 lua_pushvalue(L, i);   /* value to print */
46                 lua_call(L, 1, 1);
47                 const char *s = lua_tostring(L, -1);
48                 if (i>1)
49                         dstream << "\t";
50                 if (s)
51                         dstream << s;
52                 lua_pop(L, 1);
53         }
54         dstream << std::endl;
55         return 0;
56 }
57
58 // log([level,] text)
59 // Writes a line to the logger.
60 // The one-argument version logs to infostream.
61 // The two-argument version accept a log level: error, action, info, or verbose.
62 int ModApiUtil::l_log(lua_State *L)
63 {
64         NO_MAP_LOCK_REQUIRED;
65         std::string text;
66         LogMessageLevel level = LMT_INFO;
67         if (lua_isnone(L, 2)) {
68                 text = lua_tostring(L, 1);
69         }
70         else {
71                 std::string levelname = luaL_checkstring(L, 1);
72                 text = luaL_checkstring(L, 2);
73                 if(levelname == "error")
74                         level = LMT_ERROR;
75                 else if(levelname == "action")
76                         level = LMT_ACTION;
77                 else if(levelname == "verbose")
78                         level = LMT_VERBOSE;
79         }
80         log_printline(level, text);
81         return 0;
82 }
83
84 // setting_set(name, value)
85 int ModApiUtil::l_setting_set(lua_State *L)
86 {
87         NO_MAP_LOCK_REQUIRED;
88         const char *name = luaL_checkstring(L, 1);
89         const char *value = luaL_checkstring(L, 2);
90         g_settings->set(name, value);
91         return 0;
92 }
93
94 // setting_get(name)
95 int ModApiUtil::l_setting_get(lua_State *L)
96 {
97         NO_MAP_LOCK_REQUIRED;
98         const char *name = luaL_checkstring(L, 1);
99         try{
100                 std::string value = g_settings->get(name);
101                 lua_pushstring(L, value.c_str());
102         } catch(SettingNotFoundException &e){
103                 lua_pushnil(L);
104         }
105         return 1;
106 }
107
108 // setting_setbool(name)
109 int ModApiUtil::l_setting_setbool(lua_State *L)
110 {
111         NO_MAP_LOCK_REQUIRED;
112         const char *name = luaL_checkstring(L, 1);
113         bool value = lua_toboolean(L, 2);
114         g_settings->setBool(name, value);
115         return 0;
116 }
117
118 // setting_getbool(name)
119 int ModApiUtil::l_setting_getbool(lua_State *L)
120 {
121         NO_MAP_LOCK_REQUIRED;
122         const char *name = luaL_checkstring(L, 1);
123         try{
124                 bool value = g_settings->getBool(name);
125                 lua_pushboolean(L, value);
126         } catch(SettingNotFoundException &e){
127                 lua_pushnil(L);
128         }
129         return 1;
130 }
131
132 // setting_save()
133 int ModApiUtil::l_setting_save(lua_State *L)
134 {
135         NO_MAP_LOCK_REQUIRED;
136         if(g_settings_path != "")
137                 g_settings->updateConfigFile(g_settings_path.c_str());
138         return 0;
139 }
140
141 // get_dig_params(groups, tool_capabilities[, time_from_last_punch])
142 int ModApiUtil::l_get_dig_params(lua_State *L)
143 {
144         NO_MAP_LOCK_REQUIRED;
145         std::map<std::string, int> groups;
146         read_groups(L, 1, groups);
147         ToolCapabilities tp = read_tool_capabilities(L, 2);
148         if(lua_isnoneornil(L, 3))
149                 push_dig_params(L, getDigParams(groups, &tp));
150         else
151                 push_dig_params(L, getDigParams(groups, &tp,
152                                         luaL_checknumber(L, 3)));
153         return 1;
154 }
155
156 // get_hit_params(groups, tool_capabilities[, time_from_last_punch])
157 int ModApiUtil::l_get_hit_params(lua_State *L)
158 {
159         NO_MAP_LOCK_REQUIRED;
160         std::map<std::string, int> groups;
161         read_groups(L, 1, groups);
162         ToolCapabilities tp = read_tool_capabilities(L, 2);
163         if(lua_isnoneornil(L, 3))
164                 push_hit_params(L, getHitParams(groups, &tp));
165         else
166                 push_hit_params(L, getHitParams(groups, &tp,
167                                         luaL_checknumber(L, 3)));
168         return 1;
169 }
170
171 // get_password_hash(name, raw_password)
172 int ModApiUtil::l_get_password_hash(lua_State *L)
173 {
174         NO_MAP_LOCK_REQUIRED;
175         std::string name = luaL_checkstring(L, 1);
176         std::string raw_password = luaL_checkstring(L, 2);
177         std::string hash = translatePassword(name,
178                         narrow_to_wide(raw_password));
179         lua_pushstring(L, hash.c_str());
180         return 1;
181 }
182
183 void ModApiUtil::Initialize(lua_State *L, int top)
184 {
185         API_FCT(debug);
186         API_FCT(log);
187
188         API_FCT(setting_set);
189         API_FCT(setting_get);
190         API_FCT(setting_setbool);
191         API_FCT(setting_getbool);
192         API_FCT(setting_save);
193
194         API_FCT(get_dig_params);
195         API_FCT(get_hit_params);
196
197         API_FCT(get_password_hash);
198 }
199