add a special case for null mod to allow listening for all mod events

This commit is contained in:
HJfod 2023-04-09 11:19:48 +03:00
parent 473a15b55a
commit fe27260abc
2 changed files with 14 additions and 1 deletions

View file

@ -17,6 +17,9 @@ namespace geode {
DataSaved,
};
/**
* Event that is fired when a mod is loaded / unloaded / enabled / disabled
*/
class GEODE_DLL ModStateEvent : public Event {
protected:
ModEventType m_type;
@ -28,6 +31,9 @@ namespace geode {
Mod* getMod() const;
};
/**
* Listener for mod load/enable/disable/unload/data save events
*/
class GEODE_DLL ModStateFilter : public EventFilter<ModStateEvent> {
public:
using Callback = void(ModStateEvent*);
@ -38,6 +44,13 @@ namespace geode {
public:
ListenerResult handle(utils::MiniFunction<Callback> fn, ModStateEvent* event);
/**
* Create a mod state listener
* @param mod The mod whose events to listen to, or nullptr to listen to
* all mods' all state events
* @param type Type of event to listen to. Ignored if mod is nullptr
*/
ModStateFilter(Mod* mod, ModEventType type);
};
}

View file

@ -13,7 +13,7 @@ Mod* ModStateEvent::getMod() const {
}
ListenerResult ModStateFilter::handle(utils::MiniFunction<Callback> fn, ModStateEvent* event) {
if (event->getMod() == m_mod && event->getType() == m_type) {
if (!m_mod || (event->getMod() == m_mod && event->getType() == m_type)) {
fn(event);
}
return ListenerResult::Propagate;