]> git.lizzy.rs Git - minetest.git/blob - src/script/lua_api/l_playermeta.cpp
reportMetadataChange; Silence clang warnings
[minetest.git] / src / script / lua_api / l_playermeta.cpp
1 /*
2 Minetest
3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4 Copyright (C) 2017-8 rubenwardy <rw@rubenwardy.com>
5
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.
10
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.
15
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.
19 */
20
21 #include "lua_api/l_playermeta.h"
22 #include "lua_api/l_internal.h"
23 #include "common/c_content.h"
24
25 /*
26         PlayerMetaRef
27 */
28 PlayerMetaRef *PlayerMetaRef::checkobject(lua_State *L, int narg)
29 {
30         luaL_checktype(L, narg, LUA_TUSERDATA);
31         void *ud = luaL_checkudata(L, narg, className);
32         if (!ud)
33                 luaL_typerror(L, narg, className);
34
35         return *(PlayerMetaRef **)ud; // unbox pointer
36 }
37
38 Metadata *PlayerMetaRef::getmeta(bool auto_create)
39 {
40         return metadata;
41 }
42
43 void PlayerMetaRef::clearMeta()
44 {
45         metadata->clear();
46 }
47
48 void PlayerMetaRef::reportMetadataChange(const std::string *name)
49 {
50         // TODO
51 }
52
53 // garbage collector
54 int PlayerMetaRef::gc_object(lua_State *L)
55 {
56         PlayerMetaRef *o = *(PlayerMetaRef **)(lua_touserdata(L, 1));
57         delete o;
58         return 0;
59 }
60
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)
64 {
65         PlayerMetaRef *o = new PlayerMetaRef(metadata);
66         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
67         luaL_getmetatable(L, className);
68         lua_setmetatable(L, -2);
69 }
70
71 void PlayerMetaRef::Register(lua_State *L)
72 {
73         lua_newtable(L);
74         int methodtable = lua_gettop(L);
75         luaL_newmetatable(L, className);
76         int metatable = lua_gettop(L);
77
78         lua_pushliteral(L, "__metatable");
79         lua_pushvalue(L, methodtable);
80         lua_settable(L, metatable); // hide metatable from Lua getmetatable()
81
82         lua_pushliteral(L, "metadata_class");
83         lua_pushlstring(L, className, strlen(className));
84         lua_settable(L, metatable);
85
86         lua_pushliteral(L, "__index");
87         lua_pushvalue(L, methodtable);
88         lua_settable(L, metatable);
89
90         lua_pushliteral(L, "__gc");
91         lua_pushcfunction(L, gc_object);
92         lua_settable(L, metatable);
93
94         lua_pushliteral(L, "__eq");
95         lua_pushcfunction(L, l_equals);
96         lua_settable(L, metatable);
97
98         lua_pop(L, 1); // drop metatable
99
100         luaL_openlib(L, 0, methods, 0);
101         lua_pop(L, 1);
102
103         // Cannot be created from Lua
104         // lua_register(L, className, create_object);
105 }
106
107 // clang-format off
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),
121         {0,0}
122 };
123 // clang-format on