]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_settings.cpp
Fix compiler warning about sign comparison
[dragonfireclient.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         if (!o->m_settings->set(key, value))
77                 throw LuaError("Invalid sequence found in setting parameters");
78
79         return 0;
80 }
81
82 // remove(self, key) -> success
83 int LuaSettings::l_remove(lua_State* L)
84 {
85         NO_MAP_LOCK_REQUIRED;
86         LuaSettings* o = checkobject(L, 1);
87
88         std::string key = std::string(luaL_checkstring(L, 2));
89
90         bool success = o->m_settings->remove(key);
91         lua_pushboolean(L, success);
92
93         return 1;
94 }
95
96 // get_names(self) -> {key1, ...}
97 int LuaSettings::l_get_names(lua_State* L)
98 {
99         NO_MAP_LOCK_REQUIRED;
100         LuaSettings* o = checkobject(L, 1);
101
102         std::vector<std::string> keys = o->m_settings->getNames();
103
104         lua_newtable(L);
105         for (unsigned int i=0; i < keys.size(); i++)
106         {
107                 lua_pushstring(L, keys[i].c_str());
108                 lua_rawseti(L, -2, i + 1);
109         }
110
111         return 1;
112 }
113
114 // write(self) -> success
115 int LuaSettings::l_write(lua_State* L)
116 {
117         NO_MAP_LOCK_REQUIRED;
118         LuaSettings* o = checkobject(L, 1);
119
120         bool success = o->m_settings->updateConfigFile(o->m_filename.c_str());
121         lua_pushboolean(L, success);
122
123         return 1;
124 }
125
126 // to_table(self) -> {[key1]=value1,...}
127 int LuaSettings::l_to_table(lua_State* L)
128 {
129         NO_MAP_LOCK_REQUIRED;
130         LuaSettings* o = checkobject(L, 1);
131
132         std::vector<std::string> keys = o->m_settings->getNames();
133
134         lua_newtable(L);
135         for (unsigned int i=0; i < keys.size(); i++)
136         {
137                 lua_pushstring(L, o->m_settings->get(keys[i]).c_str());
138                 lua_setfield(L, -2, keys[i].c_str());
139         }
140
141         return 1;
142 }
143
144 LuaSettings::LuaSettings(const char* filename)
145 {
146         m_filename = std::string(filename);
147
148         m_settings = new Settings();
149         m_settings->readConfigFile(m_filename.c_str());
150 }
151
152 LuaSettings::~LuaSettings()
153 {
154         delete m_settings;
155 }
156
157 void LuaSettings::Register(lua_State* L)
158 {
159         lua_newtable(L);
160         int methodtable = lua_gettop(L);
161         luaL_newmetatable(L, className);
162         int metatable = lua_gettop(L);
163
164         lua_pushliteral(L, "__metatable");
165         lua_pushvalue(L, methodtable);
166         lua_settable(L, metatable);  // hide metatable from Lua getmetatable()
167
168         lua_pushliteral(L, "__index");
169         lua_pushvalue(L, methodtable);
170         lua_settable(L, metatable);
171
172         lua_pushliteral(L, "__gc");
173         lua_pushcfunction(L, gc_object);
174         lua_settable(L, metatable);
175
176         lua_pop(L, 1);  // drop metatable
177
178         luaL_openlib(L, 0, methods, 0);  // fill methodtable
179         lua_pop(L, 1);  // drop methodtable
180
181         // Can be created from Lua (Settings(filename))
182         lua_register(L, className, create_object);
183 }
184
185 // LuaSettings(filename)
186 // Creates an LuaSettings and leaves it on top of stack
187 int LuaSettings::create_object(lua_State* L)
188 {
189         NO_MAP_LOCK_REQUIRED;
190         const char* filename = luaL_checkstring(L, 1);
191         LuaSettings* o = new LuaSettings(filename);
192         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
193         luaL_getmetatable(L, className);
194         lua_setmetatable(L, -2);
195         return 1;
196 }
197
198 LuaSettings* LuaSettings::checkobject(lua_State* L, int narg)
199 {
200         NO_MAP_LOCK_REQUIRED;
201         luaL_checktype(L, narg, LUA_TUSERDATA);
202         void *ud = luaL_checkudata(L, narg, className);
203         if(!ud) luaL_typerror(L, narg, className);
204         return *(LuaSettings**)ud;  // unbox pointer
205 }
206
207 const char LuaSettings::className[] = "Settings";
208 const luaL_reg LuaSettings::methods[] = {
209         luamethod(LuaSettings, get),
210         luamethod(LuaSettings, get_bool),
211         luamethod(LuaSettings, set),
212         luamethod(LuaSettings, remove),
213         luamethod(LuaSettings, get_names),
214         luamethod(LuaSettings, write),
215         luamethod(LuaSettings, to_table),
216         {0,0}
217 };