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,9 +3,9 @@
*/ */
/*global base*/ /*global base*/
var log = base.getLog().static; var log = base.getLog().static;
var Level = Java.type('java.util.logging.Level'); (function(global){
var Console = function (name) { var Level = Java.type('java.util.logging.Level');
this.name = name ? '[' + name + '] ' : ''; var Console = function (name) {
Object.defineProperty(this, 'name', { Object.defineProperty(this, 'name', {
get: function () { get: function () {
return this._name; return this._name;
@ -14,6 +14,7 @@ var Console = function (name) {
this._name = name ? '[' + name + '] ' : ''; this._name = name ? '[' + name + '] ' : '';
}.bind(this) }.bind(this)
}); });
this.name = name;
this.log = function () { this.log = function () {
log.i(this.name + Array.prototype.join.call(arguments, ' ')); log.i(this.name + Array.prototype.join.call(arguments, ' '));
}, },
@ -26,5 +27,7 @@ var Console = function (name) {
this.debug = function () { this.debug = function () {
log.d(this.name + Array.prototype.join.call(arguments, ' ')); 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 // noinspection JSUnusedLocalSymbols
function init(root, plugin) { function init(root, plugin) {
global.root = root; global.root = root;
initDir();
loadCore(); loadCore();
loadRequire(); loadRequire();
loadLib4Bukkit(); loadLib4Bukkit();
loadPlugins(plugin); loadPlugins(plugin);
} }
/**
* 初始化目录
*/
function initDir() {
// 核心目录
global.core_dir = root + "/core";
// 模块目录
global.miao_module_dir = root + "/modules";
// 插件目录
global.plugins_dir = root + "/plugins";
}
/** /**
* 初始化核心 * 初始化核心
*/ */
function loadCore() { function loadCore() {
// 加载基础模块 // 加载基础模块
load(core_dir + '/ext.js'); load(root + '/core/ext.js');
load(core_dir + '/console.js'); load(root + '/core/console.js');
// 加载补丁和扩展 // 加载补丁和扩展
load(core_dir + '/patch.js'); load(root + '/core/patch.js');
} }
/** /**
@ -40,20 +27,20 @@ function loadCore() {
*/ */
function loadRequire() { function loadRequire() {
// 初始化加载器 // 初始化加载器
global.require = load(core_dir + '/require.js')(root); global.require = load(root + '/core/require.js')(root);
} }
function loadLib4Bukkit() { function loadLib4Bukkit() {
require('modules/event'); require('modules/event');
var task = require('modules/task'); var task = require('modules/task');
global.setTimeout = function (func, time, async) { global.setTimeout = function (func, time, _async) {
return async ? task.laterAsync(func, time) : task.later(func, time); return _async ? task.laterAsync(func, time) : task.later(func, time);
}; };
global.clearTimeout = function (task) { global.clearTimeout = function (task) {
task.cancel(); task.cancel();
}; };
global.setInterval = function (func, time, async) { global.setInterval = function (func, time, _async) {
return async ? task.timerAsync(func, time) : task.timer(func, time); return _async ? task.timerAsync(func, time) : task.timer(func, time);
}; };
global.clearInterval = function (task) { global.clearInterval = function (task) {
task.cancel(); task.cancel();
@ -66,7 +53,7 @@ function loadLib4Bukkit() {
function loadPlugins(plugin) { function loadPlugins(plugin) {
// 初始化本体插件 // 初始化本体插件
global.pluginManager = require('modules/plugin'); global.pluginManager = require('modules/plugin');
pluginManager.init(plugin, plugins_dir); pluginManager.init(plugin, root + '/plugins');
// 只有当在正式环境运行的时候才加载 // 只有当在正式环境运行的时候才加载
if (pluginManager.$) { if (pluginManager.$) {
pluginManager.load(); pluginManager.load();

View File

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