From fe27260abcdcd096faac461f0463a030c1520693 Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Sun, 9 Apr 2023 11:19:48 +0300 Subject: [PATCH] add a special case for null mod to allow listening for all mod events --- loader/include/Geode/loader/ModEvent.hpp | 13 +++++++++++++ loader/src/loader/ModEvent.cpp | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/loader/include/Geode/loader/ModEvent.hpp b/loader/include/Geode/loader/ModEvent.hpp index e77f11e2..e8b66f51 100644 --- a/loader/include/Geode/loader/ModEvent.hpp +++ b/loader/include/Geode/loader/ModEvent.hpp @@ -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 { public: using Callback = void(ModStateEvent*); @@ -38,6 +44,13 @@ namespace geode { public: ListenerResult handle(utils::MiniFunction 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); }; } diff --git a/loader/src/loader/ModEvent.cpp b/loader/src/loader/ModEvent.cpp index 3f063eba..606cc625 100644 --- a/loader/src/loader/ModEvent.cpp +++ b/loader/src/loader/ModEvent.cpp @@ -13,7 +13,7 @@ Mod* ModStateEvent::getMod() const { } ListenerResult ModStateFilter::handle(utils::MiniFunction 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;