refactor: remove some shit
This commit is contained in:
Vendored
+2
-2
@@ -13,7 +13,7 @@ declare class Event {
|
|||||||
) => boolean | void
|
) => boolean | void
|
||||||
): boolean;
|
): boolean;
|
||||||
|
|
||||||
/** 客户端登录后事件 */
|
/** 客户端登录后事件(不可以拦截) */
|
||||||
static listen(
|
static listen(
|
||||||
/** 事件名 */
|
/** 事件名 */
|
||||||
event: "onClientLogin",
|
event: "onClientLogin",
|
||||||
@@ -27,7 +27,7 @@ declare class Event {
|
|||||||
serverXuid: string,
|
serverXuid: string,
|
||||||
/** 玩家在客户端的xuid */
|
/** 玩家在客户端的xuid */
|
||||||
clientXuid: string
|
clientXuid: string
|
||||||
) => bool
|
) => void
|
||||||
): boolean;
|
): boolean;
|
||||||
|
|
||||||
/** 天气改变事件事件 */
|
/** 天气改变事件事件 */
|
||||||
|
|||||||
+8
-10
@@ -1,11 +1,11 @@
|
|||||||
/** 事件创建函数 @type {function(string,string):boolean} */
|
/** 事件创建函数 @type {function(string,string):boolean} */
|
||||||
const CallEvent = ll.import("GMLIB_API", "callCustomEvent");
|
const CallEvent = ll.import("GMLIB_Event_API", "callCustomEvent");
|
||||||
|
/** 获取事件ID函数 @type {function():string} */
|
||||||
|
const getNextEventId = ll.import("GMLIB_Event_API", "getNextScriptEventId");
|
||||||
|
|
||||||
/** 事件类 */
|
/** 事件类 */
|
||||||
class Event {
|
class Event {
|
||||||
constructor() {
|
constructor() { throw new Error("Static class cannot be instantiated"); }
|
||||||
throw new Error("Static class cannot be instantiated");
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听事件
|
* 监听事件
|
||||||
@@ -14,17 +14,15 @@ class Event {
|
|||||||
* @returns {boolean} 是否创建成功
|
* @returns {boolean} 是否创建成功
|
||||||
*/
|
*/
|
||||||
static listen(event, callback) {
|
static listen(event, callback) {
|
||||||
let eventId = "GMLIB_Event_" + Math.random().toString(16).slice(2);
|
let eventId = getNextEventId();
|
||||||
ll.export(callback, event, eventId);
|
ll.export(callback, event, eventId);
|
||||||
let result = CallEvent(event, eventId);
|
let result = CallEvent(event, eventId);
|
||||||
if (!result) {
|
if (!result) {
|
||||||
logger.error(`Cannot listen event "${event}"!`);
|
logger.error(`Cannot listen event "${event}" !`);
|
||||||
logger.error(`Event "${event}" No Found!`);
|
logger.error(`GMLIB Script Event "${event}" does not exist !`);
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {Event};
|
||||||
Event
|
|
||||||
};
|
|
||||||
+44
-19
@@ -1,38 +1,63 @@
|
|||||||
#include "Global.h"
|
#include "Global.h"
|
||||||
using namespace ll::hash_utils;
|
using namespace ll::hash_utils;
|
||||||
|
|
||||||
|
class LegacyScriptEventManager {
|
||||||
|
private:
|
||||||
|
int64 mNextEventId = 0;
|
||||||
|
std::unordered_map<std::string, ll::event::ListenerPtr> mEventListeners;
|
||||||
|
|
||||||
|
public:
|
||||||
|
LegacyScriptEventManager() {};
|
||||||
|
~LegacyScriptEventManager() {};
|
||||||
|
|
||||||
|
std::string getNextEventId() {
|
||||||
|
mNextEventId++;
|
||||||
|
return std::to_string(mNextEventId);
|
||||||
|
}
|
||||||
|
|
||||||
|
void emplaceListener(std::string const& scriptEventId, ll::event::ListenerPtr listenerPtr) {
|
||||||
|
mEventListeners[scriptEventId] = listenerPtr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void removeListener(std::string const& scriptEventId) {
|
||||||
|
ll::event::EventBus::getInstance().removeListener(mEventListeners[scriptEventId]);
|
||||||
|
mEventListeners.erase(scriptEventId);
|
||||||
|
}
|
||||||
|
|
||||||
|
static std::unique_ptr<LegacyScriptEventManager>& getInstance() {
|
||||||
|
static std::unique_ptr<LegacyScriptEventManager> instance;
|
||||||
|
return instance;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
#define REGISTER_EVENT_LISTEN(eventType, callFunction, eventParams, cancelFunction, otherFunction) \
|
#define REGISTER_EVENT_LISTEN(eventType, callFunction, eventParams, cancelFunction, otherFunction) \
|
||||||
if (eventListeners.contains(eventName + "_" + eventId)) \
|
eventManager->emplaceListener( \
|
||||||
eventBus.removeListener(eventListeners.at(eventName + "_" + eventId)); \
|
eventId, \
|
||||||
eventListeners[eventName + "_" + eventId] = \
|
eventBus.emplaceListener<eventType>([eventName, eventId, &eventBus, &eventManager](eventType& ev) -> void { \
|
||||||
eventBus.emplaceListener<eventType>([eventName, eventId, &eventBus](eventType& ev) -> void { \
|
|
||||||
if (!RemoteCall::hasFunc(eventName, eventId)) { \
|
if (!RemoteCall::hasFunc(eventName, eventId)) { \
|
||||||
eventBus.removeListener(eventListeners.at(eventName + "_" + eventId)); \
|
eventManager->removeListener(eventId); \
|
||||||
eventListeners.erase(eventName + "_" + eventId); \
|
|
||||||
return; \
|
return; \
|
||||||
} \
|
} \
|
||||||
bool result = true; \
|
bool result = true; \
|
||||||
try { \
|
try { \
|
||||||
otherFunction; \
|
otherFunction; \
|
||||||
result = RemoteCall::importAs<bool callFunction>(eventName, eventId) eventParams; \
|
result = RemoteCall::importAs<bool callFunction>(eventName, eventId) eventParams; \
|
||||||
} catch (std::exception & e) { \
|
} catch (...) {} \
|
||||||
if (std::string(e.what()) != "bad variant access") { \
|
|
||||||
logger.error("Exception in event listener: {0}", e.what()); \
|
|
||||||
logger.error("Event: {0}, Id: {1}", eventName, eventId); \
|
|
||||||
} \
|
|
||||||
} \
|
|
||||||
if (!result) cancelFunction; \
|
if (!result) cancelFunction; \
|
||||||
}); \
|
}) \
|
||||||
|
); \
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
std::unordered_map<std::string, ll::event::ListenerPtr> eventListeners;
|
|
||||||
|
|
||||||
void Export_Event_API() {
|
void Export_Event_API() {
|
||||||
auto& eventBus = ll::event::EventBus::getInstance();
|
RemoteCall::exportAs("GMLIB_Event_API", "getNextScriptEventId", []() -> std::string {
|
||||||
|
return LegacyScriptEventManager::getInstance()->getNextEventId();
|
||||||
|
});
|
||||||
|
auto& eventBus = ll::event::EventBus::getInstance();
|
||||||
|
auto& eventManager = LegacyScriptEventManager::getInstance();
|
||||||
RemoteCall::exportAs(
|
RemoteCall::exportAs(
|
||||||
"GMLIB_API",
|
"GMLIB_Event_API",
|
||||||
"callCustomEvent",
|
"callCustomEvent",
|
||||||
[&eventBus](std::string const& eventName, std::string const& eventId) -> bool {
|
[&eventBus, &eventManager](std::string const& eventName, std::string const& eventId) -> bool {
|
||||||
if (!RemoteCall::hasFunc(eventName, eventId)) return false;
|
if (!RemoteCall::hasFunc(eventName, eventId)) return false;
|
||||||
switch (doHash(eventName)) {
|
switch (doHash(eventName)) {
|
||||||
case doHash("onClientLogin"): {
|
case doHash("onClientLogin"): {
|
||||||
@@ -43,7 +68,7 @@ void Export_Event_API() {
|
|||||||
std::string const& serverXuid,
|
std::string const& serverXuid,
|
||||||
std::string const& clientXuid),
|
std::string const& clientXuid),
|
||||||
(ev.getRealName(), ev.getUuid().asString(), ev.getServerAuthXuid(), ev.getClientAuthXuid()),
|
(ev.getRealName(), ev.getUuid().asString(), ev.getServerAuthXuid(), ev.getClientAuthXuid()),
|
||||||
ev.disConnectClient(),
|
logger.error("Event \"onClientLogin\" cannot be intercepted"),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
case doHash("onWeatherChange"): {
|
case doHash("onWeatherChange"): {
|
||||||
|
|||||||
Reference in New Issue
Block a user