Skip to content

Commit

Permalink
Game Events: fixed a few oversights in 056d7ac
Browse files Browse the repository at this point in the history
* Fixed assertion failure when removing single-use custom events. Not actually something
  broken, I just forgot to add a "not already disabled" check when doing so since I added
  that assertion in event_handler::disable.
* Added a message to the aforementioned assertion.
* Ensure handler cleanup actually happens when removing custom events.
* Account for events with multiple names in cleanup.
  • Loading branch information
Vultraz committed Nov 29, 2017
1 parent 3f646cc commit 2c12d13
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
3 changes: 1 addition & 2 deletions src/game_events/handlers.cpp
Expand Up @@ -57,11 +57,10 @@ event_handler::event_handler(const config& cfg, bool imi)

void event_handler::disable()
{
assert(!disabled_);
assert(!disabled_ && "Trying to disable a disabled event. Shouldn't happen!");
disabled_ = true;
}


void event_handler::handle_event(const queued_event& event_info, game_lua_kernel& lk)
{
if(disabled_) {
Expand Down
18 changes: 11 additions & 7 deletions src/game_events/manager_impl.cpp
Expand Up @@ -149,14 +149,15 @@ void event_handlers::remove_event_handler(const std::string& id)
if(find_it != id_map_.end()) {
handler_ptr handler = find_it->second.lock();

// Remove handler.
if(handler) {
if(handler && !handler->disabled()) {
handler->disable();
}

// Do this even if the lock failed.
// The index by name will self-adjust later. No need to adjust it now.
id_map_.erase(find_it);

// Remove handler from other lists.
clean_up_expired_handlers(handler->get_config()["name"]);
}

log_handlers();
Expand All @@ -171,10 +172,13 @@ void event_handlers::clean_up_expired_handlers(const std::string& event_name)

active_.erase(to_remove, active_.end());

// Then remove any now-unlockable weak_ptrs from tbe by-name list.
get(event_name).remove_if(
[](weak_handler_ptr ptr) { return ptr.expired(); }
);
// Then remove any now-unlockable weak_ptrs from the by-name list.
// Might be more than one so we split.
for(const std::string& name : utils::split(event_name)) {
get(name).remove_if(
[](weak_handler_ptr ptr) { return ptr.expired(); }
);
}

// And finally remove any now-unlockable weak_ptrs from the with-variables name list.
dynamic_.remove_if(
Expand Down

0 comments on commit 2c12d13

Please sign in to comment.