From e49032eaf74331162ad491963fe99ce9d4a979a4 Mon Sep 17 00:00:00 2001 From: Louis-Antoine Date: Sat, 3 Oct 2020 18:40:37 +0200 Subject: [PATCH] Let Lua scripts access userdata metatables --- src/lua_baselib.c | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lua_baselib.c b/src/lua_baselib.c index 6bdc65786..6f9bcbb23 100644 --- a/src/lua_baselib.c +++ b/src/lua_baselib.c @@ -240,7 +240,7 @@ static int lib_userdataType(lua_State *L) // Takes a metatable as first and only argument // Only callable during script loading -static int lib_registermetatable(lua_State *L) +static int lib_registerMetatable(lua_State *L) { static UINT32 nextid = 1; @@ -265,6 +265,16 @@ static int lib_registermetatable(lua_State *L) return 0; } +// Takes a string as only argument and returns the metatable +// associated to the userdata type this string refers to +// Returns nil if the string does not refer to a valid userdata type +static int lib_userdataMetatable(lua_State *L) +{ + const char *udname = luaL_checkstring(L, 1); + luaL_getmetatable(L, udname); + return 1; +} + static int lib_isPlayerAdmin(lua_State *L) { player_t *player = *((player_t **)luaL_checkudata(L, 1, META_PLAYER)); @@ -3460,7 +3470,8 @@ static luaL_Reg lib[] = { {"chatprint", lib_chatprint}, {"chatprintf", lib_chatprintf}, {"userdataType", lib_userdataType}, - {"registermetatable", lib_registermetatable}, + {"registerMetatable", lib_registerMetatable}, + {"userdataMetatable", lib_userdataMetatable}, {"IsPlayerAdmin", lib_isPlayerAdmin}, {"reserveLuabanks", lib_reserveLuabanks},