From 7632af3c73fc4e4ae3ad4c98c90c39b47c3b8379 Mon Sep 17 00:00:00 2001 From: Jude Melton-Houghton Date: Tue, 4 Oct 2022 08:31:36 -0400 Subject: Consolidate API object code (#12728) Co-authored-by: sfan5 --- src/script/lua_api/l_vmanip.cpp | 71 ++++++++++++----------------------------- 1 file changed, 21 insertions(+), 50 deletions(-) (limited to 'src/script/lua_api/l_vmanip.cpp') diff --git a/src/script/lua_api/l_vmanip.cpp b/src/script/lua_api/l_vmanip.cpp index f6426771f..8f40b7d4a 100644 --- a/src/script/lua_api/l_vmanip.cpp +++ b/src/script/lua_api/l_vmanip.cpp @@ -44,7 +44,7 @@ int LuaVoxelManip::l_read_from_map(lua_State *L) { MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); MMVManip *vm = o->vm; if (vm->isOrphan()) return 0; @@ -65,7 +65,7 @@ int LuaVoxelManip::l_get_data(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); bool use_buffer = lua_istable(L, 2); MMVManip *vm = o->vm; @@ -90,7 +90,7 @@ int LuaVoxelManip::l_set_data(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); MMVManip *vm = o->vm; if (!lua_istable(L, 2)) @@ -113,7 +113,7 @@ int LuaVoxelManip::l_write_to_map(lua_State *L) { MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); bool update_light = !lua_isboolean(L, 2) || readParam(L, 2); GET_ENV_PTR; @@ -141,7 +141,7 @@ int LuaVoxelManip::l_get_node_at(lua_State *L) const NodeDefManager *ndef = getServer(L)->getNodeDefManager(); - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); v3s16 pos = check_v3s16(L, 2); pushnode(L, o->vm->getNodeNoExNoEmerge(pos), ndef); @@ -154,7 +154,7 @@ int LuaVoxelManip::l_set_node_at(lua_State *L) const NodeDefManager *ndef = getServer(L)->getNodeDefManager(); - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); v3s16 pos = check_v3s16(L, 2); MapNode n = readnode(L, 3, ndef); @@ -167,7 +167,7 @@ int LuaVoxelManip::l_update_liquids(lua_State *L) { GET_ENV_PTR; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); ServerMap *map = &(env->getServerMap()); const NodeDefManager *ndef = getServer(L)->getNodeDefManager(); @@ -187,7 +187,7 @@ int LuaVoxelManip::l_calc_lighting(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); if (!o->is_mapgen_vm) { warningstream << "VoxelManip:calc_lighting called for a non-mapgen " "VoxelManip object" << std::endl; @@ -223,7 +223,7 @@ int LuaVoxelManip::l_set_lighting(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); if (!o->is_mapgen_vm) { warningstream << "VoxelManip:set_lighting called for a non-mapgen " "VoxelManip object" << std::endl; @@ -259,7 +259,7 @@ int LuaVoxelManip::l_get_light_data(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); bool use_buffer = lua_istable(L, 2); MMVManip *vm = o->vm; @@ -284,7 +284,7 @@ int LuaVoxelManip::l_set_light_data(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); MMVManip *vm = o->vm; if (!lua_istable(L, 2)) @@ -308,7 +308,7 @@ int LuaVoxelManip::l_get_param2_data(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); bool use_buffer = lua_istable(L, 2); MMVManip *vm = o->vm; @@ -333,7 +333,7 @@ int LuaVoxelManip::l_set_param2_data(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); MMVManip *vm = o->vm; if (!lua_istable(L, 2)) @@ -362,7 +362,7 @@ int LuaVoxelManip::l_was_modified(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); MMVManip *vm = o->vm; lua_pushboolean(L, vm->m_is_dirty); @@ -374,7 +374,7 @@ int LuaVoxelManip::l_get_emerged_area(lua_State *L) { NO_MAP_LOCK_REQUIRED; - LuaVoxelManip *o = checkobject(L, 1); + LuaVoxelManip *o = checkObject(L, 1); push_v3s16(L, o->vm->m_area.MinEdge); push_v3s16(L, o->vm->m_area.MaxEdge); @@ -425,22 +425,9 @@ int LuaVoxelManip::create_object(lua_State *L) return 1; } -LuaVoxelManip *LuaVoxelManip::checkobject(lua_State *L, int narg) -{ - NO_MAP_LOCK_REQUIRED; - - luaL_checktype(L, narg, LUA_TUSERDATA); - - void *ud = luaL_checkudata(L, narg, className); - if (!ud) - luaL_typerror(L, narg, className); - - return *(LuaVoxelManip **)ud; // unbox pointer -} - void *LuaVoxelManip::packIn(lua_State *L, int idx) { - LuaVoxelManip *o = checkobject(L, idx); + LuaVoxelManip *o = checkObject(L, idx); if (o->is_mapgen_vm) throw LuaError("nope"); @@ -468,27 +455,11 @@ void LuaVoxelManip::packOut(lua_State *L, void *ptr) void LuaVoxelManip::Register(lua_State *L) { - lua_newtable(L); - int methodtable = lua_gettop(L); - luaL_newmetatable(L, className); - int metatable = lua_gettop(L); - - lua_pushliteral(L, "__metatable"); - lua_pushvalue(L, methodtable); - lua_settable(L, metatable); // hide metatable from Lua getmetatable() - - lua_pushliteral(L, "__index"); - lua_pushvalue(L, methodtable); - lua_settable(L, metatable); - - lua_pushliteral(L, "__gc"); - lua_pushcfunction(L, gc_object); - lua_settable(L, metatable); - - lua_pop(L, 1); // drop metatable - - luaL_register(L, nullptr, methods); // fill methodtable - lua_pop(L, 1); // drop methodtable + static const luaL_Reg metamethods[] = { + {"__gc", gc_object}, + {0, 0} + }; + registerClass(L, className, methods, metamethods); // Can be created from Lua (VoxelManip()) lua_register(L, className, create_object); -- cgit v1.2.3