2019-09-07 04:23:15 +00:00
|
|
|
'use strict';
|
|
|
|
/**
|
|
|
|
* MiaoScript Event处理类
|
|
|
|
*/
|
|
|
|
import '@ms/core'
|
|
|
|
import '@ms/nashorn'
|
2019-09-10 09:21:00 +00:00
|
|
|
import { injectable } from '@ms/container'
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-27 01:49:45 +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-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 {
|
|
|
|
private mapEvent = [];
|
|
|
|
private listenerMap = [];
|
2019-09-27 01:49:45 +00:00
|
|
|
|
|
|
|
protected baseEventDir = '';
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
constructor(baseEventDir: string) {
|
|
|
|
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() {
|
|
|
|
if (this.baseEventDir === "") {
|
2020-01-15 08:48:18 +00:00
|
|
|
throw new Error("base event dir is empty, can't map event name !");
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2019-09-27 01:49:45 +00:00
|
|
|
let count = 0;
|
|
|
|
let jar = this.getJarFile(this.baseEventDir);
|
|
|
|
let entries = jar.entries();
|
|
|
|
while (entries.hasMoreElements()) {
|
|
|
|
let entry = entries.nextElement();
|
|
|
|
let name = entry.name;
|
|
|
|
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
2020-01-15 08:48:18 +00:00
|
|
|
// replace name to qualifiedName
|
2019-09-27 01:49:45 +00:00
|
|
|
let qualifiedName = name.replaceAll('/', '.');
|
|
|
|
try {
|
|
|
|
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6));
|
|
|
|
if (this.isValidEvent(clazz)) {
|
|
|
|
let simpleName = this.class2Name(clazz).toLowerCase();
|
2020-01-15 08:48:18 +00:00
|
|
|
console.trace(`Mapping Event [${clazz.canonicalName}] => ${simpleName}`);
|
2019-09-27 01:49:45 +00:00
|
|
|
this.mapEvent[simpleName] = clazz;
|
|
|
|
count++;
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-27 01:49:45 +00:00
|
|
|
} catch (ex) {
|
|
|
|
//ignore already loaded class
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
return count;
|
2019-09-27 01:49:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getJarFile(resource: string) {
|
|
|
|
let dirs = Thread.currentThread().getContextClassLoader().getResources(resource);
|
|
|
|
if (dirs.hasMoreElements()) {
|
|
|
|
let url = dirs.nextElement();
|
|
|
|
if (url.protocol === "jar") { return url.openConnection().jarFile; }
|
|
|
|
}
|
|
|
|
throw new Error(`Can't Mapping Event Because not found Resources ${resource}!`)
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2020-01-15 08:48:18 +00:00
|
|
|
class2Name(clazz: any) {
|
2019-09-10 09:21:00 +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) {
|
2019-09-10 09:21:00 +00:00
|
|
|
var eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event'];
|
|
|
|
if (!eventCls) {
|
|
|
|
try {
|
|
|
|
eventCls = base.getClass(eventCls);
|
|
|
|
this.mapEvent[event] = eventCls;
|
|
|
|
} catch (ex) {
|
|
|
|
console.console(`§6插件 §b${name} §6注册事件 §c${event} §6失败 §4事件未找到!`);
|
2020-01-15 08:48:18 +00:00
|
|
|
console.ex(new Error(`Plugin ${name} register event error ${event} not found!`));
|
2019-09-10 09:21:00 +00:00
|
|
|
return;
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-10 09:21:00 +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 {
|
|
|
|
var time = new Date().getTime()
|
|
|
|
exec(args[args.length - 1]);
|
|
|
|
var cost = new Date().getTime() - time;
|
|
|
|
if (cost > 20) {
|
|
|
|
console.console(`§c注意! §6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件 §c耗时 §4${cost}ms !`)
|
|
|
|
}
|
|
|
|
} catch (ex) {
|
|
|
|
console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`);
|
|
|
|
console.ex(ex);
|
|
|
|
}
|
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) {
|
2019-09-19 10:59:00 +00:00
|
|
|
if (!plugin || !plugin.description || !plugin.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
|
|
|
|
var name = plugin.description.name;
|
2019-09-10 09:21:00 +00:00
|
|
|
var eventCls = this.name2Class(name, event);
|
|
|
|
if (!eventCls) { return; }
|
|
|
|
if (typeof priority === 'boolean') {
|
|
|
|
ignoreCancel = priority;
|
2020-01-15 08:48:18 +00:00
|
|
|
priority = EventPriority.NORMAL;
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|
2020-01-15 08:48:18 +00:00
|
|
|
priority = priority || EventPriority.NORMAL;
|
2019-09-10 09:21:00 +00:00
|
|
|
ignoreCancel = ignoreCancel || false;
|
|
|
|
// noinspection JSUnusedGlobalSymbols
|
|
|
|
var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel);
|
|
|
|
var listenerMap = this.listenerMap;
|
2020-01-15 08:48:18 +00:00
|
|
|
// add to cache Be used for close plugin to close event
|
2019-09-10 09:21:00 +00:00
|
|
|
if (!listenerMap[name]) listenerMap[name] = [];
|
|
|
|
var offExec = () => {
|
|
|
|
this.unregister(eventCls, listener);
|
2020-01-15 08:48:18 +00:00
|
|
|
console.debug(`[${name}] unregister event ${this.class2Name(eventCls)}`);
|
2019-09-10 09:21:00 +00:00
|
|
|
};
|
|
|
|
var off = {
|
|
|
|
event: eventCls,
|
|
|
|
listener: listener,
|
|
|
|
off: offExec
|
|
|
|
};
|
|
|
|
listenerMap[name].push(off);
|
|
|
|
// noinspection JSUnresolvedVariable
|
2020-01-15 08:48:18 +00:00
|
|
|
console.debug(`[${name}] register event ${this.class2Name(eventCls)} => ${exec.name || '[anonymous]'}`);
|
2019-09-10 09:21:00 +00:00
|
|
|
return off;
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
|
2019-09-10 09:21:00 +00:00
|
|
|
disable(plugin: any) {
|
|
|
|
var eventCache = this.listenerMap[plugin.description.name];
|
|
|
|
if (eventCache) {
|
|
|
|
eventCache.forEach(t => t.off());
|
|
|
|
delete this.listenerMap[plugin.description.name];
|
|
|
|
}
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
|
|
|
|
abstract isValidEvent(clazz: any): boolean;
|
|
|
|
abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any;
|
|
|
|
abstract unregister(event: any, listener: any): void;
|
2019-09-07 04:23:15 +00:00
|
|
|
}
|
2019-09-10 09:21:00 +00:00
|
|
|
}
|