feat: 调整API接口 新增封装工具

This commit is contained in:
coding 2017-12-28 08:29:10 +00:00
parent 9ef64d9bc2
commit fe615564ce
6 changed files with 87 additions and 61 deletions

View File

@ -1,9 +1,9 @@
/*global Java, base, module, exports, require*/
function impl(name) {
return require('../internal/' + DetectServerType + '/' + name);
return require('../internal/' + DetectServerType + '/' + name, {warnNotFound: false});
}
module.exports = {
exports = module.exports = {
command: impl('command'),
event: impl('event'),
permission: impl('permission'),

View File

@ -118,9 +118,7 @@ function afterLoadHook(plugin) {
// 给 console 添加插件名称
plugin.console.name = plugin.description.name;
// 赋值 self
for (var i in plugin) {
plugin.self[i] = plugin[i];
}
Object.assign(plugin.self, plugin);
}
/**
@ -202,16 +200,15 @@ function initPluginConfig(plugin) {
}
}
function runAndCatch(jsp, exec, ext) {
function runAndCatch(jsp, name, ext) {
var exec = jsp[name];
if (exec) {
try {
// 绑定方法的this到插件自身
exec.bind(jsp)();
if (ext) {
ext();
}
if (ext) ext();
} catch (ex) {
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, exec.name, ex.message));
console.console('§6插件 §b%s §6执行 §d%s §6方法时发生错误 §4%s'.format(jsp.description.name, name, ex.message));
console.ex(ex);
}
}
@ -234,9 +231,7 @@ function checkAndGet(args) {
var plugins = [];
exports.$ = server.plugin.self;
exports.plugins = plugins;
exports.init = function (path) {
function init(path) {
var plugin = exports.$
if (plugin !== null) {
// 如果过plugin不等于null 则代表是正式环境
@ -244,23 +239,36 @@ exports.init = function (path) {
}
loadPlugins(path);
};
exports.load = function () {
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.load));
function load() {
checkAndGet(arguments).forEach(function (p) runAndCatch(p, 'init'));
};
exports.enable = function () {
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.enable));
function enable() {
checkAndGet(arguments).forEach(function (p) runAndCatch(p, 'enable'));
};
exports.disable = function () {
checkAndGet(arguments).forEach(function (p) runAndCatch(p, p.disable, function () {
function disable() {
checkAndGet(arguments).forEach(function (p) runAndCatch(p, 'disable', function () {
event.disable(p);
// task.cancel();
}));
};
exports.reload = function () {
function reload() {
checkAndGet(arguments).forEach(function (p) {
exports.disable(p);
disable(p);
p = loadPlugin(p.__FILE__);
exports.load(p);
exports.enable(p);
load(p);
enable(p);
});
};
};
exports = module.exports = {
$: server.plugin.self,
plugins: plugins,
init: init,
load: load,
enable: enable,
disable: disable,
reload: reload
}

View File

@ -0,0 +1,5 @@
/*global Java, base, module, exports, require*/
var player = require('./player');
module.exports = {
player: player.$
}

View File

@ -8,8 +8,6 @@ var global = this;
*/
(function () {
var loader;
var Files = Java.type("java.nio.file.Files");
var Paths = Java.type("java.nio.file.Paths");
boot = function (root, logger) {
log = logger;
// 开发环境下初始化
@ -18,7 +16,7 @@ var global = this;
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
global.debug = true;
}
if (Files.exists(Paths.get(root, "debug"))) {
if (java.nio.file.Files.exists(java.nio.file.Paths.get(root, "debug"))) {
logger.info('已开启调试模式!');
global.debug = true;
}
@ -49,11 +47,9 @@ var global = this;
}
function release(root, regex, replace) {
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
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))) {
if (!java.nio.file.Files.exists(java.nio.file.Paths.get(jarPath))) {
jarPath = "/" + jarPath;
}
var jar = new java.util.jar.JarFile(jarPath);
@ -62,11 +58,11 @@ var global = this;
try {
if (!entry.isDirectory()) {
if (r.test(entry.name)) {
var path = Paths.get(root, entry.name);
var path = java.nio.file.Paths.get(root, entry.name);
var parentFile = path.toFile().parentFile;
if (!parentFile.exists()) { parentFile.mkdirs(); }
if (!Files.exists(path) || replace) {
Files.copy(loader.getResourceAsStream(entry.name), path, StandardCopyOption['REPLACE_EXISTING']);
if (!java.nio.file.Files.exists(path) || replace) {
java.nio.file.Files.copy(loader.getResourceAsStream(entry.name), path, java.nio.file.StandardCopyOption['REPLACE_EXISTING']);
}
}
}

View File

@ -38,8 +38,13 @@
console.log(i, '=>', obj[i])
}
}
console.ex = function (ex) {
this.console('§4' + ex);
console.ex = function (message, ex) {
if (!ex) {
this.console('§4' + message);
ex = message;
} else {
this.console('§4' + message + ' ' + ex);
}
var track = ex.getStackTrace();
if (track.class) {
track = Arrays.asList(track)

View File

@ -1,7 +1,6 @@
'use strict';
/*global Java, base, module, exports, require, __FILE__*/
var String = Java.type("java.lang.String");
var File = Java.type("java.io.File");
var Files = Java.type("java.nio.file.Files");
var separatorChar = File.separatorChar;
@ -10,7 +9,7 @@ var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
/**
* 用文件分割符合并路径
*/
exports.concat = function () {
function concat() {
return Array.prototype.join.call(arguments, separatorChar);
};
/**
@ -19,13 +18,13 @@ exports.concat = function () {
* @constructor(dir,file)
* @returns {*}
*/
exports.file = function () {
function file() {
if (!arguments[0]) {
console.warn("文件名称不得为 undefined 或者 null !");
}
switch (arguments.length) {
case 1:
if (exports.canonical(arguments[0])) {
if (path(arguments[0])) {
return arguments[0];
}
return new File(arguments[0]);
@ -37,7 +36,7 @@ exports.file = function () {
* 创建目录
* @param file
*/
exports.mkdirs = function (file) {
function mkdirs(file) {
// noinspection JSUnresolvedVariable
file.parentFile.mkdirs();
};
@ -45,11 +44,11 @@ exports.mkdirs = function (file) {
* 创建文件
* @param file
*/
exports.create = function (file) {
file = exports.file(file);
if (!file.exists()) {
exports.mkdirs(file);
file.createNewFile();
function create(file) {
f = file(file);
if (!f.exists()) {
mkdirs(f);
f.createNewFile();
}
};
/**
@ -57,7 +56,7 @@ exports.create = function (file) {
* @param file
* @returns {*}
*/
exports.path = exports.canonical = function (file) {
function path(file) {
// noinspection JSUnresolvedVariable
return file.canonicalPath;
};
@ -67,21 +66,21 @@ exports.path = exports.canonical = function (file) {
* @param target 目标文件
* @param override 是否覆盖
*/
exports.copy = function (inputStream, target, override) {
function copy(inputStream, target, override) {
Files.copy(inputStream, target.toPath(), StandardCopyOption[override ? 'REPLACE_EXISTING' : 'ATOMIC_MOVE']);
};
/**
* 读取文件
* @param file 文件路径
*/
exports.read = function (file) {
file = exports.file(file);
if (!file.exists()) {
console.warn('读取文件', file, '错误 文件不存在!');
function read(file) {
f = file(file);
if (!f.exists()) {
console.warn('读取文件', f, '错误 文件不存在!');
return;
}
// noinspection JSPrimitiveTypeWrapperUsage
return new String(Files.readAllBytes(file.toPath()), "UTF-8");
return new java.lang.String(Files.readAllBytes(f.toPath()), "UTF-8");
};
/**
* 保存内容文件
@ -89,7 +88,7 @@ exports.read = function (file) {
* @param content 内容
* @param override 是否覆盖
*/
exports.save = function (path, content, override) {
function save(path, content, override) {
Files.write(new File(path).toPath(),
content.getBytes("UTF-8"),
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']);
@ -98,12 +97,12 @@ exports.save = function (path, content, override) {
* 列出目录文件
* @param path
*/
exports.list = function (path) {
var dir = exports.file(path);
function list(path) {
var dir = file(path);
if (dir.isDirectory()) {
return Files.list(dir.toPath());
}
console.warn('路径', path, '不是一个目录 返回空数组!');
console.debug('路径', path, '不是一个目录 返回空数组!');
return [];
};
/**
@ -112,8 +111,21 @@ exports.list = function (path) {
* @param des 目标目录
* @param override 是否覆盖
*/
exports.move = function (src, des, override) {
Files.move(exports.file(src).toPath(),
exports.file(des).toPath(),
function move(src, des, override) {
Files.move(file(src).toPath(), file(des).toPath(),
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE'])
};
};
exports = module.exports = {
canonical: path,
concat: concat,
create: create,
mkdirs: mkdirs,
file: file,
path: path,
copy: copy,
read: read,
save: save,
list: list,
move: move
}