]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/script/lua_api/l_areastore.cpp
Attachments: Fix interpolation from (0,0,0) after detach
[dragonfireclient.git] / src / script / lua_api / l_areastore.cpp
index 4148780a1d36168934dd23b173614063e2f84695..908c766b065ca20a7dda1d354ce39926790c2662 100644 (file)
@@ -23,11 +23,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #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,
@@ -73,6 +70,22 @@ static inline void push_areas(lua_State *L, const std::vector<Area *> &areas,
        }
 }
 
+// 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)
 {
@@ -98,6 +111,9 @@ int LuaAreaStore::l_get_area(lua_State *L)
        const Area *res;
 
        res = ast->getArea(id);
+       if (!res)
+               return 0;
+
        push_area(L, res, include_borders, include_data);
 
        return 1;
@@ -140,7 +156,7 @@ int LuaAreaStore::l_get_areas_in_area(lua_State *L)
        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;
@@ -151,7 +167,7 @@ int LuaAreaStore::l_get_areas_in_area(lua_State *L)
        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;
@@ -159,26 +175,19 @@ int LuaAreaStore::l_insert_area(lua_State *L)
        LuaAreaStore *o = checkobject(L, 1);
        AreaStore *ast = o->as;
 
-       Area a;
-
-       a.minedge = check_v3s16(L, 2);
-       a.maxedge = check_v3s16(L, 3);
-
-       a.extremifyEdges();
-       a.id = ast->getFreeId(a.minedge, a.maxedge);
-
-       if (a.id == AREA_ID_INVALID) {
-               // couldn't get free id
-               lua_pushnil(L);
-               return 1;
-       }
+       Area a(check_v3s16(L, 2), check_v3s16(L, 3));
 
        size_t d_len;
        const char *data = luaL_checklstring(L, 4, &d_len);
 
        a.data = std::string(data, d_len);
 
-       ast->insertArea(a);
+       if (lua_isnumber(L, 5))
+               a.id = lua_tonumber(L, 5);
+
+       // Insert & assign a new ID if necessary
+       if (!ast->insertArea(&a))
+               return 0;
 
        lua_pushnumber(L, a.id);
        return 1;
@@ -231,17 +240,15 @@ int LuaAreaStore::l_set_cache_params(lua_State *L)
        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());
@@ -257,7 +264,7 @@ int LuaAreaStore::l_to_file(lua_State *L)
        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);
@@ -272,16 +279,12 @@ int LuaAreaStore::l_from_string(lua_State *L)
        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)
@@ -290,37 +293,27 @@ int LuaAreaStore::l_from_file(lua_State *L)
        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();
        }
 }
 
@@ -336,7 +329,7 @@ int LuaAreaStore::create_object(lua_State *L)
        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;
@@ -387,7 +380,7 @@ void LuaAreaStore::Register(lua_State *L)
 }
 
 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),
@@ -395,9 +388,9 @@ const luaL_reg LuaAreaStore::methods[] = {
        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}
 };