diff --git a/src/main/resources/core/ext/patch.js b/src/main/resources/core/ext/Date.js similarity index 65% rename from src/main/resources/core/ext/patch.js rename to src/main/resources/core/ext/Date.js index bb7c12e..eec8960 100644 --- a/src/main/resources/core/ext/patch.js +++ b/src/main/resources/core/ext/Date.js @@ -3,24 +3,6 @@ */ (function () { - // Java格式化方法 - var str = Java.type('java.lang.String'); - String.prototype.format = function () { - return str.format(this, Array.prototype.slice.call(arguments, 0)) - }; - - // ========== 暂不扩展Object ========== - // // JSON快捷方法 - // Object.prototype.toJson = function () { - // return JSON.stringify(this); - // }; - - // // YAML快速生成 - // var yaml = require('modules/yaml'); - // Object.prototype.toYaml = function () { - // return yaml.safeDump(this); - // }; - /** * 日期格式化 * 例: new Date().format('yyyy-MM-dd hh:mm:ss.s') => "2017-08-24 16:15:40.693" diff --git a/src/main/resources/core/ext/Object.js b/src/main/resources/core/ext/Object.js new file mode 100644 index 0000000..5c06cab --- /dev/null +++ b/src/main/resources/core/ext/Object.js @@ -0,0 +1,50 @@ +/** + * 补丁和方法扩展 + */ + +(function () { + // Object.assign Polyfill + if (!Object.assign) { + Object.defineProperty(Object, "assign", { + enumerable: false, + configurable: true, + writable: true, + 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; + } + }); + } + + // // JSON快捷方法 + if(!Object.toJson){ + Object.defineProperty(Object, "toJson", { + enumerable: false, + value: function() { + return JSON.stringify(this); + } + }); + } + // Object.prototype.toJson = function () { + // return JSON.stringify(this); + // }; + + // // YAML快速生成 + // var yaml = require('modules/yaml'); + // Object.prototype.toYaml = function () { + // return yaml.safeDump(this); + // }; +})(); diff --git a/src/main/resources/core/ext/String.js b/src/main/resources/core/ext/String.js new file mode 100644 index 0000000..3822c93 --- /dev/null +++ b/src/main/resources/core/ext/String.js @@ -0,0 +1,11 @@ +/** + * 补丁和方法扩展 + */ + +(function () { + // Java格式化方法 + var str = Java.type('java.lang.String'); + String.prototype.format = function () { + return str.format(this, Array.prototype.slice.call(arguments, 0)) + }; +})(); diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index 38ce341..6a5c24a 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -8,9 +8,9 @@ global.noop = function () { }; loadCore(); + loadExt(); loadRequire(); try { - loadExt(); loadServerLib(); loadPlugins(); } catch (ex) { @@ -43,8 +43,7 @@ * 加载补丁 */ function loadExt() { - var fs = require('core/fs'); - fs.list(fs.file(root, 'core/ext')).forEach(function (path) { + java.nio.file.Files.list(new java.io.File(root, 'core/ext').toPath()).forEach(function (path) { console.log('加载扩展类库', path); try { load(path.toFile()); @@ -59,7 +58,7 @@ } /** - * 加载Bukkit的类库 + * 加载系统类库 */ function loadServerLib() { var task = require('api/task'); diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index 3f3fae9..65e22c8 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -106,7 +106,6 @@ file = ext.notNull(dir) ? new File(dir, file) : new File(file); // 直接文件 if (file.isFile()) { - console.log(file); return file; } // JS文件 @@ -184,7 +183,6 @@ if (_canonical(file).endsWith('.msm')) { throw Error("暂不支持解析 MiaoScript 模块"); } - console.debug('模块', name, '编译成功!'); } catch (ex) { console.console('§4警告! §c模块§a', name, '§c编译失败! §4ERR:', ex); console.ex(ex); @@ -240,11 +238,13 @@ function _require(name, path, optional) { var file = new File(name); file = _isFile(file) ? file : resolve(name, path); + optional = Object.assign({cache: true, warnNotFound: true}, optional); if (file === undefined) { - console.console('§c目录§b', path, '§c下模块§a', name, '§c加载失败! §4未找到该模块!'); + if (optional.warnNotFound) { + console.console('§c目录§b', path, '§c下模块§a', name, '§c加载失败! §4未找到该模块!'); + } return {exports: {}}; } - if (!optional) optional = {cache: true}; // 重定向文件名称和类型 return getCacheModule(_canonical(file), file.name.split(".")[0], file, optional); }