feat: 添加类加载器异常检查 核心类去除patch的方法

This commit is contained in:
coding 2017-11-15 10:40:17 +00:00
parent c307fc1ae3
commit fad15751dc
4 changed files with 52 additions and 36 deletions

View File

@ -1,49 +1,62 @@
'use strict';
var log;
var boot;
var loader;
var disable;
/**
* 初始化框架引擎
*/
(function () {
boot = function (root, logger) {
log = logger;
// 开发环境下初始化
root = root || "src/main/resources";
var debug = false;
if (__FILE__ !== "<eval>") {
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
debug = true;
}
// 检查类加载器 防止找不到核心文件
loader = checkClassLoader();
// 解压文件到根目录 非调试模式直接从jar解压覆盖
release(root, "[api|core|internal|modules]/.*", !debug);
release(root, "plugins/.*");
load(root + '/core/init.js');
// 初始化必须在load之后 不然global找不到
global.debug = debug;
try {
log = logger;
// 开发环境下初始化
root = root || "src/main/resources";
// 解压文件到根目录
release(root, "[api|core|internal|modules]/.*", true);
release(root, "plugins/.*");
load(root + '/core/init.js');
if (__FILE__ !== "<eval>") {
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
global.debug = true;
}
init(root);
} catch (ex) {
if (console && console.ex) {
console.ex(ex);
} else {
ex.printStackTrace();
throw ex;
}
ex.printStackTrace();
} finally {
disable = disablePlugins;
disable = function () {
if (disablePlugins && typeof(disablePlugins) === "function") {
disablePlugins();
}
}
}
};
var pluginYml;
function checkClassLoader(){
var classLoader = java.lang.Thread.currentThread().getContextClassLoader();
pluginYml = classLoader.getResource("plugin.yml");
if (pluginYml === null) {
log.info("==================== ERROR ====================");
log.info("异常的类加载器: " + classLoader.class.name);
log.info("==================== ERROR ====================");
throw Error('MiaoScript核心类库初始化失败 异常的类加载器!');
}
return classLoader;
}
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;
}
var upath = url.getFile().substring(url.getFile().indexOf("/") + 1);
var upath = pluginYml.getFile().substring(pluginYml.getFile().indexOf("/") + 1);
var jarPath = java.net.URLDecoder.decode(upath.substring(0, upath.indexOf('!')));
if (!Files.exists(Paths.get(jarPath))) {
jarPath = "/" + jarPath;
@ -65,7 +78,6 @@ var disable;
}
})
} catch (ex) {
} finally {
}
}
})();

View File

@ -41,9 +41,9 @@
}
track.forEach(function (stack) {
if (stack.className.startsWith('<')) {
this.console(' §e位于 §c%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber));
} else {
this.console(' §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber));
this.console(' §e位于§c', stack.fileName, '=>§c', stack.methodName, '§4行', stack.lineNumber);
} else {// %s.%s(§4%s:%s§c)
this.console(' §e位于§c', stack.className + '.' + stack.methodName + '(§4' + stack.fileName + ':' + stack.lineNumber + '§c)');
}
}.bind(this));
};

View File

@ -77,7 +77,7 @@ exports.copy = function (inputStream, target, override) {
exports.read = function (file) {
file = exports.file(file);
if (!file.exists()) {
console.warn("读取文件 %s 错误 文件不存在!".format(file));
console.warn('读取文件', file, '错误 文件不存在!');
return;
}
// noinspection JSPrimitiveTypeWrapperUsage
@ -103,7 +103,7 @@ exports.list = function (path) {
if (dir.isDirectory()) {
return Files.list(dir.toPath());
}
console.warn("路径 %s 不是一个目录 返回空数组!".format(path));
console.warn('路径', path, '不是一个目录 返回空数组!');
return [];
};
/**

View File

@ -113,7 +113,11 @@
base.save(cacheFile, "(function (module, exports, require, __dirname, __filename) {" + origin + "});");
// 使用 load 可以保留行号和文件名称
var obj = load(cacheFile);
base.delete(cacheFile);
try {
base.delete(cacheFile);
} catch (ex) {
cacheFile.deleteOnExit();
}
return obj;
}
@ -126,7 +130,7 @@
* @returns {Object}
*/
function compileModule(id, name, file, optional) {
console.debug('加载模块 %s 位于 %s Optional %s'.format(name, id, optional.toJson()));
console.debug('加载模块', name, '位于', id, 'Optional', JSON.stringify(optional));
// noinspection JSUnresolvedVariable
var module = {
id: id,
@ -139,10 +143,10 @@
compiledWrapper.apply(module.exports, [
module, module.exports, module.require, file.parentFile, file
]);
console.debug('模块 %s 编译成功!'.format(name));
console.debug('模块', name, '编译成功!');
module.loaded = true;
} catch (ex) {
console.console("§4警告! §c模块 §a%s §c编译失败! §4ERR: %s".format(name, ex));
console.console('§4警告! §c模块§a', name, '§c编译失败! §4ERR:', ex);
console.ex(ex);
}
return module;
@ -179,7 +183,7 @@
var file = new File(name);
file = _isFile(file) ? file : resolve(name, path);
if (file === undefined) {
console.console("§c模块 §a%s §c加载失败! §4未找到该模块!".format(name));
console.console('§c模块§a', name, '§c加载失败! §4未找到该模块!');
return {exports: {}};
}
if (!optional) optional = {cache: true};