3 Copyright (C) 2017 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
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.
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.
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.
22 #include "lua_api/l_modchannels.h"
23 #include "l_internal.h"
24 #include "modchannels.h"
26 int ModApiChannels::l_mod_channel_join(lua_State *L)
28 if (!lua_isstring(L, 1))
31 std::string channel = luaL_checkstring(L, 1);
35 getGameDef(L)->joinModChannel(channel);
36 assert(getGameDef(L)->getModChannel(channel) != nullptr);
37 ModChannelRef::create(L, channel);
39 int object = lua_gettop(L);
40 lua_pushvalue(L, object);
44 void ModApiChannels::Initialize(lua_State *L, int top)
46 API_FCT(mod_channel_join);
53 ModChannelRef::ModChannelRef(const std::string &modchannel) :
54 m_modchannel_name(modchannel)
58 int ModChannelRef::l_leave(lua_State *L)
60 ModChannelRef *ref = checkobject(L, 1);
61 getGameDef(L)->leaveModChannel(ref->m_modchannel_name);
65 int ModChannelRef::l_send_all(lua_State *L)
67 ModChannelRef *ref = checkobject(L, 1);
68 ModChannel *channel = getobject(L, ref);
69 if (!channel || !channel->canWrite())
72 // @TODO serialize message
73 std::string message = luaL_checkstring(L, 2);
75 getGameDef(L)->sendModChannelMessage(channel->getName(), message);
79 int ModChannelRef::l_is_writeable(lua_State *L)
81 ModChannelRef *ref = checkobject(L, 1);
82 ModChannel *channel = getobject(L, ref);
86 lua_pushboolean(L, channel->canWrite());
89 void ModChannelRef::Register(lua_State *L)
92 int methodtable = lua_gettop(L);
93 luaL_newmetatable(L, className);
94 int metatable = lua_gettop(L);
96 lua_pushliteral(L, "__metatable");
97 lua_pushvalue(L, methodtable);
98 lua_settable(L, metatable); // hide metatable from lua getmetatable()
100 lua_pushliteral(L, "__index");
101 lua_pushvalue(L, methodtable);
102 lua_settable(L, metatable);
104 lua_pushliteral(L, "__gc");
105 lua_pushcfunction(L, gc_object);
106 lua_settable(L, metatable);
108 lua_pop(L, 1); // Drop metatable
110 luaL_openlib(L, 0, methods, 0); // fill methodtable
111 lua_pop(L, 1); // Drop methodtable
114 void ModChannelRef::create(lua_State *L, const std::string &channel)
116 ModChannelRef *o = new ModChannelRef(channel);
117 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
118 luaL_getmetatable(L, className);
119 lua_setmetatable(L, -2);
122 int ModChannelRef::gc_object(lua_State *L)
124 ModChannelRef *o = *(ModChannelRef **)(lua_touserdata(L, 1));
129 ModChannelRef *ModChannelRef::checkobject(lua_State *L, int narg)
131 luaL_checktype(L, narg, LUA_TUSERDATA);
133 void *ud = luaL_checkudata(L, narg, className);
135 luaL_typerror(L, narg, className);
137 return *(ModChannelRef **)ud; // unbox pointer
140 ModChannel *ModChannelRef::getobject(lua_State *L, ModChannelRef *ref)
142 return getGameDef(L)->getModChannel(ref->m_modchannel_name);
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),