3 Copyright (C) 2015 est31 <mtest31@outlook.com>
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.
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.
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.
21 #include "lua_api/l_areastore.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_converter.h"
24 #include "cpp_api/s_security.h"
26 #include "areastore.h"
29 #include "cmake_config.h"
33 static inline void get_data_and_border_flags(lua_State *L, u8 start_i,
34 bool *borders, bool *data)
36 if (!lua_isboolean(L, start_i))
38 *borders = lua_toboolean(L, start_i);
39 if (!lua_isboolean(L, start_i + 1))
41 *data = lua_toboolean(L, start_i + 1);
44 static void push_area(lua_State *L, const Area *a,
45 bool include_borders, bool include_data)
47 if (!include_borders && !include_data) {
48 lua_pushboolean(L, true);
52 if (include_borders) {
53 push_v3s16(L, a->minedge);
54 lua_setfield(L, -2, "min");
55 push_v3s16(L, a->maxedge);
56 lua_setfield(L, -2, "max");
59 lua_pushlstring(L, a->data.c_str(), a->data.size());
60 lua_setfield(L, -2, "data");
64 static inline void push_areas(lua_State *L, const std::vector<Area *> &areas,
65 bool borders, bool data)
68 size_t cnt = areas.size();
69 for (size_t i = 0; i < cnt; i++) {
70 lua_pushnumber(L, areas[i]->id);
71 push_area(L, areas[i], borders, data);
77 int LuaAreaStore::gc_object(lua_State *L)
79 LuaAreaStore *o = *(LuaAreaStore **)(lua_touserdata(L, 1));
84 // get_area(id, include_borders, include_data)
85 int LuaAreaStore::l_get_area(lua_State *L)
89 LuaAreaStore *o = checkobject(L, 1);
90 AreaStore *ast = o->as;
92 u32 id = luaL_checknumber(L, 2);
94 bool include_borders = true;
95 bool include_data = false;
96 get_data_and_border_flags(L, 3, &include_borders, &include_data);
100 res = ast->getArea(id);
101 push_area(L, res, include_borders, include_data);
106 // get_areas_for_pos(pos, include_borders, include_data)
107 int LuaAreaStore::l_get_areas_for_pos(lua_State *L)
109 NO_MAP_LOCK_REQUIRED;
111 LuaAreaStore *o = checkobject(L, 1);
112 AreaStore *ast = o->as;
114 v3s16 pos = check_v3s16(L, 2);
116 bool include_borders = true;
117 bool include_data = false;
118 get_data_and_border_flags(L, 3, &include_borders, &include_data);
120 std::vector<Area *> res;
122 ast->getAreasForPos(&res, pos);
123 push_areas(L, res, include_borders, include_data);
128 // get_areas_in_area(edge1, edge2, accept_overlap, include_borders, include_data)
129 int LuaAreaStore::l_get_areas_in_area(lua_State *L)
131 NO_MAP_LOCK_REQUIRED;
133 LuaAreaStore *o = checkobject(L, 1);
134 AreaStore *ast = o->as;
136 v3s16 minedge = check_v3s16(L, 2);
137 v3s16 maxedge = check_v3s16(L, 3);
139 bool include_borders = true;
140 bool include_data = false;
141 bool accept_overlap = false;
142 if (lua_isboolean(L, 4)) {
143 accept_overlap = lua_toboolean(L, 4);
144 get_data_and_border_flags(L, 5, &include_borders, &include_data);
146 std::vector<Area *> res;
148 ast->getAreasInArea(&res, minedge, maxedge, accept_overlap);
149 push_areas(L, res, include_borders, include_data);
154 // insert_area(edge1, edge2, data)
155 int LuaAreaStore::l_insert_area(lua_State *L)
157 NO_MAP_LOCK_REQUIRED;
159 LuaAreaStore *o = checkobject(L, 1);
160 AreaStore *ast = o->as;
162 Area a(check_v3s16(L, 2), check_v3s16(L, 3));
165 const char *data = luaL_checklstring(L, 4, &d_len);
167 a.data = std::string(data, d_len);
169 if (!ast->insertArea(&a))
172 lua_pushnumber(L, a.id);
177 int LuaAreaStore::l_reserve(lua_State *L)
179 NO_MAP_LOCK_REQUIRED;
181 LuaAreaStore *o = checkobject(L, 1);
182 AreaStore *ast = o->as;
184 size_t count = luaL_checknumber(L, 2);
190 int LuaAreaStore::l_remove_area(lua_State *L)
192 NO_MAP_LOCK_REQUIRED;
194 LuaAreaStore *o = checkobject(L, 1);
195 AreaStore *ast = o->as;
197 u32 id = luaL_checknumber(L, 2);
198 bool success = ast->removeArea(id);
200 lua_pushboolean(L, success);
204 // set_cache_params(params)
205 int LuaAreaStore::l_set_cache_params(lua_State *L)
207 NO_MAP_LOCK_REQUIRED;
209 LuaAreaStore *o = checkobject(L, 1);
210 AreaStore *ast = o->as;
212 luaL_checktype(L, 2, LUA_TTABLE);
214 bool enabled = getboolfield_default(L, 2, "enabled", true);
215 u8 block_radius = getintfield_default(L, 2, "block_radius", 64);
216 size_t limit = getintfield_default(L, 2, "block_radius", 1000);
218 ast->setCacheParams(enabled, block_radius, limit);
225 int LuaAreaStore::l_to_string(lua_State *L)
227 NO_MAP_LOCK_REQUIRED;
229 LuaAreaStore *o = checkobject(L, 1);
230 AreaStore *ast = o->as;
232 std::ostringstream os(std::ios_base::binary);
234 std::string str = os.str();
236 lua_pushlstring(L, str.c_str(), str.length());
241 int LuaAreaStore::l_to_file(lua_State *L)
243 NO_MAP_LOCK_REQUIRED;
245 LuaAreaStore *o = checkobject(L, 1);
246 AreaStore *ast = o->as;
248 const char *filename = luaL_checkstring(L, 2);
249 CHECK_SECURE_PATH_OPTIONAL(L, filename);
251 std::ostringstream os(std::ios_base::binary);
254 lua_pushboolean(L, fs::safeWriteToFile(filename, os.str()));
259 int LuaAreaStore::l_from_string(lua_State *L)
261 NO_MAP_LOCK_REQUIRED;
263 LuaAreaStore *o = checkobject(L, 1);
264 AreaStore *ast = o->as;
267 const char *str = luaL_checklstring(L, 2, &len);
269 std::istringstream is(std::string(str, len), std::ios::binary);
270 bool success = ast->deserialize(is);
272 lua_pushboolean(L, success);
276 // from_file(filename)
277 int LuaAreaStore::l_from_file(lua_State *L)
279 NO_MAP_LOCK_REQUIRED;
281 LuaAreaStore *o = checkobject(L, 1);
282 AreaStore *ast = o->as;
284 const char *filename = luaL_checkstring(L, 2);
285 CHECK_SECURE_PATH_OPTIONAL(L, filename);
287 std::ifstream is(filename, std::ios::binary);
288 bool success = ast->deserialize(is);
290 lua_pushboolean(L, success);
295 LuaAreaStore::LuaAreaStore()
298 this->as = new SpatialAreaStore();
300 this->as = new VectorAreaStore();
304 LuaAreaStore::LuaAreaStore(const std::string &type)
307 if (type == "LibSpatial") {
308 this->as = new SpatialAreaStore();
312 this->as = new VectorAreaStore();
316 LuaAreaStore::~LuaAreaStore()
322 // Creates an LuaAreaStore and leaves it on top of stack
323 int LuaAreaStore::create_object(lua_State *L)
325 NO_MAP_LOCK_REQUIRED;
327 LuaAreaStore *o = (lua_isstring(L, 1)) ?
328 new LuaAreaStore(lua_tostring(L, 1)) :
331 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
332 luaL_getmetatable(L, className);
333 lua_setmetatable(L, -2);
337 LuaAreaStore *LuaAreaStore::checkobject(lua_State *L, int narg)
339 NO_MAP_LOCK_REQUIRED;
341 luaL_checktype(L, narg, LUA_TUSERDATA);
343 void *ud = luaL_checkudata(L, narg, className);
345 luaL_typerror(L, narg, className);
347 return *(LuaAreaStore **)ud; // unbox pointer
350 void LuaAreaStore::Register(lua_State *L)
353 int methodtable = lua_gettop(L);
354 luaL_newmetatable(L, className);
355 int metatable = lua_gettop(L);
357 lua_pushliteral(L, "__metatable");
358 lua_pushvalue(L, methodtable);
359 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
361 lua_pushliteral(L, "__index");
362 lua_pushvalue(L, methodtable);
363 lua_settable(L, metatable);
365 lua_pushliteral(L, "__gc");
366 lua_pushcfunction(L, gc_object);
367 lua_settable(L, metatable);
369 lua_pop(L, 1); // drop metatable
371 luaL_openlib(L, 0, methods, 0); // fill methodtable
372 lua_pop(L, 1); // drop methodtable
374 // Can be created from Lua (AreaStore())
375 lua_register(L, className, create_object);
378 const char LuaAreaStore::className[] = "AreaStore";
379 const luaL_reg LuaAreaStore::methods[] = {
380 luamethod(LuaAreaStore, get_area),
381 luamethod(LuaAreaStore, get_areas_for_pos),
382 luamethod(LuaAreaStore, get_areas_in_area),
383 luamethod(LuaAreaStore, insert_area),
384 luamethod(LuaAreaStore, reserve),
385 luamethod(LuaAreaStore, remove_area),
386 luamethod(LuaAreaStore, set_cache_params),
387 /* luamethod(LuaAreaStore, to_string),
388 luamethod(LuaAreaStore, to_file),
389 luamethod(LuaAreaStore, from_string),
390 luamethod(LuaAreaStore, from_file),*/