feat: 添加类加载器异常检查 核心类去除patch的方法
This commit is contained in:
		@@ -1,49 +1,62 @@
 | 
				
			|||||||
'use strict';
 | 
					'use strict';
 | 
				
			||||||
var log;
 | 
					var log;
 | 
				
			||||||
var boot;
 | 
					var boot;
 | 
				
			||||||
 | 
					var loader;
 | 
				
			||||||
var disable;
 | 
					var disable;
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 * 初始化框架引擎
 | 
					 * 初始化框架引擎
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
(function () {
 | 
					(function () {
 | 
				
			||||||
    boot = function (root, logger) {
 | 
					    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 {
 | 
					        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);
 | 
					            init(root);
 | 
				
			||||||
        } catch (ex) {
 | 
					        } catch (ex) {
 | 
				
			||||||
            if (console && console.ex) {
 | 
					            ex.printStackTrace();
 | 
				
			||||||
                console.ex(ex);
 | 
					 | 
				
			||||||
            } else {
 | 
					 | 
				
			||||||
                ex.printStackTrace();
 | 
					 | 
				
			||||||
                throw ex;
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        } finally {
 | 
					        } 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) {
 | 
					    function release(root, regex, replace) {
 | 
				
			||||||
        var Files = Java.type("java.nio.file.Files");
 | 
					        var Files = Java.type("java.nio.file.Files");
 | 
				
			||||||
        var Paths = Java.type("java.nio.file.Paths");
 | 
					        var Paths = Java.type("java.nio.file.Paths");
 | 
				
			||||||
        var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
 | 
					        var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        var classLoader = java.lang.Thread.currentThread().getContextClassLoader();
 | 
					        var upath = pluginYml.getFile().substring(pluginYml.getFile().indexOf("/") + 1);
 | 
				
			||||||
        var url = classLoader.getResource("plugin.yml");
 | 
					 | 
				
			||||||
        if (url === null) {
 | 
					 | 
				
			||||||
            return;
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        var upath = url.getFile().substring(url.getFile().indexOf("/") + 1);
 | 
					 | 
				
			||||||
        var jarPath = java.net.URLDecoder.decode(upath.substring(0, upath.indexOf('!')));
 | 
					        var jarPath = java.net.URLDecoder.decode(upath.substring(0, upath.indexOf('!')));
 | 
				
			||||||
        if (!Files.exists(Paths.get(jarPath))) {
 | 
					        if (!Files.exists(Paths.get(jarPath))) {
 | 
				
			||||||
            jarPath = "/" + jarPath;
 | 
					            jarPath = "/" + jarPath;
 | 
				
			||||||
@@ -65,7 +78,6 @@ var disable;
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
            })
 | 
					            })
 | 
				
			||||||
        } catch (ex) {
 | 
					        } catch (ex) {
 | 
				
			||||||
        } finally {
 | 
					 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
})();
 | 
					})();
 | 
				
			||||||
@@ -41,9 +41,9 @@
 | 
				
			|||||||
                }
 | 
					                }
 | 
				
			||||||
                track.forEach(function (stack) {
 | 
					                track.forEach(function (stack) {
 | 
				
			||||||
                    if (stack.className.startsWith('<')) {
 | 
					                    if (stack.className.startsWith('<')) {
 | 
				
			||||||
                        this.console('    §e位于 §c%s => §c%s §4行%s'.format(stack.fileName, stack.methodName, stack.lineNumber));
 | 
					                        this.console('    §e位于§c', stack.fileName, '=>§c', stack.methodName, '§4行', stack.lineNumber);
 | 
				
			||||||
                    } else {
 | 
					                    } else {// %s.%s(§4%s:%s§c)
 | 
				
			||||||
                        this.console('    §e位于 §c%s.%s(§4%s:%s§c)'.format(stack.className, stack.methodName, stack.fileName, stack.lineNumber));
 | 
					                        this.console('    §e位于§c', stack.className + '.' + stack.methodName + '(§4' + stack.fileName + ':' + stack.lineNumber + '§c)');
 | 
				
			||||||
                    }
 | 
					                    }
 | 
				
			||||||
                }.bind(this));
 | 
					                }.bind(this));
 | 
				
			||||||
            };
 | 
					            };
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -77,7 +77,7 @@ exports.copy = function (inputStream, target, override) {
 | 
				
			|||||||
exports.read = function (file) {
 | 
					exports.read = function (file) {
 | 
				
			||||||
    file = exports.file(file);
 | 
					    file = exports.file(file);
 | 
				
			||||||
    if (!file.exists()) {
 | 
					    if (!file.exists()) {
 | 
				
			||||||
        console.warn("读取文件 %s 错误 文件不存在!".format(file));
 | 
					        console.warn('读取文件', file, '错误 文件不存在!');
 | 
				
			||||||
        return;
 | 
					        return;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    // noinspection JSPrimitiveTypeWrapperUsage
 | 
					    // noinspection JSPrimitiveTypeWrapperUsage
 | 
				
			||||||
@@ -103,7 +103,7 @@ exports.list = function (path) {
 | 
				
			|||||||
    if (dir.isDirectory()) {
 | 
					    if (dir.isDirectory()) {
 | 
				
			||||||
        return Files.list(dir.toPath());
 | 
					        return Files.list(dir.toPath());
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    console.warn("路径 %s 不是一个目录 返回空数组!".format(path));
 | 
					    console.warn('路径', path, '不是一个目录 返回空数组!');
 | 
				
			||||||
    return [];
 | 
					    return [];
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
/**
 | 
					/**
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -113,7 +113,11 @@
 | 
				
			|||||||
        base.save(cacheFile, "(function (module, exports, require, __dirname, __filename) {" + origin + "});");
 | 
					        base.save(cacheFile, "(function (module, exports, require, __dirname, __filename) {" + origin + "});");
 | 
				
			||||||
        // 使用 load 可以保留行号和文件名称
 | 
					        // 使用 load 可以保留行号和文件名称
 | 
				
			||||||
        var obj = load(cacheFile);
 | 
					        var obj = load(cacheFile);
 | 
				
			||||||
        base.delete(cacheFile);
 | 
					        try {
 | 
				
			||||||
 | 
					            base.delete(cacheFile);
 | 
				
			||||||
 | 
					        } catch (ex) {
 | 
				
			||||||
 | 
					            cacheFile.deleteOnExit();
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
        return obj;
 | 
					        return obj;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -126,7 +130,7 @@
 | 
				
			|||||||
     * @returns {Object}
 | 
					     * @returns {Object}
 | 
				
			||||||
     */
 | 
					     */
 | 
				
			||||||
    function compileModule(id, name, file, optional) {
 | 
					    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
 | 
					        // noinspection JSUnresolvedVariable
 | 
				
			||||||
        var module = {
 | 
					        var module = {
 | 
				
			||||||
            id: id,
 | 
					            id: id,
 | 
				
			||||||
@@ -139,10 +143,10 @@
 | 
				
			|||||||
            compiledWrapper.apply(module.exports, [
 | 
					            compiledWrapper.apply(module.exports, [
 | 
				
			||||||
                module, module.exports, module.require, file.parentFile, file
 | 
					                module, module.exports, module.require, file.parentFile, file
 | 
				
			||||||
            ]);
 | 
					            ]);
 | 
				
			||||||
            console.debug('模块 %s 编译成功!'.format(name));
 | 
					            console.debug('模块', name, '编译成功!');
 | 
				
			||||||
            module.loaded = true;
 | 
					            module.loaded = true;
 | 
				
			||||||
        } catch (ex) {
 | 
					        } 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);
 | 
					            console.ex(ex);
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return module;
 | 
					        return module;
 | 
				
			||||||
@@ -179,7 +183,7 @@
 | 
				
			|||||||
        var file = new File(name);
 | 
					        var file = new File(name);
 | 
				
			||||||
        file = _isFile(file) ? file : resolve(name, path);
 | 
					        file = _isFile(file) ? file : resolve(name, path);
 | 
				
			||||||
        if (file === undefined) {
 | 
					        if (file === undefined) {
 | 
				
			||||||
            console.console("§c模块 §a%s §c加载失败! §4未找到该模块!".format(name));
 | 
					            console.console('§c模块§a', name, '§c加载失败! §4未找到该模块!');
 | 
				
			||||||
            return {exports: {}};
 | 
					            return {exports: {}};
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        if (!optional) optional = {cache: true};
 | 
					        if (!optional) optional = {cache: true};
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user