refactor: 减少全局变量 调试require选项

This commit is contained in:
coding 2017-10-17 02:53:51 +00:00
parent b668795518
commit 5b621d3d99
3 changed files with 37 additions and 47 deletions

View File

@ -3,28 +3,31 @@
*/
/*global base*/
var log = base.getLog().static;
var Level = Java.type('java.util.logging.Level');
var Console = function (name) {
this.name = name ? '[' + name + '] ' : '';
Object.defineProperty(this, 'name', {
get: function () {
return this._name;
}.bind(this),
set: function (name) {
this._name = name ? '[' + name + '] ' : '';
}.bind(this)
});
this.log = function () {
log.i(this.name + Array.prototype.join.call(arguments, ' '));
},
this.warn = function () {
log.w(this.name + Array.prototype.join.call(arguments, ' '));
},
this.error = function () {
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
},
this.debug = function () {
log.d(this.name + Array.prototype.join.call(arguments, ' '));
(function(global){
var Level = Java.type('java.util.logging.Level');
var Console = function (name) {
Object.defineProperty(this, 'name', {
get: function () {
return this._name;
}.bind(this),
set: function (name) {
this._name = name ? '[' + name + '] ' : '';
}.bind(this)
});
this.name = name;
this.log = function () {
log.i(this.name + Array.prototype.join.call(arguments, ' '));
},
this.warn = function () {
log.w(this.name + Array.prototype.join.call(arguments, ' '));
},
this.error = function () {
log.log(Level.SEVERE, this.name + Array.prototype.join.call(arguments, ' '));
},
this.debug = function () {
log.d(this.name + Array.prototype.join.call(arguments, ' '));
}
}
}
global.console = new Console();
global.Console = Console;
global.console = new Console();
})(global)

View File

@ -5,34 +5,21 @@ var global = this;
// noinspection JSUnusedLocalSymbols
function init(root, plugin) {
global.root = root;
initDir();
loadCore();
loadRequire();
loadLib4Bukkit();
loadPlugins(plugin);
}
/**
* 初始化目录
*/
function initDir() {
// 核心目录
global.core_dir = root + "/core";
// 模块目录
global.miao_module_dir = root + "/modules";
// 插件目录
global.plugins_dir = root + "/plugins";
}
/**
* 初始化核心
*/
function loadCore() {
// 加载基础模块
load(core_dir + '/ext.js');
load(core_dir + '/console.js');
load(root + '/core/ext.js');
load(root + '/core/console.js');
// 加载补丁和扩展
load(core_dir + '/patch.js');
load(root + '/core/patch.js');
}
/**
@ -40,20 +27,20 @@ function loadCore() {
*/
function loadRequire() {
// 初始化加载器
global.require = load(core_dir + '/require.js')(root);
global.require = load(root + '/core/require.js')(root);
}
function loadLib4Bukkit() {
require('modules/event');
var task = require('modules/task');
global.setTimeout = function (func, time, async) {
return async ? task.laterAsync(func, time) : task.later(func, time);
global.setTimeout = function (func, time, _async) {
return _async ? task.laterAsync(func, time) : task.later(func, time);
};
global.clearTimeout = function (task) {
task.cancel();
};
global.setInterval = function (func, time, async) {
return async ? task.timerAsync(func, time) : task.timer(func, time);
global.setInterval = function (func, time, _async) {
return _async ? task.timerAsync(func, time) : task.timer(func, time);
};
global.clearInterval = function (task) {
task.cancel();
@ -66,7 +53,7 @@ function loadLib4Bukkit() {
function loadPlugins(plugin) {
// 初始化本体插件
global.pluginManager = require('modules/plugin');
pluginManager.init(plugin, plugins_dir);
pluginManager.init(plugin, root + '/plugins');
// 只有当在正式环境运行的时候才加载
if (pluginManager.$) {
pluginManager.load();

View File

@ -103,7 +103,7 @@
* @returns {Object}
*/
function compileModule(id, name, file, optional) {
log.d('加载模块 %s 位于 %s', name, id);
log.d('加载模块 %s 位于 %s Optional %s', name, id, optional.toJson());
// noinspection JSUnresolvedVariable
var module = {
id: id,