diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index 0306356..cb380c4 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -1,76 +1,17 @@ -/** - * 控制台输出类 - */ -/*global base*/ -(function(global) { - var Arrays = Java.type('java.util.Arrays'); - var Level = Java.type('java.util.logging.Level'); - var ignoreLogPrefix = ['java.', 'net.minecraft.', 'org.bukkit.', 'jdk.nashorn.']; - global.ConsoleDefault = function ConsoleDefault(name) { - Object.defineProperty(this, 'name', { - get: function() { - return this._name; - }.bind(this), - set: function(name) { - this._name = name ? '[' + name + '] ' : ''; - // noinspection JSUnusedGlobalSymbols - this.prefix = name ? '§6[§cMS§6][§b' + name + '§6]§r ' : '§6[§bMiaoScript§6]§r '; - }.bind(this) - }); - this.name = name; - this.log = this.info = function() { - log.info(this.name + Array.prototype.join.call(arguments, ' ')); - }; - // noinspection JSUnusedGlobalSymbols - this.warn = function() { - log.warning(this.name + Array.prototype.join.call(arguments, ' ')); - }; - this.error = function() { - log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' ')); - }; - this.debug = function() { - log.info(this.name + '[DEBUG] ' + Array.prototype.join.call(arguments, ' ')); - }; - this.debug = global.debug ? this.debug : global.noop; - this.sender = this.info; - this.console = this.info; - this.object = function(obj) { - for (var i in obj) { - this.log(i, '=>', obj[i]) - } - }; - this.ex = function(ex) { - this.stack(ex).forEach(function(line) { - this.console(line) - }.bind(this)) - }; - this.stack = function(ex) { - var stack = ex.getStackTrace(); - var cache = ['§4' + ex]; - if (stack.class) { - stack = Arrays.asList(stack) - } - stack.forEach(function(trace) { - if (trace.className.startsWith('<')) { - var fileName = trace.fileName - fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : fileName; - cache.push(' §e->§c %s => §4%s:%s'.format(fileName, trace.methodName, trace.lineNumber)) - } else {// %s.%s(§4%s:%s§c) - var className = trace.className; - if (className.startsWith('jdk.nashorn.internal.scripts')) { - className = className.substr(className.lastIndexOf('$') + 1) - } else { - for (var prefix in ignoreLogPrefix) { - if (className.startsWith(ignoreLogPrefix[prefix])) { - return; - } - } - } - cache.push(' §e->§c %s.%s(§4%s:%s§c)'.format(className, trace.methodName, trace.fileName, trace.lineNumber)); - } - }); - return cache; +(function(logger) { + function log() { + logger.info(Array.prototype.join.call(arguments, ' ')) + } + function _proxy(prefix) { + return function() { + log('[' + prefix + ']', Array.prototype.join.call(arguments, ' ')) } + } + return { + log: log, + info: log, + debug: global.debug ? _proxy('DEBUG') : global.noop, + warn: _proxy('WARN'), + error: _proxy('ERROR') }; - global.Console = ConsoleDefault; -})(global); \ No newline at end of file +}) diff --git a/src/main/resources/core/detect.js b/src/main/resources/core/detect.js deleted file mode 100644 index 9ad2a6d..0000000 --- a/src/main/resources/core/detect.js +++ /dev/null @@ -1,22 +0,0 @@ -/** - * 服务器探测类 - */ -/*global base*/ -var ServerType = { - Bukkit: 'bukkit', - Sponge: 'sponge' -}; -var MServer; -var DetectServerType; -try { - MServer = Java.type("org.bukkit.Bukkit"); - DetectServerType = ServerType.Bukkit; -} catch (ex) { - // IGNORE -} -try { - MServer = Java.type("org.spongepowered.api.Sponge"); - DetectServerType = ServerType.Sponge; -} catch (ex) { - // IGNORE -} diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js deleted file mode 100644 index f101e9a..0000000 --- a/src/main/resources/core/init.js +++ /dev/null @@ -1,136 +0,0 @@ -'use strict'; -/*global base*/ - -(function(global) { - // noinspection JSUnusedLocalSymbols - global.init = function init(root) { - global.root = root; - global.noop = function() { - }; - var startTime = new Date().getTime(); - loadCore(); - loadPatch(); - loadRequire(); - try { - loadServerConsole(); - loadServerLib(); - loadPlugins(); - } catch (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)!'); - }; - - /** - * 初始化核心 - */ - function loadCore() { - // 加载Console - load(root + '/core/console.js'); - global.console = new global.Console(); - // 探测服务器类型 - 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); - } - }) - } - - /** - * 初始化模块 - */ - function loadRequire() { - global.engineLoad = load; - global.load = function __PreventGlobalLoadFunction__() { - throw new Error('Internal engine system not allow use `load` function!'); - }; - // 初始化加载器 - global.require = engineLoad(root + '/core/require.js')(root); - global.requireInternal = function requireInternal(name, ignoreError) { - try { - return require('internal/' + DetectServerType + '/' + name + '.js'); - } catch (ex) { - if (ignoreError) { return {} } - throw ex; - } - } - } - - /** - * 尝试加载特殊的Console类 - */ - function loadServerConsole() { - if (DetectServerType) { - try { - requireInternal('console'); - global.console = new global.Console(); - } catch (ex) { - // IGNORE - } - } - } - - /** - * 加载系统类库 - */ - function loadServerLib() { - var task = require('api/task'); - global.setTimeout = function setTimeout(func, time, _async) { - return _async ? task.laterAsync(func, time) : task.later(func, time); - }; - global.clearTimeout = function clearTimeout(task) { - task.cancel(); - }; - global.setInterval = function setInterval(func, time, _async) { - return _async ? task.timerAsync(func, time) : task.timer(func, time); - }; - global.clearInterval = function clearInterval(task) { - task.cancel(); - }; - } - - /** - * 加载JS插件 - */ - function loadPlugins() { - // 初始化本体插件 - global.manager = require('api/plugin'); - if (global.manager && global.manager.$) { - global.manager.init('plugins'); - // 只有当在正式环境运行的时候才加载 - global.manager.load(); - global.manager.enable(); - } else { - console.console('§4Current server does not support MiaoScript plugin system!' + DetectServerType); - } - } - - /** - * 关闭插件Hook - */ - global.engineDisable = function disable() { - try { - if (global.manager && global.manager.$) { - global.manager.disable(); - } - var server = require('api/server'); - if (server.shutdown) { - server.shutdown(); - } - } catch (ex) { - console.console("§3MiaoScript engine §ashutdown §4error... ERROR: ", ex); - console.ex(ex); - } - } -})(global); diff --git a/src/main/resources/core/patch/Array.js b/src/main/resources/core/patch/Array.js deleted file mode 100644 index 45bd1d1..0000000 --- a/src/main/resources/core/patch/Array.js +++ /dev/null @@ -1,20 +0,0 @@ -/** - * 补丁和方法扩展 - */ -(function() { - // noinspection JSUnresolvedVariable - if (!Array.prototype.copyPartialMatches) { - Object.defineProperty(Array.prototype, "copyPartialMatches", { - enumerable: false, - value: function(token, array) { - if (!token) { return this } - this.forEach(function(e) { - if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) { - array.push(e) - } - }); - return array - } - }); - } -})(); diff --git a/src/main/resources/core/patch/Date.js b/src/main/resources/core/patch/Date.js deleted file mode 100644 index 0f39538..0000000 --- a/src/main/resources/core/patch/Date.js +++ /dev/null @@ -1,29 +0,0 @@ -/** - * 补丁和方法扩展 - */ -(function() { - /** - * 日期格式化 - * 例: new Date().format('yyyy-MM-dd hh:mm:ss.s') => "2017-08-24 16:15:40.693" - * @param fmt 格式化字符串 - * @returns {*} - */ - Date.prototype.format = function(fmt) { - var o = { - "M+": this.getMonth() + 1, //月份 - "d+": this.getDate(), //日 - "h+": this.getHours(), //小时 - "m+": this.getMinutes(), //分 - "s+": this.getSeconds(), //秒 - "q+": Math.floor((this.getMonth() + 3) / 3), //季度 - "S": this.getMilliseconds() //毫秒 - }; - if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length)); - for (var k in o) { - if (new RegExp("(" + k + ")").test(fmt)) { - fmt = fmt.replace(RegExp.$1, (RegExp.$1.length === 1) ? o[k] : (("00" + o[k]).substr(("" + o[k]).length))); - } - } - return fmt; - }; -})(); diff --git a/src/main/resources/core/patch/Object.js b/src/main/resources/core/patch/Object.js deleted file mode 100644 index 65c4e62..0000000 --- a/src/main/resources/core/patch/Object.js +++ /dev/null @@ -1,54 +0,0 @@ -/** - * 补丁和方法扩展 - */ - -(function() { - // Object.assign Polyfill - if (!Object.assign) { - Object.defineProperty(Object, "assign", { - enumerable: false, - value: function(target) { - "use strict"; - if (target === undefined || target === null) - throw new TypeError("Cannot convert first argument to object"); - var to = Object(target); - for (var i = 1; i < arguments.length; i++) { - var nextSource = arguments[i]; - if (nextSource === undefined || nextSource === null) continue; - var keysArray = Object.keys(Object(nextSource)); - for (var nextIndex = 0, len = keysArray.length; nextIndex < len; nextIndex++) { - var nextKey = keysArray[nextIndex]; - var desc = Object.getOwnPropertyDescriptor(nextSource, nextKey); - if (desc !== undefined && desc.enumerable) to[nextKey] = nextSource[nextKey]; - } - } - return to; - } - }); - } - // Object.values Polyfill - if (!Object.values) { - Object.defineProperty(Object, "values", { - enumerable: false, - value: function(target) { - "use strict"; - var values = []; - for (var key in target) { - var desc = Object.getOwnPropertyDescriptor(target, key); - if (desc !== undefined && desc.enumerable) values.push(target[key]); - } - return values; - } - }); - } - - // JSON快捷方法 - if (!Object.toJson) { - Object.defineProperty(Object.prototype, "toJson", { - enumerable: false, - value: function() { - return JSON.stringify(this); - } - }); - } -})(); diff --git a/src/main/resources/core/patch/String.js b/src/main/resources/core/patch/String.js deleted file mode 100644 index 1143d9e..0000000 --- a/src/main/resources/core/patch/String.js +++ /dev/null @@ -1,14 +0,0 @@ -/** - * 补丁和方法扩展 - */ -(function() { - // Java格式化方法 - var str = Java.type('java.lang.String'); - String.prototype.format = function() { - return str.format(this, Array.prototype.slice.call(arguments, 0)) - }; - var indexOf = String.prototype.indexOf; - String.prototype.contains = function(searchString/*, position*/) { - return indexOf.call(this, searchString, arguments[1]) > -1; - }; -})(); diff --git a/src/main/resources/core/ployfill.js b/src/main/resources/core/ployfill.js new file mode 100644 index 0000000..cd8d777 --- /dev/null +++ b/src/main/resources/core/ployfill.js @@ -0,0 +1,16 @@ +(function(root, logger) { + global.root = root; + global.noop = global.engineDisable = function() { }; + // disable + global.engineLoad = load; + global.load = function __PreventGlobalLoadFunction__() { + throw new Error('Internal engine system not allow use `load` function!'); + }; + global.console = engineLoad(global.root + '/core/console.js')(logger); + global.require = engineLoad(global.root + '/core/require.js')(root); + String.prototype.contains = function(searchString/*, position*/) { + return String.prototype.indexOf.call(this, searchString, arguments[1]) > -1; + }; + require('es6-map/implement'); + require('es6-symbol/implement'); +}); diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index d144a19..1760dd1 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -33,6 +33,16 @@ var cacheDir = parent + separatorChar + "runtime"; var paths = [parent + separatorChar + 'node_modules', parent]; + function __assign(t) { + for (var s, i = 1, n = arguments.length; i < n; i++) { + s = arguments[i]; + if (s === undefined) { continue; }; + for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) + t[p] = s[p]; + } + return t; + }; + try { base.delete(cacheDir); } catch (ex) { @@ -247,7 +257,7 @@ function _require(name, path, optional) { var file = new File(name); file = _isFile(file) ? file : resolve(name, path); - optional = Object.assign({ cache: true }, optional); + optional = __assign({ cache: true }, optional); if (file === undefined) { throw Error("Can't found module " + name + " in directory " + path) }