From 96f93754e6279ce67f2c38dd5ed002b3ac85166e Mon Sep 17 00:00:00 2001 From: coding Date: Tue, 9 Jan 2018 12:19:02 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96API=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8Dhttp=E6=A8=A1=E5=9D=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/api/event.js | 6 ++-- src/main/resources/api/plugin.js | 45 +++++++++++++----------------- src/main/resources/modules/http.js | 14 ++++++---- 3 files changed, 30 insertions(+), 35 deletions(-) diff --git a/src/main/resources/api/event.js b/src/main/resources/api/event.js index f6a5257..7550610 100644 --- a/src/main/resources/api/event.js +++ b/src/main/resources/api/event.js @@ -111,8 +111,8 @@ function EventHandlerDefault() { * @param ignoreCancel */ this.listen = function listen(jsp, event, exec, priority, ignoreCancel) { + if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!'); var name = jsp.description.name; - if (ext.isNull(name)) throw new TypeError('插件名称为空 请检查传入参数!'); var eventCls = this.name2Class(name, event); if (!eventCls) { return; } if (typeof priority === 'boolean') { @@ -128,7 +128,7 @@ function EventHandlerDefault() { if (!listenerMap[name]) listenerMap[name] = []; var offExec = function () { this.unregister(eventCls, listener); - console.debug('插件 %s 注销事件 %s'.format(name, this.class2Name(eventCls))); + console.debug('插件 %s 注销事件 %s'.format(name, eventCls.name.substring(eventCls.name.lastIndexOf(".") + 1))); }.bind(this); var off = { event: eventCls, @@ -153,4 +153,4 @@ module.exports = { delete EventHandler.listenerMap[jsp.description.name]; } } -}; \ No newline at end of file +}; diff --git a/src/main/resources/api/plugin.js b/src/main/resources/api/plugin.js index 1458870..a423360 100644 --- a/src/main/resources/api/plugin.js +++ b/src/main/resources/api/plugin.js @@ -2,7 +2,7 @@ /** * MiaoScript脚本插件加载类 */ -/*global Java, base, module, exports, require, __FILE__*/ +/*global Java, module, exports, require, __FILE__*/ // var zip = require("core/zip"); var fs = require('core/fs'); var yaml = require('modules/yaml'); @@ -87,7 +87,7 @@ function loadPlugin(file) { hook: function (origin) { return beforeLoadHook(origin); } - }); + }) console.debug("插件编译结果: %s".format(JSON.stringify(plugin))); var desc = plugin.description; if (!desc || !desc.name) { @@ -127,16 +127,15 @@ function initPlugin(file, plugin) { // 初始化 __FILE__ plugin.__FILE__ = file; // 初始化 __DATA__ - plugin.__DATA__ = fs.file(file.parentFile, plugin.description.name); + plugin.__DATA__ = plugin.dataFolder = fs.file(file.parentFile, plugin.description.name); // 初始化 getDataFolder() - plugin.getDataFolder = function () { + plugin.getDataFolder = function getDataFolder() { return plugin.__DATA__; - }; + } // 初始化 getFile() - plugin.getFile = function (name) { + plugin.getFile = plugin.file = function getFile(name) { return fs.file(plugin.getDataFolder(), name); }; - // 初始化插件配置相关方法 initPluginConfig(plugin); @@ -164,9 +163,9 @@ function initPluginConfig(plugin) { if (!file.isFile) { file = plugin.getFile(file); } - return yaml.safeLoad(base.read(file)); + return yaml.safeLoad(fs.read(file), { json: true }); } - }; + } /** * 重载配置文件 * @constructor @@ -174,7 +173,7 @@ function initPluginConfig(plugin) { */ plugin.reloadConfig = function () { plugin.config = plugin.getConfig(plugin.configFile); - }; + } /** * 保存配置文件 * @constructor @@ -184,13 +183,13 @@ function initPluginConfig(plugin) { switch (arguments.length) { case 0: plugin.configFile.parentFile.mkdirs(); - base.save(plugin.configFile, yaml.safeDump(plugin.config)); + fs.save(plugin.configFile, yaml.safeDump(plugin.config)); break; case 2: - base.save(arguments[0], yaml.safeDump(arguments[1])); + fs.save(fs.file(plugin.__DATA__, arguments[0]), yaml.safeDump(arguments[1])); break; } - }; + } if (plugin.configFile.isFile()) { plugin.config = plugin.getConfig('config.yml'); } else if (plugin.description.config) { @@ -199,10 +198,6 @@ function initPluginConfig(plugin) { } } -function runAndCatch(jsp, name, ext) { - -} - function checkAndGet(args) { if (args.length === 0) { return plugins; @@ -223,15 +218,13 @@ function checkAndRun(args, name, ext) { for (var i in pls) { var jsp = pls[i]; var exec = jsp[name]; - if (exec) { - try { - // 绑定方法的this到插件自身 - exec.bind(jsp)(); - if (ext) ext.bind(jsp)(); - } catch (ex) { - console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message)); - console.ex(ex); - } + try { + // 绑定方法的this到插件自身 + if (typeof exec === "function") exec.call(jsp); + if (ext) ext.call(jsp); + } catch (ex) { + console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message)); + console.ex(ex); } } } diff --git a/src/main/resources/modules/http.js b/src/main/resources/modules/http.js index b883cab..a90bc93 100644 --- a/src/main/resources/modules/http.js +++ b/src/main/resources/modules/http.js @@ -47,7 +47,7 @@ function open(url, method, header) { var conn = new URL(url).openConnection(); if (conn instanceof HttpsURLConnection) { conn.setHostnameVerifier(TrustAnyHostnameVerifier); - conn.setSSLSocketFactory(TrustAnyHostnameVerifier); + conn.setSSLSocketFactory(SSLSocketFactory); } conn.setRequestMethod(method); conn.setDoOutput(true); @@ -81,10 +81,12 @@ function request(url, method, header, params, body) { var conn = open(buildUrl(url, params), method, header); try { conn.connect(); - var out = conn.getOutputStream(); - out.write(new String(body).getBytes(config.Charset)); - out.flush(); - out.close(); + if (body) { + var out = conn.getOutputStream(); + out.write(new String(body).getBytes(config.Charset)); + out.flush(); + out.close(); + } return response(conn); } finally { conn.disconnect(); @@ -110,4 +112,4 @@ var http = { } }) -exports = module.exports = http; \ No newline at end of file +exports = module.exports = http;