diff --git a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java index 6730c93..cdf5417 100644 --- a/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java +++ b/src/main/java/pw/yumc/MiaoScript/ScriptEngine.java @@ -18,15 +18,16 @@ public class ScriptEngine { private String root; private Object logger; private MiaoScriptEngine engine; + private ScriptEngineManager manager; public ScriptEngine(String root, Object logger) { this.root = root; this.logger = logger; + this.manager = new ScriptEngineManager(); } @SneakyThrows public void enableEngine() { - ScriptEngineManager manager = new ScriptEngineManager(); this.engine = new MiaoScriptEngine(manager, "nashorn"); this.engine.put("base", new Base()); this.engine.put("ScriptEngineContextHolder", this); @@ -42,7 +43,8 @@ public class ScriptEngine { @SneakyThrows public void disableEngine() { - engine.invokeFunction("engineDisable"); + this.engine.invokeFunction("engineDisable"); + this.engine = null; } public MiaoScriptEngine getEngine() { diff --git a/src/main/resources/api/chat.js b/src/main/resources/api/chat.js index f1be139..ec8942a 100644 --- a/src/main/resources/api/chat.js +++ b/src/main/resources/api/chat.js @@ -1,3 +1,7 @@ +'use strict'; +/** + * MiaoScript Chat处理类 + */ /*global Java, base, module, exports, require*/ function ChatHandlerDefault() { // noinspection JSUnusedGlobalSymbols @@ -5,6 +9,4 @@ function ChatHandlerDefault() { this.json(sender, JSON.stringify(raw)); } } -var ChatHandler = Object.assign(new ChatHandlerDefault(), requireInternal('chat')); - -exports = module.exports = ChatHandler; \ No newline at end of file +exports = module.exports = Object.assign(new ChatHandlerDefault(), requireInternal('chat')); diff --git a/src/main/resources/api/command.js b/src/main/resources/api/command.js index 2b90c40..e03fc8b 100644 --- a/src/main/resources/api/command.js +++ b/src/main/resources/api/command.js @@ -1,3 +1,7 @@ +'use strict'; +/** + * MiaoScript Command处理类 + */ /*global Java, base, module, exports, require*/ function CommandHandlerDefault() { this.on = function(jsp, name, exec) { @@ -13,6 +17,4 @@ function CommandHandlerDefault() { } } } -var CommandHandler = Object.assign(new CommandHandlerDefault(), requireInternal('command')); - -exports = module.exports = CommandHandler; +exports = module.exports = Object.assign(new CommandHandlerDefault(), requireInternal('command')); diff --git a/src/main/resources/api/event.js b/src/main/resources/api/event.js index 7b5ad57..b629674 100644 --- a/src/main/resources/api/event.js +++ b/src/main/resources/api/event.js @@ -1,10 +1,9 @@ 'use strict'; /** - * Bukkit 事件相关类 + * MiaoScript Event处理类 */ /*global Java, base, module, exports, require, __FILE__*/ - function EventHandlerDefault() { var Thread = Java.type("java.lang.Thread"); @@ -14,9 +13,6 @@ function EventHandlerDefault() { this.listenerMap = []; this.baseEventDir = ''; - // noinspection JSUnusedLocalSymbols - var self = this; - /** * 扫描包 org.bukkit.event 下的所有事件 * 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent @@ -101,7 +97,12 @@ function EventHandlerDefault() { this.execute = function execute(name, exec, eventCls) { return function execute() { try { + var time = new Date().getTime() exec(arguments[arguments.length - 1]); + var cost = new Date().getTime() - time; + if (cost > 20) { + console.console('§c注意! §6插件 §b%s §6处理 §d%s §6事件 §c耗时 §4%sms !'.format(name, this.class2Name(eventCls), cost)) + } } catch (ex) { console.console('§6插件 §b%s §6处理 §d%s §6事件时发生异常 §4%s'.format(name, this.class2Name(eventCls), ex)); console.ex(ex); @@ -121,9 +122,7 @@ function EventHandlerDefault() { if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!'); var name = jsp.description.name; var eventCls = this.name2Class(name, event); - if (!eventCls) { - return; - } + if (!eventCls) { return; } if (typeof priority === 'boolean') { ignoreCancel = priority; priority = 'NORMAL'; @@ -146,7 +145,7 @@ function EventHandlerDefault() { }; listenerMap[name].push(off); // noinspection JSUnresolvedVariable - console.debug('插件 %s 注册事件 %s => %s'.format(name, this.class2Name(eventCls), exec.name === '' ? '匿名方法' : exec.name)); + console.debug('插件 %s 注册事件 %s => %s'.format(name, this.class2Name(eventCls), exec.name || '匿名方法')); return off; } } diff --git a/src/main/resources/api/item.js b/src/main/resources/api/item.js index 3b0e1b9..d38ad11 100644 --- a/src/main/resources/api/item.js +++ b/src/main/resources/api/item.js @@ -1,2 +1,9 @@ +'use strict'; +/** + * MiaoScript Item处理类 + */ /*global Java, base, module, exports, require*/ -module.exports = requireInternal('item'); \ No newline at end of file +/*global Java, base, module, exports, require*/ +function ItemHandlerDefault() { +} +exports = module.exports = Object.assign(new ItemHandlerDefault(), requireInternal('item'));; diff --git a/src/main/resources/api/permission.js b/src/main/resources/api/permission.js index fafaf62..7274513 100644 --- a/src/main/resources/api/permission.js +++ b/src/main/resources/api/permission.js @@ -1,2 +1,8 @@ +'use strict'; +/** + * MiaoScript Permission处理类 + */ /*global Java, base, module, exports, require*/ -module.exports = requireInternal('permission', {warnNotFound: false}); \ No newline at end of file +function PermissionHandlerDefault() { +} +exports = module.exports = Object.assign(new PermissionHandlerDefault(), requireInternal('permission', true)); diff --git a/src/main/resources/api/plugin.js b/src/main/resources/api/plugin.js index db00262..1917f8a 100644 --- a/src/main/resources/api/plugin.js +++ b/src/main/resources/api/plugin.js @@ -1,7 +1,6 @@ 'use strict'; /** * MiaoScript脚本插件加载类 - * @namespace plugin.configFile.parentFile, command.enable, permission.enable */ /*global Java, module, exports, require, __FILE__*/ var fs = require('core/fs'); @@ -68,20 +67,20 @@ function loadJsPlugins(files) { files.filter(function filterJsPlugin(file) { return file.name.endsWith(".js") }).forEach(function loadJsPlugin(file) { - loadPlugin(file) + try { + loadPlugin(file) + } catch (ex) { + console.console('§6插件 §b%s §6初始化时发生错误 §4%s'.format(file.name, ex.message)); + console.ex(ex); + } }) } function loadPlugin(file) { - try { - var plugin = readPlugin(file); - initPlugin(plugin); - plugins[plugin.description.name] = plugin; - return plugin - } catch (ex) { - console.console('§6插件 §b%s §6初始化时发生错误 §4%s'.format(file.name, ex.message)); - console.ex(ex); - } + var plugin = readPlugin(file); + initPlugin(plugin); + plugins[plugin.description.name] = plugin; + return plugin } function readPlugin(file) { @@ -148,10 +147,7 @@ function internalInitPlugin(plugin) { }; // 初始化插件配置相关方法 initPluginConfig(plugin); - - /** @namespace command.enable */ if (command.enable) command.enable(plugin); - /** @namespace permission.enable */ if (permission.enable) permission.enable(plugin); } @@ -227,7 +223,7 @@ function checkAndGet(args) { } var plugin = exports.plugins[name]; if (!plugin) { - throw new Error("插件 " + name + " 不存在!"); + throw new Error("插件 " + name + " 不存在!", args); } return [plugin]; } @@ -276,8 +272,10 @@ function disable() { function reloadPlugin(p) { disable(p); p = loadPlugin(p.__FILE__); - load(p); - enable(p); + if (p) { + load(p); + enable(p); + } } function reload() { diff --git a/src/main/resources/api/server.js b/src/main/resources/api/server.js index 288c132..e3d0979 100644 --- a/src/main/resources/api/server.js +++ b/src/main/resources/api/server.js @@ -13,6 +13,4 @@ function ServerHandlerDefault() { } }; } -var ServerHandler = Object.assign(new ServerHandlerDefault(), requireInternal('server')); - -exports = module.exports = ServerHandler; +exports = module.exports = Object.assign(new ServerHandlerDefault(), requireInternal('server')); diff --git a/src/main/resources/api/task.js b/src/main/resources/api/task.js index 32ac645..14eaeea 100644 --- a/src/main/resources/api/task.js +++ b/src/main/resources/api/task.js @@ -6,6 +6,4 @@ /*global Java, base, module, exports, require*/ function TaskHandlerDefault() { } -var TaskHandler = Object.assign(new TaskHandlerDefault(), requireInternal('task')); - -exports = module.exports = TaskHandler; +exports = module.exports = Object.assign(new TaskHandlerDefault(), requireInternal('task')); diff --git a/src/main/resources/api/wrapper.js b/src/main/resources/api/wrapper.js index 662fde4..0508c06 100644 --- a/src/main/resources/api/wrapper.js +++ b/src/main/resources/api/wrapper.js @@ -2,4 +2,4 @@ var player = requireInternal('wrapper/player'); module.exports = { player: player.$ -}; \ No newline at end of file +}; diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index bfde13c..7b4ea1b 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -39,26 +39,39 @@ } }; this.ex = function(message, ex) { - if (!ex) { - this.console('§4' + message); - ex = message; - } else { - this.console('§4' + message + ' ' + ex); + switch (toString.call(message)) { + case "[object String]": + message = message + ' ' //message = 'xxxx' ex =Error + break + case "[object Error]": + ex = message // message = Error ex=null + message = '' + break } - var track = ex.getStackTrace(); + this.console('§4 ' + message + ex) + this.stack(ex).forEach(function(line) { + this.console(line) + }) + }; + this.stack = function(ex) { + var track = ex ? ex.getStackTrace() : new Error().getStackTrace(); + var cache = ['§4' + ex]; if (track.class) { track = Arrays.asList(track) } track.forEach(function(stack) { if (stack.className.startsWith('<')) { - this.console(' §e位于§c', stack.fileName, '=>§c', stack.methodName, '§4行', stack.lineNumber); + var fileName = stack.fileName + fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : fileName; + cache.push(' §e->§c %s => §4%s:%s'.format(fileName, stack.methodName, stack.lineNumber)) } else {// %s.%s(§4%s:%s§c) var className = stack.className className = className.startsWith('jdk.nashorn.internal.scripts') ? className.substr(className.lastIndexOf('$') + 1) : className - this.console(' §e位于§c', className + '.' + stack.methodName + '(§4' + stack.fileName + ':' + stack.lineNumber + '§c)'); + cache.push(' §e->§c %s.%s(§4%s:%s§c)'.format(className, stack.methodName, stack.fileName, stack.lineNumber)); } - }.bind(this)); - }; + }); + return cache; + } }; global.Console = ConsoleDefault; })(global); \ No newline at end of file diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index ace6f58..1da09d3 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -1,11 +1,11 @@ 'use strict'; /*global base*/ -(function (global) { +(function(global) { // noinspection JSUnusedLocalSymbols global.init = function init(root) { global.root = root; - global.noop = function () { + global.noop = function() { }; var startTime = new Date().getTime(); loadCore(); @@ -15,7 +15,7 @@ loadServerLib(); loadPlugins(); } catch (ex) { - console.console("§4Init plugin system lib failed! ERROR:§c", ex); + console.console("§4Initialization plugin system lib failed! ERROR:§c", ex); console.ex(ex); } console.log('MiaoScript engine loading completed... Done (' + (new Date().getTime() - startTime) / 1000 + 's)!'); @@ -33,6 +33,20 @@ load(root + '/core/detect.js'); } + /** + * 加载补丁 + */ + function loadPatch() { + java.nio.file.Files.list(new java.io.File(root, 'core/patch').toPath()).forEach(function(path) { + console.log('Loading ext lib', path); + try { + load(path.toFile()); + } catch (ex) { + console.ex(ex); + } + }) + } + /** * 初始化模块 */ @@ -44,22 +58,13 @@ // 初始化加载器 global.require = engineLoad(root + '/core/require.js')(root); global.requireInternal = function requireInternal(name) { - return require(root + '/internal/' + DetectServerType + '/' + name + '.js', arguments[1]); - } - } - - /** - * 加载补丁 - */ - function loadPatch() { - java.nio.file.Files.list(new java.io.File(root, 'core/patch').toPath()).forEach(function (path) { - console.log('Loading ext lib', path); try { - load(path.toFile()); + return require(root + '/internal/' + DetectServerType + '/' + name + '.js'); } catch (ex) { - console.ex(ex); + if (!arguments[1]) { return {} } + throw ex; } - }) + } } /** diff --git a/src/main/resources/internal/bukkit/console.js b/src/main/resources/internal/bukkit/console.js index fdaab7a..0de1418 100644 --- a/src/main/resources/internal/bukkit/console.js +++ b/src/main/resources/internal/bukkit/console.js @@ -1,18 +1,24 @@ /*global Java, base, module, exports, require, __FILE__*/ -(function (global) { +(function(global) { global.Console = function BukkitConsole() { ConsoleDefault.call(this); - this.sender = function () { + this.sender = function() { var sender = arguments[0]; if (!(sender instanceof org.bukkit.command.CommandSender)) { this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!") - return + return; } - var args = Array.prototype.slice.call(arguments, 1); - sender.sendMessage(console.prefix + args.join(' ')); - }; - this.console = function () { + if (toString.call(arguments[1]) === "[object Array]") { + arguments[1].forEach(function(line) { + sender.sendMessage(this.prefix + line); + }.bind(this)) + } else { + var args = Array.prototype.slice.call(arguments, 1); + sender.sendMessage(this.prefix + args.join(' ')); + } + }.bind(this); + this.console = function() { this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' ')); - }; + }.bind(this); }; })(global); \ No newline at end of file diff --git a/src/main/resources/internal/sponge/console.js b/src/main/resources/internal/sponge/console.js index 084cf70..723857d 100644 --- a/src/main/resources/internal/sponge/console.js +++ b/src/main/resources/internal/sponge/console.js @@ -1,22 +1,28 @@ /*global Java, base, module, exports, require, __FILE__*/ -(function (global) { +(function(global) { global.Console = function SpongeConsole() { ConsoleDefault.call(this); - this.sender = function () { + this.sender = function() { var Text = Java.type("org.spongepowered.api.text.Text"); var sender = arguments[0]; if (!(sender instanceof org.spongepowered.api.command.CommandSource)) { this.error("第一个参数未实现 org.spongepowered.api.command.CommandSource 无法发送消息!") return } - var args = Array.prototype.slice.call(arguments, 1); - sender.sendMessage(Text.of(console.prefix + args.join(' '))); + if (toString.call(arguments[1]) === "[object Array]") { + arguments[1].forEach(function(line) { + sender.sendMessage(Text.of(this.prefix + line)); + }.bind(this)) + } else { + var args = Array.prototype.slice.call(arguments, 1); + sender.sendMessage(Text.of(this.prefix + args.join(' '))); + } }; - this.console = function () { + this.console = function() { this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' ')); }; // noinspection JSUnusedGlobalSymbols - this.warn = function () { + this.warn = function() { log.warn(this.name + Array.prototype.join.call(arguments, ' ')); }; };