Skip to content

Commit e36af6f

Browse files
red-001SmallJoker
authored andcommittedJun 27, 2018
Fix mod channels crash (#7481)
1 parent a43a4e2 commit e36af6f

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed
 

Diff for: ‎src/script/lua_api/l_modchannels.cpp

+10-18
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ int ModApiChannels::l_mod_channel_join(lua_State *L)
3333
return 0;
3434

3535
getGameDef(L)->joinModChannel(channel);
36-
ModChannel *channelObj = getGameDef(L)->getModChannel(channel);
37-
assert(channelObj);
38-
ModChannelRef::create(L, channelObj);
36+
assert(getGameDef(L)->getModChannel(channel) != nullptr);
37+
ModChannelRef::create(L, channel);
3938

4039
int object = lua_gettop(L);
4140
lua_pushvalue(L, object);
@@ -51,29 +50,22 @@ void ModApiChannels::Initialize(lua_State *L, int top)
5150
* ModChannelRef
5251
*/
5352

54-
ModChannelRef::ModChannelRef(ModChannel *modchannel) : m_modchannel(modchannel)
53+
ModChannelRef::ModChannelRef(const std::string &modchannel) :
54+
m_modchannel_name(modchannel)
5555
{
5656
}
5757

5858
int ModChannelRef::l_leave(lua_State *L)
5959
{
6060
ModChannelRef *ref = checkobject(L, 1);
61-
ModChannel *channel = getobject(ref);
62-
if (!channel)
63-
return 0;
64-
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;
61+
getGameDef(L)->leaveModChannel(ref->m_modchannel_name);
7062
return 0;
7163
}
7264

7365
int ModChannelRef::l_send_all(lua_State *L)
7466
{
7567
ModChannelRef *ref = checkobject(L, 1);
76-
ModChannel *channel = getobject(ref);
68+
ModChannel *channel = getobject(L, ref);
7769
if (!channel || !channel->canWrite())
7870
return 0;
7971

@@ -87,7 +79,7 @@ int ModChannelRef::l_send_all(lua_State *L)
8779
int ModChannelRef::l_is_writeable(lua_State *L)
8880
{
8981
ModChannelRef *ref = checkobject(L, 1);
90-
ModChannel *channel = getobject(ref);
82+
ModChannel *channel = getobject(L, ref);
9183
if (!channel)
9284
return 0;
9385

@@ -119,7 +111,7 @@ void ModChannelRef::Register(lua_State *L)
119111
lua_pop(L, 1); // Drop methodtable
120112
}
121113

122-
void ModChannelRef::create(lua_State *L, ModChannel *channel)
114+
void ModChannelRef::create(lua_State *L, const std::string &channel)
123115
{
124116
ModChannelRef *o = new ModChannelRef(channel);
125117
*(void **)(lua_newuserdata(L, sizeof(void *))) = o;
@@ -145,9 +137,9 @@ ModChannelRef *ModChannelRef::checkobject(lua_State *L, int narg)
145137
return *(ModChannelRef **)ud; // unbox pointer
146138
}
147139

148-
ModChannel *ModChannelRef::getobject(ModChannelRef *ref)
140+
ModChannel *ModChannelRef::getobject(lua_State *L, ModChannelRef *ref)
149141
{
150-
return ref->m_modchannel;
142+
return getGameDef(L)->getModChannel(ref->m_modchannel_name);
151143
}
152144

153145
// clang-format off

Diff for: ‎src/script/lua_api/l_modchannels.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class ModApiChannels : public ModApiBase
3737
class ModChannelRef : public ModApiBase
3838
{
3939
public:
40-
ModChannelRef(ModChannel *modchannel);
40+
ModChannelRef(const std::string &modchannel);
4141
~ModChannelRef() = default;
4242

4343
static void Register(lua_State *L);
44-
static void create(lua_State *L, ModChannel *channel);
44+
static void create(lua_State *L, const std::string &channel);
4545

4646
// leave()
4747
static int l_leave(lua_State *L);
@@ -57,9 +57,9 @@ class ModChannelRef : public ModApiBase
5757
static int gc_object(lua_State *L);
5858

5959
static ModChannelRef *checkobject(lua_State *L, int narg);
60-
static ModChannel *getobject(ModChannelRef *ref);
60+
static ModChannel *getobject(lua_State *L, ModChannelRef *ref);
6161

62-
ModChannel *m_modchannel = nullptr;
62+
std::string m_modchannel_name;
6363

6464
static const char className[];
6565
static const luaL_Reg methods[];

0 commit comments

Comments
 (0)
Please sign in to comment.