#include "common/c_converter.h"
#include "cpp_api/s_security.h"
#include "irr_v3d.h"
-#include "areastore.h"
+#include "util/areastore.h"
#include "filesys.h"
-#ifndef ANDROID
- #include "cmake_config.h"
-#endif
#include <fstream>
static inline void get_data_and_border_flags(lua_State *L, u8 start_i,
}
}
+// Deserializes value and handles errors
+static int deserialization_helper(lua_State *L, AreaStore *as,
+ std::istream &is)
+{
+ try {
+ as->deserialize(is);
+ } catch (const SerializationError &e) {
+ lua_pushboolean(L, false);
+ lua_pushstring(L, e.what());
+ return 2;
+ }
+
+ lua_pushboolean(L, true);
+ return 1;
+}
+
// garbage collector
int LuaAreaStore::gc_object(lua_State *L)
{
const Area *res;
res = ast->getArea(id);
+ if (!res)
+ return 0;
+
push_area(L, res, include_borders, include_data);
return 1;
bool include_data = false;
bool accept_overlap = false;
if (lua_isboolean(L, 4)) {
- accept_overlap = lua_toboolean(L, 4);
+ accept_overlap = readParam<bool>(L, 4);
get_data_and_border_flags(L, 5, &include_borders, &include_data);
}
std::vector<Area *> res;
return 1;
}
-// insert_area(edge1, edge2, data)
+// insert_area(edge1, edge2, data, id)
int LuaAreaStore::l_insert_area(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
a.data = std::string(data, d_len);
+ if (lua_isnumber(L, 5))
+ a.id = lua_tonumber(L, 5);
+
+ // Insert & assign a new ID if necessary
if (!ast->insertArea(&a))
return 0;
return 0;
}
-#if 0
// to_string()
int LuaAreaStore::l_to_string(lua_State *L)
{
NO_MAP_LOCK_REQUIRED;
LuaAreaStore *o = checkobject(L, 1);
- AreaStore *ast = o->as;
std::ostringstream os(std::ios_base::binary);
- ast->serialize(os);
+ o->as->serialize(os);
std::string str = os.str();
lua_pushlstring(L, str.c_str(), str.length());
AreaStore *ast = o->as;
const char *filename = luaL_checkstring(L, 2);
- CHECK_SECURE_PATH_OPTIONAL(L, filename);
+ CHECK_SECURE_PATH(L, filename, true);
std::ostringstream os(std::ios_base::binary);
ast->serialize(os);
NO_MAP_LOCK_REQUIRED;
LuaAreaStore *o = checkobject(L, 1);
- AreaStore *ast = o->as;
size_t len;
const char *str = luaL_checklstring(L, 2, &len);
std::istringstream is(std::string(str, len), std::ios::binary);
- bool success = ast->deserialize(is);
-
- lua_pushboolean(L, success);
- return 1;
+ return deserialization_helper(L, o->as, is);
}
// from_file(filename)
NO_MAP_LOCK_REQUIRED;
LuaAreaStore *o = checkobject(L, 1);
- AreaStore *ast = o->as;
const char *filename = luaL_checkstring(L, 2);
- CHECK_SECURE_PATH_OPTIONAL(L, filename);
+ CHECK_SECURE_PATH(L, filename, false);
std::ifstream is(filename, std::ios::binary);
- bool success = ast->deserialize(is);
-
- lua_pushboolean(L, success);
- return 1;
+ return deserialization_helper(L, o->as, is);
}
-#endif
-LuaAreaStore::LuaAreaStore()
+LuaAreaStore::LuaAreaStore() : as(AreaStore::getOptimalImplementation())
{
-#if USE_SPATIAL
- this->as = new SpatialAreaStore();
-#else
- this->as = new VectorAreaStore();
-#endif
}
LuaAreaStore::LuaAreaStore(const std::string &type)
{
#if USE_SPATIAL
if (type == "LibSpatial") {
- this->as = new SpatialAreaStore();
+ as = new SpatialAreaStore();
} else
#endif
{
- this->as = new VectorAreaStore();
+ as = new VectorAreaStore();
}
}
NO_MAP_LOCK_REQUIRED;
LuaAreaStore *o = (lua_isstring(L, 1)) ?
- new LuaAreaStore(lua_tostring(L, 1)) :
+ new LuaAreaStore(readParam<std::string>(L, 1)) :
new LuaAreaStore();
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
}
const char LuaAreaStore::className[] = "AreaStore";
-const luaL_reg LuaAreaStore::methods[] = {
+const luaL_Reg LuaAreaStore::methods[] = {
luamethod(LuaAreaStore, get_area),
luamethod(LuaAreaStore, get_areas_for_pos),
luamethod(LuaAreaStore, get_areas_in_area),
luamethod(LuaAreaStore, reserve),
luamethod(LuaAreaStore, remove_area),
luamethod(LuaAreaStore, set_cache_params),
- /* luamethod(LuaAreaStore, to_string),
+ luamethod(LuaAreaStore, to_string),
luamethod(LuaAreaStore, to_file),
luamethod(LuaAreaStore, from_string),
- luamethod(LuaAreaStore, from_file),*/
+ luamethod(LuaAreaStore, from_file),
{0,0}
};