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 ModChannel *channelObj = getGameDef(L)->getModChannel(channel);
38 ModChannelRef::create(L, channelObj);
40 int object = lua_gettop(L);
41 lua_pushvalue(L, object);
45 void ModApiChannels::Initialize(lua_State *L, int top)
47 API_FCT(mod_channel_join);
54 ModChannelRef::ModChannelRef(ModChannel *modchannel) : m_modchannel(modchannel)
58 int ModChannelRef::l_leave(lua_State *L)
60 ModChannelRef *ref = checkobject(L, 1);
61 ModChannel *channel = getobject(ref);
65 getGameDef(L)->leaveModChannel(channel->getName());
66 // Channel left, invalidate the channel object ptr
67 // This permits to invalidate every object action from Lua because core removed
68 // channel consuming link
69 ref->m_modchannel = nullptr;
73 int ModChannelRef::l_send_all(lua_State *L)
75 ModChannelRef *ref = checkobject(L, 1);
76 ModChannel *channel = getobject(ref);
77 if (!channel || !channel->canWrite())
80 // @TODO serialize message
81 std::string message = luaL_checkstring(L, 2);
83 getGameDef(L)->sendModChannelMessage(channel->getName(), message);
87 int ModChannelRef::l_is_writeable(lua_State *L)
89 ModChannelRef *ref = checkobject(L, 1);
90 ModChannel *channel = getobject(ref);
94 lua_pushboolean(L, channel->canWrite());
97 void ModChannelRef::Register(lua_State *L)
100 int methodtable = lua_gettop(L);
101 luaL_newmetatable(L, className);
102 int metatable = lua_gettop(L);
104 lua_pushliteral(L, "__metatable");
105 lua_pushvalue(L, methodtable);
106 lua_settable(L, metatable); // hide metatable from lua getmetatable()
108 lua_pushliteral(L, "__index");
109 lua_pushvalue(L, methodtable);
110 lua_settable(L, metatable);
112 lua_pushliteral(L, "__gc");
113 lua_pushcfunction(L, gc_object);
114 lua_settable(L, metatable);
116 lua_pop(L, 1); // Drop metatable
118 luaL_openlib(L, 0, methods, 0); // fill methodtable
119 lua_pop(L, 1); // Drop methodtable
122 void ModChannelRef::create(lua_State *L, ModChannel *channel)
124 ModChannelRef *o = new ModChannelRef(channel);
125 *(void **)(lua_newuserdata(L, sizeof(void *))) = o;
126 luaL_getmetatable(L, className);
127 lua_setmetatable(L, -2);
130 int ModChannelRef::gc_object(lua_State *L)
132 ModChannelRef *o = *(ModChannelRef **)(lua_touserdata(L, 1));
137 ModChannelRef *ModChannelRef::checkobject(lua_State *L, int narg)
139 luaL_checktype(L, narg, LUA_TUSERDATA);
141 void *ud = luaL_checkudata(L, narg, className);
143 luaL_typerror(L, narg, className);
145 return *(ModChannelRef **)ud; // unbox pointer
148 ModChannel *ModChannelRef::getobject(ModChannelRef *ref)
150 return ref->m_modchannel;
154 const char ModChannelRef::className[] = "ModChannelRef";
155 const luaL_Reg ModChannelRef::methods[] = {
156 luamethod(ModChannelRef, leave),
157 luamethod(ModChannelRef, is_writeable),
158 luamethod(ModChannelRef, send_all),