From e454066ccea2182ea6834ad0aa4b5c6be5f4b658 Mon Sep 17 00:00:00 2001 From: 502647092 Date: Mon, 30 Oct 2017 20:47:10 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E9=87=8D=E6=96=B0=E8=B0=83?= =?UTF-8?q?=E6=95=B4=E9=A1=B9=E7=9B=AE=E6=A1=86=E6=9E=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- src/main/resources/api/command.js | 2 + src/main/resources/api/event.js | 2 + src/main/resources/api/mserver.js | 13 + .../resources/{modules => api}/permission.js | 1 + src/main/resources/api/plugin.js | 2 + src/main/resources/api/server.js | 2 + src/main/resources/api/task.js | 2 + src/main/resources/bios.js | 8 +- src/main/resources/core/base64.js | 214 ----------------- src/main/resources/core/console.js | 10 +- src/main/resources/core/fs.js | 2 +- src/main/resources/core/init.js | 7 +- src/main/resources/core/patch.js | 14 +- src/main/resources/core/require.js | 32 ++- .../{modules => internal}/bukkit/command.js | 2 +- .../{modules => internal}/bukkit/event.js | 6 +- .../bukkit/permission.js | 0 .../{modules => internal}/bukkit/plugin.js | 2 +- .../{modules => internal}/bukkit/server.js | 1 - .../{modules => internal}/bukkit/task.js | 0 .../{modules => internal}/sponge/command.js | 0 .../{modules => internal}/sponge/server.js | 1 - .../{modules => internal}/sponge/task.js | 0 src/main/resources/modules/base64.js | 225 ++++++++++++++++++ src/main/resources/modules/bukkit/console.js | 19 -- src/main/resources/modules/command.js | 1 - src/main/resources/modules/event.js | 1 - src/main/resources/modules/mserver.js | 8 - src/main/resources/modules/plugin.js | 1 - .../resources/{core => modules}/reflect.js | 4 +- src/main/resources/modules/server.js | 1 - src/main/resources/modules/sponge/console.js | 23 -- src/main/resources/modules/task.js | 1 - src/main/resources/{core => modules}/zip.js | 2 +- src/main/resources/plugins/HelloWorld.js | 14 +- src/main/resources/plugins/MiaoTag.js | 14 +- src/main/resources/plugins/ext/papi.js | 2 +- 37 files changed, 320 insertions(+), 319 deletions(-) create mode 100644 src/main/resources/api/command.js create mode 100644 src/main/resources/api/event.js create mode 100644 src/main/resources/api/mserver.js rename src/main/resources/{modules => api}/permission.js (50%) create mode 100644 src/main/resources/api/plugin.js create mode 100644 src/main/resources/api/server.js create mode 100644 src/main/resources/api/task.js delete mode 100644 src/main/resources/core/base64.js rename src/main/resources/{modules => internal}/bukkit/command.js (98%) rename src/main/resources/{modules => internal}/bukkit/event.js (96%) rename src/main/resources/{modules => internal}/bukkit/permission.js (100%) rename src/main/resources/{modules => internal}/bukkit/plugin.js (99%) rename src/main/resources/{modules => internal}/bukkit/server.js (98%) rename src/main/resources/{modules => internal}/bukkit/task.js (100%) rename src/main/resources/{modules => internal}/sponge/command.js (100%) rename src/main/resources/{modules => internal}/sponge/server.js (96%) rename src/main/resources/{modules => internal}/sponge/task.js (100%) create mode 100644 src/main/resources/modules/base64.js delete mode 100644 src/main/resources/modules/bukkit/console.js delete mode 100644 src/main/resources/modules/command.js delete mode 100644 src/main/resources/modules/event.js delete mode 100644 src/main/resources/modules/mserver.js delete mode 100644 src/main/resources/modules/plugin.js rename src/main/resources/{core => modules}/reflect.js (99%) delete mode 100644 src/main/resources/modules/server.js delete mode 100644 src/main/resources/modules/sponge/console.js delete mode 100644 src/main/resources/modules/task.js rename src/main/resources/{core => modules}/zip.js (97%) diff --git a/src/main/resources/api/command.js b/src/main/resources/api/command.js new file mode 100644 index 0000000..1bf0396 --- /dev/null +++ b/src/main/resources/api/command.js @@ -0,0 +1,2 @@ +/*global Java, base, module, exports, require*/ +module.exports = require('./mserver').command; \ No newline at end of file diff --git a/src/main/resources/api/event.js b/src/main/resources/api/event.js new file mode 100644 index 0000000..585de74 --- /dev/null +++ b/src/main/resources/api/event.js @@ -0,0 +1,2 @@ +/*global Java, base, module, exports, require*/ +module.exports = require('./mserver').event; \ No newline at end of file diff --git a/src/main/resources/api/mserver.js b/src/main/resources/api/mserver.js new file mode 100644 index 0000000..bddbd10 --- /dev/null +++ b/src/main/resources/api/mserver.js @@ -0,0 +1,13 @@ +/*global Java, base, module, exports, require*/ +function impl(name) { + return require('../internal/' + DetectServerType + '/' + name); +} + +module.exports = { + command: impl('command'), + event: impl('event'), + permission: impl('permission'), + plugin: impl('plugin'), + server: impl('server'), + task: impl('task') +}; \ No newline at end of file diff --git a/src/main/resources/modules/permission.js b/src/main/resources/api/permission.js similarity index 50% rename from src/main/resources/modules/permission.js rename to src/main/resources/api/permission.js index c112c6a..107faaa 100644 --- a/src/main/resources/modules/permission.js +++ b/src/main/resources/api/permission.js @@ -1 +1,2 @@ +/*global Java, base, module, exports, require*/ module.exports = require('./mserver').permission; \ No newline at end of file diff --git a/src/main/resources/api/plugin.js b/src/main/resources/api/plugin.js new file mode 100644 index 0000000..628f5d1 --- /dev/null +++ b/src/main/resources/api/plugin.js @@ -0,0 +1,2 @@ +/*global Java, base, module, exports, require*/ +module.exports = require('./mserver').plugin; \ No newline at end of file diff --git a/src/main/resources/api/server.js b/src/main/resources/api/server.js new file mode 100644 index 0000000..8f09fea --- /dev/null +++ b/src/main/resources/api/server.js @@ -0,0 +1,2 @@ +/*global Java, base, module, exports, require*/ +module.exports = require('./mserver').server; \ No newline at end of file diff --git a/src/main/resources/api/task.js b/src/main/resources/api/task.js new file mode 100644 index 0000000..6ab9212 --- /dev/null +++ b/src/main/resources/api/task.js @@ -0,0 +1,2 @@ +/*global Java, base, module, exports, require*/ +module.exports = require('./mserver').task; \ No newline at end of file diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index feb21ae..b95344d 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -39,11 +39,15 @@ var disable; var classLoader = java.lang.Thread.currentThread().getContextClassLoader(); var url = classLoader.getResource("plugin.yml"); - if (url === null) { return; } + if (url === null) { + return; + } var upath = url.getFile().substring(url.getFile().indexOf("/") + 1); var jarPath = java.net.URLDecoder.decode(upath.substring(0, upath.indexOf('!'))); - if (!Files.exists(Paths.get(jarPath))) { jarPath = "/" + jarPath; } + if (!Files.exists(Paths.get(jarPath))) { + jarPath = "/" + jarPath; + } try { var jar = new java.util.jar.JarFile(jarPath); diff --git a/src/main/resources/core/base64.js b/src/main/resources/core/base64.js deleted file mode 100644 index fe311a5..0000000 --- a/src/main/resources/core/base64.js +++ /dev/null @@ -1,214 +0,0 @@ -/* - * $Id: base64.js,v 2.15 2014/04/05 12:58:57 dankogai Exp dankogai $ - * - * Licensed under the BSD 3-Clause License. - * http://opensource.org/licenses/BSD-3-Clause - * - * References: - * http://en.wikipedia.org/wiki/Base64 - */ - -(function(global) { - 'use strict'; - // existing version for noConflict() - var _Base64 = global.Base64; - var version = "2.3.2"; - // if node.js, we use Buffer - var buffer; - if (typeof module !== 'undefined' && module.exports) { - try { - buffer = require('buffer').Buffer; - } catch (err) {} - } - // constants - var b64chars - = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; - var b64tab = function(bin) { - var t = {}; - for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i; - return t; - }(b64chars); - var fromCharCode = String.fromCharCode; - // encoder stuff - var cb_utob = function(c) { - if (c.length < 2) { - var cc = c.charCodeAt(0); - return cc < 0x80 ? c - : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) - + fromCharCode(0x80 | (cc & 0x3f))) - : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) - + fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) - + fromCharCode(0x80 | ( cc & 0x3f))); - } else { - var cc = 0x10000 - + (c.charCodeAt(0) - 0xD800) * 0x400 - + (c.charCodeAt(1) - 0xDC00); - return (fromCharCode(0xf0 | ((cc >>> 18) & 0x07)) - + fromCharCode(0x80 | ((cc >>> 12) & 0x3f)) - + fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) - + fromCharCode(0x80 | ( cc & 0x3f))); - } - }; - var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g; - var utob = function(u) { - return u.replace(re_utob, cb_utob); - }; - var cb_encode = function(ccc) { - var padlen = [0, 2, 1][ccc.length % 3], - ord = ccc.charCodeAt(0) << 16 - | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8) - | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)), - chars = [ - b64chars.charAt( ord >>> 18), - b64chars.charAt((ord >>> 12) & 63), - padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63), - padlen >= 1 ? '=' : b64chars.charAt(ord & 63) - ]; - return chars.join(''); - }; - var btoa = global.btoa ? function(b) { - return global.btoa(b); - } : function(b) { - return b.replace(/[\s\S]{1,3}/g, cb_encode); - }; - var _encode = buffer ? - buffer.from && buffer.from !== Uint8Array.from ? function (u) { - return (u.constructor === buffer.constructor ? u : buffer.from(u)) - .toString('base64') - } - : function (u) { - return (u.constructor === buffer.constructor ? u : new buffer(u)) - .toString('base64') - } - : function (u) { return btoa(utob(u)) } - ; - var encode = function(u, urisafe) { - return !urisafe - ? _encode(String(u)) - : _encode(String(u)).replace(/[+\/]/g, function(m0) { - return m0 == '+' ? '-' : '_'; - }).replace(/=/g, ''); - }; - var encodeURI = function(u) { return encode(u, true) }; - // decoder stuff - var re_btou = new RegExp([ - '[\xC0-\xDF][\x80-\xBF]', - '[\xE0-\xEF][\x80-\xBF]{2}', - '[\xF0-\xF7][\x80-\xBF]{3}' - ].join('|'), 'g'); - var cb_btou = function(cccc) { - switch(cccc.length) { - case 4: - var cp = ((0x07 & cccc.charCodeAt(0)) << 18) - | ((0x3f & cccc.charCodeAt(1)) << 12) - | ((0x3f & cccc.charCodeAt(2)) << 6) - | (0x3f & cccc.charCodeAt(3)), - offset = cp - 0x10000; - return (fromCharCode((offset >>> 10) + 0xD800) - + fromCharCode((offset & 0x3FF) + 0xDC00)); - case 3: - return fromCharCode( - ((0x0f & cccc.charCodeAt(0)) << 12) - | ((0x3f & cccc.charCodeAt(1)) << 6) - | (0x3f & cccc.charCodeAt(2)) - ); - default: - return fromCharCode( - ((0x1f & cccc.charCodeAt(0)) << 6) - | (0x3f & cccc.charCodeAt(1)) - ); - } - }; - var btou = function(b) { - return b.replace(re_btou, cb_btou); - }; - var cb_decode = function(cccc) { - var len = cccc.length, - padlen = len % 4, - n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0) - | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0) - | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0) - | (len > 3 ? b64tab[cccc.charAt(3)] : 0), - chars = [ - fromCharCode( n >>> 16), - fromCharCode((n >>> 8) & 0xff), - fromCharCode( n & 0xff) - ]; - chars.length -= [0, 0, 2, 1][padlen]; - return chars.join(''); - }; - var atob = global.atob ? function(a) { - return global.atob(a); - } : function(a){ - return a.replace(/[\s\S]{1,4}/g, cb_decode); - }; - var _decode = buffer ? - buffer.from && buffer.from !== Uint8Array.from ? function(a) { - return (a.constructor === buffer.constructor - ? a : buffer.from(a, 'base64')).toString(); - } - : function(a) { - return (a.constructor === buffer.constructor - ? a : new buffer(a, 'base64')).toString(); - } - : function(a) { return btou(atob(a)) }; - var decode = function(a){ - return _decode( - String(a).replace(/[-_]/g, function(m0) { return m0 == '-' ? '+' : '/' }) - .replace(/[^A-Za-z0-9\+\/]/g, '') - ); - }; - var noConflict = function() { - var Base64 = global.Base64; - global.Base64 = _Base64; - return Base64; - }; - // export Base64 - global.Base64 = { - VERSION: version, - atob: atob, - btoa: btoa, - fromBase64: decode, - toBase64: encode, - utob: utob, - encode: encode, - encodeURI: encodeURI, - btou: btou, - decode: decode, - noConflict: noConflict - }; - // if ES5 is available, make Base64.extendString() available - if (typeof Object.defineProperty === 'function') { - var noEnum = function(v){ - return {value:v,enumerable:false,writable:true,configurable:true}; - }; - global.Base64.extendString = function () { - Object.defineProperty( - String.prototype, 'fromBase64', noEnum(function () { - return decode(this) - })); - Object.defineProperty( - String.prototype, 'toBase64', noEnum(function (urisafe) { - return encode(this, urisafe) - })); - Object.defineProperty( - String.prototype, 'toBase64URI', noEnum(function () { - return encode(this, true) - })); - }; - } - // module.exports and AMD are mutually exclusive. - // module.exports has precedence. - if (typeof module !== 'undefined' && module.exports) { - module.exports = global.Base64; - } - else if (typeof define === 'function' && define.amd) { - // AMD. Register as an anonymous module. - define([], function(){ return global.Base64 }); - } - // that's it! -})( typeof self !== 'undefined' ? self - : typeof window !== 'undefined' ? window - : typeof global !== 'undefined' ? global - : this -); \ No newline at end of file diff --git a/src/main/resources/core/console.js b/src/main/resources/core/console.js index ef20f7e..3d39e95 100644 --- a/src/main/resources/core/console.js +++ b/src/main/resources/core/console.js @@ -6,7 +6,7 @@ var Arrays = Java.type('java.util.Arrays'); var Level = Java.type('java.util.logging.Level'); var Console = { - createNew: function(name) { + createNew: function (name) { var console = {}; Object.defineProperty(console, 'name', { get: function () { @@ -46,10 +46,10 @@ this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber)); } }.bind(this)); - } + }; return console; } - } + }; var BukkitConsole = { createNew: function () { var console = Console.createNew(); @@ -66,7 +66,7 @@ }; return console; } - } + }; var SpongeConsole = { createNew: function () { var console = Console.createNew(); @@ -87,7 +87,7 @@ }; return console; } - } + }; switch (DetectServerType) { case ServerType.Bukkit: global.Console = BukkitConsole; diff --git a/src/main/resources/core/fs.js b/src/main/resources/core/fs.js index 125b798..c6e7fc9 100644 --- a/src/main/resources/core/fs.js +++ b/src/main/resources/core/fs.js @@ -12,7 +12,7 @@ var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption"); */ exports.concat = function () { return Array.prototype.join.call(arguments, separatorChar); -} +}; /** * 获得文件 * @constructor(file) diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index 7f2724a..474d31f 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -17,7 +17,8 @@ function init(root) { * 初始化核心 */ function loadCore() { - global.noop = function () {}; + global.noop = function () { + }; // 加载基础模块 load(root + '/core/ext.js'); // 探测服务器类型 @@ -46,7 +47,7 @@ function loadPatch() { * 加载Bukkit的类库 */ function loadServerLib() { - var task = require('modules/task'); + var task = require('api/task'); global.setTimeout = function (func, time, _async) { return _async ? task.laterAsync(func, time) : task.later(func, time); }; @@ -66,7 +67,7 @@ function loadServerLib() { */ function loadPlugins() { // 初始化本体插件 - global.manager = require('modules/plugin'); + global.manager = require('api/plugin'); if (manager) { manager.init('plugins'); // 只有当在正式环境运行的时候才加载 diff --git a/src/main/resources/core/patch.js b/src/main/resources/core/patch.js index 391e215..e4689c4 100644 --- a/src/main/resources/core/patch.js +++ b/src/main/resources/core/patch.js @@ -2,15 +2,21 @@ * 补丁和方法扩展 */ -(function(){ +(function () { // Java格式化方法 var str = Java.type('java.lang.String'); - String.prototype.format = function(){ return str.format(this, Array.prototype.slice.call(arguments, 0))} + String.prototype.format = function () { + return str.format(this, Array.prototype.slice.call(arguments, 0)) + }; // JSON快捷方法 - Object.prototype.toJson = 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); } + Object.prototype.toYaml = function () { + return yaml.safeDump(this); + } })(); diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index 8269664..efd6949 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -1,6 +1,27 @@ /** - * 符合 CommonJS 规范的 模块化加载 - * + * 符合 CommonJS 规范的 类似 Node 的模块化加载 + * 一. 注: MiaoScript 中 require.main 不存在 + * 二. 加载 require 流程 例如 在 dir 目录下 调用 require('xx'); + * a) 加载流程 + * 1. 如果xx模块是一个內建模块 + * a. 编译并返回该模块 + * b. 停止执行 + * 2. 如果模块以 `./` `../` 开头 + * a. 尝试使用 resolveAsFile(dir/xx) 加载文件 + * b. 尝试使用 resolveAsDirectory(dir/xx) 加载目录 + * 3. 尝试去 root root/core root/modules 用方法2加载模块 + * 4. 抛出 not found 异常 + * b) resolveAsFile 解析流程 + * 1. 如果 xx 是一个文件 则作为 `javascript` 文本加载 并停止执行 + * 2. 如果 xx.js 是一个文件 则作为 `javascript` 文本加载 并停止执行 + * 暂不支持 3. 如果 xx.json 是一个文件 则使用 `JSON.parse(xx.json)` 解析为对象加载 并停止执行 + * 暂不支持 4. 如果 xx.ms 是一个文件 则使用MScript解析器解析 并停止执行 + * c) resolveAsDirectory 解析流程 + * 1. 如果 xx/package.json 存在 则使用 `JSON.parse(xx/package.json)` 解析并取得 main 字段使用 resolveAsFile(main) 加载 + * 2. 如果 xx/index.js 存在 则使用 resolveAsFile(xx/index.js) 加载 + * 暂不支持 3. 如果 xx/index.json 存在 则使用 `xx/index.json` 解析为对象加载 并停止执行 + * 暂不支持 4. 如果 xx/index.ms 是一个文件 则使用MScript解析器解析 并停止执行 + * 注: MiaoScript 暂不支持多层 modules 加载 暂时不需要(估计以后也不会需要) */ /*global Java, base*/ (function (parent) { @@ -151,7 +172,7 @@ * @private */ function _require(name, path, optional) { - var file = _canonical(name) ? name : resolve(name, path); + var file = name.isFile && name.isFile() ? name : resolve(name, path); if (file === undefined) { console.console("§c模块 §a%s §c加载失败! §4未找到该模块!".format(name)); return {exports: {}}; @@ -181,9 +202,8 @@ var cacheDir = parent + separatorChar + "runtime"; - // 等于 undefined 说明 parent 是一个字符串 需要转成File - // 可能有更加准确的方案 - if (_canonical(parent) === undefined) { + // 判断是否存在 isFile 不存在说明 parent 是一个字符串 需要转成File + if (parent.isFile) { parent = new File(parent); } var cacheModules = []; diff --git a/src/main/resources/modules/bukkit/command.js b/src/main/resources/internal/bukkit/command.js similarity index 98% rename from src/main/resources/modules/bukkit/command.js rename to src/main/resources/internal/bukkit/command.js index 93c545f..3a98cdf 100644 --- a/src/main/resources/modules/bukkit/command.js +++ b/src/main/resources/internal/bukkit/command.js @@ -4,7 +4,7 @@ */ /*global Java, base, module, exports, require, __FILE__*/ -var ref = require('core/reflect'); +var ref = require('modules/reflect'); var bukkit = require('./server'); var plugin = bukkit.plugin.self; var commandMap = ref.on(bukkit.plugin.manager).get('commandMap').get(); diff --git a/src/main/resources/modules/bukkit/event.js b/src/main/resources/internal/bukkit/event.js similarity index 96% rename from src/main/resources/modules/bukkit/event.js rename to src/main/resources/internal/bukkit/event.js index b43f2cf..8966d36 100644 --- a/src/main/resources/modules/bukkit/event.js +++ b/src/main/resources/internal/bukkit/event.js @@ -8,10 +8,8 @@ var Bukkit = Java.type("org.bukkit.Bukkit"); var Listener = Java.type("org.bukkit.event.Listener"); var Modifier = Java.type("java.lang.reflect.Modifier"); var BukkitEvent = Java.type("org.bukkit.event.Event"); -var HandlerList = Java.type('org.bukkit.event.HandlerList'); var EventPriority = Java.type("org.bukkit.event.EventPriority"); var EventExecutor = Java.type("org.bukkit.plugin.EventExecutor"); -var IllegalStateException = Java.type("java.lang.IllegalStateException"); var plugin = require('./server').plugin.self; @@ -126,7 +124,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) { plugin, ignoreCancel); // 添加到缓存 用于关闭插件的时候关闭事件 - if (!listenerMap[name]) listenerMap[name] = [] + if (!listenerMap[name]) listenerMap[name] = []; var listeners = listenerMap[name]; var off = { event: eventCls, @@ -135,7 +133,7 @@ function listen(jsp, event, exec, priority, ignoreCancel) { ref.on(this.event).call('getHandlerList').get().unregister(this.listener); console.debug('插件 %s 注销事件 %s'.format(name, this.event.simpleName)); } - } + }; listeners.push(off); // noinspection JSUnresolvedVariable console.debug('插件 %s 注册事件 %s => %s'.format(name, eventCls.simpleName, exec.name === '' ? '匿名方法' : exec.name)); diff --git a/src/main/resources/modules/bukkit/permission.js b/src/main/resources/internal/bukkit/permission.js similarity index 100% rename from src/main/resources/modules/bukkit/permission.js rename to src/main/resources/internal/bukkit/permission.js diff --git a/src/main/resources/modules/bukkit/plugin.js b/src/main/resources/internal/bukkit/plugin.js similarity index 99% rename from src/main/resources/modules/bukkit/plugin.js rename to src/main/resources/internal/bukkit/plugin.js index 34079a4..1df15fb 100644 --- a/src/main/resources/modules/bukkit/plugin.js +++ b/src/main/resources/internal/bukkit/plugin.js @@ -13,7 +13,7 @@ var permission = require('./permission'); /** * 载入插件 - * @param path + * @param dir */ function loadPlugins(dir) { var plugin = fs.file(root, dir); diff --git a/src/main/resources/modules/bukkit/server.js b/src/main/resources/internal/bukkit/server.js similarity index 98% rename from src/main/resources/modules/bukkit/server.js rename to src/main/resources/internal/bukkit/server.js index 875a75a..23b5a6e 100644 --- a/src/main/resources/modules/bukkit/server.js +++ b/src/main/resources/internal/bukkit/server.js @@ -6,7 +6,6 @@ /*global Java, base, module, exports, require, __FILE__*/ var Bukkit = MServer; -// noinspection JSUnresolvedVariable var Server = Bukkit.server; var PluginManager = Server.pluginManager; exports.$ = Bukkit; diff --git a/src/main/resources/modules/bukkit/task.js b/src/main/resources/internal/bukkit/task.js similarity index 100% rename from src/main/resources/modules/bukkit/task.js rename to src/main/resources/internal/bukkit/task.js diff --git a/src/main/resources/modules/sponge/command.js b/src/main/resources/internal/sponge/command.js similarity index 100% rename from src/main/resources/modules/sponge/command.js rename to src/main/resources/internal/sponge/command.js diff --git a/src/main/resources/modules/sponge/server.js b/src/main/resources/internal/sponge/server.js similarity index 96% rename from src/main/resources/modules/sponge/server.js rename to src/main/resources/internal/sponge/server.js index 292df6f..4707729 100644 --- a/src/main/resources/modules/sponge/server.js +++ b/src/main/resources/internal/sponge/server.js @@ -6,7 +6,6 @@ /*global Java, base, module, exports, require, __FILE__*/ var Sponge = MServer; -// noinspection JSUnresolvedVariable var Server = Sponge.server; var PluginManager = Sponge.pluginManager; exports.$ = Sponge; diff --git a/src/main/resources/modules/sponge/task.js b/src/main/resources/internal/sponge/task.js similarity index 100% rename from src/main/resources/modules/sponge/task.js rename to src/main/resources/internal/sponge/task.js diff --git a/src/main/resources/modules/base64.js b/src/main/resources/modules/base64.js new file mode 100644 index 0000000..0c98bba --- /dev/null +++ b/src/main/resources/modules/base64.js @@ -0,0 +1,225 @@ +/* + * $Id: base64.js,v 2.15 2014/04/05 12:58:57 dankogai Exp dankogai $ + * + * Licensed under the BSD 3-Clause License. + * http://opensource.org/licenses/BSD-3-Clause + * + * References: + * http://en.wikipedia.org/wiki/Base64 + */ + +(function (global) { + 'use strict'; + // existing version for noConflict() + var _Base64 = global.Base64; + var version = "2.3.2"; + // if node.js, we use Buffer + var buffer; + if (typeof module !== 'undefined' && module.exports) { + try { + buffer = require('buffer').Buffer; + } catch (err) { + } + } + // constants + var b64chars + = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; + var b64tab = function (bin) { + var t = {}; + for (var i = 0, l = bin.length; i < l; i++) t[bin.charAt(i)] = i; + return t; + }(b64chars); + var fromCharCode = String.fromCharCode; + // encoder stuff + var cb_utob = function (c) { + if (c.length < 2) { + var cc = c.charCodeAt(0); + return cc < 0x80 ? c + : cc < 0x800 ? (fromCharCode(0xc0 | (cc >>> 6)) + + fromCharCode(0x80 | (cc & 0x3f))) + : (fromCharCode(0xe0 | ((cc >>> 12) & 0x0f)) + + fromCharCode(0x80 | ((cc >>> 6) & 0x3f)) + + fromCharCode(0x80 | ( cc & 0x3f))); + } else { + var ccc = 0x10000 + + (c.charCodeAt(0) - 0xD800) * 0x400 + + (c.charCodeAt(1) - 0xDC00); + return (fromCharCode(0xf0 | ((ccc >>> 18) & 0x07)) + + fromCharCode(0x80 | ((ccc >>> 12) & 0x3f)) + + fromCharCode(0x80 | ((ccc >>> 6) & 0x3f)) + + fromCharCode(0x80 | ( ccc & 0x3f))); + } + }; + var re_utob = /[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g; + var utob = function (u) { + return u.replace(re_utob, cb_utob); + }; + var cb_encode = function (ccc) { + var padlen = [0, 2, 1][ccc.length % 3], + ord = ccc.charCodeAt(0) << 16 + | ((ccc.length > 1 ? ccc.charCodeAt(1) : 0) << 8) + | ((ccc.length > 2 ? ccc.charCodeAt(2) : 0)), + chars = [ + b64chars.charAt(ord >>> 18), + b64chars.charAt((ord >>> 12) & 63), + padlen >= 2 ? '=' : b64chars.charAt((ord >>> 6) & 63), + padlen >= 1 ? '=' : b64chars.charAt(ord & 63) + ]; + return chars.join(''); + }; + var btoa = global.btoa ? function (b) { + return global.btoa(b); + } : function (b) { + return b.replace(/[\s\S]{1,3}/g, cb_encode); + }; + var _encode = buffer ? + buffer.from && buffer.from !== Uint8Array.from ? function (u) { + return (u.constructor === buffer.constructor ? u : buffer.from(u)) + .toString('base64') + } + : function (u) { + return (u.constructor === buffer.constructor ? u : new buffer(u)) + .toString('base64') + } + : function (u) { + return btoa(utob(u)) + } + ; + var encode = function (u, urisafe) { + return !urisafe + ? _encode(String(u)) + : _encode(String(u)).replace(/[+\/]/g, function (m0) { + return m0 === '+' ? '-' : '_'; + }).replace(/=/g, ''); + }; + var encodeURI = function (u) { + return encode(u, true) + }; + // decoder stuff + var re_btou = new RegExp([ + '[\xC0-\xDF][\x80-\xBF]', + '[\xE0-\xEF][\x80-\xBF]{2}', + '[\xF0-\xF7][\x80-\xBF]{3}' + ].join('|'), 'g'); + var cb_btou = function (cccc) { + switch (cccc.length) { + case 4: + var cp = ((0x07 & cccc.charCodeAt(0)) << 18) + | ((0x3f & cccc.charCodeAt(1)) << 12) + | ((0x3f & cccc.charCodeAt(2)) << 6) + | (0x3f & cccc.charCodeAt(3)), + offset = cp - 0x10000; + return (fromCharCode((offset >>> 10) + 0xD800) + + fromCharCode((offset & 0x3FF) + 0xDC00)); + case 3: + return fromCharCode( + ((0x0f & cccc.charCodeAt(0)) << 12) + | ((0x3f & cccc.charCodeAt(1)) << 6) + | (0x3f & cccc.charCodeAt(2)) + ); + default: + return fromCharCode( + ((0x1f & cccc.charCodeAt(0)) << 6) + | (0x3f & cccc.charCodeAt(1)) + ); + } + }; + var btou = function (b) { + return b.replace(re_btou, cb_btou); + }; + var cb_decode = function (cccc) { + var len = cccc.length, + padlen = len % 4, + n = (len > 0 ? b64tab[cccc.charAt(0)] << 18 : 0) + | (len > 1 ? b64tab[cccc.charAt(1)] << 12 : 0) + | (len > 2 ? b64tab[cccc.charAt(2)] << 6 : 0) + | (len > 3 ? b64tab[cccc.charAt(3)] : 0), + chars = [ + fromCharCode(n >>> 16), + fromCharCode((n >>> 8) & 0xff), + fromCharCode(n & 0xff) + ]; + chars.length -= [0, 0, 2, 1][padlen]; + return chars.join(''); + }; + var atob = global.atob ? function (a) { + return global.atob(a); + } : function (a) { + return a.replace(/[\s\S]{1,4}/g, cb_decode); + }; + var _decode = buffer ? + buffer.from && buffer.from !== Uint8Array.from ? function (a) { + return (a.constructor === buffer.constructor + ? a : buffer.from(a, 'base64')).toString(); + } + : function (a) { + return (a.constructor === buffer.constructor + ? a : new buffer(a, 'base64')).toString(); + } + : function (a) { + return btou(atob(a)) + }; + var decode = function (a) { + return _decode( + String(a).replace(/[-_]/g, function (m0) { + return m0 === '-' ? '+' : '/' + }) + .replace(/[^A-Za-z0-9+\/]/g, '') + ); + }; + var noConflict = function () { + var Base64 = global.Base64; + global.Base64 = _Base64; + return Base64; + }; + // export Base64 + global.Base64 = { + VERSION: version, + atob: atob, + btoa: btoa, + fromBase64: decode, + toBase64: encode, + utob: utob, + encode: encode, + encodeURI: encodeURI, + btou: btou, + decode: decode, + noConflict: noConflict + }; + // if ES5 is available, make Base64.extendString() available + if (typeof Object.defineProperty === 'function') { + var noEnum = function (v) { + return {value: v, enumerable: false, writable: true, configurable: true}; + }; + global.Base64.extendString = function () { + Object.defineProperty( + String.prototype, 'fromBase64', noEnum(function () { + return decode(this) + })); + Object.defineProperty( + String.prototype, 'toBase64', noEnum(function (urisafe) { + return encode(this, urisafe) + })); + Object.defineProperty( + String.prototype, 'toBase64URI', noEnum(function () { + return encode(this, true) + })); + }; + } + // module.exports and AMD are mutually exclusive. + // module.exports has precedence. + if (typeof module !== 'undefined' && module.exports) { + module.exports = global.Base64; + } + else if (typeof define === 'function' && define.amd) { + // AMD. Register as an anonymous module. + define([], function () { + return global.Base64 + }); + } + // that's it! +})(typeof self !== 'undefined' ? self + : typeof window !== 'undefined' ? window + : typeof global !== 'undefined' ? global + : this + ); \ No newline at end of file diff --git a/src/main/resources/modules/bukkit/console.js b/src/main/resources/modules/bukkit/console.js deleted file mode 100644 index 7d2d4be..0000000 --- a/src/main/resources/modules/bukkit/console.js +++ /dev/null @@ -1,19 +0,0 @@ -var BukkitConsole = { - createNew: function () { - var console = Console.createNew(); - console.sender = function () { - var sender = arguments[0]; - if (!(sender instanceof org.bukkit.command.CommandSender)) { - this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!") - } - var args = Array.prototype.slice.call(arguments, 1); - sender.sendMessage(console.prefix + args.join(' ')); - }; - console.console = function () { - this.sender(MServer.consoleSender, Array.prototype.join.call(arguments, ' ')); - }; - return console; - } -} -global.Console = BukkitConsole; -exports = global.Console; \ No newline at end of file diff --git a/src/main/resources/modules/command.js b/src/main/resources/modules/command.js deleted file mode 100644 index 9057bd4..0000000 --- a/src/main/resources/modules/command.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./mserver').command; \ No newline at end of file diff --git a/src/main/resources/modules/event.js b/src/main/resources/modules/event.js deleted file mode 100644 index fb1bdbd..0000000 --- a/src/main/resources/modules/event.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./mserver').event; \ No newline at end of file diff --git a/src/main/resources/modules/mserver.js b/src/main/resources/modules/mserver.js deleted file mode 100644 index 2f6d478..0000000 --- a/src/main/resources/modules/mserver.js +++ /dev/null @@ -1,8 +0,0 @@ -module.exports = { - command: require('./' + DetectServerType + '/command'), - event: require('./' + DetectServerType + '/event'), - permission: require('./' + DetectServerType + '/permission'), - plugin: require('./' + DetectServerType + '/plugin'), - server: require('./' + DetectServerType + '/server'), - task: require('./' + DetectServerType + '/task') -} \ No newline at end of file diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js deleted file mode 100644 index bcc7a02..0000000 --- a/src/main/resources/modules/plugin.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./mserver').plugin; \ No newline at end of file diff --git a/src/main/resources/core/reflect.js b/src/main/resources/modules/reflect.js similarity index 99% rename from src/main/resources/core/reflect.js rename to src/main/resources/modules/reflect.js index 0fb7d12..19544fb 100644 --- a/src/main/resources/core/reflect.js +++ b/src/main/resources/modules/reflect.js @@ -67,8 +67,8 @@ function Reflect(obj) { accessible(this.class.getDeclaredField(name)).set(this.obj, value); } return this; - } - + }; + this.create = function () { var param = Array.prototype.slice.call(arguments); return on(declaredConstructor(this.class, param).newInstance(param)); diff --git a/src/main/resources/modules/server.js b/src/main/resources/modules/server.js deleted file mode 100644 index de1c408..0000000 --- a/src/main/resources/modules/server.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./mserver').server; \ No newline at end of file diff --git a/src/main/resources/modules/sponge/console.js b/src/main/resources/modules/sponge/console.js deleted file mode 100644 index 7a39b07..0000000 --- a/src/main/resources/modules/sponge/console.js +++ /dev/null @@ -1,23 +0,0 @@ -var SpongeConsole = { - createNew: function () { - var console = Console.createNew(); - console.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 无法发送消息!") - } - var args = Array.prototype.slice.call(arguments, 1); - sender.sendMessage(Text.of(console.prefix + args.join(' '))); - }; - console.console = function () { - this.sender(MServer.server.console, Array.prototype.join.call(arguments, ' ')); - }; - console.warn = function () { - log.warn(this.name + Array.prototype.join.call(arguments, ' ')); - }; - return console; - } -} -global.Console = SpongeConsole; -exports = global.Console; \ No newline at end of file diff --git a/src/main/resources/modules/task.js b/src/main/resources/modules/task.js deleted file mode 100644 index 72b5928..0000000 --- a/src/main/resources/modules/task.js +++ /dev/null @@ -1 +0,0 @@ -module.exports = require('./mserver').task; \ No newline at end of file diff --git a/src/main/resources/core/zip.js b/src/main/resources/modules/zip.js similarity index 97% rename from src/main/resources/core/zip.js rename to src/main/resources/modules/zip.js index c114c99..bb9eef8 100644 --- a/src/main/resources/core/zip.js +++ b/src/main/resources/modules/zip.js @@ -3,7 +3,7 @@ /*global Java, base, module, exports, require, __FILE__*/ var ZipFile = Java.type("java.util.zip.ZipFile"); -var fs = require('fs'); +var fs = require('core/fs'); /** * 解压文件 diff --git a/src/main/resources/plugins/HelloWorld.js b/src/main/resources/plugins/HelloWorld.js index 3d9b10e..548dd28 100644 --- a/src/main/resources/plugins/HelloWorld.js +++ b/src/main/resources/plugins/HelloWorld.js @@ -4,19 +4,19 @@ */ /*global Java, base, module, exports, require*/ -var event = require('modules/event'); -var command = require('modules/command'); +var event = require('api/event'); +var command = require('api/command'); var papi = require('./ext/papi'); var join; var description = { name: 'HelloWorld', version: '1.0', - //commands: { - // 'hello': { - // description: 'HelloWorld主命令' - // } - //} + commands: { + 'hello': { + description: 'HelloWorld主命令' + } + } }; function load() { diff --git a/src/main/resources/plugins/MiaoTag.js b/src/main/resources/plugins/MiaoTag.js index b2711d5..b08bbdd 100644 --- a/src/main/resources/plugins/MiaoTag.js +++ b/src/main/resources/plugins/MiaoTag.js @@ -4,13 +4,9 @@ * 可兼容任何记分板 */ /*global Java, base, module, exports, require*/ - -var fs = require('core/fs'); -var event = require('modules/event'); -var bukkit = require('modules/server'); -var command = require('modules/command'); - -var papi = require('./ext/papi'); +var event = require('api/event'); +var bukkit = require('api/server'); +var command = require('api/command'); var fakeTag; @@ -25,7 +21,7 @@ var description = { 'mtag': { description: 'MiaoTag主命令', usage: '', - permission: 'MiaoTag.admin', + permission: 'MiaoTag.admin' } }, permissions: { @@ -86,8 +82,6 @@ function disable() { function FakeTag(name) { // NMS CLASS - var Scoreboard = bukkit.nmsCls('Scoreboard'); - var ScoreboardScore = bukkit.nmsCls('ScoreboardScore'); var ScoreboardBaseCriteria = bukkit.nmsCls('ScoreboardBaseCriteria'); var PacketPlayOutScoreboardScore = bukkit.nmsCls('PacketPlayOutScoreboardScore'); var PacketPlayOutScoreboardObjective = bukkit.nmsCls('PacketPlayOutScoreboardObjective'); diff --git a/src/main/resources/plugins/ext/papi.js b/src/main/resources/plugins/ext/papi.js index 9fda132..37c9670 100644 --- a/src/main/resources/plugins/ext/papi.js +++ b/src/main/resources/plugins/ext/papi.js @@ -4,7 +4,7 @@ */ /*global Java, base, module, exports, require, __FILE__*/ var PlaceholderAPI; -var bukkit = require('modules/server'); +var bukkit = require('api/server'); if (bukkit.plugin.load("PlaceholderAPI")) { PlaceholderAPI = ext.getStatic("me.clip.placeholderapi.PlaceholderAPI"); } else {