2019-09-07 04:23:15 +00:00
|
|
|
/**
|
|
|
|
* MiaoScript Event处理类
|
|
|
|
*/
|
2020-05-02 16:14:47 +00:00
|
|
|
import i18n from '@ccms/i18n'
|
|
|
|
import { injectable, unmanaged } from '@ccms/container'
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-07-28 01:39:15 +00:00
|
|
|
const Thread = Java.type('java.lang.Thread')
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
export namespace event {
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 事件监听优先级
|
|
|
|
*/
|
2020-01-15 08:48:18 +00:00
|
|
|
export enum EventPriority {
|
|
|
|
LOWEST = "LOWEST",
|
|
|
|
LOW = "LOW",
|
|
|
|
NORMAL = "NORMAL",
|
|
|
|
HIGH = "HIGH",
|
|
|
|
HIGHEST = "HIGHEST",
|
|
|
|
MONITOR = "MONITOR",
|
|
|
|
}
|
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
@injectable()
|
|
|
|
export abstract class Event {
|
2020-05-11 09:20:50 +00:00
|
|
|
// export EventPriority to blockly
|
|
|
|
public EventPriority = EventPriority;
|
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
private mapEvent = [];
|
|
|
|
private listenerMap = [];
|
2019-09-27 01:49:45 +00:00
|
|
|
|
|
|
|
protected baseEventDir = '';
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-02-26 02:36:30 +00:00
|
|
|
constructor(@unmanaged() baseEventDir: string) {
|
2020-07-28 01:39:15 +00:00
|
|
|
this.baseEventDir = baseEventDir
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
|
|
|
|
/**
|
2020-01-15 08:48:18 +00:00
|
|
|
* abstract event map function
|
|
|
|
* ig: org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
|
|
|
* org.spongepowered.api.event.game.GameRegistryEvent.Register => gameregistryevent$register
|
2019-09-10 09:21:00 +00:00
|
|
|
*/
|
|
|
|
mapEventName() {
|
2020-07-28 01:39:15 +00:00
|
|
|
if (this.baseEventDir === "") { throw new Error(i18n.translate('ms.api.event.empty.event.dir')) }
|
|
|
|
let count = 0
|
|
|
|
let jar = this.getJarFile(this.baseEventDir)
|
|
|
|
let entries = jar.entries()
|
2019-09-27 01:49:45 +00:00
|
|
|
while (entries.hasMoreElements()) {
|
2020-07-28 01:39:15 +00:00
|
|
|
let entry = entries.nextElement()
|
|
|
|
let name = entry.name
|
2019-09-27 01:49:45 +00:00
|
|
|
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
2020-01-15 08:48:18 +00:00
|
|
|
// replace name to qualifiedName
|
2020-07-28 01:39:15 +00:00
|
|
|
let qualifiedName = name.replaceAll('/', '.')
|
2019-09-27 01:49:45 +00:00
|
|
|
try {
|
2020-07-28 01:39:15 +00:00
|
|
|
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6))
|
2019-09-27 01:49:45 +00:00
|
|
|
if (this.isValidEvent(clazz)) {
|
2020-07-28 01:39:15 +00:00
|
|
|
let simpleName = this.class2Name(clazz).toLowerCase()
|
|
|
|
console.trace(i18n.translate("ms.api.event.mapping", { canonicalName: clazz.canonicalName, simpleName }))
|
|
|
|
this.mapEvent[simpleName] = clazz
|
|
|
|
count++
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2019-09-27 01:49:45 +00:00
|
|
|
//ignore already loaded class
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-28 01:39:15 +00:00
|
|
|
return count
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
|
|
|
|
2020-04-24 05:05:46 +00:00
|
|
|
getJarFile(resource: string, loader?: any) {
|
2020-07-28 01:39:15 +00:00
|
|
|
let dirs = (loader || Thread.currentThread().getContextClassLoader()).getResources(resource)
|
2019-09-27 01:49:45 +00:00
|
|
|
if (dirs.hasMoreElements()) {
|
2020-07-28 01:39:15 +00:00
|
|
|
let url = dirs.nextElement()
|
|
|
|
if (url.protocol === "jar") { return url.openConnection().jarFile }
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
2020-02-27 04:11:18 +00:00
|
|
|
throw new Error(i18n.translate("ms.api.event.resource.not.found", { resource }))
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-01-15 08:48:18 +00:00
|
|
|
class2Name(clazz: any) {
|
2020-07-28 01:39:15 +00:00
|
|
|
return clazz.simpleName
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-01-15 08:48:18 +00:00
|
|
|
name2Class(name: any, event: string) {
|
2020-07-28 01:39:15 +00:00
|
|
|
let eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event']
|
2019-09-10 09:21:00 +00:00
|
|
|
if (!eventCls) {
|
|
|
|
try {
|
2021-12-25 17:21:06 +00:00
|
|
|
eventCls = base.getClass(event)
|
2020-07-28 01:39:15 +00:00
|
|
|
this.mapEvent[event] = eventCls
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2020-02-27 04:11:18 +00:00
|
|
|
console.i18n("ms.api.event.not.found", { name, event })
|
2020-07-28 01:39:15 +00:00
|
|
|
return
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-28 01:39:15 +00:00
|
|
|
return eventCls
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
execute(name, exec, eventCls) {
|
2020-01-15 08:48:18 +00:00
|
|
|
return (...args: any[]) => {
|
2019-09-10 09:21:00 +00:00
|
|
|
try {
|
2020-07-28 01:39:15 +00:00
|
|
|
let event = args[args.length - 1]
|
2020-02-25 15:15:39 +00:00
|
|
|
if (eventCls.isAssignableFrom(event.getClass())) {
|
|
|
|
let time = Date.now()
|
2020-07-28 01:39:15 +00:00
|
|
|
exec(event)
|
|
|
|
let cost = Date.now() - time
|
2022-04-01 09:18:32 +00:00
|
|
|
if (cost > global.ScriptSlowExecuteTime) {
|
2020-02-27 04:11:18 +00:00
|
|
|
console.i18n("ms.api.event.execute.slow", { name, event: this.class2Name(eventCls), cost })
|
2020-02-25 15:15:39 +00:00
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2021-12-25 17:21:06 +00:00
|
|
|
} catch (ex: any) {
|
2020-02-27 04:11:18 +00:00
|
|
|
console.i18n("ms.api.event.execute.error", { name, event: this.class2Name(eventCls), ex })
|
2020-07-28 01:39:15 +00:00
|
|
|
console.ex(ex)
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
/**
|
|
|
|
* 添加事件监听
|
2020-01-15 08:48:18 +00:00
|
|
|
* @param plugin {any}
|
|
|
|
* @param event {string}
|
2019-09-10 09:21:00 +00:00
|
|
|
* @param exec {function}
|
2020-01-15 08:48:18 +00:00
|
|
|
* @param priority {string} [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
|
2019-09-10 09:21:00 +00:00
|
|
|
* @param ignoreCancel
|
|
|
|
*/
|
2020-02-24 10:36:08 +00:00
|
|
|
listen(plugin: any, event: string, exec: (event: any) => void, priority: EventPriority = EventPriority.NORMAL, ignoreCancel = false) {
|
2020-07-28 01:39:15 +00:00
|
|
|
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError(i18n.translate("ms.api.event.listen.plugin.name.empty"))
|
|
|
|
var name = plugin.description.name
|
|
|
|
var eventCls = this.name2Class(name, event)
|
|
|
|
if (!eventCls) { return }
|
2019-09-10 09:21:00 +00:00
|
|
|
if (typeof priority === 'boolean') {
|
2020-07-28 01:39:15 +00:00
|
|
|
ignoreCancel = priority
|
|
|
|
priority = EventPriority.NORMAL
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2020-07-28 01:39:15 +00:00
|
|
|
priority = priority || EventPriority.NORMAL
|
|
|
|
ignoreCancel = ignoreCancel || false
|
2020-09-17 09:44:48 +00:00
|
|
|
// @ts-ignore
|
|
|
|
let executor = exec.name || exec.executor || '[anonymous]'
|
2019-09-10 09:21:00 +00:00
|
|
|
// noinspection JSUnusedGlobalSymbols
|
2022-04-19 01:29:57 +00:00
|
|
|
var listener = this.register(
|
|
|
|
eventCls,
|
|
|
|
this.execute(name, exec, eventCls),
|
|
|
|
priority,
|
|
|
|
ignoreCancel
|
|
|
|
)
|
2020-07-28 01:39:15 +00:00
|
|
|
var listenerMap = this.listenerMap
|
2020-01-15 08:48:18 +00:00
|
|
|
// add to cache Be used for close plugin to close event
|
2020-07-28 01:39:15 +00:00
|
|
|
if (!listenerMap[name]) listenerMap[name] = []
|
2020-09-17 09:44:48 +00:00
|
|
|
var off = () => {
|
2020-09-22 09:33:59 +00:00
|
|
|
if (off['offed']) return
|
|
|
|
off['offed'] = true
|
2020-07-28 01:39:15 +00:00
|
|
|
this.unregister(eventCls, listener)
|
2022-04-19 01:29:57 +00:00
|
|
|
console.debug(i18n.translate("ms.api.event.unregister", {
|
|
|
|
name,
|
|
|
|
event: this.class2Name(eventCls),
|
|
|
|
exec: executor
|
|
|
|
}))
|
2020-07-28 01:39:15 +00:00
|
|
|
}
|
|
|
|
listenerMap[name].push(off)
|
2019-09-10 09:21:00 +00:00
|
|
|
// noinspection JSUnresolvedVariable
|
2022-04-19 01:29:57 +00:00
|
|
|
console.debug(i18n.translate("ms.api.event.register", {
|
|
|
|
name,
|
|
|
|
event: this.class2Name(eventCls),
|
|
|
|
exec: executor,
|
|
|
|
priority,
|
|
|
|
ignore: ignoreCancel
|
|
|
|
}))
|
2020-07-28 01:39:15 +00:00
|
|
|
return off
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 关闭插件监听任务
|
|
|
|
* @param plugin 插件
|
|
|
|
*/
|
2019-09-10 09:21:00 +00:00
|
|
|
disable(plugin: any) {
|
2020-07-28 01:39:15 +00:00
|
|
|
var eventCache = this.listenerMap[plugin.description.name]
|
2019-09-10 09:21:00 +00:00
|
|
|
if (eventCache) {
|
2020-09-17 09:44:48 +00:00
|
|
|
eventCache.forEach(off => off())
|
2020-07-28 01:39:15 +00:00
|
|
|
delete this.listenerMap[plugin.description.name]
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 判断
|
|
|
|
* @param clazz 事件类
|
|
|
|
*/
|
2020-07-28 01:39:15 +00:00
|
|
|
abstract isValidEvent(clazz: any): boolean
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 注册事件
|
|
|
|
* @param eventCls 事件类
|
|
|
|
* @param exec 执行器
|
|
|
|
* @param priority 优先级
|
|
|
|
* @param ignoreCancel 是否忽略已取消的事件
|
|
|
|
*/
|
2020-07-28 01:39:15 +00:00
|
|
|
abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any
|
2020-03-24 09:59:18 +00:00
|
|
|
/**
|
|
|
|
* 取消监听事件
|
|
|
|
* @param event 事件
|
|
|
|
* @param listener 监听器
|
|
|
|
*/
|
2020-07-28 01:39:15 +00:00
|
|
|
abstract unregister(event: any, listener: any): void
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|