From 6c0fa674b91057fe27da23802c969adcb8f55385 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Rollo Date: Tue, 25 Jun 2019 10:59:30 +0200 Subject: [PATCH] Add FFI pointer and size retrieval and sanity check functions diff --git a/src/mapnode.cpp b/src/mapnode.cpp index 9761a661..41a7e71b 100644 --- a/src/mapnode.cpp +++ b/src/mapnode.cpp @@ -30,6 +30,7 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "util/numeric.h" #include #include +#include static const Rotation wallmounted_to_rot[] = { ROTATE_0, ROTATE_180, ROTATE_90, ROTATE_270 @@ -44,6 +45,21 @@ static const u8 rot_to_wallmounted[] = { MapNode */ +extern "C" { + +#ifdef WIN32 +__declspec(dllexport) +#endif +int get_mapnode_version(void) +{ + if (sizeof(MapNode) == 4 && offsetof(MapNode, param0) == 0 + && offsetof(MapNode, param1) == 2 && offsetof(MapNode, param2) == 3) + return 1; + return 0; +} + +} // extern "C" + void MapNode::getColor(const ContentFeatures &f, video::SColor *color) const { if (f.palette) { diff --git a/src/script/lua_api/l_noise.cpp b/src/script/lua_api/l_noise.cpp index e38d319f..11363e39 100644 --- a/src/script/lua_api/l_noise.cpp +++ b/src/script/lua_api/l_noise.cpp @@ -17,6 +17,7 @@ with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. */ +#include "config.h" #include "lua_api/l_noise.h" #include "lua_api/l_internal.h" #include "common/c_converter.h" @@ -401,6 +402,58 @@ luaL_Reg LuaPerlinNoiseMap::methods[] = { {0,0} }; +extern "C" { + +#ifdef WIN32 +__declspec(dllexport) +#endif +size_t PerlinNoiseMap_get_area(void **pnmp) +{ + NO_MAP_LOCK_REQUIRED; + + if (pnmp == nullptr) + throw ModError("Nil pointer in C call"); + + LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)pnmp; + + Noise *n = o->getNoise(); + + return n->sx * n->sy; +} + +#ifdef WIN32 +__declspec(dllexport) +#endif +size_t PerlinNoiseMap_get_volume(void **pnmp) +{ + NO_MAP_LOCK_REQUIRED; + + if (pnmp == nullptr) + throw ModError("Nil pointer in C call"); + + LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)pnmp; + + Noise *n = o->getNoise(); + + return n->sx * n->sy * n->sz; +} + +#ifdef WIN32 +__declspec(dllexport) +#endif +void PerlinNoiseMap_get_pointer(void **pnmp, void **ptr) +{ + NO_MAP_LOCK_REQUIRED; + + if (pnmp == nullptr || ptr == nullptr) + throw ModError("Nil pointer in C call"); + + LuaPerlinNoiseMap *o = *(LuaPerlinNoiseMap **)pnmp; + *ptr = o->getNoise(); +} + +} // extern "C" + /////////////////////////////////////// /* LuaPseudoRandom diff --git a/src/script/lua_api/l_noise.h b/src/script/lua_api/l_noise.h index 9f50dfd3..49a7ec6e 100644 --- a/src/script/lua_api/l_noise.h +++ b/src/script/lua_api/l_noise.h @@ -91,6 +91,8 @@ class LuaPerlinNoiseMap : public ModApiBase static LuaPerlinNoiseMap *checkobject(lua_State *L, int narg); static void Register(lua_State *L); + + Noise *getNoise() const { return noise; } }; /* diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index c92983bd..574883fa 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -30,6 +30,32 @@ with this program; if not, write to the Free Software Foundation, Inc., #include "mapgen/mapgen.h" #include "voxelalgorithms.h" +extern "C" { + +#ifdef WIN32 +__declspec(dllexport) +#endif +void VoxelManip_get_pointer(void **lvmp, void **ptr) +{ + if (lvmp == nullptr || ptr == nullptr) + throw ModError("Nil pointer in C call"); + + *ptr = (*(LuaVoxelManip **)lvmp)->vm->m_data; +} + +#ifdef WIN32 +__declspec(dllexport) +#endif +s32 VoxelManip_get_volume(void **lvmp) +{ + if (lvmp == nullptr) + throw ModError("Nil pointer in C call"); + + return (*(LuaVoxelManip **)lvmp)->vm->m_area.getVolume(); +} + +} // extern "C" + // garbage collector int LuaVoxelManip::gc_object(lua_State *L) { -- 2.20.1