Formalize event handlers as stackable

It seems event handlers may be invoked from event handlers, which 87b81ceb45 did not anticipate, oops.
This commit is contained in:
Tamás Bálint Misius
2025-05-05 10:28:10 +02:00
parent 96cd4b6996
commit e6f297680b
2 changed files with 10 additions and 6 deletions

View File

@@ -405,12 +405,11 @@ bool CommandInterface::HandleEvent(const GameControllerEvent &event)
{
auto *lsi = static_cast<LuaScriptInterface *>(this);
auto *L = lsi->L;
assert(!lsi->currentEventHandlerIt);
auto eventType = int(event.index());
auto &list = lsi->gameControllerEventHandlers[eventType];
auto it = list.begin();
auto end = list.end();
lsi->currentEventHandlerIt = &it;
lsi->currentEventHandlerIts.push_back(&it);
bool cont = true;
while (it != end)
{
@@ -441,7 +440,8 @@ bool CommandInterface::HandleEvent(const GameControllerEvent &event)
}
lua_pop(L, 1);
}
lsi->currentEventHandlerIt = nullptr;
assert(lsi->currentEventHandlerIts.back() == &it);
lsi->currentEventHandlerIts.pop_back();
return cont;
}
@@ -463,9 +463,12 @@ void LuaScriptInterface::RemoveEventHandler(int eventType, int stackIndex)
lua_pop(L, 1);
if (equal)
{
if (currentEventHandlerIt && *currentEventHandlerIt == it)
for (auto currentEventHandlerIt : currentEventHandlerIts)
{
++*currentEventHandlerIt;
if (*currentEventHandlerIt == it)
{
++*currentEventHandlerIt;
}
}
list.erase(it);
break;

View File

@@ -9,6 +9,7 @@
#include <map>
#include <memory>
#include <list>
#include <deque>
namespace http
{
@@ -108,7 +109,7 @@ public:
private:
std::vector<std::list<LuaSmartRef>> gameControllerEventHandlers; // must come after luaState
std::list<LuaSmartRef>::iterator *currentEventHandlerIt = nullptr;
std::deque<std::list<LuaSmartRef>::iterator *> currentEventHandlerIts;
public:
void AddEventHandler(int eventType, int stackIndex);