feat: Optimization framework
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
72ae1b4b29
commit
f98814bf2d
@ -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() {
|
||||
|
@ -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;
|
||||
exports = module.exports = Object.assign(new ChatHandlerDefault(), requireInternal('chat'));
|
||||
|
@ -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'));
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -1,2 +1,9 @@
|
||||
'use strict';
|
||||
/**
|
||||
* MiaoScript Item处理类
|
||||
*/
|
||||
/*global Java, base, module, exports, require*/
|
||||
module.exports = requireInternal('item');
|
||||
/*global Java, base, module, exports, require*/
|
||||
function ItemHandlerDefault() {
|
||||
}
|
||||
exports = module.exports = Object.assign(new ItemHandlerDefault(), requireInternal('item'));;
|
||||
|
@ -1,2 +1,8 @@
|
||||
'use strict';
|
||||
/**
|
||||
* MiaoScript Permission处理类
|
||||
*/
|
||||
/*global Java, base, module, exports, require*/
|
||||
module.exports = requireInternal('permission', {warnNotFound: false});
|
||||
function PermissionHandlerDefault() {
|
||||
}
|
||||
exports = module.exports = Object.assign(new PermissionHandlerDefault(), requireInternal('permission', true));
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
|
||||
function loadPlugin(file) {
|
||||
try {
|
||||
var plugin = readPlugin(file);
|
||||
initPlugin(plugin);
|
||||
plugins[plugin.description.name] = plugin;
|
||||
return plugin
|
||||
loadPlugin(file)
|
||||
} catch (ex) {
|
||||
console.console('§6插件 §b%s §6初始化时发生错误 §4%s'.format(file.name, ex.message));
|
||||
console.ex(ex);
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
function loadPlugin(file) {
|
||||
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__);
|
||||
if (p) {
|
||||
load(p);
|
||||
enable(p);
|
||||
}
|
||||
}
|
||||
|
||||
function reload() {
|
||||
|
@ -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'));
|
||||
|
@ -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'));
|
||||
|
@ -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));
|
||||
}
|
||||
});
|
||||
return cache;
|
||||
}
|
||||
}.bind(this));
|
||||
};
|
||||
};
|
||||
global.Console = ConsoleDefault;
|
||||
})(global);
|
@ -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;
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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;
|
||||
}
|
||||
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(console.prefix + args.join(' '));
|
||||
};
|
||||
this.console = function () {
|
||||
sender.sendMessage(this.prefix + args.join(' '));
|
||||
}
|
||||
}.bind(this);
|
||||
this.console = function() {
|
||||
this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' '));
|
||||
};
|
||||
}.bind(this);
|
||||
};
|
||||
})(global);
|
@ -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
|
||||
}
|
||||
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(console.prefix + args.join(' ')));
|
||||
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, ' '));
|
||||
};
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user