Added configurable selectionbox width. Min width = 1, Max = 5
[oweals/minetest.git] / src / script / lua_api / l_settings.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 PilzAdam <pilzadam@minetest.net>
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_settings.h"
21 #include "lua_api/l_internal.h"
22 #include "settings.h"
23 #include "log.h"
24
25 // garbage collector
26 int LuaSettings::gc_object(lua_State* L)
27 {
28         LuaSettings* o = *(LuaSettings **)(lua_touserdata(L, 1));
29         delete o;
30         return 0;
31 }
32
33 // get(self, key) -> value
34 int LuaSettings::l_get(lua_State* L)
35 {
36         NO_MAP_LOCK_REQUIRED;
37         LuaSettings* o = checkobject(L, 1);
38
39         std::string key = std::string(luaL_checkstring(L, 2));
40         if (o->m_settings->exists(key)) {
41                 std::string value = o->m_settings->get(key);
42                 lua_pushstring(L, value.c_str());
43         } else {
44                 lua_pushnil(L);
45         }
46
47         return 1;
48 }
49
50 // get_bool(self, key) -> boolean
51 int LuaSettings::l_get_bool(lua_State* L)
52 {
53         NO_MAP_LOCK_REQUIRED;
54         LuaSettings* o = checkobject(L, 1);
55
56         std::string key = std::string(luaL_checkstring(L, 2));
57         if (o->m_settings->exists(key)) {
58                 bool value = o->m_settings->getBool(key);
59                 lua_pushboolean(L, value);
60         } else {
61                 lua_pushnil(L);
62         }
63
64         return 1;
65 }
66
67 // set(self, key, value)
68 int LuaSettings::l_set(lua_State* L)
69 {
70         NO_MAP_LOCK_REQUIRED;
71         LuaSettings* o = checkobject(L, 1);
72
73         std::string key = std::string(luaL_checkstring(L, 2));
74         const char* value = luaL_checkstring(L, 3);
75
76         o->m_settings->set(key, value);
77
78         return 1;
79 }
80
81 // remove(self, key) -> success
82 int LuaSettings::l_remove(lua_State* L)
83 {
84         NO_MAP_LOCK_REQUIRED;
85         LuaSettings* o = checkobject(L, 1);
86
87         std::string key = std::string(luaL_checkstring(L, 2));
88
89         bool success = o->m_settings->remove(key);
90         lua_pushboolean(L, success);
91
92         return 1;
93 }
94
95 // get_names(self) -> {key1, ...}
96 int LuaSettings::l_get_names(lua_State* L)
97 {
98         NO_MAP_LOCK_REQUIRED;
99         LuaSettings* o = checkobject(L, 1);
100
101         std::vector<std::string> keys = o->m_settings->getNames();
102
103         lua_newtable(L);
104         for (unsigned int i=0; i < keys.size(); i++)
105         {
106                 lua_pushstring(L, keys[i].c_str());
107                 lua_rawseti(L, -2, i + 1);
108         }
109
110         return 1;
111 }
112
113 // write(self) -> success
114 int LuaSettings::l_write(lua_State* L)
115 {
116         NO_MAP_LOCK_REQUIRED;
117         LuaSettings* o = checkobject(L, 1);
118
119         bool success = o->m_settings->updateConfigFile(o->m_filename.c_str());
120         lua_pushboolean(L, success);
121
122         return 1;
123 }
124
125 // to_table(self) -> {[key1]=value1,...}
126 int LuaSettings::l_to_table(lua_State* L)
127 {
128         NO_MAP_LOCK_REQUIRED;
129         LuaSettings* o = checkobject(L, 1);
130
131         std::vector<std::string> keys = o->m_settings->getNames();
132
133         lua_newtable(L);
134         for (unsigned int i=0; i < keys.size(); i++)
135         {
136                 lua_pushstring(L, o->m_settings->get(keys[i]).c_str());
137                 lua_setfield(L, -2, keys[i].c_str());
138         }
139
140         return 1;
141 }
142
143 LuaSettings::LuaSettings(const char* filename)
144 {
145         m_filename = std::string(filename);
146
147         m_settings = new Settings();
148         m_settings->readConfigFile(m_filename.c_str());
149 }
150
151 LuaSettings::~LuaSettings()
152 {
153         delete m_settings;
154 }
155
156 void LuaSettings::Register(lua_State* L)
157 {
158         lua_newtable(L);
159         int methodtable = lua_gettop(L);
160         luaL_newmetatable(L, className);
161         int metatable = lua_gettop(L);
162
163         lua_pushliteral(L, "__metatable");
164         lua_pushvalue(L, methodtable);
165         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
166
167         lua_pushliteral(L, "__index");
168         lua_pushvalue(L, methodtable);
169         lua_settable(L, metatable);
170
171         lua_pushliteral(L, "__gc");
172         lua_pushcfunction(L, gc_object);
173         lua_settable(L, metatable);
174
175         lua_pop(L, 1);  // drop metatable
176
177         luaL_openlib(L, 0, methods, 0);  // fill methodtable
178         lua_pop(L, 1);  // drop methodtable
179
180         // Can be created from Lua (Settings(filename))
181         lua_register(L, className, create_object);
182 }
183
184 // LuaSettings(filename)
185 // Creates an LuaSettings and leaves it on top of stack
186 int LuaSettings::create_object(lua_State* L)
187 {
188         NO_MAP_LOCK_REQUIRED;
189         const char* filename = luaL_checkstring(L, 1);
190         LuaSettings* o = new LuaSettings(filename);
191         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
192         luaL_getmetatable(L, className);
193         lua_setmetatable(L, -2);
194         return 1;
195 }
196
197 LuaSettings* LuaSettings::checkobject(lua_State* L, int narg)
198 {
199         NO_MAP_LOCK_REQUIRED;
200         luaL_checktype(L, narg, LUA_TUSERDATA);
201         void *ud = luaL_checkudata(L, narg, className);
202         if(!ud) luaL_typerror(L, narg, className);
203         return *(LuaSettings**)ud;  // unbox pointer
204 }
205
206 const char LuaSettings::className[] = "Settings";
207 const luaL_reg LuaSettings::methods[] = {
208         luamethod(LuaSettings, get),
209         luamethod(LuaSettings, get_bool),
210         luamethod(LuaSettings, set),
211         luamethod(LuaSettings, remove),
212         luamethod(LuaSettings, get_names),
213         luamethod(LuaSettings, write),
214         luamethod(LuaSettings, to_table),
215         {0,0}
216 };