feat: 调整API接口 新增封装工具
This commit is contained in:
parent
9ef64d9bc2
commit
fe615564ce
@ -1,9 +1,9 @@
|
|||||||
/*global Java, base, module, exports, require*/
|
/*global Java, base, module, exports, require*/
|
||||||
function impl(name) {
|
function impl(name) {
|
||||||
return require('../internal/' + DetectServerType + '/' + name);
|
return require('../internal/' + DetectServerType + '/' + name, {warnNotFound: false});
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
exports = module.exports = {
|
||||||
command: impl('command'),
|
command: impl('command'),
|
||||||
event: impl('event'),
|
event: impl('event'),
|
||||||
permission: impl('permission'),
|
permission: impl('permission'),
|
||||||
|
@ -118,9 +118,7 @@ function afterLoadHook(plugin) {
|
|||||||
// 给 console 添加插件名称
|
// 给 console 添加插件名称
|
||||||
plugin.console.name = plugin.description.name;
|
plugin.console.name = plugin.description.name;
|
||||||
// 赋值 self
|
// 赋值 self
|
||||||
for (var i in plugin) {
|
Object.assign(plugin.self, plugin);
|
||||||
plugin.self[i] = plugin[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -202,16 +200,15 @@ function initPluginConfig(plugin) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function runAndCatch(jsp, exec, ext) {
|
function runAndCatch(jsp, name, ext) {
|
||||||
|
var exec = jsp[name];
|
||||||
if (exec) {
|
if (exec) {
|
||||||
try {
|
try {
|
||||||
// 绑定方法的this到插件自身
|
// 绑定方法的this到插件自身
|
||||||
exec.bind(jsp)();
|
exec.bind(jsp)();
|
||||||
if (ext) {
|
if (ext) ext();
|
||||||
ext();
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
} 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);
|
console.ex(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -234,9 +231,7 @@ function checkAndGet(args) {
|
|||||||
|
|
||||||
var plugins = [];
|
var plugins = [];
|
||||||
|
|
||||||
exports.$ = server.plugin.self;
|
function init(path) {
|
||||||
exports.plugins = plugins;
|
|
||||||
exports.init = function (path) {
|
|
||||||
var plugin = exports.$
|
var plugin = exports.$
|
||||||
if (plugin !== null) {
|
if (plugin !== null) {
|
||||||
// 如果过plugin不等于null 则代表是正式环境
|
// 如果过plugin不等于null 则代表是正式环境
|
||||||
@ -244,23 +239,36 @@ exports.init = function (path) {
|
|||||||
}
|
}
|
||||||
loadPlugins(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);
|
event.disable(p);
|
||||||
// task.cancel();
|
|
||||||
}));
|
}));
|
||||||
};
|
};
|
||||||
exports.reload = function () {
|
|
||||||
|
function reload() {
|
||||||
checkAndGet(arguments).forEach(function (p) {
|
checkAndGet(arguments).forEach(function (p) {
|
||||||
exports.disable(p);
|
disable(p);
|
||||||
p = loadPlugin(p.__FILE__);
|
p = loadPlugin(p.__FILE__);
|
||||||
exports.load(p);
|
load(p);
|
||||||
exports.enable(p);
|
enable(p);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports = module.exports = {
|
||||||
|
$: server.plugin.self,
|
||||||
|
plugins: plugins,
|
||||||
|
init: init,
|
||||||
|
load: load,
|
||||||
|
enable: enable,
|
||||||
|
disable: disable,
|
||||||
|
reload: reload
|
||||||
|
}
|
||||||
|
5
src/main/resources/api/wrapper.js
Normal file
5
src/main/resources/api/wrapper.js
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
/*global Java, base, module, exports, require*/
|
||||||
|
var player = require('./player');
|
||||||
|
module.exports = {
|
||||||
|
player: player.$
|
||||||
|
}
|
@ -8,8 +8,6 @@ var global = this;
|
|||||||
*/
|
*/
|
||||||
(function () {
|
(function () {
|
||||||
var loader;
|
var loader;
|
||||||
var Files = Java.type("java.nio.file.Files");
|
|
||||||
var Paths = Java.type("java.nio.file.Paths");
|
|
||||||
boot = function (root, logger) {
|
boot = function (root, logger) {
|
||||||
log = logger;
|
log = logger;
|
||||||
// 开发环境下初始化
|
// 开发环境下初始化
|
||||||
@ -18,7 +16,7 @@ var global = this;
|
|||||||
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
|
logger.info('载入自定义 BIOS 文件 ' + __FILE__);
|
||||||
global.debug = true;
|
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('已开启调试模式!');
|
logger.info('已开启调试模式!');
|
||||||
global.debug = true;
|
global.debug = true;
|
||||||
}
|
}
|
||||||
@ -49,11 +47,9 @@ var global = this;
|
|||||||
}
|
}
|
||||||
|
|
||||||
function release(root, regex, replace) {
|
function release(root, regex, replace) {
|
||||||
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
|
|
||||||
|
|
||||||
var upath = pluginYml.getFile().substring(pluginYml.getFile().indexOf("/") + 1);
|
var upath = pluginYml.getFile().substring(pluginYml.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 (!java.nio.file.Files.exists(java.nio.file.Paths.get(jarPath))) {
|
||||||
jarPath = "/" + jarPath;
|
jarPath = "/" + jarPath;
|
||||||
}
|
}
|
||||||
var jar = new java.util.jar.JarFile(jarPath);
|
var jar = new java.util.jar.JarFile(jarPath);
|
||||||
@ -62,11 +58,11 @@ var global = this;
|
|||||||
try {
|
try {
|
||||||
if (!entry.isDirectory()) {
|
if (!entry.isDirectory()) {
|
||||||
if (r.test(entry.name)) {
|
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;
|
var parentFile = path.toFile().parentFile;
|
||||||
if (!parentFile.exists()) { parentFile.mkdirs(); }
|
if (!parentFile.exists()) { parentFile.mkdirs(); }
|
||||||
if (!Files.exists(path) || replace) {
|
if (!java.nio.file.Files.exists(path) || replace) {
|
||||||
Files.copy(loader.getResourceAsStream(entry.name), path, StandardCopyOption['REPLACE_EXISTING']);
|
java.nio.file.Files.copy(loader.getResourceAsStream(entry.name), path, java.nio.file.StandardCopyOption['REPLACE_EXISTING']);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -38,8 +38,13 @@
|
|||||||
console.log(i, '=>', obj[i])
|
console.log(i, '=>', obj[i])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
console.ex = function (ex) {
|
console.ex = function (message, ex) {
|
||||||
this.console('§4' + ex);
|
if (!ex) {
|
||||||
|
this.console('§4' + message);
|
||||||
|
ex = message;
|
||||||
|
} else {
|
||||||
|
this.console('§4' + message + ' ' + ex);
|
||||||
|
}
|
||||||
var track = ex.getStackTrace();
|
var track = ex.getStackTrace();
|
||||||
if (track.class) {
|
if (track.class) {
|
||||||
track = Arrays.asList(track)
|
track = Arrays.asList(track)
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
'use strict';
|
'use strict';
|
||||||
|
|
||||||
/*global Java, base, module, exports, require, __FILE__*/
|
/*global Java, base, module, exports, require, __FILE__*/
|
||||||
var String = Java.type("java.lang.String");
|
|
||||||
var File = Java.type("java.io.File");
|
var File = Java.type("java.io.File");
|
||||||
var Files = Java.type("java.nio.file.Files");
|
var Files = Java.type("java.nio.file.Files");
|
||||||
var separatorChar = File.separatorChar;
|
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);
|
return Array.prototype.join.call(arguments, separatorChar);
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@ -19,13 +18,13 @@ exports.concat = function () {
|
|||||||
* @constructor(dir,file)
|
* @constructor(dir,file)
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
exports.file = function () {
|
function file() {
|
||||||
if (!arguments[0]) {
|
if (!arguments[0]) {
|
||||||
console.warn("文件名称不得为 undefined 或者 null !");
|
console.warn("文件名称不得为 undefined 或者 null !");
|
||||||
}
|
}
|
||||||
switch (arguments.length) {
|
switch (arguments.length) {
|
||||||
case 1:
|
case 1:
|
||||||
if (exports.canonical(arguments[0])) {
|
if (path(arguments[0])) {
|
||||||
return arguments[0];
|
return arguments[0];
|
||||||
}
|
}
|
||||||
return new File(arguments[0]);
|
return new File(arguments[0]);
|
||||||
@ -37,7 +36,7 @@ exports.file = function () {
|
|||||||
* 创建目录
|
* 创建目录
|
||||||
* @param file
|
* @param file
|
||||||
*/
|
*/
|
||||||
exports.mkdirs = function (file) {
|
function mkdirs(file) {
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
file.parentFile.mkdirs();
|
file.parentFile.mkdirs();
|
||||||
};
|
};
|
||||||
@ -45,11 +44,11 @@ exports.mkdirs = function (file) {
|
|||||||
* 创建文件
|
* 创建文件
|
||||||
* @param file
|
* @param file
|
||||||
*/
|
*/
|
||||||
exports.create = function (file) {
|
function create(file) {
|
||||||
file = exports.file(file);
|
f = file(file);
|
||||||
if (!file.exists()) {
|
if (!f.exists()) {
|
||||||
exports.mkdirs(file);
|
mkdirs(f);
|
||||||
file.createNewFile();
|
f.createNewFile();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@ -57,7 +56,7 @@ exports.create = function (file) {
|
|||||||
* @param file
|
* @param file
|
||||||
* @returns {*}
|
* @returns {*}
|
||||||
*/
|
*/
|
||||||
exports.path = exports.canonical = function (file) {
|
function path(file) {
|
||||||
// noinspection JSUnresolvedVariable
|
// noinspection JSUnresolvedVariable
|
||||||
return file.canonicalPath;
|
return file.canonicalPath;
|
||||||
};
|
};
|
||||||
@ -67,21 +66,21 @@ exports.path = exports.canonical = function (file) {
|
|||||||
* @param target 目标文件
|
* @param target 目标文件
|
||||||
* @param override 是否覆盖
|
* @param override 是否覆盖
|
||||||
*/
|
*/
|
||||||
exports.copy = function (inputStream, target, override) {
|
function copy(inputStream, target, override) {
|
||||||
Files.copy(inputStream, target.toPath(), StandardCopyOption[override ? 'REPLACE_EXISTING' : 'ATOMIC_MOVE']);
|
Files.copy(inputStream, target.toPath(), StandardCopyOption[override ? 'REPLACE_EXISTING' : 'ATOMIC_MOVE']);
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
* 读取文件
|
* 读取文件
|
||||||
* @param file 文件路径
|
* @param file 文件路径
|
||||||
*/
|
*/
|
||||||
exports.read = function (file) {
|
function read(file) {
|
||||||
file = exports.file(file);
|
f = file(file);
|
||||||
if (!file.exists()) {
|
if (!f.exists()) {
|
||||||
console.warn('读取文件', file, '错误 文件不存在!');
|
console.warn('读取文件', f, '错误 文件不存在!');
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
// noinspection JSPrimitiveTypeWrapperUsage
|
// 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 content 内容
|
||||||
* @param override 是否覆盖
|
* @param override 是否覆盖
|
||||||
*/
|
*/
|
||||||
exports.save = function (path, content, override) {
|
function save(path, content, override) {
|
||||||
Files.write(new File(path).toPath(),
|
Files.write(new File(path).toPath(),
|
||||||
content.getBytes("UTF-8"),
|
content.getBytes("UTF-8"),
|
||||||
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']);
|
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE']);
|
||||||
@ -98,12 +97,12 @@ exports.save = function (path, content, override) {
|
|||||||
* 列出目录文件
|
* 列出目录文件
|
||||||
* @param path
|
* @param path
|
||||||
*/
|
*/
|
||||||
exports.list = function (path) {
|
function list(path) {
|
||||||
var dir = exports.file(path);
|
var dir = file(path);
|
||||||
if (dir.isDirectory()) {
|
if (dir.isDirectory()) {
|
||||||
return Files.list(dir.toPath());
|
return Files.list(dir.toPath());
|
||||||
}
|
}
|
||||||
console.warn('路径', path, '不是一个目录 返回空数组!');
|
console.debug('路径', path, '不是一个目录 返回空数组!');
|
||||||
return [];
|
return [];
|
||||||
};
|
};
|
||||||
/**
|
/**
|
||||||
@ -112,8 +111,21 @@ exports.list = function (path) {
|
|||||||
* @param des 目标目录
|
* @param des 目标目录
|
||||||
* @param override 是否覆盖
|
* @param override 是否覆盖
|
||||||
*/
|
*/
|
||||||
exports.move = function (src, des, override) {
|
function move(src, des, override) {
|
||||||
Files.move(exports.file(src).toPath(),
|
Files.move(file(src).toPath(), file(des).toPath(),
|
||||||
exports.file(des).toPath(),
|
|
||||||
override ? StandardCopyOption['REPLACE_EXISTING'] : StandardCopyOption['ATOMIC_MOVE'])
|
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
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user