Skip to content

Commit 1c756bc

Browse files
committed
fix: fix some issues of node engine #197
1 parent b9bc19e commit 1c756bc

File tree

1 file changed

+42
-35
lines changed

1 file changed

+42
-35
lines changed

src/legacy/api/EventAPI.h

Lines changed: 42 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -134,59 +134,66 @@ extern bool hasListened[int(EVENT_TYPES::EVENT_COUNT)];
134134
// 监听器异常拦截
135135
inline std::string EventTypeToString(EVENT_TYPES e) { return std::string(magic_enum::enum_name(e)); }
136136

137+
#define CallEvent(type, ...) \
138+
[&]() { \
139+
std::list<ListenerListType>& nowList = listenerList[(int)type]; \
140+
bool returnValue = true; \
141+
for (auto& listener : nowList) { \
142+
EngineScope enter(listener.engine); \
143+
CallEventImpl(listener, returnValue, type, __VA_ARGS__); \
144+
} \
145+
return returnValue; \
146+
}()
147+
148+
template <typename... T>
149+
void CallEventImpl(ListenerListType& listener, bool& returnValue, EVENT_TYPES type, T&&... args) {
150+
try {
151+
auto result = listener.func.get().call({}, args...);
152+
if (result.isBoolean() && result.asBoolean().value() == false) {
153+
returnValue = false;
154+
}
155+
} catch (const Exception& e) {
156+
lse::getSelfModInstance().getLogger().error("CallEvent Callback Failed!");
157+
ll::error_utils::printException(e, lse::getSelfModInstance().getLogger());
158+
lse::getSelfModInstance().getLogger().error("In Event: " + EventTypeToString(type));
159+
lse::getSelfModInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
160+
} catch (...) {
161+
lse::getSelfModInstance().getLogger().error("CallEvent Callback Failed!");
162+
ll::error_utils::printCurrentException(lse::getSelfModInstance().getLogger());
163+
lse::getSelfModInstance().getLogger().error("In Event: " + EventTypeToString(type));
164+
lse::getSelfModInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
165+
}
166+
}
167+
168+
#define FakeCallEvent(type, ...) \
169+
std::list<ListenerListType>& nowList = listenerList[(int)type]; \
170+
for (auto& listener : nowList) { \
171+
EngineScope enter(listener.engine); \
172+
FakeCallEventImpl(listener, type, __VA_ARGS__); \
173+
}
174+
137175
template <typename... T>
138-
bool CallEvent(EVENT_TYPES type, T const&... args) {
139-
std::list<ListenerListType>& nowList = listenerList[(int)type];
140-
bool returnValue = true;
141-
for (auto& listener : nowList) {
142-
EngineScope enter(listener.engine);
176+
void FakeCallEventImpl(ListenerListType& listener, ScriptEngine* engine, EVENT_TYPES type, T&&... args) {
177+
if (listener.engine == engine) {
143178
try {
144-
auto result = listener.func.get().call({}, args...);
145-
if (result.isBoolean() && result.asBoolean().value() == false) {
146-
returnValue = false;
147-
}
179+
listener.func.get().call({}, args...);
148180
} catch (const Exception& e) {
149181
lse::getSelfModInstance().getLogger().error("CallEvent Callback Failed!");
150182
ll::error_utils::printException(e, lse::getSelfModInstance().getLogger());
151183
lse::getSelfModInstance().getLogger().error("In Event: " + EventTypeToString(type));
152184
lse::getSelfModInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
153185
} catch (...) {
154-
lse::getSelfModInstance().getLogger().error("CallEvent Callback Failed!");
186+
lse::getSelfModInstance().getLogger().error("FakeCallEvent Callback Failed!");
155187
ll::error_utils::printCurrentException(lse::getSelfModInstance().getLogger());
156188
lse::getSelfModInstance().getLogger().error("In Event: " + EventTypeToString(type));
157189
lse::getSelfModInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
158190
}
159191
}
160-
return returnValue;
161-
}
162-
163-
template <typename... T>
164-
void FakeCallEvent(ScriptEngine* engine, EVENT_TYPES type, T&&... args) {
165-
std::list<ListenerListType>& nowList = listenerList[int(type)];
166-
for (auto& listener : nowList) {
167-
if (listener.engine == engine) {
168-
EngineScope enter(listener.engine);
169-
try {
170-
listener.func.get().call({}, args...);
171-
} catch (const Exception& e) {
172-
lse::getSelfModInstance().getLogger().error("CallEvent Callback Failed!");
173-
ll::error_utils::printException(e, lse::getSelfModInstance().getLogger());
174-
lse::getSelfModInstance().getLogger().error("In Event: " + EventTypeToString(type));
175-
lse::getSelfModInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
176-
} catch (...) {
177-
lse::getSelfModInstance().getLogger().error("FakeCallEvent Callback Failed!");
178-
ll::error_utils::printCurrentException(lse::getSelfModInstance().getLogger());
179-
lse::getSelfModInstance().getLogger().error("In Event: " + EventTypeToString(type));
180-
lse::getSelfModInstance().getLogger().error("In Plugin: " + getEngineOwnData()->pluginName);
181-
}
182-
}
183-
}
184192
}
185193

186194
// 异常检查
187195
#define IF_LISTENED(TYPE) \
188196
if (!listenerList[int(TYPE)].empty()) { \
189-
EngineScope enter(listenerList[int(TYPE)].front().engine); \
190197
try
191198
#define IF_LISTENED_END(TYPE) \
192199
catch (...) { \

0 commit comments

Comments
 (0)