Skip to content

Commit c830b40

Browse files
committed
Refactor CEvents class to use a context stack instead of a single current context pointer
1 parent 1aad7ca commit c830b40

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

Server/mods/deathmatch/logic/CEvents.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ CEvents::CEvents()
1919
{
2020
m_bWasEventCancelled = false;
2121
m_bEventCancelled = false;
22-
m_pCurrentContext = nullptr;
2322
}
2423

2524
bool CEvents::AddEvent(const char* szName, const char* szArguments, CLuaMain* pLuaMain, bool bAllowRemoteTrigger)
@@ -132,8 +131,8 @@ void CEvents::PreEventPulse(CEventContext* pContext)
132131
assert(pContext);
133132

134133
m_CancelledList.push_back(m_bEventCancelled);
134+
m_ContextStack.push_back(pContext);
135135

136-
m_pCurrentContext = pContext;
137136
pContext->Reset();
138137

139138
m_bEventCancelled = false;
@@ -144,13 +143,13 @@ void CEvents::PreEventPulse(CEventContext* pContext)
144143
void CEvents::PostEventPulse(CEventContext* pContext)
145144
{
146145
assert(pContext);
147-
assert(m_pCurrentContext == pContext);
146+
assert(!m_ContextStack.empty());
147+
assert(m_ContextStack.back() == pContext);
148148

149149
m_bWasEventCancelled = pContext->IsCancelled();
150150
m_bEventCancelled = m_CancelledList.back() ? true : false;
151151
m_CancelledList.pop_back();
152-
153-
m_pCurrentContext = nullptr;
152+
m_ContextStack.pop_back();
154153
}
155154

156155
void CEvents::CancelEvent(bool bCancelled)
@@ -164,12 +163,13 @@ void CEvents::CancelEvent(bool bCancelled, const char* szReason)
164163
m_bEventCancelled = bCancelled;
165164

166165
// Also update context if it exists
167-
if (m_pCurrentContext)
166+
if (!m_ContextStack.empty())
168167
{
168+
CEventContext* pCurrentContext = m_ContextStack.back();
169169
if (bCancelled)
170-
m_pCurrentContext->Cancel(szReason);
170+
pCurrentContext->Cancel(szReason);
171171
else
172-
m_pCurrentContext->Reset();
172+
pCurrentContext->Reset();
173173
}
174174

175175
if (szReason)
@@ -178,16 +178,16 @@ void CEvents::CancelEvent(bool bCancelled, const char* szReason)
178178

179179
bool CEvents::WasEventCancelled()
180180
{
181-
if (m_pCurrentContext)
182-
return m_pCurrentContext->IsCancelled();
181+
if (!m_ContextStack.empty())
182+
return m_ContextStack.back()->IsCancelled();
183183

184184
return m_bEventCancelled || m_bWasEventCancelled;
185185
}
186186

187187
const char* CEvents::GetLastError()
188188
{
189-
if (m_pCurrentContext)
190-
return m_pCurrentContext->GetCancelReason().c_str();
189+
if (!m_ContextStack.empty())
190+
return m_ContextStack.back()->GetCancelReason().c_str();
191191

192192
return m_strLastError;
193193
}

Server/mods/deathmatch/logic/CEvents.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,10 @@ class CEvents
5454

5555
CFastHashMap<SString, SEvent*> m_EventHashMap;
5656

57-
std::vector<int> m_CancelledList;
58-
bool m_bEventCancelled;
59-
bool m_bWasEventCancelled;
60-
SString m_strLastError;
57+
std::vector<int> m_CancelledList;
58+
bool m_bEventCancelled;
59+
bool m_bWasEventCancelled;
60+
SString m_strLastError;
6161

62-
CEventContext* m_pCurrentContext;
62+
std::vector<CEventContext*> m_ContextStack;
6363
};

0 commit comments

Comments
 (0)