From 4fd9dd8d7d46dddac9178bf4b5104fe50f894eb6 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:28:57 +0200 Subject: [PATCH 1/3] feat(event): add globalListen + EventListener operator= --- loader/include/Geode/loader/Event.hpp | 40 +++++++++++++++++++++++++-- 1 file changed, 38 insertions(+), 2 deletions(-) diff --git a/loader/include/Geode/loader/Event.hpp b/loader/include/Geode/loader/Event.hpp index 8f88cb54e..cb5a7a4e2 100644 --- a/loader/include/Geode/loader/Event.hpp +++ b/loader/include/Geode/loader/Event.hpp @@ -3,11 +3,14 @@ #include "../utils/casts.hpp" #include +#include +#include +#include #include #include #include -#include #include +#include namespace geode { class Mod; @@ -190,6 +193,32 @@ namespace geode { this->enable(); } + EventListener& operator=(EventListener const& other) { + if (this == &other) { + return *this; + } + + m_callback = other.m_callback; + m_filter = other.m_filter; + m_filter.setListener(this); + + return *this; + } + + EventListener& operator=(EventListener&& other) { + if (this == &other) { + return *this; + } + + m_callback = std::move(other.m_callback); + m_filter = std::move(other.m_filter); + + m_filter.setListener(this); + other.disable(); + + return *this; + } + void bind(std::function fn) { m_callback = fn; } @@ -239,4 +268,11 @@ namespace geode { virtual ~Event(); }; -} + + // Creates an EventListener that is active for the entire lifetime of the game. You have no way of disabling the listener, so only use this if you want to always listen for certain events! + template + void globalListen(typename T::Callback callback, T filter = T()) { + static std::vector> listeners; + listeners.push_back(std::make_unique>(EventListener {callback, filter})); + } +} \ No newline at end of file From 7ac5d5647175cbb370683033e51bb7fdad894b59 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:38:30 +0200 Subject: [PATCH 2/3] fix(event): leak globalListen :( --- loader/include/Geode/loader/Event.hpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/loader/include/Geode/loader/Event.hpp b/loader/include/Geode/loader/Event.hpp index cb5a7a4e2..e26d0ddd3 100644 --- a/loader/include/Geode/loader/Event.hpp +++ b/loader/include/Geode/loader/Event.hpp @@ -272,7 +272,6 @@ namespace geode { // Creates an EventListener that is active for the entire lifetime of the game. You have no way of disabling the listener, so only use this if you want to always listen for certain events! template void globalListen(typename T::Callback callback, T filter = T()) { - static std::vector> listeners; - listeners.push_back(std::make_unique>(EventListener {callback, filter})); + new EventListener({callback, filter}); } } \ No newline at end of file From 6804dff92538993b344a1c63f055b462458b4492 Mon Sep 17 00:00:00 2001 From: Fleeym <61891787+Fleeym@users.noreply.github.com> Date: Tue, 14 Jan 2025 00:44:07 +0200 Subject: [PATCH 3/3] fix(event): use the proper constructor here --- loader/include/Geode/loader/Event.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/loader/include/Geode/loader/Event.hpp b/loader/include/Geode/loader/Event.hpp index e26d0ddd3..9b23fc783 100644 --- a/loader/include/Geode/loader/Event.hpp +++ b/loader/include/Geode/loader/Event.hpp @@ -272,6 +272,6 @@ namespace geode { // Creates an EventListener that is active for the entire lifetime of the game. You have no way of disabling the listener, so only use this if you want to always listen for certain events! template void globalListen(typename T::Callback callback, T filter = T()) { - new EventListener({callback, filter}); + new EventListener(callback, filter); } } \ No newline at end of file