From b4b9986fa529088434ee996fdb5c6c09e2d0fd4a Mon Sep 17 00:00:00 2001 From: HJfod <60038575+HJfod@users.noreply.github.com> Date: Wed, 7 Dec 2022 12:35:50 +0200 Subject: [PATCH] tryna fix crash due to events --- loader/include/Geode/loader/Event.hpp | 10 +++++++--- loader/include/Geode/loader/ModEvent.hpp | 2 +- loader/include/Geode/ui/EnterLayerEvent.hpp | 13 ++++--------- loader/src/hooks/LoadingLayer.cpp | 7 +++---- loader/src/loader/Event.cpp | 11 +++++++++-- loader/src/ui/nodes/EnterLayerEvent.cpp | 14 +++----------- 6 files changed, 27 insertions(+), 30 deletions(-) diff --git a/loader/include/Geode/loader/Event.hpp b/loader/include/Geode/loader/Event.hpp index 1c48d268..22e5e4da 100644 --- a/loader/include/Geode/loader/Event.hpp +++ b/loader/include/Geode/loader/Event.hpp @@ -59,8 +59,10 @@ namespace geode { using MemberFn = typename to_member::value; ListenerResult passThrough(Event* e) override { - if (auto myev = dynamic_cast(e)) { - return m_filter.handle(m_callback, myev); + if (m_callback) { + if (auto myev = typeinfo_cast(e)) { + return m_filter.handle(m_callback, myev); + } } return ListenerResult::Propagate; } @@ -81,14 +83,16 @@ namespace geode { } void bind(std::function fn) { + std::cout << "this: " << this << "\n"; m_callback = fn; } template void bind(C* cls, MemberFn fn) { + std::cout << "this: " << this << "\n"; m_callback = std::bind(fn, cls, std::placeholders::_1); } protected: - std::function m_callback; + std::function m_callback = nullptr; T m_filter; }; diff --git a/loader/include/Geode/loader/ModEvent.hpp b/loader/include/Geode/loader/ModEvent.hpp index 016464da..2b48e104 100644 --- a/loader/include/Geode/loader/ModEvent.hpp +++ b/loader/include/Geode/loader/ModEvent.hpp @@ -47,7 +47,7 @@ namespace { \ } \ static inline auto GEODE_CONCAT(Exec, __LINE__) = (geode::Loader::get()->scheduleOnModLoad(\ geode::Mod::get(), []() { \ - static auto _ = geode::EventListener( \ + new geode::EventListener( \ &GEODE_CONCAT(geodeExecFunction, __LINE__),\ geode::ModStateFilter(geode::Mod::get(), geode::ModEventType::type)\ ); \ diff --git a/loader/include/Geode/ui/EnterLayerEvent.hpp b/loader/include/Geode/ui/EnterLayerEvent.hpp index e339123d..4ce24a7a 100644 --- a/loader/include/Geode/ui/EnterLayerEvent.hpp +++ b/loader/include/Geode/ui/EnterLayerEvent.hpp @@ -11,19 +11,14 @@ namespace geode { concept InheritsCCNode = std::is_base_of_v; // Base class; exists so event listeners can be placed dynamically at runtime - class GEODE_DLL AEnterLayerEvent : public Event { - protected: - std::string m_layerID; - cocos2d::CCNode* m_layer; + struct GEODE_DLL AEnterLayerEvent : public Event { + const std::string layerID; + cocos2d::CCNode* layer; - public: AEnterLayerEvent( std::string const& layerID, cocos2d::CCNode* layer ); - - std::string getID() const; - cocos2d::CCNode* getLayer() const; }; class GEODE_DLL AEnterLayerFilter : public EventFilter { @@ -50,7 +45,7 @@ namespace geode { ) : AEnterLayerEvent(layerID, layer) {} T* getLayer() const { - return static_cast(m_layer); + return static_cast(this->layer); } }; diff --git a/loader/src/hooks/LoadingLayer.cpp b/loader/src/hooks/LoadingLayer.cpp index 65045549..baa684ec 100644 --- a/loader/src/hooks/LoadingLayer.cpp +++ b/loader/src/hooks/LoadingLayer.cpp @@ -28,10 +28,9 @@ struct CustomLoadingLayer : Modify { label->setID("geode-loaded-info"); this->addChild(label); - m_fields->m_resourceListener.bind(std::bind( - &CustomLoadingLayer::updateResourcesProgress, - this, std::placeholders::_1 - )); + m_fields->m_resourceListener.bind( + this, &CustomLoadingLayer::updateResourcesProgress + ); // verify loader resources if (!InternalLoader::get()->verifyLoaderResources()) { diff --git a/loader/src/loader/Event.cpp b/loader/src/loader/Event.cpp index 4f56aa69..3d71d8af 100644 --- a/loader/src/loader/Event.cpp +++ b/loader/src/loader/Event.cpp @@ -5,10 +5,12 @@ USE_GEODE_NAMESPACE(); std::unordered_set Event::s_listeners = {}; void EventListenerProtocol::enable() { + std::cout << "enable " << this << ": " << typeid(*this).name() << "\n"; Event::s_listeners.insert(this); } void EventListenerProtocol::disable() { + std::cout << "disable " << this << "\n"; Event::s_listeners.erase(this); } @@ -22,8 +24,13 @@ void Event::postFrom(Mod* m) { if (m) this->sender = m; for (auto h : Event::s_listeners) { - if (h->passThrough(this) == ListenerResult::Stop) { - break; + try { + std::cout << h << ": " << typeid(*h).name() << "\n"; + if (h->passThrough(this) == ListenerResult::Stop) { + break; + } + } catch(std::exception& e) { + std::cout << "fuck: " << h << ": " << e.what() << "\n"; } } } diff --git a/loader/src/ui/nodes/EnterLayerEvent.cpp b/loader/src/ui/nodes/EnterLayerEvent.cpp index a3a0f8ad..a1908f09 100644 --- a/loader/src/ui/nodes/EnterLayerEvent.cpp +++ b/loader/src/ui/nodes/EnterLayerEvent.cpp @@ -5,19 +5,11 @@ USE_GEODE_NAMESPACE(); AEnterLayerEvent::AEnterLayerEvent( std::string const& layerID, cocos2d::CCNode* layer -) : m_layerID(layerID), - m_layer(layer) {} - -std::string AEnterLayerEvent::getID() const { - return m_layerID; -} - -cocos2d::CCNode* AEnterLayerEvent::getLayer() const { - return m_layer; -} +) : layerID(layerID), + layer(layer) {} ListenerResult AEnterLayerFilter::handle(Callback fn, AEnterLayerEvent* event) { - if (m_targetID == event->getID()) { + if (m_targetID == event->layerID) { fn(event); } return ListenerResult::Propagate;