feat: 优化插件加载逻辑
This commit is contained in:
@ -95,7 +95,6 @@ function loadPlugin(file) {
|
|||||||
} else {
|
} else {
|
||||||
initPlugin(file, plugin);
|
initPlugin(file, plugin);
|
||||||
afterLoadHook(plugin);
|
afterLoadHook(plugin);
|
||||||
plugins.push(plugin);
|
|
||||||
plugins[plugin.description.name] = plugin;
|
plugins[plugin.description.name] = plugin;
|
||||||
console.info('载入插件 %s 版本 %s By %s'.format(desc.name, desc.version || '未知', desc.author || '未知'));
|
console.info('载入插件 %s 版本 %s By %s'.format(desc.name, desc.version || '未知', desc.author || '未知'));
|
||||||
}
|
}
|
||||||
@ -107,7 +106,7 @@ function beforeLoadHook(origin) {
|
|||||||
// 处理 event 为了不影响 正常逻辑 event 还是手动require吧
|
// 处理 event 为了不影响 正常逻辑 event 还是手动require吧
|
||||||
// result = result + 'var event = {}; module.exports.event = event;';
|
// result = result + 'var event = {}; module.exports.event = event;';
|
||||||
// 注入 console 对象 // 给插件注入单独的 console
|
// 注入 console 对象 // 给插件注入单独的 console
|
||||||
result = result + 'var console = Console.createNew(); module.exports.console = console;';
|
result = result + 'var console = new Console(); module.exports.console = console;';
|
||||||
// 插件注入 self 对象
|
// 插件注入 self 对象
|
||||||
result = result + 'var self = {}; module.exports.self = self;';
|
result = result + 'var self = {}; module.exports.self = self;';
|
||||||
return result;
|
return result;
|
||||||
@ -201,17 +200,7 @@ function initPluginConfig(plugin) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function runAndCatch(jsp, name, ext) {
|
function runAndCatch(jsp, name, ext) {
|
||||||
var exec = jsp[name];
|
|
||||||
if (exec) {
|
|
||||||
try {
|
|
||||||
// 绑定方法的this到插件自身
|
|
||||||
exec.bind(jsp)();
|
|
||||||
if (ext) ext();
|
|
||||||
} catch (ex) {
|
|
||||||
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message));
|
|
||||||
console.ex(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkAndGet(args) {
|
function checkAndGet(args) {
|
||||||
@ -229,6 +218,24 @@ function checkAndGet(args) {
|
|||||||
return [exports.plugins[name]];
|
return [exports.plugins[name]];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function checkAndRun(args, name, ext) {
|
||||||
|
var pls = checkAndGet(args);
|
||||||
|
for (var i in pls) {
|
||||||
|
var jsp = pls[i];
|
||||||
|
var exec = jsp[name];
|
||||||
|
if (exec) {
|
||||||
|
try {
|
||||||
|
// 绑定方法的this到插件自身
|
||||||
|
exec.bind(jsp)();
|
||||||
|
if (ext) ext.bind(jsp)();
|
||||||
|
} catch (ex) {
|
||||||
|
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message));
|
||||||
|
console.ex(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var plugins = [];
|
var plugins = [];
|
||||||
|
|
||||||
function init(path) {
|
function init(path) {
|
||||||
@ -241,21 +248,21 @@ function init(path) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function load() {
|
function load() {
|
||||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, 'init'));
|
checkAndRun(arguments, 'init');
|
||||||
};
|
};
|
||||||
|
|
||||||
function enable() {
|
function enable() {
|
||||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, 'enable'));
|
checkAndRun(arguments, 'enable');
|
||||||
};
|
};
|
||||||
|
|
||||||
function disable() {
|
function disable() {
|
||||||
checkAndGet(arguments).forEach(function (p) runAndCatch(p, 'disable', function () {
|
checkAndRun(arguments, 'disable', function eventDisable() {
|
||||||
event.disable(p);
|
event.disable(this);
|
||||||
}));
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function reload() {
|
function reload() {
|
||||||
checkAndGet(arguments).forEach(function (p) {
|
checkAndRun(arguments, function (p) {
|
||||||
disable(p);
|
disable(p);
|
||||||
p = loadPlugin(p.__FILE__);
|
p = loadPlugin(p.__FILE__);
|
||||||
load(p);
|
load(p);
|
||||||
|
@ -25,10 +25,10 @@
|
|||||||
function loadCore() {
|
function loadCore() {
|
||||||
// 加载基础模块
|
// 加载基础模块
|
||||||
load(root + '/core/ext.js');
|
load(root + '/core/ext.js');
|
||||||
// 探测服务器类型
|
|
||||||
load(root + '/core/detect.js');
|
|
||||||
// 加载Console
|
// 加载Console
|
||||||
load(root + '/core/console.js');
|
load(root + '/core/console.js');
|
||||||
|
// 探测服务器类型
|
||||||
|
load(root + '/core/detect.js');
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -37,6 +37,9 @@
|
|||||||
function loadRequire() {
|
function loadRequire() {
|
||||||
// 初始化加载器
|
// 初始化加载器
|
||||||
global.require = load(root + '/core/require.js')(root);
|
global.require = load(root + '/core/require.js')(root);
|
||||||
|
global.requireInternal = function (name) {
|
||||||
|
return require(root + '/internal/' + DetectServerType + '/' + name + '.js');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,10 +54,6 @@
|
|||||||
console.ex(ex);
|
console.ex(ex);
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
// 加载补丁和扩展
|
|
||||||
// load(root + '/core/patch.js');
|
|
||||||
// 加载underscore类库
|
|
||||||
// load('https://cdn.bootcss.com/underscore.js/1.8.3/underscore-min.js');
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -57,15 +57,6 @@ function register(jsp, cmd) {
|
|||||||
return cmd;
|
return cmd;
|
||||||
}
|
}
|
||||||
|
|
||||||
// var exec = {
|
|
||||||
// cmd: function (sender, command, args) {
|
|
||||||
//
|
|
||||||
// },
|
|
||||||
// tab: function (sender, command, args) {
|
|
||||||
//
|
|
||||||
// }
|
|
||||||
// };
|
|
||||||
|
|
||||||
function on(jsp, name, exec) {
|
function on(jsp, name, exec) {
|
||||||
var c = get(name) || create(jsp, name);
|
var c = get(name) || create(jsp, name);
|
||||||
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c));
|
console.debug('插件 %s 设置命令 %s(%s) 执行器 ...'.format(jsp.description.name, name, c));
|
||||||
|
Reference in New Issue
Block a user