3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License along
17 with this program; if not, write to the Free Software Foundation, Inc.,
18 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "lua_api/l_playermeta.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_content.h"
28 PlayerMetaRef *PlayerMetaRef::checkobject(lua_State *L, int narg)
30 luaL_checktype(L, narg, LUA_TUSERDATA);
31 void *ud = luaL_checkudata(L, narg, className);
33 luaL_typerror(L, narg, className);
35 return *(PlayerMetaRef **)ud; // unbox pointer
38 Metadata *PlayerMetaRef::getmeta(bool auto_create)
43 void PlayerMetaRef::clearMeta()
48 void PlayerMetaRef::reportMetadataChange()
54 int PlayerMetaRef::gc_object(lua_State *L)
56 PlayerMetaRef *o = *(PlayerMetaRef **)(lua_touserdata(L, 1));
61 // Creates an PlayerMetaRef and leaves it on top of stack
62 // Not callable from Lua; all references are created on the C side.
63 void PlayerMetaRef::create(lua_State *L, Metadata *metadata)
65 PlayerMetaRef *o = new PlayerMetaRef(metadata);
66 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
67 luaL_getmetatable(L, className);
68 lua_setmetatable(L, -2);
71 void PlayerMetaRef::Register(lua_State *L)
74 int methodtable = lua_gettop(L);
75 luaL_newmetatable(L, className);
76 int metatable = lua_gettop(L);
78 lua_pushliteral(L, "__metatable");
79 lua_pushvalue(L, methodtable);
80 lua_settable(L, metatable); // hide metatable from Lua getmetatable()
82 lua_pushliteral(L, "metadata_class");
83 lua_pushlstring(L, className, strlen(className));
84 lua_settable(L, metatable);
86 lua_pushliteral(L, "__index");
87 lua_pushvalue(L, methodtable);
88 lua_settable(L, metatable);
90 lua_pushliteral(L, "__gc");
91 lua_pushcfunction(L, gc_object);
92 lua_settable(L, metatable);
94 lua_pushliteral(L, "__eq");
95 lua_pushcfunction(L, l_equals);
96 lua_settable(L, metatable);
98 lua_pop(L, 1); // drop metatable
100 luaL_openlib(L, 0, methods, 0);
103 // Cannot be created from Lua
104 // lua_register(L, className, create_object);
108 const char PlayerMetaRef::className[] = "PlayerMetaRef";
109 const luaL_Reg PlayerMetaRef::methods[] = {
110 luamethod(MetaDataRef, contains),
111 luamethod(MetaDataRef, get),
112 luamethod(MetaDataRef, get_string),
113 luamethod(MetaDataRef, set_string),
114 luamethod(MetaDataRef, get_int),
115 luamethod(MetaDataRef, set_int),
116 luamethod(MetaDataRef, get_float),
117 luamethod(MetaDataRef, set_float),
118 luamethod(MetaDataRef, to_table),
119 luamethod(MetaDataRef, from_table),
120 luamethod(MetaDataRef, equals),