MiaoScript/src/main/resources/bios.js

71 lines
2.4 KiB
JavaScript
Raw Normal View History

'use strict';
var log;
var boot;
var disable;
/**
* 初始化框架引擎
*/
(function () {
boot = function (root, logger) {
try {
2017-10-30 03:17:42 +00:00
log = logger;
// 开发环境下初始化
root = root || "src/main/resources";
// 解压文件到根目录
release(root, "[api|core|internal|modules]/.*", true);
2017-10-30 03:17:42 +00:00
release(root, "plugins/.*");
load(root + '/core/init.js');
if (__FILE__ !== "<eval>") {
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
global.debug = true;
}
init(root);
} catch (ex) {
2017-10-30 03:17:42 +00:00
if (console && console.ex) {
console.ex(ex);
2017-10-30 03:17:42 +00:00
} else {
ex.printStackTrace();
throw ex;
}
} finally {
2017-10-30 03:17:42 +00:00
disable = disablePlugins;
}
};
function release(root, regex, replace) {
var Files = Java.type("java.nio.file.Files");
var Paths = Java.type("java.nio.file.Paths");
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
var classLoader = java.lang.Thread.currentThread().getContextClassLoader();
var url = classLoader.getResource("plugin.yml");
if (url === null) {
return;
}
2017-10-30 03:17:42 +00:00
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;
}
2017-10-30 03:17:42 +00:00
try {
var jar = new java.util.jar.JarFile(jarPath);
var r = new RegExp(regex);// "[core|modules]/.*"
jar.stream().forEach(function (entry) {
if (!entry.isDirectory()) {
if (r.test(entry.name)) {
var path = Paths.get(root, entry.name);
var parentFile = path.toFile().parentFile;
if (!parentFile.exists()) {
parentFile.mkdirs();
}
Files.copy(classLoader.getResourceAsStream(entry.name), path, StandardCopyOption[replace ? 'REPLACE_EXISTING' : 'ATOMIC_MOVE']);
}
}
})
} catch (ex) {
} finally {
}
}
})();