tryna fix crash due to events

This commit is contained in:
HJfod 2022-12-07 12:35:50 +02:00
parent 956ad1d6d5
commit b4b9986fa5
6 changed files with 27 additions and 30 deletions

View file

@ -59,8 +59,10 @@ namespace geode {
using MemberFn = typename to_member<C, Callback>::value; using MemberFn = typename to_member<C, Callback>::value;
ListenerResult passThrough(Event* e) override { ListenerResult passThrough(Event* e) override {
if (auto myev = dynamic_cast<typename T::Event*>(e)) { if (m_callback) {
return m_filter.handle(m_callback, myev); if (auto myev = typeinfo_cast<typename T::Event*>(e)) {
return m_filter.handle(m_callback, myev);
}
} }
return ListenerResult::Propagate; return ListenerResult::Propagate;
} }
@ -81,14 +83,16 @@ namespace geode {
} }
void bind(std::function<Callback> fn) { void bind(std::function<Callback> fn) {
std::cout << "this: " << this << "\n";
m_callback = fn; m_callback = fn;
} }
template <typename C> template <typename C>
void bind(C* cls, MemberFn<C> fn) { void bind(C* cls, MemberFn<C> fn) {
std::cout << "this: " << this << "\n";
m_callback = std::bind(fn, cls, std::placeholders::_1); m_callback = std::bind(fn, cls, std::placeholders::_1);
} }
protected: protected:
std::function<Callback> m_callback; std::function<Callback> m_callback = nullptr;
T m_filter; T m_filter;
}; };

View file

@ -47,7 +47,7 @@ namespace { \
} \ } \
static inline auto GEODE_CONCAT(Exec, __LINE__) = (geode::Loader::get()->scheduleOnModLoad(\ static inline auto GEODE_CONCAT(Exec, __LINE__) = (geode::Loader::get()->scheduleOnModLoad(\
geode::Mod::get(), []() { \ geode::Mod::get(), []() { \
static auto _ = geode::EventListener( \ new geode::EventListener( \
&GEODE_CONCAT(geodeExecFunction, __LINE__)<GEODE_CONCAT(ExecFuncUnique, __LINE__)>,\ &GEODE_CONCAT(geodeExecFunction, __LINE__)<GEODE_CONCAT(ExecFuncUnique, __LINE__)>,\
geode::ModStateFilter(geode::Mod::get(), geode::ModEventType::type)\ geode::ModStateFilter(geode::Mod::get(), geode::ModEventType::type)\
); \ ); \

View file

@ -11,19 +11,14 @@ namespace geode {
concept InheritsCCNode = std::is_base_of_v<cocos2d::CCNode, T>; concept InheritsCCNode = std::is_base_of_v<cocos2d::CCNode, T>;
// Base class; exists so event listeners can be placed dynamically at runtime // Base class; exists so event listeners can be placed dynamically at runtime
class GEODE_DLL AEnterLayerEvent : public Event { struct GEODE_DLL AEnterLayerEvent : public Event {
protected: const std::string layerID;
std::string m_layerID; cocos2d::CCNode* layer;
cocos2d::CCNode* m_layer;
public:
AEnterLayerEvent( AEnterLayerEvent(
std::string const& layerID, std::string const& layerID,
cocos2d::CCNode* layer cocos2d::CCNode* layer
); );
std::string getID() const;
cocos2d::CCNode* getLayer() const;
}; };
class GEODE_DLL AEnterLayerFilter : public EventFilter<AEnterLayerEvent> { class GEODE_DLL AEnterLayerFilter : public EventFilter<AEnterLayerEvent> {
@ -50,7 +45,7 @@ namespace geode {
) : AEnterLayerEvent(layerID, layer) {} ) : AEnterLayerEvent(layerID, layer) {}
T* getLayer() const { T* getLayer() const {
return static_cast<T*>(m_layer); return static_cast<T*>(this->layer);
} }
}; };

View file

@ -28,10 +28,9 @@ struct CustomLoadingLayer : Modify<CustomLoadingLayer, LoadingLayer> {
label->setID("geode-loaded-info"); label->setID("geode-loaded-info");
this->addChild(label); this->addChild(label);
m_fields->m_resourceListener.bind(std::bind( m_fields->m_resourceListener.bind(
&CustomLoadingLayer::updateResourcesProgress, this, &CustomLoadingLayer::updateResourcesProgress
this, std::placeholders::_1 );
));
// verify loader resources // verify loader resources
if (!InternalLoader::get()->verifyLoaderResources()) { if (!InternalLoader::get()->verifyLoaderResources()) {

View file

@ -5,10 +5,12 @@ USE_GEODE_NAMESPACE();
std::unordered_set<EventListenerProtocol*> Event::s_listeners = {}; std::unordered_set<EventListenerProtocol*> Event::s_listeners = {};
void EventListenerProtocol::enable() { void EventListenerProtocol::enable() {
std::cout << "enable " << this << ": " << typeid(*this).name() << "\n";
Event::s_listeners.insert(this); Event::s_listeners.insert(this);
} }
void EventListenerProtocol::disable() { void EventListenerProtocol::disable() {
std::cout << "disable " << this << "\n";
Event::s_listeners.erase(this); Event::s_listeners.erase(this);
} }
@ -22,8 +24,13 @@ void Event::postFrom(Mod* m) {
if (m) this->sender = m; if (m) this->sender = m;
for (auto h : Event::s_listeners) { for (auto h : Event::s_listeners) {
if (h->passThrough(this) == ListenerResult::Stop) { try {
break; 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";
} }
} }
} }

View file

@ -5,19 +5,11 @@ USE_GEODE_NAMESPACE();
AEnterLayerEvent::AEnterLayerEvent( AEnterLayerEvent::AEnterLayerEvent(
std::string const& layerID, std::string const& layerID,
cocos2d::CCNode* layer cocos2d::CCNode* layer
) : m_layerID(layerID), ) : layerID(layerID),
m_layer(layer) {} layer(layer) {}
std::string AEnterLayerEvent::getID() const {
return m_layerID;
}
cocos2d::CCNode* AEnterLayerEvent::getLayer() const {
return m_layer;
}
ListenerResult AEnterLayerFilter::handle(Callback fn, AEnterLayerEvent* event) { ListenerResult AEnterLayerFilter::handle(Callback fn, AEnterLayerEvent* event) {
if (m_targetID == event->getID()) { if (m_targetID == event->layerID) {
fn(event); fn(event);
} }
return ListenerResult::Propagate; return ListenerResult::Propagate;