feat: 优化插件事件注册流程
This commit is contained in:
@ -17,7 +17,7 @@ var plugin = base.plugin;
|
|||||||
|
|
||||||
var ref = require('reflect');
|
var ref = require('reflect');
|
||||||
|
|
||||||
var jspListener = [];
|
var listenerMap = [];
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 扫描包 org.bukkit.event 下的所有事件
|
* 扫描包 org.bukkit.event 下的所有事件
|
||||||
@ -81,10 +81,8 @@ function isVaildEvent(clz) {
|
|||||||
* @param ignoreCancel
|
* @param ignoreCancel
|
||||||
*/
|
*/
|
||||||
function listen(jsp, event, exec, priority, ignoreCancel) {
|
function listen(jsp, event, exec, priority, ignoreCancel) {
|
||||||
var pname = jsp.description.name;
|
var name = jsp.description.name;
|
||||||
if (ext.isNull(pname)) {
|
if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||||
throw new TypeError('插件名称为空 请检查传入参数!');
|
|
||||||
}
|
|
||||||
var eventCls = mapEvent[event];
|
var eventCls = mapEvent[event];
|
||||||
if (!eventCls) {
|
if (!eventCls) {
|
||||||
try {
|
try {
|
||||||
@ -100,9 +98,6 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
|||||||
if (ignoreCancel === undefined) {
|
if (ignoreCancel === undefined) {
|
||||||
ignoreCancel = false;
|
ignoreCancel = false;
|
||||||
}
|
}
|
||||||
if (!jspListener[jsp.description.name]) {
|
|
||||||
jspListener[jsp.description.name] = [];
|
|
||||||
}
|
|
||||||
var listener = new Listener({});
|
var listener = new Listener({});
|
||||||
// noinspection JSUnusedGlobalSymbols
|
// noinspection JSUnusedGlobalSymbols
|
||||||
/**
|
/**
|
||||||
@ -125,18 +120,19 @@ function listen(jsp, event, exec, priority, ignoreCancel) {
|
|||||||
plugin,
|
plugin,
|
||||||
ignoreCancel);
|
ignoreCancel);
|
||||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||||
var listeners = jspListener[jsp.description.name];
|
if (!listenerMap[name]) listenerMap[name] = []
|
||||||
|
var listeners = listenerMap[name];
|
||||||
var off = {
|
var off = {
|
||||||
event: eventCls,
|
event: eventCls,
|
||||||
listener: listener,
|
listener: listener,
|
||||||
off: function(){
|
off: function(){
|
||||||
ref.on(this.event).call('getHandlerList').get().unregister(this.listener);
|
ref.on(this.event).call('getHandlerList').get().unregister(this.listener);
|
||||||
log.d('插件 %s 注销事件 %s', pname, this.event.simpleName);
|
log.d('插件 %s 注销事件 %s', name, this.event.simpleName);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
listeners.push(off);
|
listeners.push(off);
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
log.d('插件 %s 注册事件 %s 方法 %s', pname, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
|
log.d('插件 %s 注册事件 %s 方法 %s', name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name);
|
||||||
return off;
|
return off;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -148,12 +144,10 @@ mapEventName();
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
on: listen,
|
on: listen,
|
||||||
disable: function (jsp) {
|
disable: function (jsp) {
|
||||||
var jspl = jspListener[jsp.description.name];
|
var jspl = listenerMap[jsp.description.name];
|
||||||
if (jspl) {
|
if (jspl) {
|
||||||
jspListener[jsp.description.name].forEach(function (t) {
|
jspl.forEach(function (t) t.off());
|
||||||
t.off();
|
delete listenerMap[jsp.description.name];
|
||||||
});
|
|
||||||
delete jspListener[jsp.description.name];
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
@ -71,23 +71,40 @@ function loadJsPlugins(files) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function loadPlugin(file) {
|
function loadPlugin(file) {
|
||||||
var p = require(file, {
|
var plugin = require(file, {
|
||||||
cache: false,
|
cache: false,
|
||||||
// 给插件注入单独的 console
|
// 给插件注入单独的 console
|
||||||
hook: function (origin) {
|
hook: function (origin) {
|
||||||
return 'var console = new Console();' + origin + 'module.exports.console = console;'
|
return beforeLoadHook(origin);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
log.d("插件编译结果: %s", p.toJson());
|
log.d("插件编译结果: %s", plugin.toJson());
|
||||||
if (!p.description || !p.description.name) {
|
var desc = plugin.description;
|
||||||
|
if (!desc || !desc.name) {
|
||||||
log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file);
|
log.w("文件 %s 不存在 description 描述信息 无法加载插件!", file);
|
||||||
} else {
|
} else {
|
||||||
initPlugin(file, p);
|
initPlugin(file, plugin);
|
||||||
plugins.push(p);
|
afterLoadHook(plugin);
|
||||||
plugins[p.description.name] = p;
|
plugins.push(plugin);
|
||||||
log.i('载入插件 %s 版本 %s By %s', p.description.name, p.description.version || '未知', p.description.author || '未知');
|
plugins[plugin.description.name] = plugin;
|
||||||
|
log.i('载入插件 %s 版本 %s By %s', desc.name, desc.version || '未知', desc.author || '未知');
|
||||||
}
|
}
|
||||||
return p;
|
return plugin;
|
||||||
|
}
|
||||||
|
|
||||||
|
function beforeLoadHook(origin) {
|
||||||
|
var result = origin;
|
||||||
|
// 处理 event 为了不影响 正常逻辑 event 还是手动require吧
|
||||||
|
// result = result + 'var event = {}; module.exports.event = event;';
|
||||||
|
// 注入 console 对象
|
||||||
|
result = result + 'var console = new Console(); module.exports.console = console;';
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
function afterLoadHook(plugin) {
|
||||||
|
// plugin.event.on = event.on.bind(plugin);
|
||||||
|
// 给 console 添加插件名称
|
||||||
|
plugin.console.name = plugin.description.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -102,8 +119,6 @@ function initPlugin(file, plugin){
|
|||||||
plugin.getFile = function(name) {
|
plugin.getFile = function(name) {
|
||||||
return fs.file(plugin.__DATA__, name);
|
return fs.file(plugin.__DATA__, name);
|
||||||
}
|
}
|
||||||
// 给 console 添加插件名称
|
|
||||||
plugin.console.name = plugin.description.name;
|
|
||||||
// 初始化 getConfig()
|
// 初始化 getConfig()
|
||||||
/**
|
/**
|
||||||
* 获取配置文件
|
* 获取配置文件
|
||||||
@ -133,8 +148,10 @@ function initPlugin(file, plugin){
|
|||||||
case 0:
|
case 0:
|
||||||
plugin.configFile.parentFile.mkdirs()
|
plugin.configFile.parentFile.mkdirs()
|
||||||
base.save(plugin.configFile, plugin.config.toYaml());
|
base.save(plugin.configFile, plugin.config.toYaml());
|
||||||
|
break;
|
||||||
case 2:
|
case 2:
|
||||||
base.save(arguments[0], arguments[1].toYaml());
|
base.save(arguments[0], arguments[1].toYaml());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// 初始化 getDataFolder()
|
// 初始化 getDataFolder()
|
||||||
@ -156,9 +173,7 @@ function runAndCatch(jsp, exec, ext) {
|
|||||||
try {
|
try {
|
||||||
// 绑定方法的this到插件自身
|
// 绑定方法的this到插件自身
|
||||||
exec.bind(jsp)();
|
exec.bind(jsp)();
|
||||||
if (ext) {
|
if (ext) { ext(); }
|
||||||
ext();
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
log.w('插件 %s 执行 %s 发生错误: %s', jsp.description.name, exec.name, ex.message);
|
log.w('插件 %s 执行 %s 发生错误: %s', jsp.description.name, exec.name, ex.message);
|
||||||
ex.printStackTrace();
|
ex.printStackTrace();
|
||||||
@ -200,7 +215,10 @@ exports.enable = function () {
|
|||||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.enable));
|
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.enable));
|
||||||
};
|
};
|
||||||
exports.disable = function () {
|
exports.disable = function () {
|
||||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function () event.disable(p)));
|
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function(){
|
||||||
|
event.disable(p);
|
||||||
|
task.cancel();
|
||||||
|
}));
|
||||||
};
|
};
|
||||||
exports.reload = function () {
|
exports.reload = function () {
|
||||||
checkAndGet(arguments).forEach(function (p) {
|
checkAndGet(arguments).forEach(function (p) {
|
||||||
|
Reference in New Issue
Block a user