From 9556008bee3eabcc664eb0274b21b68eded1bf7a Mon Sep 17 00:00:00 2001 From: 502647092 Date: Sat, 23 Sep 2017 18:30:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=BC=93=E5=AD=98=E7=BC=96=E8=AF=91?= =?UTF-8?q?=E7=BB=93=E6=9E=9C=20=E4=BD=BF=E7=94=A8load=E8=BD=BD=E5=85=A5?= =?UTF-8?q?=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 502647092 --- .gitignore | 4 ++- .../java/pw/yumc/MiaoScript/MiaoScript.java | 1 - src/main/resources/bios.js | 7 ++++- src/main/resources/core/fs.js | 6 +++- src/main/resources/core/init.js | 6 ++-- src/main/resources/core/require.js | 28 +++++++++---------- src/main/resources/modules/plugin.js | 3 +- 7 files changed, 32 insertions(+), 23 deletions(-) diff --git a/.gitignore b/.gitignore index 043fc2a..ae3dc93 100644 --- a/.gitignore +++ b/.gitignore @@ -35,4 +35,6 @@ /src/main/resources/Soulbound # Atlassian Stuff -/atlassian-ide-plugin.xml \ No newline at end of file +/atlassian-ide-plugin.xml + +/src/main/resources/cache/ \ No newline at end of file diff --git a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java index 8d49de7..c63ca6a 100644 --- a/src/main/java/pw/yumc/MiaoScript/MiaoScript.java +++ b/src/main/java/pw/yumc/MiaoScript/MiaoScript.java @@ -57,7 +57,6 @@ public class MiaoScript extends JavaPlugin { this.engine.eval(new InputStreamReader(this.getResource("bios.js"))); engine.invokeFunction("boot", this); } catch (Exception e) { - Log.w("脚本引擎初始化失败! %s:%s", e.getClass().getName(), e.getMessage()); Log.d(e); } finally { currentThread.setContextClassLoader(previousClassLoader); diff --git a/src/main/resources/bios.js b/src/main/resources/bios.js index 51ae079..5a7994a 100644 --- a/src/main/resources/bios.js +++ b/src/main/resources/bios.js @@ -13,7 +13,12 @@ var disable; root = plugin.dataFolder.canonicalPath; } load(root + '/core/init.js'); - init(root, plugin); + try { + init(root, plugin); + } catch (ex) { + log.w("MiaoScript 初始化失败! %s", ex); + throw ex; + } disable = disablePlugins }; })(); \ No newline at end of file diff --git a/src/main/resources/core/fs.js b/src/main/resources/core/fs.js index 83cd28b..e0f015e 100644 --- a/src/main/resources/core/fs.js +++ b/src/main/resources/core/fs.js @@ -13,6 +13,9 @@ var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption"); * @returns {*} */ exports.file = function () { + if (!arguments[0]) { + log.w("文件名称不得为 undefined 或者 null !"); + } if (exports.canonical(arguments[0])) { return arguments[0]; } @@ -28,7 +31,8 @@ exports.file = function () { * @param file */ exports.mkdirs = function (file) { - file.getParentFile().mkdirs(); + // noinspection JSUnresolvedVariable + file.parentFile.mkdirs(); }; /** * 创建文件 diff --git a/src/main/resources/core/init.js b/src/main/resources/core/init.js index 7f76a8a..9dbabf5 100644 --- a/src/main/resources/core/init.js +++ b/src/main/resources/core/init.js @@ -47,8 +47,10 @@ function loadPlugins(plugin) { // 初始化本体插件 var self = require('modules/plugin'); self.init(plugin, plugins_dir); - self.load(); - self.enable(); + if (!self.$) { + self.load(); + self.enable(); + } } // noinspection JSUnusedLocalSymbols diff --git a/src/main/resources/core/require.js b/src/main/resources/core/require.js index ff3db39..c01af1c 100644 --- a/src/main/resources/core/require.js +++ b/src/main/resources/core/require.js @@ -8,6 +8,7 @@ var File = Java.type("java.io.File"); var Files = Java.type("java.nio.file.Files"); var String = Java.type("java.lang.String"); + var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption"); /** * 解析模块名称为文件 @@ -48,26 +49,20 @@ log.w("模块目录: %s", _canonical(moduleFile)); } - /** - * 使用NIO读取文件内容 - * @param file 文件 - * @private - */ - function _readFile(file) { - // noinspection JSPrimitiveTypeWrapperUsage - return new String(Files.readAllBytes(file.toPath()), "UTF-8"); - } - /** * 预编译模块 + * @param name * @param src * @returns {Object} */ - function compileJs(src) { + function compileJs(name, src) { var head = "(function (module, exports, require) {\n"; var tail = "\n});"; var fulljs = head + src + tail; - return eval(fulljs); + var cacheFile = cacheDir + "/" + name + ".js"; + log.d(cacheFile); + base.save(cacheFile, fulljs); + return load(cacheFile); } /** @@ -104,16 +99,17 @@ exports: {}, require: exports(file.parentFile) }; - var src = _readFile(file); + var src = base.read(file); try { // 预编译模块 - var compiledWrapper = compileJs(src); + var compiledWrapper = compileJs(name, src); compiledWrapper.apply(module.exports, [ module, module.exports, module.require ]); } catch (ex) { log.w("模块 %s 编译失败!", name); - log.w(ex); + log.d(ex); + return; } log.d('模块 %s 编译成功!', name); module.loaded = true; @@ -132,6 +128,8 @@ }; } + var cacheDir = parent + "/cache"; + // 等于 undefined 说明 parent 是一个字符串 需要转成File // 可能更加准确的方案 if (_canonical(parent) === undefined) { diff --git a/src/main/resources/modules/plugin.js b/src/main/resources/modules/plugin.js index 67be00c..18fa71a 100644 --- a/src/main/resources/modules/plugin.js +++ b/src/main/resources/modules/plugin.js @@ -11,7 +11,7 @@ var fs = require('core/fs'); function loadPlugins(path) { path = fs.file(path); log.i("开始扫描 %s 下的插件...", path); - updatePlugins(); + updatePlugins(path); var files = []; fs.list(path).forEach(function (file) { files.push(file.toFile()); @@ -54,7 +54,6 @@ function loadJsPlugin(files) { return file.name.endsWith(".js") }).forEach(function (file) { var p = require(file); - log.d(JSON.stringify(p)); if (!p.description || !p.description.name) { log.w("文件 %s 不存在 description 描述信息 无法加载插件!"); } else {