mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-11 17:51:04 +00:00
Consolidate API object code (#12728)
Co-authored-by: sfan5 <sfan5@live.de>
This commit is contained in:
parent
b21fb18379
commit
7632af3c73
42 changed files with 463 additions and 1079 deletions
|
@ -401,17 +401,6 @@ int LuaLocalPlayer::l_hud_get(lua_State *L)
|
|||
return 1;
|
||||
}
|
||||
|
||||
LuaLocalPlayer *LuaLocalPlayer::checkobject(lua_State *L, int narg)
|
||||
{
|
||||
luaL_checktype(L, narg, LUA_TUSERDATA);
|
||||
|
||||
void *ud = luaL_checkudata(L, narg, className);
|
||||
if (!ud)
|
||||
luaL_typerror(L, narg, className);
|
||||
|
||||
return *(LuaLocalPlayer **)ud;
|
||||
}
|
||||
|
||||
LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
|
||||
{
|
||||
return ref->m_localplayer;
|
||||
|
@ -419,7 +408,7 @@ LocalPlayer *LuaLocalPlayer::getobject(LuaLocalPlayer *ref)
|
|||
|
||||
LocalPlayer *LuaLocalPlayer::getobject(lua_State *L, int narg)
|
||||
{
|
||||
LuaLocalPlayer *ref = checkobject(L, narg);
|
||||
LuaLocalPlayer *ref = checkObject<LuaLocalPlayer>(L, narg);
|
||||
assert(ref);
|
||||
LocalPlayer *player = getobject(ref);
|
||||
assert(player);
|
||||
|
@ -435,27 +424,11 @@ int LuaLocalPlayer::gc_object(lua_State *L)
|
|||
|
||||
void LuaLocalPlayer::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);
|
||||
}
|
||||
|
||||
const char LuaLocalPlayer::className[] = "LocalPlayer";
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue