]> git.lizzy.rs Git - dragonfireclient.git/blob - src/script/lua_api/l_modchannels.cpp
Added the API additions from waspsaliva
[dragonfireclient.git] / src / script / lua_api / l_modchannels.cpp
1 /*
2 Minetest
3 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 GNU Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #include <cassert>
21 #include <log.h>
22 #include "lua_api/l_modchannels.h"
23 #include "l_internal.h"
24 #include "modchannels.h"
25
26 int ModApiChannels::l_mod_channel_join(lua_State *L)
27 {
28         if (!lua_isstring(L, 1))
29                 return 0;
30
31         std::string channel = luaL_checkstring(L, 1);
32         if (channel.empty())
33                 return 0;
34
35         getGameDef(L)->joinModChannel(channel);
36         assert(getGameDef(L)->getModChannel(channel) != nullptr);
37         ModChannelRef::create(L, channel);
38
39         int object = lua_gettop(L);
40         lua_pushvalue(L, object);
41         return 1;
42 }
43
44 void ModApiChannels::Initialize(lua_State *L, int top)
45 {
46         API_FCT(mod_channel_join);
47 }
48
49 /*
50  * ModChannelRef
51  */
52
53 ModChannelRef::ModChannelRef(const std::string &modchannel) :
54                 m_modchannel_name(modchannel)
55 {
56 }
57
58 int ModChannelRef::l_leave(lua_State *L)
59 {
60         ModChannelRef *ref = checkobject(L, 1);
61         getGameDef(L)->leaveModChannel(ref->m_modchannel_name);
62         return 0;
63 }
64
65 int ModChannelRef::l_send_all(lua_State *L)
66 {
67         ModChannelRef *ref = checkobject(L, 1);
68         ModChannel *channel = getobject(L, ref);
69         if (!channel || !channel->canWrite())
70                 return 0;
71
72         // @TODO serialize message
73         std::string message = luaL_checkstring(L, 2);
74
75         getGameDef(L)->sendModChannelMessage(channel->getName(), message);
76         return 0;
77 }
78
79 int ModChannelRef::l_is_writeable(lua_State *L)
80 {
81         ModChannelRef *ref = checkobject(L, 1);
82         ModChannel *channel = getobject(L, ref);
83         if (!channel)
84                 return 0;
85
86         lua_pushboolean(L, channel->canWrite());
87         return 1;
88 }
89 void ModChannelRef::Register(lua_State *L)
90 {
91         lua_newtable(L);
92         int methodtable = lua_gettop(L);
93         luaL_newmetatable(L, className);
94         int metatable = lua_gettop(L);
95
96         lua_pushliteral(L, "__metatable");
97         lua_pushvalue(L, methodtable);
98         lua_settable(L, metatable); // hide metatable from lua getmetatable()
99
100         lua_pushliteral(L, "__index");
101         lua_pushvalue(L, methodtable);
102         lua_settable(L, metatable);
103
104         lua_pushliteral(L, "__gc");
105         lua_pushcfunction(L, gc_object);
106         lua_settable(L, metatable);
107
108         lua_pop(L, 1); // Drop metatable
109
110         luaL_openlib(L, 0, methods, 0); // fill methodtable
111         lua_pop(L, 1);                  // Drop methodtable
112 }
113
114 void ModChannelRef::create(lua_State *L, const std::string &channel)
115 {
116         ModChannelRef *o = new ModChannelRef(channel);
117         *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
118         luaL_getmetatable(L, className);
119         lua_setmetatable(L, -2);
120 }
121
122 int ModChannelRef::gc_object(lua_State *L)
123 {
124         ModChannelRef *o = *(ModChannelRef **)(lua_touserdata(L, 1));
125         delete o;
126         return 0;
127 }
128
129 ModChannelRef *ModChannelRef::checkobject(lua_State *L, int narg)
130 {
131         luaL_checktype(L, narg, LUA_TUSERDATA);
132
133         void *ud = luaL_checkudata(L, narg, className);
134         if (!ud)
135                 luaL_typerror(L, narg, className);
136
137         return *(ModChannelRef **)ud; // unbox pointer
138 }
139
140 ModChannel *ModChannelRef::getobject(lua_State *L, ModChannelRef *ref)
141 {
142         return getGameDef(L)->getModChannel(ref->m_modchannel_name);
143 }
144
145 // clang-format off
146 const char ModChannelRef::className[] = "ModChannelRef";
147 const luaL_Reg ModChannelRef::methods[] = {
148         luamethod(ModChannelRef, leave),
149         luamethod(ModChannelRef, is_writeable),
150         luamethod(ModChannelRef, send_all),
151         {0, 0},
152 };
153 // clang-format on